Contiki-NG
Files | Data Structures | Functions
SPI Hardware Abstraction Layer

The SPI HAL provides a set of common functions that can be used in a platform-independent fashion. More...

Files

file  spi.c
 Implementation of the platform-independent aspects of the SPI HAL.
 
file  spi.h
 Header file for the SPI HAL.
 

Data Structures

struct  spi_device
 SPI Device Configuration. More...
 

Functions

spi_status_t spi_acquire (spi_device_t *dev)
 Locks and then opens an SPI controller. More...
 
spi_status_t spi_release (spi_device_t *dev)
 Closes and then unlocks an SPI controller. More...
 
spi_status_t spi_select (spi_device_t *dev)
 Selects the SPI peripheral. More...
 
spi_status_t spi_deselect (spi_device_t *dev)
 Deselects the SPI peripheral. More...
 
bool spi_has_bus (spi_device_t *dev)
 Checks if a device has locked an SPI controller. More...
 
spi_status_t spi_write_byte (spi_device_t *dev, uint8_t data)
 Writes a single byte to an SPI device. More...
 
spi_status_t spi_write (spi_device_t *dev, const uint8_t *data, int size)
 Writes a buffer to an SPI device. More...
 
spi_status_t spi_read_byte (spi_device_t *dev, uint8_t *data)
 Reads a single byte from an SPI device. More...
 
spi_status_t spi_read (spi_device_t *dev, uint8_t *data, int size)
 Reads a buffer from an SPI device. More...
 
spi_status_t spi_read_skip (spi_device_t *dev, int size)
 Reads and ignores data from an SPI device. More...
 
spi_status_t spi_transfer (spi_device_t *dev, const uint8_t *data, int wsize, uint8_t *buf, int rsize, int ignore)
 Performs a generic SPI transfer. More...
 
spi_status_t spi_read_register (spi_device_t *dev, uint8_t reg, uint8_t *data, int size)
 Reads a buffer of bytes from a register of an SPI device. More...
 
spi_status_t spi_strobe (spi_device_t *dev, uint8_t strobe, uint8_t *status)
 Reads and Writes one byte from/to an SPI device. More...
 
bool spi_arch_has_lock (spi_device_t *dev)
 Checks if a device has locked an SPI controller. More...
 
bool spi_arch_is_bus_locked (spi_device_t *dev)
 Checks if an SPI controller is locked by any device. More...
 
spi_status_t spi_arch_lock_and_open (spi_device_t *dev)
 Locks and opens an SPI controller to the configuration specified. More...
 
spi_status_t spi_arch_close_and_unlock (spi_device_t *dev)
 Closes and unlocks an SPI controller. More...
 
spi_status_t spi_arch_transfer (spi_device_t *dev, const uint8_t *data, int wlen, uint8_t *buf, int rlen, int ignore_len)
 Performs an SPI transfer. More...
 
spi_status_t spi_arch_select (spi_device_t *dev)
 Selects an SPI device. More...
 
spi_status_t spi_arch_deselect (spi_device_t *dev)
 Deselects an SPI device. More...
 
typedef struct spi_device spi_device_t
 SPI Device Configuration. More...
 

Detailed Description

The SPI HAL provides a set of common functions that can be used in a platform-independent fashion.

Typedef Documentation

typedef struct spi_device spi_device_t

SPI Device Configuration.

This is a structure to an architecture-independent SPI configuration.

Function Documentation

spi_status_t spi_acquire ( spi_device_t dev)

Locks and then opens an SPI controller.

Parameters
devAn SPI device configuration which defines the controller to be locked and the opening configuration.
Returns
SPI return code

Definition at line 46 of file spi.c.

References spi_arch_lock_and_open().

Referenced by ext_flash_open().

spi_status_t spi_arch_close_and_unlock ( spi_device_t dev)

Closes and unlocks an SPI controller.

Parameters
devAn SPI device configuration that specifies the controller.
Returns
SPI return code

This should turn off the SPI controller to put it in low power mode and unlock it. It should work only if the device has already locked the SPI controller.

Definition at line 268 of file spi-arch.c.

References mutex_unlock, spi_arch_has_lock(), and SYS_CTRL_RCGCSSI.

Referenced by spi_release().

spi_status_t spi_arch_deselect ( spi_device_t dev)

Deselects an SPI device.

Parameters
devAn SPI device configuration that specifies the CS pin.
Returns
SPI return code

