2019年3月12日-HAL库UART使用DMA收发(一)

HAL库用起来的确相对来说更直观一些,但是用习惯了标准库感觉来回调用函数,有些繁琐,然而CubeMX直接生成HAL库非常好用,还是要学习一下的。

首先Cube中配置完成后,基本不用改什么,直接调用

uint8_t aTxBuffer[] = "*********SENDING DATA with DMA***********\r\n";

HAL_UART_Transmit_DMA(&huart1,aTxBuffer,sizeof(aTxBuffer));// DMA发生数据

就可以实现单次的发送了;

然后为了可以连续发送,需要将HAL_UART_StateTypeDef复位成HAL_UART_STATE_READY;

typedef enum
{
  HAL_UART_STATE_RESET             = 0x00U,    /*!< Peripheral is not yet Initialized
                                                   Value is allowed for gState and RxState */
  HAL_UART_STATE_READY             = 0x20U,    /*!< Peripheral Initialized and ready for use
                                                   Value is allowed for gState and RxState */
  HAL_UART_STATE_BUSY              = 0x24U,    /*!< an internal process is ongoing
                                                   Value is allowed for gState only */
  HAL_UART_STATE_BUSY_TX           = 0x21U,    /*!< Data Transmission process is ongoing
                                                   Value is allowed for gState only */
  HAL_UART_STATE_BUSY_RX           = 0x22U,    /*!< Data Reception process is ongoing
                                                   Value is allowed for RxState only */
  HAL_UART_STATE_BUSY_TX_RX        = 0x23U,    /*!< Data Transmission and Reception process is ongoing
                                                   Not to be used for neither gState nor RxState.
                                                   Value is result of combination (Or) between gState and RxState values */
  HAL_UART_STATE_TIMEOUT           = 0xA0U,    /*!< Timeout state
                                                   Value is allowed for gState only */
  HAL_UART_STATE_ERROR             = 0xE0U     /*!< Error
                                                   Value is allowed for gState only */
} HAL_UART_StateTypeDef;

定位到发送完成函数,官方说了别改它,要用的话在自己的文件中实现去;

2019年3月12日-HAL库UART使用DMA收发(一)_第1张图片
好吧,在自己的文件中定义一下;

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{ 
  huart->gState = HAL_UART_STATE_READY;
}

基本的发送功能就这样了。


串口定长接收函数,数据会存储到*aRxBuffer中;

uint8_t aRxBuffer[64];      //接收缓存
HAL_UART_Receive_DMA(&huart1,aRxBuffer,26);// 启动DMA接收

不定长,待续。。。

你可能感兴趣的:(STM32)