From ed37535d1fc382d98657d865b99c888c565ad667 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Sun, 25 Jun 2017 19:59:43 +0200 Subject: [PATCH] stm32-l053r8-em9304: complete hal_uart_dma implementation --- port/stm32-l053r8-em9304/port.c | 135 +++++++++++++++++++++----------- 1 file changed, 91 insertions(+), 44 deletions(-) diff --git a/port/stm32-l053r8-em9304/port.c b/port/stm32-l053r8-em9304/port.c index fc35da18b..c7f4fcaca 100644 --- a/port/stm32-l053r8-em9304/port.c +++ b/port/stm32-l053r8-em9304/port.c @@ -89,8 +89,11 @@ static uint8_t hal_spi_em9304_slave_status[2]; static uint8_t hal_spi_em9304_rx_buffer[SPI_EM9304_RX_BUFFER_SIZE]; static uint16_t hal_spi_em9304_rx_pos; -static const uint8_t * hal_spi_em9304_tx_data; -static uint16_t hal_spi_em9304_tx_size; +static const uint8_t * hal_uart_dma_tx_data; +static uint16_t hal_uart_dma_tx_size; + +static uint8_t * hal_uart_dma_rx_buffer; +static uint16_t hal_uart_dma_rx_len; static void dummy_handler(void); @@ -100,7 +103,6 @@ static void (*tx_done_handler)(void) = &dummy_handler; // test static int test_done; -static int event_received; static volatile int run_loop_triggered; static inline void hal_spi_em9304_trigger_run_loop(void){ @@ -162,12 +164,35 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){ } } +static void hal_spi_em9304_transfer_rx_data(void){ + // log_info("hal_spi_em9304_transfer_rx_data: rx buffer %u -> hci buffer %u\n", hal_spi_em9304_rx_pos, hal_uart_dma_rx_len); + while (hal_spi_em9304_rx_pos && hal_uart_dma_rx_len){ + uint16_t bytes_to_copy = hal_uart_dma_rx_len; + if (hal_uart_dma_rx_len > hal_spi_em9304_rx_pos){ + bytes_to_copy = hal_spi_em9304_rx_pos; + } + memcpy(hal_uart_dma_rx_buffer, hal_spi_em9304_rx_buffer, bytes_to_copy); + hal_uart_dma_rx_buffer += bytes_to_copy; + hal_uart_dma_rx_len -= bytes_to_copy; + hal_spi_em9304_rx_pos -= bytes_to_copy; + + // shift rest of data - could be skipped if ring buffer is used + if (hal_spi_em9304_rx_pos){ + // log_info("move %u bytes down\n", hal_spi_em9304_rx_pos); + memmove(hal_spi_em9304_rx_buffer, &hal_spi_em9304_rx_buffer[bytes_to_copy], hal_spi_em9304_rx_pos); + } + + if (hal_uart_dma_rx_len == 0){ + (*rx_done_handler)(); + } + } +} + static void hal_spi_em9304_process(void){ uint16_t bytes_to_read; uint16_t bytes_ready; uint16_t max_bytes_to_send; uint16_t bytes_to_send; - int i; switch (hal_spi_em9304_state){ case SPI_EM9304_IDLE: @@ -182,7 +207,7 @@ static void hal_spi_em9304_process(void){ // 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){ + if (hal_uart_dma_tx_size){ // chip select HAL_GPIO_WritePin(SPI1_CSN_GPIO_Port, SPI1_CSN_Pin, GPIO_PIN_RESET); @@ -211,13 +236,10 @@ static void hal_spi_em9304_process(void){ HAL_GPIO_WritePin(SPI1_CSN_GPIO_Port, SPI1_CSN_Pin, GPIO_PIN_SET); // done - printf("EVT: "); - for (i=0;i max_bytes_to_send){ bytes_to_send = max_bytes_to_send; } @@ -252,8 +274,8 @@ static void hal_spi_em9304_process(void){ 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; + HAL_SPI_Transmit_DMA(&hspi1, (uint8_t*) hal_uart_dma_tx_data, bytes_to_send); + hal_uart_dma_tx_size -= bytes_to_send; break; case SPI_EM9304_TX_DATA_SENT: @@ -291,12 +313,17 @@ int hal_uart_dma_set_baud(uint32_t baud){ } void hal_uart_dma_send_block(const uint8_t *buffer, uint16_t length){ - hal_spi_em9304_tx_data = hci_reset; - hal_spi_em9304_tx_size = sizeof(hci_reset); + hal_uart_dma_tx_data = buffer; + hal_uart_dma_tx_size = length; hal_spi_em9304_process(); } -void hal_uart_dma_receive_block(uint8_t *buffer, uint16_t len){ +void hal_uart_dma_receive_block(uint8_t *buffer, uint16_t length){ + // log_info("hal_uart_dma_receive_block: len %u\n", length); + hal_uart_dma_rx_buffer = buffer; + hal_uart_dma_rx_len = length; + hal_spi_em9304_transfer_rx_data(); + hal_spi_em9304_process(); } void hal_uart_dma_set_csr_irq_handler( void (*csr_irq_handler)(void)){ @@ -307,8 +334,42 @@ void hal_uart_dma_set_sleep(uint8_t sleep){ // -void port_tx_done(void){ +static int port_test_state = 0; +static int port_test_event_len; +static uint8_t event[10]; +void port_tx_done(void){ + printf("HCI Reset sent\n"); +} + +void port_rx_done(void){ + + printf("EVT: "); + int i; + for (i=0;i