Set the CS pin. Locking the SPI controller is not needed.

Definition at line 298 of file spi-arch.c.

Referenced by spi_deselect().

bool spi_arch_has_lock ( spi_device_t dev)

Checks if a device has locked an SPI controller.

Parameters
devAn SPI device configuration which defines the controller to be checked if it is locked and the respective device.
Returns
1 if the device has the lock, 0 otherwise.

Definition at line 151 of file spi-arch.c.

Referenced by spi_arch_close_and_unlock(), spi_arch_select(), spi_arch_transfer(), spi_has_bus(), spi_read(), spi_read_byte(), spi_read_skip(), spi_strobe(), spi_transfer(), spi_write(), and spi_write_byte().

bool spi_arch_is_bus_locked ( spi_device_t dev)

Checks if an SPI controller is locked by any device.

Parameters
devAn SPI device configuration which defines the controller to be checked.
Returns
1 if the controller is locked, 0 otherwise.

Definition at line 161 of file spi-arch.c.

spi_status_t spi_arch_lock_and_open ( spi_device_t dev)

Locks and opens an SPI controller to the configuration specified.

Parameters
devAn SPI device configuration.
Returns
SPI return code

This should work only if the device has already locked the SPI controller.

Definition at line 171 of file spi-arch.c.

References GPIO_PERIPHERAL_CONTROL, GPIO_PIN_MASK, GPIO_PORT_TO_BASE, GPIO_SET_OUTPUT, GPIO_SET_PIN, GPIO_SOFTWARE_CONTROL, IOC_OVERRIDE_DIS, ioc_set_over(), ioc_set_sel(), mutex_try_lock, SSI_CC, SSI_CPSR, SSI_CR0, SSI_CR0_SCR_M, SSI_CR0_SCR_S, SSI_CR0_SPH, SSI_CR0_SPO, SSI_CR1, SSI_CR1_SSE, and SYS_CTRL_RCGCSSI.

Referenced by spi_acquire().

spi_status_t spi_arch_select ( spi_device_t dev)

Selects an SPI device.

Parameters
devAn SPI device configuration that specifies the CS pin.
Returns
SPI return code

Clears the CS pin. It should work only if the device has already locked the SPI controller.

Definition at line 285 of file spi-arch.c.

References spi_arch_has_lock().

Referenced by spi_select().

spi_status_t spi_arch_transfer ( spi_device_t dev,
const uint8_t *  data,
int  wlen,
uint8_t *  buf,
int  rlen,
int  ignore_len 
)

Performs an SPI transfer.

Parameters
devAn SPI device configuration that specifies the controller.
dataA pointer to the data to be written. Set it to NULL to skip writing.
wlenLength of data to write.
bufA pointer to buffer to copy the data read. Set to NULL to skip reading.
rlenLength of data to read.
ignore_lenLength of data to read and ignore.
Returns
SPI return code

It should work only if the device has already locked the SPI controller. A total of rlen+ignore_len bytes will be read. The first rlen bytes will be copied to buf. The remaining ignore_len bytes won't be copied to the buffer. The maximum of wlen and rlen+ignore_len of bytes will be transfered.

Definition at line 307 of file spi-arch.c.

References spi_arch_has_lock().

Referenced by spi_read(), spi_read_byte(), spi_read_skip(), spi_strobe(), spi_transfer(), spi_write(), and spi_write_byte().

spi_status_t spi_deselect ( spi_device_t dev)

Deselects the SPI peripheral.

Parameters
devAn SPI device configuration which defines the CS pin.
Returns
SPI return code

Sets the CS pin. Lock is not required.

Definition at line 74 of file spi.c.

References spi_arch_deselect().

Referenced by deselect().

bool spi_has_bus ( spi_device_t dev)

Checks if a device has locked an SPI controller.

Parameters
devAn SPI device configuration which defines the controller.
Returns
true if the device has the lock, false otherwise.

Definition at line 80 of file spi.c.

References spi_arch_has_lock().

spi_status_t spi_read ( spi_device_t dev,
uint8_t *  data,
int  size 
)

Reads a buffer from an SPI device.

Parameters
devAn SPI device configuration.
dataA pointer to the data
sizeSize of the data to read
Returns
SPI return code

It should work only if the device has already locked the SPI controller.

Definition at line 132 of file spi.c.

References spi_arch_has_lock(), and spi_arch_transfer().

