SPI Slave

Description

SPI Slave Interface driver.

Flexible SPI Slave driver implementation for use in an NCP scenario.

This driver will support both blocking and non-blocking operation, with LDMA backing the background transfers to support nonblocking.

Functions

void spislave_init (void)
void spislave_deinit (void)
int32_t spislave_sendBuffer (uint8_t *buffer, size_t length)
int32_t spislave_sendByte (uint8_t byte)
size_t spislave_getTxBytesLeft (void)
void spislave_enableTransmitter (bool enable)
void spislave_enableReceiver (bool enable)
size_t spislave_getRxAvailableBytes (void)
int32_t spislave_receiveBuffer (uint8_t *buffer, size_t requestedLength, size_t *receivedLength, bool blocking, uint32_t timeout)
int32_t spislave_receiveByte (uint8_t *byte)
void spislave_flush (bool flushTx, bool flushRx)

Function Documentation

void spislave_init ( void )

Initialize the configured USART peripheral for SPI slave operation. Also sets up GPIO settings for MOSI, MISO, SCLK and SS. After initialization, the SPI slave will have RX enabled.

Returns
BOOTLOADER_OK if succesful, error code otherwise
void spislave_deinit ( void )

Disable the configured USART peripheral for SPI operation.

int32_t spislave_sendBuffer ( uint8_t * buffer,
size_t length
)

Write a data buffer to the master next time the master starts clocking SCLK. This transfer will be non-blocking, and its progress can be tracked through spislave_getTxBytesLeft

Parameters
[in] buffer The data buffer to send
[in] length Number of bytes in the buffer to send
Returns
BOOTLOADER_OK if successful, error code otherwise
int32_t spislave_sendByte ( uint8_t byte )

Write one byte to the master in a blocking fashion.

Warning
If the master goes down, this will block forever.
Parameters
[in] byte The byte to send
Returns
BOOTLOADER_OK if succesful, error code otherwise
size_t spislave_getTxBytesLeft ( void )

Gets the amount of bytes left in the TX data buffer.

Returns
Number of bytes in the transmit buffer still needing to go out
void spislave_enableTransmitter ( bool enable )

Enable/disable MISO output

Parameters
[in] enable True to enable the transmitter, false to disable
void spislave_enableReceiver ( bool enable )

Enable/disable receiving bytes from the master into our internal buffer. The purpose of this function is to avoid filling up the buffer with 0xFF while a master is polling for new data.

Parameters
[in] enable True to enable the receiver, false to disable
size_t spislave_getRxAvailableBytes ( void )

Gets the amount of bytes ready for reading.

Returns
Number of bytes in the receive buffer available for reading with spislave_receiveBuffer
int32_t spislave_receiveBuffer ( uint8_t * buffer,
size_t requestedLength,
size_t * receivedLength,
bool blocking,
uint32_t timeout
)

Read from the RX buffer into a local buffer.

Parameters
[out] buffer The data buffer to receive into
[in] requestedLength Number of bytes we'd like to read
[out] receivedLength Number of bytes read
[in] blocking Indicate whether we should wait for requestedLength bytes to be available and read before returning, or we can read out whatever is currently in the buffer and return.
[in] timeout Number of milliseconds to wait for data in blocking mode
Returns
BOOTLOADER_OK if succesful, error code otherwise
int32_t spislave_receiveByte ( uint8_t * byte )

Get one byte from the SPI slave in a blocking fashion.

Warning
If the master never clocks in a byte, this function will block forever.
Parameters
[out] byte Pointer to where to put the received byte
Returns
BOOTLOADER_OK if succesful, error code otherwise
void spislave_flush ( bool flushTx,
bool flushRx
)

Flush one or both buffers.

Parameters
[in] flushTx Flush the transmit buffer when true
[in] flushRx Flush the receive buffer when true