group::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

defineSPI_VERSION


defineSPI_IOC_IDENT_CHAR


defineI_SPI_GETVERSION


defineI_SPI_GETINFO


defineI_SPI_SETATTR


defineI_SPI_SETACTION


defineI_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);

defineI_SPI_TOTAL


enumspi_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)


date: “2019-06-27” layout: post title: struct::spi_info_t katex: true

categories: struct

Methods

Details

public u32o_flags

Bitmask of supported flags


public u32o_events

Bitmask of supported events


public u32resd



date: “2019-06-27” layout: post title: struct::spi_pin_assignment_t katex: true

categories: struct

Methods

Details

publicmcu_pin_tmiso

MISO pin


publicmcu_pin_tmosi

MOSI pin


publicmcu_pin_tsck

Serial clock pin


publicmcu_pin_tcs

Chip select pin



date: “2019-06-27” layout: post title: struct::spi_attr_t katex: true

categories: struct

Methods

Details

public u32o_flags

Flag bitmask


publicspi_pin_assignment_tpin_assignment

Pin Assignment


public u32freq

Target operating frequency


public u8width

Width for transactions


public u32resd


X

Thanks for Coming!

Subscribe to news and updates