mraa  1.8.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
Functions | Typedefs | Enumerations
spi.h File Reference

API Description

This file defines the spi interface for libmraa. A Spi object in libmraa represents a spidev device. Linux spidev devices are created per spi bus and every chip select available on that bus has another spidev 'file'. A lot more information on spidev devices is available here.

spi = mraa_spi_init(0);
printf("Hello, SPI initialised\n");
uint8_t data[] = { 0x00, 100 };
uint8_t* recv;
while (1) {
int i;
for (i = 90; i < 130; i++) {
data[1] = i;
recv = mraa_spi_write_buf(spi, data, 2);
printf("Writing -%i", i);
printf("RECIVED-%i-%i\n", recv[0], recv[1]);
usleep(100000);
}
for (i = 130; i > 90; i--) {
data[1] = i;
recv = mraa_spi_write_buf(spi, data, 2);
printf("Writing -%i", i);
printf("RECIVED-%i-%i\n", recv[0], recv[1]);
usleep(100000);
}
}

Go to the source code of this file.

Functions

mraa_spi_context mraa_spi_init (int bus)
 
mraa_spi_context mraa_spi_init_raw (unsigned int bus, unsigned int cs)
 
mraa_result_t mraa_spi_mode (mraa_spi_context dev, mraa_spi_mode_t mode)
 
mraa_result_t mraa_spi_frequency (mraa_spi_context dev, int hz)
 
int mraa_spi_write (mraa_spi_context dev, uint8_t data)
 
int mraa_spi_write_word (mraa_spi_context dev, uint16_t data)
 
uint8_t * mraa_spi_write_buf (mraa_spi_context dev, uint8_t *data, int length)
 
uint16_t * mraa_spi_write_buf_word (mraa_spi_context dev, uint16_t *data, int length)
 
mraa_result_t mraa_spi_transfer_buf (mraa_spi_context dev, uint8_t *data, uint8_t *rxbuf, int length)
 
mraa_result_t mraa_spi_transfer_buf_word (mraa_spi_context dev, uint16_t *data, uint16_t *rxbuf, int length)
 
mraa_result_t mraa_spi_lsbmode (mraa_spi_context dev, mraa_boolean_t lsb)
 
mraa_result_t mraa_spi_bit_per_word (mraa_spi_context dev, unsigned int bits)
 
mraa_result_t mraa_spi_stop (mraa_spi_context dev)
 

Typedefs

typedef struct _spi * mraa_spi_context
 

Enumerations

enum  mraa_spi_mode_t { MRAA_SPI_MODE0 = 0, MRAA_SPI_MODE1 = 1, MRAA_SPI_MODE2 = 2, MRAA_SPI_MODE3 = 3 }
 

Function Documentation

mraa_spi_context mraa_spi_init ( int  bus)

Initialise SPI_context, uses board mapping. Sets the muxes

Parameters
busBus to use, as listed in platform definition, normally 0
Returns
Spi context or NULL

Here is the caller graph for this function:

mraa_spi_context mraa_spi_init_raw ( unsigned int  bus,
unsigned int  cs 
)

Initialise SPI_context without any board configuration, selects a bus and a mux.

Parameters
busBus to use as listed by spidev
csChip select to use as listed in spidev
Returns
Spi context or NULL

Here is the caller graph for this function:

mraa_result_t mraa_spi_mode ( mraa_spi_context  dev,
mraa_spi_mode_t  mode 
)

Set the SPI device mode. see spidev 0-3.

Parameters
devThe Spi context
modeThe SPI mode, See Linux spidev
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_spi_frequency ( mraa_spi_context  dev,
int  hz 
)

Set the SPI device operating clock frequency.

Parameters
devthe Spi context
hzthe frequency in hz
Returns
Result of operation

Here is the caller graph for this function:

int mraa_spi_write ( mraa_spi_context  dev,
uint8_t  data 
)

Write Single Byte to the SPI device.

Parameters
devThe Spi context
dataData to send
Returns
Data received on the miso line or -1 in case of error

Here is the caller graph for this function:

int mraa_spi_write_word ( mraa_spi_context  dev,
uint16_t  data 
)

Write Two Bytes to the SPI device.

Parameters
devThe Spi context
dataData to send
Returns
Data received on the miso line

Here is the caller graph for this function:

uint8_t* mraa_spi_write_buf ( mraa_spi_context  dev,
uint8_t *  data,
int  length 
)

Write Buffer of bytes to the SPI device. The pointer return has to be free'd by the caller. It will return a NULL pointer in cases of error.

Parameters
devThe Spi context
datato send
lengthelements within buffer, Max 4096
Returns
Data received on the miso line, same length as passed in

Here is the caller graph for this function:

uint16_t* mraa_spi_write_buf_word ( mraa_spi_context  dev,
uint16_t *  data,
int  length 
)

Write Buffer of uint16 to the SPI device. The pointer return has to be free'd by the caller. It will return a NULL pointer in cases of error.

Parameters
devThe Spi context
datato send
lengthelements (in bytes) within buffer, Max 4096
Returns
Data received on the miso line, same length as passed in

Here is the caller graph for this function:

mraa_result_t mraa_spi_transfer_buf ( mraa_spi_context  dev,
uint8_t *  data,
uint8_t *  rxbuf,
int  length 
)

Transfer Buffer of bytes to the SPI device. Both send and recv buffers are passed in

Parameters
devThe Spi context
datato send
rxbufbuffer to recv data back, may be NULL
lengthelements within buffer, Max 4096
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_spi_transfer_buf_word ( mraa_spi_context  dev,
uint16_t *  data,
uint16_t *  rxbuf,
int  length 
)

Transfer Buffer of uint16 to the SPI device. Both send and recv buffers are passed in

Parameters
devThe Spi context
datato send
rxbufbuffer to recv data back, may be NULL
lengthelements (in bytes) within buffer, Max 4096
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_spi_lsbmode ( mraa_spi_context  dev,
mraa_boolean_t  lsb 
)

Change the SPI lsb mode

Parameters
devThe Spi context
lsbUse least significant bit transmission. 0 for msbi
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_spi_bit_per_word ( mraa_spi_context  dev,
unsigned int  bits 
)

Set bits per mode on transaction, defaults at 8

Parameters
devThe Spi context
bitsbits per word
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_spi_stop ( mraa_spi_context  dev)

De-inits an mraa_spi_context device

Parameters
devThe Spi context
Returns
Result of operation

Here is the caller graph for this function:

Typedef Documentation

typedef struct _spi* mraa_spi_context

Opaque pointer definition to the internal struct _spi

Enumeration Type Documentation

MRAA SPI Modes

Enumerator
MRAA_SPI_MODE0 

CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge, output data (change) on falling edge

MRAA_SPI_MODE1 

CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge

MRAA_SPI_MODE2 

CPOL = 1, CPHA = 0, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge

MRAA_SPI_MODE3 

CPOL = 1, CPHA = 1, Clock idle low, data is clocked in on rising, edge output data (change) on falling edge

Include dependency graph for spi.h: