From d945680142d5b16d8809ea200833edd97caec2f8 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 26 Oct 2022 16:20:39 +0200 Subject: esp send with dma in seperate function --- stm32f091/consts.h | 1 + stm32f091/esp8266.c | 57 ++++++++++++----------------------------------------- stm32f091/esp8266.h | 19 ++++++++++++++---- stm32f091/test.c | 8 ++++---- 4 files changed, 33 insertions(+), 52 deletions(-) diff --git a/stm32f091/consts.h b/stm32f091/consts.h index 1d8648d..cf7d34f 100644 --- a/stm32f091/consts.h +++ b/stm32f091/consts.h @@ -5,6 +5,7 @@ #define WS_SERVER_PORT "80" #define WS_DMA_RX_BUFFER_SIZE 50 +#define WS_DMA_TX_BUFFER_SIZE 50 #define WS_PINOUT_I2C_SDA_PIN GPIO_PIN_9 #define WS_PINOUT_I2C_SDA_PORT GPIOB diff --git a/stm32f091/esp8266.c b/stm32f091/esp8266.c index 98c4e10..24860a1 100644 --- a/stm32f091/esp8266.c +++ b/stm32f091/esp8266.c @@ -7,57 +7,26 @@ #include "esp8266.h" #include "setup.h" #include "consts.h" +#include "util.h" -char g_ws_esp8266_dma_rx_buffer[WS_DMA_RX_BUFFER_SIZE]; -char MainBuf[WS_DMA_RX_BUFFER_SIZE]; // TODO: remove -int isOK = 0; // TODO: remove -uint16_t g_ws_esp8266_dma_old_pos = 0; -uint16_t g_ws_esp8266_dma_new_pos = 0; - -/* -// when rx receives data handle the message. this function is in stm32. this name needs to stay the same or else it wont work. -void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){ - if(huart->Instance == USART1) { - g_ws_esp8266_dma_old_pos = g_ws_esp8266_dma_new_pos; // Update the last position before copying new data - - if(g_ws_esp8266_dma_old_pos+Size > WS_DMA_RX_BUFFER_SIZE) { // if the current position + new data is greater than the main buffer - uint16_t dataToCopy = WS_DMA_RX_BUFFER_SIZE-g_ws_esp8266_dma_old_pos; // find remaining space left - memcpy ((uint8_t*)MainBuf+g_ws_esp8266_dma_old_pos,g_ws_esp8266_dma_rx_buffer,dataToCopy); // copy data in the remaining space - - g_ws_esp8266_dma_old_pos = 0; // point to the start of the buffer - memcpy ((uint8_t*)MainBuf,(uint8_t*)g_ws_esp8266_dma_rx_buffer+dataToCopy,(Size-dataToCopy)); // copy the remaing data - g_ws_esp8266_dma_new_pos = (Size-dataToCopy); // update position - } - // if data is less than new data - else { - memcpy ((uint8_t*)MainBuf+g_ws_esp8266_dma_old_pos,g_ws_esp8266_dma_rx_buffer,Size); - g_ws_esp8266_dma_new_pos = Size+g_ws_esp8266_dma_old_pos; - } - // start DMA again - // memset(g_ws_esp8266_dma_rx_buffer,'\0',g_ws_esp8266_dma_rx_buffer_Size); // clear rx_buff - HAL_UART_Receive_DMA(&huart1, (uint8_t*) g_ws_esp8266_dma_rx_buffer, WS_DMA_RX_BUFFER_SIZE); - __HAL_DMA_DISABLE_IT(&hdma_usart1_rx,DMA_IT_HT); - } +uint8_t g_ws_esp8266_dma_rx_buffer[WS_DMA_RX_BUFFER_SIZE]; +uint8_t g_ws_esp8266_dma_tx_buffer[WS_DMA_TX_BUFFER_SIZE]; - // check for OK messagge - for(int i=0;i #include -extern char g_ws_esp8266_dma_rx_buffer[WS_DMA_RX_BUFFER_SIZE]; +/** @brief null-terminated rx buffer string */ +extern uint8_t g_ws_esp8266_dma_rx_buffer[WS_DMA_RX_BUFFER_SIZE]; +/** @brief null-terminated tx buffer string */ +extern uint8_t g_ws_esp8266_dma_tx_buffer[WS_DMA_TX_BUFFER_SIZE]; -// void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *, uint16_t); -/** @brief This function handles DMA1 channel 2 to 3 and DMA2 channel 1 to 2 interrupts. */ +/** @brief DMA1 channel 2-3 & DMA2 channel 1-2 interrupt handler */ void DMA1_Ch2_3_DMA2_Ch1_2_IRQHandler(void); -/** @brief This function handles DMA1 channel 1 interrupt. */ +/** @brief DMA1 channel 1 interrupt handler */ void DMA1_Ch1_IRQHandler(void); +/** @brief USART1 interrupt handler */ void USART1_IRQHandler(void); +/** @brief send response to incoming request on specific channel */ +void ws_esp8266_res_send(unsigned int channel, uint8_t* data, size_t size); + +/** @brief send data to esp over uart with dma */ +void ws_esp8266_send(uint8_t* data, size_t size); + +// TODO: remove/update/document/refactor these functions void ws_esp8266_ATsendCommand(uint8_t* data); int ws_esp8266_checkOK(uint8_t *receiveData,int length); int ws_esp8266_receivingMsg(uint8_t *receiveData,int length); diff --git a/stm32f091/test.c b/stm32f091/test.c index 4d1b936..0960d0f 100644 --- a/stm32f091/test.c +++ b/stm32f091/test.c @@ -2,9 +2,9 @@ #include #include +#include "esp8266.h" #include "setup.h" - -uint8_t tx_buffer[] = "AT\r\n"; +#include "util.h" void ws_test_read_task() { uint8_t* buf; // TODO: not working @@ -15,9 +15,9 @@ void ws_test_read_task() { } void ws_test_write_task() { + uint8_t data[] = "AT\r\n"; while (1) { - HAL_UART_Transmit_DMA(&huart1, tx_buffer, sizeof(tx_buffer)); - __HAL_UART_ENABLE_IT(&huart1, UART_IT_TXE); + ws_esp8266_send(data, sizeof(data)); HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); vTaskDelay(portTICK_PERIOD_MS * 1000 * 1); -- cgit v1.2.3