Hello: We are considering a new HAL for the SPI. Following is the set of API we are considering for this HAL. Comments are appreciated. NOTE: the term “value” is used instead of “byte” as SPI interfaces may use more than 8 bits per value transferred.
NOTE: the spi_config structure is not shown here. It will contain the receive callback and the transmit done callback along with other generic SPI configuration. typedef int (*hal_spi_rx_val)(void *arg, uint8_t val); This is the prototype for the receive callback. Called for each received spi value at interrupt context when using the hal_spi_start_rx() API. typedef void (*hal_spi_tx_done)(void *arg); Callback when buffer transferred using the hal_spi_txrx API. int hal_spi_init(int spi_num, void *cfg) Initializes the spi with processor specific configuration. This does not turn on the SPI nor does it enable it from a power perspective. int hal_spi_config(int spi_num, struct spi_config *cfg); Used to configure generic SPI information after initialization. This will contain the receive callback as well as generic information like # data bits, baudrate, mode, etc. Also will include the callbacks. int hal_spi_enable(int spi_num); Turns the SPI on from a power perspective int hal_spi_disable(int spi_num); Turns the SPI off from a power perspective uint16_t hal_spi_tx_val(int spi_num, uint16_t val); Blocking call to send a value on the SPI. Returns the value received from the SPI. MASTER: Sends the value and returns the received value from the slave. SLAVE: Sets the value to send to the master when the master transfers a value. This value will be sent until another call to hal_spi_tx_val() is made. The return code is ignored for a slave. int hal_spi_txrx(int spi_num, void *txbuf, void *rxbuf, int len); Non-blocking call to send a buffer and also store the received values. For both the master and slave, the transmit done callback is executed at interrupt context when the buffer is sent. If rxbuf is NULL, no received values are stored. There is no callback per value when using this API MASTER: master sends all the values in the buffer and stores the values in the receive buffer if rxbuf is not NULL. SLAVE: Slave preloads the data to be sent to the master (values stored in txbuf) and places received data from master in rxbuf (if not NULL). No callback per received value; transmit done callback when len values are transferred. int hal_spi_start_rx(int spi_num); Enables the SPI receiver. Only valid for slaves. Returns error if the receive is not able to be started. There will be a receive callback per value. int hal_spi_abort(int spi_num); This aborts the current transfer but keeps the spi enabled. void hal_spi_set_rx_val_cb(int spi_num, hal_spi_rx_val rx_val_cb, void *arg); Sets the callback executed when a slave receives a value. Allowed to be called when the spi is enabled but not when a transfer is in progress. void hal_spi_set_tx_done_cb(int spi_num, hal_spi_tx_done tx_done_cb, void *arg); Sets the callback executed the buffer is transferred by the master or slave using the hal_spi_txrx API. Allowed to be called when the SPI is enabled but not during a transfer.