Referenced by ext_flash_read(), spi_read_register(), verify_part(), and wait_ready().

spi_status_t spi_read_byte ( spi_device_t dev,
uint8_t *  data 
)

Reads a single byte from an SPI device.

Parameters
devAn SPI device configuration.
dataA pointer to a byte of data
Returns
SPI return code

It should work only if the device has already locked the SPI controller.

Definition at line 118 of file spi.c.

References spi_arch_has_lock(), and spi_arch_transfer().

spi_status_t spi_read_register ( spi_device_t dev,
uint8_t  reg,
uint8_t *  data,
int  size 
)

Reads a buffer of bytes from a register of an SPI device.

Parameters
devAn SPI device configuration.
regRegister
dataA pointer to the data
sizeSize of the data to read
Returns
SPI return code

It should work only if the device has already locked the SPI controller.

Definition at line 184 of file spi.c.

References spi_read(), and spi_write_byte().

spi_status_t spi_read_skip ( spi_device_t dev,
int  size 
)

Reads and ignores data from an SPI device.

Parameters
devAn SPI device configuration.
sizeSize of the data to read and ignore
Returns
SPI return code

Reads size bytes from the SPI and throws them away. It should work only if the device has already locked the SPI controller.

Definition at line 146 of file spi.c.

References spi_arch_has_lock(), and spi_arch_transfer().

spi_status_t spi_release ( spi_device_t dev)

Closes and then unlocks an SPI controller.

Parameters
devAn SPI device configuration which defines the controller to be closed and unlocked.
Returns
SPI return code

Releasing an SPI controller should put it in low-power mode. This should work only if the device has already locked the SPI controller.

Definition at line 57 of file spi.c.

References spi_arch_close_and_unlock().

Referenced by ext_flash_close(), and ext_flash_open().

spi_status_t spi_select ( spi_device_t dev)

Selects the SPI peripheral.

Parameters
devAn SPI device configuration which defines the CS pin.
Returns
SPI return code

Clears the CS pin. This should work only if the device has already locked the SPI controller.

Definition at line 68 of file spi.c.

References spi_arch_select().

Referenced by select_on_bus().

spi_status_t spi_strobe ( spi_device_t dev,
uint8_t  strobe,
uint8_t *  status 
)

Reads and Writes one byte from/to an SPI device.

Parameters
devAn SPI device configuration.
strobeByte to write
statusPointer to byte to read
Returns
SPI return code

It should work only if the device has already locked the SPI controller.

Definition at line 207 of file spi.c.

References spi_arch_has_lock(), and spi_arch_transfer().

spi_status_t spi_transfer ( spi_device_t dev,
const uint8_t *  data,
int  wsize,
uint8_t *  buf,
int  rsize,
int  ignore 
)

Performs a generic SPI transfer.

Parameters
devAn SPI device configuration.
dataA pointer to the data to be written. Set it to NULL to skip writing.
wsizeSize of data to write.
bufA pointer to buffer to copy the data read. Set to NULL to skip reading.
rsizeSize of data to read.
ignoreSize of data to read and ignore.
Returns
SPI return code

It should work only if the device has already locked the SPI controller. A total of rlen+ignore_len bytes will be read. The first rlen bytes will be copied to buf. The remaining ignore_len bytes won't be copied to the buffer. The maximum of wlen and rlen+ignore_len of bytes will be transfered.

Definition at line 160 of file spi.c.

References spi_arch_has_lock(), and spi_arch_transfer().

spi_status_t spi_write ( spi_device_t dev,
const uint8_t *  data,
int  size 
)

Writes a buffer to an SPI device.

Parameters
devAn SPI device configuration.
dataA pointer to the data
sizeSize of the data to write
Returns
SPI return code

It should work only if the device has already locked the SPI controller.

Definition at line 104 of file spi.c.

References spi_arch_has_lock(), and spi_arch_transfer().

Referenced by ext_flash_erase(), ext_flash_read(), ext_flash_write(), power_standby(), verify_part(), wait_ready(), and write_enable().

spi_status_t spi_write_byte ( spi_device_t dev,
uint8_t  data 
)

Writes a single byte to an SPI device.

Parameters
devAn SPI device configuration.
dataA byte of data
Returns
SPI return code

It should work only if the device has already locked the SPI controller.

Definition at line 90 of file spi.c.

References spi_arch_has_lock(), and spi_arch_transfer().

Referenced by power_down(), and spi_read_register().