mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-31 19:20:26 +00:00
stm32-l053r8-em9304: use DMA for SPI transfers
This commit is contained in:
parent
0003d56d31
commit
da01dde272
@ -69,13 +69,17 @@ static const uint8_t hal_spi_em9304_read_command[] = {
|
||||
|
||||
const uint8_t hci_reset[] = { 0x01, 0x03, 0x0c, 0x00 };
|
||||
|
||||
static enum {
|
||||
static volatile enum {
|
||||
SPI_EM9304_IDLE,
|
||||
SPI_EM9304_RX_W4_READ_COMMAND_SENT,
|
||||
SPI_EM9304_RX_READ_COMMAND_SENT,
|
||||
SPI_EM9304_RX_W4_DATA_RECEIVED,
|
||||
SPI_EM9304_RX_DATA_RECEIVED,
|
||||
SPI_EM9304_TX_W4_RDY,
|
||||
SPI_EM9304_TX_W4_WRITE_COMMAND_SENT,
|
||||
SPI_EM9304_TX_WRITE_COMMAND_SENT,
|
||||
SPI_EM9304_TX_W4_DATA_SENT,
|
||||
SPI_EM9304_TX_DATA_SENT,
|
||||
} hal_spi_em9304_state;
|
||||
|
||||
#define SPI_EM9304_RX_BUFFER_SIZE 64
|
||||
@ -105,6 +109,43 @@ static void hal_spi_em9304_reset(void){
|
||||
hal_spi_em9304_rx_pos = 0;
|
||||
}
|
||||
|
||||
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi){
|
||||
switch (hal_spi_em9304_state){
|
||||
case SPI_EM9304_RX_W4_READ_COMMAND_SENT:
|
||||
hal_spi_em9304_state = SPI_EM9304_RX_READ_COMMAND_SENT;
|
||||
// trigger run loop;
|
||||
break;
|
||||
case SPI_EM9304_TX_W4_WRITE_COMMAND_SENT:
|
||||
hal_spi_em9304_state = SPI_EM9304_TX_WRITE_COMMAND_SENT;
|
||||
// trigger run loop;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi){
|
||||
switch (hal_spi_em9304_state){
|
||||
case SPI_EM9304_RX_W4_DATA_RECEIVED:
|
||||
hal_spi_em9304_state = SPI_EM9304_RX_DATA_RECEIVED;
|
||||
// trigger run loop;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi){
|
||||
switch (hal_spi_em9304_state){
|
||||
case SPI_EM9304_TX_W4_DATA_SENT:
|
||||
hal_spi_em9304_state = SPI_EM9304_TX_DATA_SENT;
|
||||
// trigger run loop;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void hal_spi_em9304_process(void){
|
||||
uint16_t bytes_to_read;
|
||||
uint16_t bytes_ready;
|
||||
@ -119,11 +160,11 @@ static void hal_spi_em9304_process(void){
|
||||
// chip select
|
||||
HAL_GPIO_WritePin(SPI1_CSN_GPIO_Port, SPI1_CSN_Pin, GPIO_PIN_RESET);
|
||||
|
||||
// send read command
|
||||
HAL_SPI_TransmitReceive(&hspi1, (uint8_t*) hal_spi_em9304_read_command, hal_spi_em9304_slave_status, sizeof(hal_spi_em9304_read_command), HAL_MAX_DELAY);
|
||||
|
||||
// wait for read command sent
|
||||
hal_spi_em9304_state = SPI_EM9304_RX_W4_READ_COMMAND_SENT;
|
||||
|
||||
// send read command
|
||||
HAL_SPI_TransmitReceive_DMA(&hspi1, (uint8_t*) hal_spi_em9304_read_command, hal_spi_em9304_slave_status, sizeof(hal_spi_em9304_read_command));
|
||||
}
|
||||
if (hal_spi_em9304_tx_size){
|
||||
// chip select
|
||||
@ -134,19 +175,22 @@ static void hal_spi_em9304_process(void){
|
||||
}
|
||||
break;
|
||||
|
||||
case SPI_EM9304_RX_W4_READ_COMMAND_SENT:
|
||||
case SPI_EM9304_RX_READ_COMMAND_SENT:
|
||||
bytes_ready = hal_spi_em9304_slave_status[1];
|
||||
bytes_to_read = bytes_ready;
|
||||
if (bytes_to_read > hal_spi_em9304_rx_free_bytes()){
|
||||
bytes_to_read = hal_spi_em9304_rx_free_bytes();
|
||||
}
|
||||
// read all data
|
||||
HAL_SPI_Receive(&hspi1, &hal_spi_em9304_rx_buffer[hal_spi_em9304_rx_pos], bytes_to_read, HAL_MAX_DELAY);
|
||||
hal_spi_em9304_rx_pos += bytes_to_read;
|
||||
|
||||
// wait for data received
|
||||
hal_spi_em9304_state = SPI_EM9304_RX_W4_DATA_RECEIVED;
|
||||
|
||||
// read all data
|
||||
HAL_SPI_Receive_DMA(&hspi1, &hal_spi_em9304_rx_buffer[hal_spi_em9304_rx_pos], bytes_to_read);
|
||||
hal_spi_em9304_rx_pos += bytes_to_read;
|
||||
break;
|
||||
|
||||
case SPI_EM9304_RX_W4_DATA_RECEIVED:
|
||||
case SPI_EM9304_RX_DATA_RECEIVED:
|
||||
// chip deselect
|
||||
HAL_GPIO_WritePin(SPI1_CSN_GPIO_Port, SPI1_CSN_Pin, GPIO_PIN_SET);
|
||||
|
||||
@ -162,14 +206,16 @@ static void hal_spi_em9304_process(void){
|
||||
|
||||
case SPI_EM9304_TX_W4_RDY:
|
||||
if (!hal_spi_em9304_rdy()) break;
|
||||
// send write command
|
||||
HAL_SPI_TransmitReceive(&hspi1, (uint8_t*) hal_spi_em9304_write_command, hal_spi_em9304_slave_status, sizeof(hal_spi_em9304_write_command), HAL_MAX_DELAY);
|
||||
|
||||
// wait for write command sent
|
||||
hal_spi_em9304_state = SPI_EM9304_TX_W4_WRITE_COMMAND_SENT;
|
||||
|
||||
// send write command
|
||||
HAL_SPI_TransmitReceive_DMA(&hspi1, (uint8_t*) hal_spi_em9304_write_command, hal_spi_em9304_slave_status, sizeof(hal_spi_em9304_write_command));
|
||||
|
||||
break;
|
||||
|
||||
case SPI_EM9304_TX_W4_WRITE_COMMAND_SENT:
|
||||
case SPI_EM9304_TX_WRITE_COMMAND_SENT:
|
||||
printf("TX: STS1 0x%02X, STS2 0x%02X\n", hal_spi_em9304_slave_status[0], hal_spi_em9304_slave_status[1]);
|
||||
|
||||
// check slave status and rx buffer space
|
||||
@ -186,14 +232,15 @@ static void hal_spi_em9304_process(void){
|
||||
bytes_to_send = max_bytes_to_send;
|
||||
}
|
||||
|
||||
// send command
|
||||
HAL_SPI_Transmit(&hspi1, (uint8_t*) hal_spi_em9304_tx_data, bytes_to_send, HAL_MAX_DELAY);
|
||||
hal_spi_em9304_tx_size -= bytes_to_send;
|
||||
|
||||
// wait for tx data sent
|
||||
hal_spi_em9304_state = SPI_EM9304_TX_W4_DATA_SENT;
|
||||
|
||||
// send command
|
||||
HAL_SPI_Transmit_DMA(&hspi1, (uint8_t*) hal_spi_em9304_tx_data, bytes_to_send);
|
||||
hal_spi_em9304_tx_size -= bytes_to_send;
|
||||
break;
|
||||
|
||||
case SPI_EM9304_TX_W4_DATA_SENT:
|
||||
case SPI_EM9304_TX_DATA_SENT:
|
||||
// chip deselect
|
||||
HAL_GPIO_WritePin(SPI1_CSN_GPIO_Port, SPI1_CSN_Pin, GPIO_PIN_SET);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user