123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- /*
- * uartio.c
- *
- * Created on: Jan 21, 2022
- * Author: radioman
- */
- #include "main.h"
- #include "uartio.h"
- #include <string.h>
- static uint8_t uart_rx_buffer[UART_RX_BUF_SIZE];
- static uint8_t uart_tx_buffer[UART_TX_BUF_SIZE];
- static uint32_t rd_ptr;
- static uint8_t tx_cplt = 1;
- void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
- {
- tx_cplt = 1;
- }
- void UARTIO_Init(UART_HandleTypeDef *huart)
- {
- HAL_UART_Receive_DMA(huart, uart_rx_buffer, UART_RX_BUF_SIZE);
- rd_ptr = 0;
- }
- uint32_t UARTIO_DataAvail(UART_HandleTypeDef *huart)
- {
- if((UART_RX_BUF_SIZE - huart->hdmarx->Instance->CNDTR) >= rd_ptr)
- return (UART_RX_BUF_SIZE - huart->hdmarx->Instance->CNDTR) - rd_ptr;
- else
- return (2 * UART_RX_BUF_SIZE) - rd_ptr - huart->hdmarx->Instance->CNDTR;
- }
- uint32_t UARTIO_Receive(UART_HandleTypeDef *huart, uint8_t *buf)
- {
- uint32_t dma_ptr = (UART_RX_BUF_SIZE - huart->hdmarx->Instance->CNDTR);
- if(dma_ptr > rd_ptr)
- {
- uint32_t data_avail = dma_ptr - rd_ptr;
- memcpy(&buf[0], &uart_rx_buffer[rd_ptr], data_avail);
- rd_ptr = dma_ptr;
- return (data_avail);
- }
- else if(dma_ptr < rd_ptr)
- {
- uint32_t blk1_sz = UART_RX_BUF_SIZE - rd_ptr;
- memcpy(&buf[0], &uart_rx_buffer[rd_ptr], blk1_sz);
- memcpy(&buf[blk1_sz], &uart_rx_buffer[0], dma_ptr);
- rd_ptr = dma_ptr;
- return (blk1_sz + dma_ptr);
- }
- else
- {
- return 0;
- }
- }
- uint32_t UARTIO_Transmit(UART_HandleTypeDef *huart, uint8_t *buf, uint32_t len)
- {
- memcpy(uart_tx_buffer, buf, len);
- while(!tx_cplt);
- HAL_UART_Transmit_DMA(huart, uart_tx_buffer, len);
- tx_cplt = 0;
- return len;
- }
|