Skip to main content

MSS_SPI_set_cmd_handler

Function MSS_SPI_set_cmd_handler 

Source
pub unsafe extern "C" fn MSS_SPI_set_cmd_handler(
    this_spi: *mut mss_spi_instance_t,
    cmd_handler: mss_spi_block_rx_handler_t,
    cmd_size: u32,
)
Expand description

MSS_SPI_set_cmd_handler() specifies a command handler function that is called when the number of bytes received reaches the command size specified as cmd_size parameter.

This function is used by SPI slaves performing block transfers. Its purpose is to allow an SPI slave to decide the data that gets returned to the master while an SPI transaction is taking place. Typically, one of more command bytes are sent by the master to request some specific data. The slave interprets the command byte(s) while one or more turn-around bytes are transmitted. The slave adjusts its transmit data buffer based on the command during the turnaround time.

The following table provides an example of the use of this function where the SPI slave returns data bytes D0 to D6 based on the value of a command. The 3 bytes long command is made up of a command opcode byte followed by an address byte followed by a size byte. The cmd_handler() function specified through an earlier call to MSS_SPI_set_cmd_handler() is called by the SPI driver once the third byte is received. The cmd_handler() function interprets the command bytes and calls MSS_SPI_set_cmd_response() to set the SPI slave’s response transmit buffer with the data to be transmitted after the turnaround bytes (T0 to T3). The number of turnaround bytes must be sufficient to give enough time for the cmd_handler() to execute. The number of turnaround bytes is specified by the protocol used on top of the SPI transport layer, so both the master and slave must adhere to the number of turn around bytes.

TimestampSPI TransactionBytesComments
t0COMMANDC A SC - command opcode byte, A - address byte, S - size byte
t1TURN-AROUNDT0 T1cmd_handler() called here (T0 to T3 are TURN-AROUND bytes)
t2TURN-AROUNDT2 T3MSS_SPI_set_cmd_response() called here by implementation of cmd_handler()
to set the data that is transmitted by the SPI slave.
t3DATAD0 D1 D2 D3 D4 D5 D6Data transmition (SPI slave return data bytes)

@param this_spi this_spi is a pointer to an mss_spi_instance_t structure that identifies the MSS SPI hardware block to be operated.

@param cmd_handler cmd_handler the function that is called when the number of bytes specified by cmd_size parameter has been received. Passing in a null pointer for this disables the command handler and the associated interrupt.

@param cmd_size cmd_size specifies the number of bytes that must be received before calling the command handler function specified by the cmd_handler.

@return This function does not return any value.

@example The following example demonstrates how to configure SPI1 to implement the protocol given as shown in the table above. MSS_SPI_configure_slave_mode() configures SPI1. It sets the receive and transmit buffers. The transmit buffer specified through the call to MSS_SPI_set_slave_block_buffers() specifies the data that gets returned to the master in bytes between t0 and t3. These are the bytes that is sent to the master while the master transmits the command and dummy bytes. The spi1_slave_cmd_handler() function is called by the driver at time t1 after the three command bytes have been received. The spi1_block_rx_handler() function is called by the driver at time t4 when the transaction completes and slave select signal becomes de-asserted. @code #define COMMAND_SIZE 3 #define NB_OF_DUMMY_BYTES 4 #define MAX_TRANSACTION_SIZE 16

uint8_t slave_tx_buffer[COMMAND_SIZE + NB_OF_DUMMY_BYTES]; uint8_t slave_rx_buffer[MAX_TRANSACTION_SIZE];

void configure_slave(void) { MSS_SPI_init(&g_mss_spi1_lo);

MSS_SPI_configure_slave_mode(&g_mss_spi1_lo, MSS_SPI_MODE1, MSS_SPI_BLOCK_TRANSFER_FRAME_SIZE, mss_spi_overflow_handler);

MSS_SPI_set_slave_block_buffers(&g_mss_spi1_lo, slave_tx_buffer, COMMAND_SIZE + NB_OF_DUMMY_BYTES, slave_rx_buffer, sizeof(slave_rx_buffer), spi1_block_rx_handler);

MSS_SPI_set_cmd_handler(&g_mss_spi1_lo, spi1_slave_cmd_handler, COMMAND_SIZE); }

void spi1_slave_cmd_handler(uint8_t* rx_buff, uint32_t rx_size) { uint8_t command; uint8_t address; uint8_t size;

uint8_t * p_response; uint32_t response_size;

command = rx_buff[0]; address = rx_buff[1]; size = rx_buff[2];

p_response = get_response_data(command, address, size, &response_size);

MSS_SPI_set_cmd_response(&g_mss_spi1_lo, p_response, response_size); }

void spi1_block_rx_handler(uint8_t* rx_buff, uint32_t rx_size) { process_rx_data(rx_buff, rx_size); } @endcode