Skip to content
Ankur Yadav edited this page Aug 27, 2015 · 7 revisions

#SPI API

This page contains native API functions for accessing SPI of beaglebone black. Code of this API can be found in spi.c. Sample JNI wrapper functions can be found here. Sample application for SPI can be found here

###SPI API functions description

  1. int spiOpen(const uint8_t bus, const uint8_t device, const uint32_t speed, const uint8_t mode, const uint8_t bpw) : This function takes input bus number, device number, speed of device, mode of device and bits per word for the device and internally opens the file and sets speed mode and bits per word for the device and then returns the file descriptor.

Returns : If successful then SPI file descriptor is returned and if it fails then -1 is returned.

Example :


int fd = spiOpen(1, 0, 10000, 3, 8); // It opens spi bus 1, device 0 with speed 10000, mode 3 and bpw 8 and returns the file descriptor.
  1. int spiTransfer(const int spiFD, const uint8_t tx[], const uint8_t rx[], const int len) : This function takes input SPI file descriptor, tx buffer array to transmit data, rx buffer array to receive data and length of data to be transferred. It then transfers and receives data using ioctl function call. It returns 0 if successful and -1 if it fails.

Returns : 0 if successful and -1 if it fails.

Example :


unsigned char value = 0x08; 
unsigned char null = 0x00;
int ret = spiTransfer(spiFD, &value, &null, 1); // It writes 0x08 at 0x00 address.
  1. int spiReadByte(const int spiFD, const uint8_t regAdd) : It takes spi file descriptor and register address as input and returns the value present at that address.

Returns : value present at that address.

Example :


unsigned char value = spiReadByte(spiFD, 0x00); // It returns the value at address 0x00.
  1. unsigned char spiReadBytes(const int spiFD, const int len, const uint8_t startAdd)* : It takes file descriptor, length of data to be read and starting address of register from which data is to be read and returns a pointer to the array of data bytes read. It is used to get 'length' number of bytes in array from 'startAdd' starting address.

Returns : a pointer to the array of data bytes read.

Example :


unsigned char data[10] ;
data = spiReadBytes(spiFD, 10, 0x00); // It reads 10 bytes of data from starting address 0x00.
  1. int spiWriteRegByte(const int spiFD, const uint8_t regAdd, const uint8_t data) : This function takes file descriptor, address of register to write and byte to be written as input and writes byte provided in the argument at the address provided.

Returns : 0

Example :


spiWriteRegByte(spiFD, 0x2d, 0x08); // It writes 0x08 at address 0x2d.
  1. int spiWriteBytes(const int spiFD, const uint8_t data[], const int len) : It takes spi file descriptor , array of data to be written and length of data to be written as input. It writes length number of data bytes from the data buffer provided.

Returns : 0

Example :


unsigned char data[]="abcd";
spiWriteBytes(spiFD, data, 4); // It writes 4 bytes of data to spi device.
  1. int spiSetMode(const int spiFD, const uint8_t mode) : It takes file descriptor and mode as input and sets the mode provided in the argument to the device represented by file descriptor.

Returns : 0 if successful and -1 if it fails.

Example :


int ret = spiSetMode(spiFD, 3); // It sets mode 3 for spi device.
  1. int spiSetSpeed(const int spiFD, const uint32_t speed) : It takes file descriptor and speed as input and sets the speed provided in the argument to the device represented by file descriptor.

Returns : 0 if successful and -1 if it fails.

Example :


int ret = spiSetSpeed(spiFD, 10000); // It sets speed of spi device.
  1. int spiSetBitsPerWord(const int spiFD, const uint8_t bpw) : It takes file descriptor and 'bpw' bits per word as input and sets the bpw provided in the argument to the device represented by file descriptor.

Returns : 0 if successful and -1 if it fails.

Example :


int ret = spiSetBitsPerWord(spiFD, bpw); // It sets bits per word for spi device.
  1. void spiClose(const int spiFD) : For closing file descriptor.

Example :


spiClose(spiFD); // It closes spi file descriptor.

###Screenshot of SPI android application SPI app

Video Demonstration : SPI app

Clone this wiki locally