van_BSP_ESPUart.c 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. #include "onechip_include_header.h"
  2. /* ISR is the sole writer of last; foreground is the sole writer of head.
  3. * One slot remains empty to distinguish full from empty. */
  4. uint8_t g_wifi_rx_buf[WIFI_RX_BUF_SIZE];
  5. volatile uint32_t g_wifi_rx_last, g_wifi_rx_head, g_wifi_rx_last_tick;
  6. volatile uint8_t g_wifi_rx_fault;
  7. void esp_receive_Rec_IDLE(UART_HandleTypeDef *huart)
  8. {
  9. uint32_t sr = huart->Instance->SR;
  10. if (sr & (USART_SR_RXNE | USART_SR_ORE | USART_SR_FE | USART_SR_NE)) {
  11. uint8_t byte = (uint8_t)huart->Instance->DR; /* SR then DR clears errors */
  12. if (sr & (USART_SR_ORE | USART_SR_FE | USART_SR_NE)) g_wifi_rx_fault = 2;
  13. if (sr & USART_SR_RXNE) {
  14. uint32_t next = (g_wifi_rx_last + 1) % WIFI_RX_BUF_SIZE;
  15. if (next == g_wifi_rx_head) g_wifi_rx_fault = 1;
  16. else { g_wifi_rx_buf[g_wifi_rx_last] = byte; g_wifi_rx_last = next; }
  17. g_wifi_rx_last_tick = sysinf.conter10ms;
  18. }
  19. }
  20. }
  21. /* Discard only the current snapshot; never reset the ISR write cursor. */
  22. void wifi_receive_clear(void) { g_wifi_rx_head = g_wifi_rx_last; }
  23. void esp_receive_Rec_Start(UART_HandleTypeDef *huart)
  24. {
  25. __HAL_UART_CLEAR_IDLEFLAG(huart);
  26. wifi_receive_clear();
  27. g_wifi_rx_fault = 0;
  28. __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
  29. }