mraa  2.0.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.

/* initialize SPI bus */
spi = mraa_spi_init(SPI_BUS);
if (spi == NULL) {
fprintf(stderr, "Failed to initialize SPI\n");
return EXIT_FAILURE;
}
/* set SPI frequency */
status = mraa_spi_frequency(spi, SPI_FREQ);
if (status != MRAA_SUCCESS)
goto err_exit;
/* set big endian mode */
status = mraa_spi_lsbmode(spi, 0);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
/* MAX7219/21 chip needs the data in word size */
status = mraa_spi_bit_per_word(spi, 16);
if (status != MRAA_SUCCESS) {
fprintf(stdout, "Failed to set SPI Device to 16Bit mode\n");
goto err_exit;
}
/* do not decode bits */
mraa_spi_write_word(spi, 0x0900);
/* brightness of LEDs */
mraa_spi_write_word(spi, 0x0a05);
/* show all scan lines */
mraa_spi_write_word(spi, 0x0b07);
/* set display on */
mraa_spi_write_word(spi, 0x0c01);
/* testmode off */
mraa_spi_write_word(spi, 0x0f00);
while (flag) {
/* set display pattern */
mraa_spi_write_buf_word(spi, pat, 16);
sleep(2);
/* set inverted display pattern */
mraa_spi_write_buf_word(spi, pat_inv, 16);
sleep(2);
/* clear the LED's */
mraa_spi_write_buf_word(spi, pat_clear, 16);
/* cycle through all LED's */
for (i = 1; i <= 8; i++) {
for (j = 0; j < 8; j++) {
mraa_spi_write_word(spi, (i << 8) + (1 << j));
sleep(1);
}
mraa_spi_write_word(spi, i << 8);
}
}
/* stop spi */

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: