Difference between revisions of "WiringX"
Line 61: | Line 61: | ||
− | ==== | + | ==== C language version of the function in wiringX ==== |
* int wiringXSetup(char *name, void (*func)(int, char *, int, const char *, ...)); // initialize a platform with wiringX | * int wiringXSetup(char *name, void (*func)(int, char *, int, const char *, ...)); // initialize a platform with wiringX | ||
Line 146: | Line 146: | ||
* int wiringXSerialGetChar(int fd); // returns the next character to be read for the serial port device. If there is no data, the function will wait 10 seconds and return -1 if there is still no data after 10 seconds. | * int wiringXSerialGetChar(int fd); // returns the next character to be read for the serial port device. If there is no data, the function will wait 10 seconds and return -1 if there is still no data after 10 seconds. | ||
+ | |||
+ | |||
+ | ==== python language version of the function in wiringX ==== | ||
+ | the following functions is similar to the C version above | ||
+ | * py_setup(PyObject *self, PyObject *args) | ||
+ | * py_digitalWrite(PyObject *self, PyObject *args) | ||
+ | * py_digitalRead(PyObject *self, PyObject *args) | ||
+ | * py_pinMode(PyObject *self, PyObject *args) | ||
+ | * py_validGPIO(PyObject *self, PyObject *args) | ||
+ | ===== I2C ===== | ||
+ | * py_setupI2C(PyObject *self, PyObject *args) | ||
+ | * py_I2CRead(PyObject *self, PyObject *args) | ||
+ | * py_I2CReadReg8(PyObject *self, PyObject *args) | ||
+ | * py_I2CReadReg16(PyObject *self, PyObject *args) | ||
+ | * py_I2CWrite(PyObject *self, PyObject *args) | ||
+ | * py_I2CWriteReg8(PyObject *self, PyObject *args) | ||
+ | * py_I2CWriteReg16(PyObject *self, PyObject *args) | ||
+ | |||
+ | ===== spi ===== | ||
+ | * py_SPIGetFd(PyObject *self, PyObject *args) | ||
+ | * py_SPIDataRW(PyObject *self, PyObject *args) | ||
+ | * py_setupSPI(PyObject *self, PyObject *args) | ||
Revision as of 05:03, 28 November 2022
WiringX is a GPIO library similar to wiringPi, this document describes how to build and use mraa for ROCK Pi boards.
Contents
List of ROCK Pi Boards supported
- ROCK 4 SE
- ROCK 4C Plus
- ROCK 4A/4B
- ROCK 4A/4B Plus
- ROCK 4C
List of Linux Distributions
- Ubuntu
- Debian
Preparation
Before building, please check that these tools are installed in advance
- git
- cmake make
- build-essential
If you don't have, please install them as the following command line
rock@rockpi-4cplus:~$ sudo apt-get install cmake make build-essential -y
Build wiringX with C language version
rock@rockpi-4cplus:~$ git clone https://github.com/wiringX/wiringX.git rock@rockpi-4cplus:~$ cd wiringX rock@rockpi-4cplus:~/wiringX/$ mkdir build rock@rockpi-4cplus:~/wiringX/$ cd build rock@rockpi-4cplus:~/wiringX/build$ cmake .. rock@rockpi-4cplus:~/wiringX/build$ make -j4 rock@rockpi-4cplus:~/wiringX/build$ cpack -G DEB rock@rockpi-4cplus:~/wiringX/build$ sudo dpkg -i libwiringx*.deb
Build wiringX with Python language version Please make sure python-dev is installed before generating python version's deb of wiringx
sudo apt-get install python-dev # for python2.x installs sudo apt-get install python3-dev # for python3.x installs
rock@rockpi-4cplus:~$ git clone https://github.com/wiringX/wiringX.git rock@rockpi-4cplus:~$ cd wiringX rock@rockpi-4cplus:~/wiringX$ cd python/ rock@rockpi-4cplus:~/wiringX/python$ mkdir build rock@rockpi-4cplus:~/wiringX/python/build$ cmake .. rock@rockpi-4cplus:~/wiringX/python/build$ make -j4 rock@rockpi-4cplus:~/wiringX/python/build$ cpack -G DEB rock@rockpi-4cplus:~/wiringX/python/build$ sudo dpkg -i python-wiringx-*.deb
C language version of the function in wiringX
- int wiringXSetup(char *name, void (*func)(int, char *, int, const char *, ...)); // initialize a platform with wiringX
- int pinMode(int pin, enum pinmode_t mode); // set the operating mode of the pin
mode: PINMODE_NOT_SET PINMODE_INPUT PINMODE_OUTPUT PINMODE_INTERRUPT
- int digitalWrite(int pin, enum digital_value_t value); // write a digital_value to the pin
value: LOW, HIGH
- int digitalRead(int pin) // read value of the pin
- int wiringXISR(int pin, enum isr_mode_t mode); // uses a function as an argument to get an interrupt in a particular GPIO pin
mode: ISR_MODE_UNKNOWN ISR_MODE_RISING ISR_MODE_FALLING ISR_MODE_BOTH ISR_MODE_NONE
- int waitForInterrupt(int pin, int ms); // this is the wait event interrupt function,
- int wiringXValidGPIO(int pin); // check that the pin is valid
I2C
- int wiringXI2CSetup(const char *path, int devId) // this function initializes the I2C system with the specified Acura symbol
- int wiringXI2CRead(int fd); // simple device read operation. Some Actos can be read directly without the need to send any register addresses
- int wiringXI2CReadReg8(int fd, int reg); // An 8-bit value can be read from a specified device register
- int wiringXI2CReadReg16(int fd, int reg); // An 16-bit value can be read from a specified device register
- int wiringXI2CWrite(int fd, int data); // simple device write operations. Some devices can accept data without sending any internal register addresses
- int wiringXI2CWriteReg8(int fd, int reg, int data); // An 8-bit value can be written to the specified Acura register
- int wiringXI2CWriteReg8(int fd, int reg, int data); // An 16-bit value can be written to the specified Acura register
SPI
- int wiringXSPISetup(int channel, int speed); // use this function to initialize an SPI channel
- int wiringXSPIDataRW(int channel, unsigned char *data, int len); // This function performs a simultaneous read and write operation through the selected SPI bus. The data in the buffer, will be covered with Hang SPI bus back to the data. For simple read and write operations, you can use standard system functions: read() and write()
Serial
- int wiringXSerialOpen(const char *device, struct wiringXSerial_t wiringXSerial); // this function will open the serial port device initially and set the baud rate of communication
typedef struct wiringXSerial_t { unsigned int baud; unsigned int databits; unsigned int parity; unsigned int stopbits; unsigned int flowcontrol; } wiringXSerial_t;
- void wiringXSerialClose(int fd); // shut down the device with the specified file descriptor
- void wiringXSerialFlush(int fd); // discard all received data or wait for writing to complete on the specified device
- void wiringXSerialPutChar(int fd, unsigned char c); // writes a single byte to the file descriptor of the specified device
- void wiringXSerialPuts(int fd, const char *s); // this function writes a string ending in 0 to the file descriptor of the specified device
- void wiringXSerialPrintf(int fd, const char *message, ...); // used the same way as printf
- int wiringXSerialDataAvail(int fd); // returns the number of bytes available in the serial port receive cache
- int wiringXSerialGetChar(int fd); // returns the next character to be read for the serial port device. If there is no data, the function will wait 10 seconds and return -1 if there is still no data after 10 seconds.
python language version of the function in wiringX
the following functions is similar to the C version above
- py_setup(PyObject *self, PyObject *args)
- py_digitalWrite(PyObject *self, PyObject *args)
- py_digitalRead(PyObject *self, PyObject *args)
- py_pinMode(PyObject *self, PyObject *args)
- py_validGPIO(PyObject *self, PyObject *args)
I2C
- py_setupI2C(PyObject *self, PyObject *args)
- py_I2CRead(PyObject *self, PyObject *args)
- py_I2CReadReg8(PyObject *self, PyObject *args)
- py_I2CReadReg16(PyObject *self, PyObject *args)
- py_I2CWrite(PyObject *self, PyObject *args)
- py_I2CWriteReg8(PyObject *self, PyObject *args)
- py_I2CWriteReg16(PyObject *self, PyObject *args)
spi
- py_SPIGetFd(PyObject *self, PyObject *args)
- py_SPIDataRW(PyObject *self, PyObject *args)
- py_setupSPI(PyObject *self, PyObject *args)
C language version of the example
blink.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include "wiringx.h" char *usage = "Usage: %s platform GPIO\n" " GPIO is the GPIO to write to\n" "Example: %s raspberrypi2 10\n"; int main(int argc, char *argv[]) { char *str = NULL, *platform = NULL; char usagestr[130]; int gpio = 0, invalid = 0; memset(usagestr, '\0', 130); // expect only 1 argument => argc must be 2 if(argc != 3) { snprintf(usagestr, 129, usage, argv[0], argv[0]); puts(usagestr); return -1; } // check for a valid, numeric argument platform = argv[1]; str = argv[2]; while(*str != '\0') { if(!isdigit(*str)) { invalid = 1; } str++; } if(invalid == 1) { printf("%s: Invalid GPIO %s\n", argv[0], argv[2]); return -1; } gpio = atoi(argv[2]); if(wiringXSetup(platform, NULL) == -1) { wiringXGC(); return -1; } if(wiringXValidGPIO(gpio) != 0) { printf("%s: Invalid GPIO %d\n", argv[0], gpio); wiringXGC(); return -1; } pinMode(gpio, PINMODE_OUTPUT); while(1) { printf("Writing to GPIO %d: High\n", gpio); digitalWrite(gpio, HIGH); sleep(1); printf("Writing to GPIO %d: Low\n", gpio); digitalWrite(gpio, LOW); sleep(1); } }
use the following command line to compile it
gcc blink.c -lwiringx -o blink
usage:
eg: ./blink rock4 13
interrupt.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <pthread.h> #include <sys/syscall.h> #include "wiringx.h" char *usage = "Usage: %s platform GPIO GPIO\n" " first GPIO to write to = output\n" " second GPIO reacts on an interrupt = input\n" "Example: %s raspberrypi2 16 20\n"; void *interrupt(void *gpio_void_ptr) { int i = 0; int gpio = *(int *)gpio_void_ptr; while(++i < 20) { if(waitForInterrupt(gpio, 1000) > 0) { printf(">>Interrupt on GPIO %d\n", gpio); } else { printf(" Timeout on GPIO %d\n", gpio); } } return 0; } int main(int argc, char *argv[]) { pthread_t pth; char *str = NULL, *platform = NULL; char usagestr[190]; int gpio_out = 0, gpio_in = 0; int i = 0, err = 0, invalid = 0; memset(usagestr, '\0', 190); // expect 2 arguments => argc must be 3 if(argc != 4) { snprintf(usagestr, 189, usage, argv[0], argv[0]); puts(usagestr); return -1; } // check for valid, numeric arguments for(i=2; i<argc; i++) { str = argv[i]; while(*str != '\0') { if(!isdigit(*str)) { invalid = 1; } str++; } if(invalid == 1) { printf("%s: Invalid GPIO %s\n", argv[0], argv[i]); return -1; } } platform = argv[1]; gpio_out = atoi(argv[2]); gpio_in = atoi(argv[3]); if(gpio_out == gpio_in) { printf("%s: GPIO for output and input (interrupt) should not be the same\n", argv[0]); return -1; } if(wiringXSetup(platform, NULL) == -1) { wiringXGC(); return -1; } if(wiringXValidGPIO(gpio_out) != 0) { printf("%s: Invalid GPIO %d for output\n", argv[0], gpio_out); wiringXGC(); return -1; } if(wiringXValidGPIO(gpio_in) != 0) { printf("%s: Invalid GPIO %d for input (interrupt)\n", argv[0], gpio_in); wiringXGC(); return -1; } pinMode(gpio_out, PINMODE_OUTPUT); if((wiringXISR(gpio_in, ISR_MODE_BOTH)) != 0) { printf("%s: Cannot set GPIO %d to interrupt BOTH\n", argv[0], gpio_in); wiringXGC(); return -1; } err = pthread_create(&pth, NULL, interrupt, &gpio_in); if(err != 0) { printf("Can't create thread: [%s]\n", strerror(err)); wiringXGC(); return -1; } else { printf("Thread created succesfully\n"); } for(i=0; i<5; i++) { printf(" Writing to GPIO %d: High\n", gpio_out); digitalWrite(gpio_out, HIGH); sleep(1); printf(" Writing to GPIO %d: Low\n", gpio_out); digitalWrite(gpio_out, LOW); sleep(2); } printf("Main finished, waiting for thread ...\n"); pthread_join(pth, NULL); wiringXGC(); return 0; }
use the following command line to compile it
gcc interrupt.c -lwiringx -lpthread
Serial
send.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stdint.h> #include <pthread.h> #include "wiringx.h" int main(void) { struct wiringXSerial_t wiringXSerial = {115200, 7, 'o', 2, 'x'}; unsigned char data_send = 's'; int fd = -1; if(wiringXSetup("rock4", NULL) == -1) { wiringXGC(); return -1; } if((fd = wiringXSerialOpen("/dev/ttyS2", wiringXSerial)) < 0) { fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno)); wiringXGC(); return -1; } wiringXSerialPutChar(fd, data_send); }
use the following command line to compile it
gcc send.c -lwiringx -o send
receive.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stdint.h> #include <pthread.h> #include "wiringx.h" int main(void) { struct wiringXSerial_t wiringXSerial = {115200, 7, 'o', 2, 'x'}; int fd = -1; int date_receive = 0; if(wiringXSetup("rock4", NULL) == -1) { wiringXGC(); return -1; } if((fd = wiringXSerialOpen("/dev/ttyS2", wiringXSerial)) < 0) { fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno)); wiringXGC(); return -1; } while(1) { if(wiringXSerialDataAvail(fd) > 0) { date_receive = wiringXSerialGetChar(fd); printf("Data received is: %c.\n", date_receive); } } }
use the following command line to compile it
gcc receive.c -lwiringx -o receive
I2C
i2c_test.c
#include <stdio.h> #include "wiringx.h" #include <unistd.h> int main() { int fd ; if(wiringXSetup("rock4", NULL) == -1) { printf("wiringXSetup failed ...\n"); return -1; } if((fd = wiringXI2CSetup("/dev/i2c-7",0x20)) == -1) { printf("wiringXI2CSetup failed ...\n"); return -1; } while(1) { wiringXI2CWrite(fd,0x5f); sleep(1); wiringXI2CWrite(fd,0x7f); sleep(1); } return 0; }
use the following command line to compile it
gcc i2c_test.c -lwiringx
SPI
spi_test.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stdint.h> #include <string.h> #include <errno.h> #include "wiringx.h" #define TRUE (1==1) #define FALSE (!TRUE) #define SPI_CHAN 0 #define NUM_TIMES 100 #define MAX_SIZE (1024*1024) static int myFd ; void spiSetup (int speed) { if ((myFd = wiringXSPISetup (SPI_CHAN, speed)) < 0) { fprintf (stderr, "Can't open the SPI bus: %s\n", strerror (errno)) ; exit (EXIT_FAILURE) ; } } int main (void) { int speed, times, size ; unsigned int start, end ; int spiFail ; unsigned char *myData ; double timePerTransaction, perfectTimePerTransaction, dataSpeed ; if ((myData = malloc (MAX_SIZE)) == NULL) { fprintf (stderr, "Unable to allocate buffer: %s\n", strerror (errno)) ; exit (EXIT_FAILURE) ; } if(wiringXSetup("radxa_e23", NULL) == -1) { wiringXGC(); return -1; } for (speed = 1 ; speed <= 32 ; speed *= 2) { printf ("+-------+--------+----------+----------+-----------+------------+\n") ; printf ("| MHz | Size | mS/Trans | TpS | Mb/Sec | Latency mS |\n") ; printf ("+-------+--------+----------+----------+-----------+------------+\n") ; spiFail = FALSE ; spiSetup (speed * 1000000) ; for (size = 1 ; size <= MAX_SIZE ; size *= 2) { printf ("| %5d | %6d ", speed, size) ; start = usleep(500); for (times = 0 ; times < NUM_TIMES ; ++times) if (wiringXSPIDataRW (SPI_CHAN, myData, size) == -1) { printf ("SPI failure: %s\n", strerror (errno)) ; spiFail = TRUE ; break ; } end = usleep(500); if (spiFail) break ; timePerTransaction = ((double)(end - start) / (double)NUM_TIMES) / 1000.0 ; dataSpeed = (double)(size * 8) / (1024.0 * 1024.0) / timePerTransaction ; perfectTimePerTransaction = ((double)(size * 8)) / ((double)(speed * 1000000)) ; printf ("| %8.3f ", timePerTransaction * 1000.0) ; printf ("| %8.1f ", 1.0 / timePerTransaction) ; printf ("| %9.5f ", dataSpeed) ; printf ("| %8.5f ", (timePerTransaction - perfectTimePerTransaction) * 1000.0) ; printf ("|\n") ; } close (myFd) ; printf ("+-------+--------+----------+----------+-----------+------------+\n") ; printf ("\n") ; } return 0 ; }
use the following command line to compile it
gcc spi_test.c -lwiringx
Troubleshooting
- If you have an issue, start a new post on the forum. https://forum.radxa.com/.