Please enable javascript, or click here to visit my ecommerce web site powered by Shopify.
Jump to: navigation, search

Difference between revisions of "WiringX"

Line 249: Line 249:
 
./blink rock4 13  
 
./blink rock4 13  
 
</pre>
 
</pre>
 +
  
 
interrupt.c
 
interrupt.c
Line 457: Line 458:
 
gcc receive.c -lwiringx  -o receive
 
gcc receive.c -lwiringx  -o receive
 
</pre>
 
</pre>
 +
  
 
===== I2C =====
 
===== I2C =====
Line 494: Line 496:
 
gcc i2c_test.c -lwiringx
 
gcc i2c_test.c -lwiringx
 
</pre>
 
</pre>
 
  
  
Line 631: Line 632:
 
sudo python3 blink.py
 
sudo python3 blink.py
 
</pre>
 
</pre>
 +
  
 
interrupt.py
 
interrupt.py
Line 661: Line 663:
  
 
</pre>
 
</pre>
 
 
Run the program from the following command line
 
Run the program from the following command line
 
<pre>
 
<pre>
Line 707: Line 708:
 
sudo python3 i2c-test.py
 
sudo python3 i2c-test.py
 
</pre>
 
</pre>
 
 
  
  

Revision as of 06:11, 28 November 2022

WiringX is a GPIO library similar to wiringPi, this document describes how to build and use mraa for ROCK Pi boards.


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 


Python language version of the example

blink.py

import os
import sys
from time import sleep
from wiringX import gpio

gpio.setup(gpio.ROCK4);

gpio.pinMode(gpio.PIN0, gpio.PINMODE_OUTPUT);

try:
	while True:
		gpio.digitalWrite(gpio.PIN0, gpio.HIGH);
		sleep(1);
		gpio.digitalWrite(gpio.PIN0, gpio.LOW);
		sleep(1);
except KeyboardInterrupt:
	pass

Run the program from the following command line

sudo python blink.py

or

sudo python3 blink.py


interrupt.py

import os
import sys
from time import sleep
from wiringX.gpio import gpio

def interrupt(x):
	if x > 0:
		print "interrupt"
	else:
		print "timeout"

gpio.setup(gpio.ROCK4);

gpio.pinMode(gpio.PIN0, gpio.PINMODE_OUTPUT);
gpio.wiringXISR(gpio.PIN1, gpio.ISR_EDGE_BOTH);

try:
	gpio.waitForInterrupt(interrupt, gpio.PIN1, 1000);
	while True:
		gpio.digitalWrite(gpio.PIN0, gpio.HIGH);
		sleep(1);
		gpio.digitalWrite(gpio.PIN0, gpio.LOW);
		sleep(2);	
except KeyboardInterrupt:
	pass;

Run the program from the following command line

sudo python interrupt.py

or

sudo python3 interrupt.py


i2c-test.py

from time import sleep

from wiringX import gpio

# setup wiringX
gpio.setup(gpio.ROCK4)

# get a handle to the sensor, using the default I2C address
fd = gpio.I2CSetup("/dev/i2c-0", 0x48)
while True:
    # read from the default register
    data = gpio.I2CReadReg16(fd, 0x00)
    reg = []
    # calculate the temperature
    reg.append((data>>8)&0xff)
    reg.append(data&0xff)
    res  = (reg[1] << 4) | (reg[0] >> 4)
    res = res * 0.0625

    # print the result
    print(u'Temperature: ' + str(res) + u' C')
    sleep(1)

Run the program from the following command line

sudo python i2c-test.py

or

sudo python3 i2c-test.py


spi-test.py

from time import sleep

from wiringX import gpio

# setup wiringX
gpio.setup(gpio.RASPBERRYPI1B2)

# set up the SPI device
fd = gpio.SPISetup(0, 250000)

while True:
    # write 1 2 3 4 to the display
    data = gpio.SPIDataRW(0, bytearray([0x01,0x02,0x03,0x04]), 4)

    # set the decimal point to position 2
    data = gpio.SPIDataRW(0, bytearray([0x77,0x02]), 2)
    sleep(1)

    # clear the display
    data = gpio.SPIDataRW(0, bytearray([0x76]), 1)
    sleep(1)

Run the program from the following command line

sudo python spi-test.py

or

sudo python3 spi-test.py


Troubleshooting