How to use wiringPi function in SP7021 platform
This is modified from unofficial mirror/fork of wiringPi to support SP7021 board (Sunplus evaluation board). Because of SP7021 hardware is different from Raspberry Pi so that there are some hardware-related function would not be supported. (like edge/pad drive/pwm-bal/pwm-ms ...)
WiringPi is a simple and easy-to-use function library, through wiringPi, chips such as SPI, UART and I2C can be extended. The GPIO control also become very simple through wiringPi.
If user is familiar to Raspberry Pi, mostly you will also know the wiringPi very well. The wiringPi in SP7021 function will help you quickly control peripherals of SP7021 development board.
Please follow below step to operate:
1. Environment
Platform: Bpi-F2S
OS: Raspbian + SP7021 linux kernel
2. Download wiringPi source code
$ git clone https://github.com/sunplus-plus1/wiringpiforsp7021.git
3. Set your cross compiler path from build script file
$ cd wiringpiforsp7021
$ nano build
4. Build code
$ ./build debian
5. Install deb package
$ cd debian-template
$ sudo dpkg –i wiringpi-2.61-1.deb
6. Check install result by "gpio –v" and "gpio readall"
==> gpio -v
==> gpio readall
7. Command line control method
7.1 Set pin0 to gpio output
$ gpio mode 0 output
7.2 pin0 gpio read
$ gpio read 0
7.3 pin0 gpio set high
$ gpio write 0 1
7.4 Set pin2 as pwm0
$ gpio mode 0 pwm0
7.5 Set pin2 pwm clock
$ gpio pwmc 2 100 /* pwm0 clock = 100Hz */
7.6 Set pin2 pwm duty
$ gpio pwm 2 100 /* duty ratio = 100/255 =39.2% */
8. Programming example1 - test.c
#include <stdio.h>
#include <wiringPi.h>
// LED Pin - wiringPi pin 0 is SP7021 pin 18.
#defineLED0
// Set pin2 to pwm mode - wiringPi pin 2 is SP7021 pin 19.
#definePWM2
int main (void)
{
printf ("Raspberry Pi blink\n") ;
wiringPiSetup () ;
pinMode (LED, OUTPUT) ;
pinMode (PWM, PWM_OUTPUT) ;
pwmWrite (PWM, 100) ; // duty ratio = 100/255 =39.2%
for (;
{
digitalWrite (LED, HIGH) ;// On
delay (500) ; // mS
digitalWrite (LED, LOW) ; // Off
delay (500) ; // mS
}
return 0 ;
}
==> Compile: gcc test.c -o test -lwiringPi
==> Execute result: $ ./test
9. Programming example2 - bcm280test.c
#include <wiringPi.h>
#include <stdio.h>
#include <bmp280.h>
#include <unistd.h>
int main (void)
{
int temp, pres;
int bus = BMP280_I2C;
int bOK;
wiringPiSetup () ;
if (bus==BMP280_I2C)
bOK = bmp280i2cSetup (64) ;
else
if (bus==BMP280_SPI)
bOK = bmp280spiSetup (64, 0) ;
if (bOK) {
printf ("Setup failed.\n");
return 1;
}
while (1) {
temp = analogRead(64);
pres = analogRead(65);
printf ("temp = %.2f, press = %.2f\n", temp/100.0, pres/256.0);
sleep(1);
}
return 0;
}
==> Compile: gcc bcm280test.c –o bcm280test –lwiringPi
==> Execute result: ./bcm280test
10. Programming example3 - serialRWTest.c
#include <stdio.h>
#include <wiringPi.h>
#include <wiringSerial.h>
int main(void)
{
int fd;
int dnum = 0;
int count = 0;
int pre_count = 0;
unsigned char chr[100];
wiringPiSetup();
fd = serialOpen("/dev/ttyS1", 115200);
printf("ttyS1 uart test:\n");
serialPrintf(fd, "Now you can test!\n");
while (1) {
dnum = serialDataAvail(fd);
if(dnum > 0) {
chr[count] = serialGetchar(fd);
count++;
}
else {
delay(2);
}
if (pre_count!=count) {
pre_count = count;
}
else {
if (count) {
chr[count]=0;
printf ("%s", chr);
serialPrintf(fd, "%s", chr);
if (chr[0]=='\n') break;
count = 0;
}
}
}
serialPrintf(fd, "close\n");
serialClose(fd);
return 0;
}
==> Compile: gcc serialRWTest.c –o serialRWTest –lwiringPi
==> Execute result: ./ serialRWTest
Bpi-F2S board side
PC side