SPI

This module implements a hardware SPI master. The chip select functionality is not part of this module and must be implemented separately. This allows the application to use the SPI bus to access multiple devices. More information about accessing peripheral IO is in the Device Interface section.

The following is an example of how to read/write the SPI from an application:

#include <unistd.h>
#include <fcntl.h>
#include <dev/sos/spi.h>

int fd;
char buffer[32];
spi_attr_t attr;
fd = open("/dev/spi0", O_RDWR);

attr.o_flags = SPI_FLAG_SET_MASTER | SPI_FLAG_IS_MODE0 | SPI_FLAG_IS_FORMAT_SPI;
attr.width = 8;
attr.freq = 1000000;
attr.pin_assignment.mosi = mcu_pin(0,0);
attr.pin_assignment.miso = mcu_pin(0,1);
attr.pin_assignment.sck = mcu_pin(0,2);
attr.pin_assignment.cs = mcu_pin(0xff,0xff); //only used by slave
ioctl(fd, I_SPI_SETATTR, &attr);

read(fd, buffer, 32);
write(fd, buffer, 32);

close(fd);

For full duplex operation use:

#include <unistd.h>
#include <fcntl.h>
#include <dev/sos/spi.h>

int fd;
spi_attr_t attr;
fd = open("/dev/spi0", O_RDWR);
char read_buffer[32];
char write_buffer[32];
struct aiocb aio;

attr.o_flags = SPI_FLAG_SET_MASTER | SPI_FLAG_IS_MODE0 | SPI_FLAG_IS_FORMAT_SPI | SPI_FLAG_IS_FULL_DUPLEX;
attr.width = 8;
attr.freq = 1000000;
attr.pin_assignment.mosi = mcu_pin(0,0);
attr.pin_assignment.miso = mcu_pin(0,1);
attr.pin_assignment.sck = mcu_pin(0,2);
attr.pin_assignment.cs = mcu_pin(0xff,0xff); //only used by slave
ioctl(fd, I_SPI_SETATTR, &attr);

aio.aio_buf = read_buffer;
aio.aio_nbytes = 32;
aio.aio_fildes = fd;

aio_read(fd, &aio); //this won't do anything on the bus
write(fd, write_buffer, 32); //this will read to read_buffer and write from write_buffer

printf("AIO return is %d\n", aio_return(&aio)); //AIO completes when the write completes

close(fd);

Details

define SPI_VERSION

define SPI_IOC_IDENT_CHAR

define I_SPI_GETVERSION

define I_SPI_GETINFO

define I_SPI_SETATTR

define I_SPI_SETACTION

define I_SPI_SWAP

This tells the SPI bus to get a swap a byte from the bus. spi_ioctl() returns the word read from the bus; The ctl argument to spi_ioctl() is the word to put (not a pointer to spi_attr_t).

The following example puts 0xAC on the bus and stores whatever is read from the bus in swapped_word.

int swapped_word;
swapped_word = ioctl(spi_fd, I_SPI_SWAP, 0xAC);

define I_SPI_TOTAL

enum spi_flag_t
  • SPI_FLAG_IS_FORMAT_SPI SPI Format
  • SPI_FLAG_IS_FORMAT_TI TI Format
  • SPI_FLAG_IS_FORMAT_MICROWIRE Microwire format
  • SPI_FLAG_IS_MODE0 SPI Mode 0
  • SPI_FLAG_IS_MODE1 SPI Mode 1
  • SPI_FLAG_IS_MODE2 SPI Mode 2
  • SPI_FLAG_IS_MODE3 SPI Mode 3
  • SPI_FLAG_SET_MASTER SPI Master
  • SPI_FLAG_SET_SLAVE SPI Slave
  • SPI_FLAG_IS_FULL_DUPLEX Full duplex mode (data must be read async when written syncronously)
  • SPI_FLAG_IS_HALF_DUPLEX Half duplex mode (default mode - will be used if SPI_FLAG_IS_FULL_DUPLEX not set)
  • SPI_FLAG_IS_LSB_FIRST LSB first (non-standard)

struct::spi_info_t

u32 o_flags

Bitmask of supported flags

u32 o_events

Bitmask of supported events

u32 resd

struct::spi_pin_assignment_t

mcu_pin_t miso

MISO pin

mcu_pin_t mosi

MOSI pin

mcu_pin_t sck

Serial clock pin

mcu_pin_t cs

Chip select pin


struct::spi_attr_t

u32 o_flags

Flag bitmask

spi_pin_assignment_t pin_assignment

Pin Assignment

u32 freq

Target operating frequency

u8 width

Width for transactions

u32 resd

X

Thanks for Coming!

Subscribe to news and updates