你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

如何使用HAL库,实现不定长UART数据包的DMA接收

[复制链接]
byronsong 提问时间:2016-6-23 09:47 /
本帖最后由 songshiqun2010 于 2019-6-13 13:28 编辑

uint8_t txbuf[1000]="helloworld\r\n";
uint8_t rxbuf[1000]="";
ART_HandleTypeDef huart1;


HAL_UART_Receive_DMA(&huart1,rxbuf,sizeof(rxbuf));

函数原型如下:
/**
  * @brief  Receives an amount of data in non blocking mode.
  * @param  huart: pointer to a UART_HandleTypeDef structure that contains
  *                the configuration information for the specified UART module.
  * @param  pData: Pointer to data buffer
  * @param  Size: Amount of data to be received
  * @note   When the UART parity is enabled (PCE = 1) the data received contain the parity bit.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
  uint32_t *tmp;
  uint32_t tmp1 = 0;

  tmp1 = huart->State;   
  if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_TX))
  {
    if((pData == NULL ) || (Size == 0))
    {
      return HAL_ERROR;
    }

    /* Process Locked */
    __HAL_LOCK(huart);

    huart->pRxBuffPtr = pData;
    huart->RxXferSize = Size;

    huart->ErrorCode = HAL_UART_ERROR_NONE;
    /* Check if a transmit process is ongoing or not */
    if(huart->State == HAL_UART_STATE_BUSY_TX)
    {
      huart->State = HAL_UART_STATE_BUSY_TX_RX;
    }
    else
    {
      huart->State = HAL_UART_STATE_BUSY_RX;
    }

    /* Set the UART DMA transfer complete callback */
    huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;

    /* Set the UART DMA Half transfer complete callback */
    huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;

    /* Set the DMA error callback */
    huart->hdmarx->XferErrorCallback = UART_DMAError;

    /* Enable the DMA Stream */
    tmp = (uint32_t*)&pData;
    HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size);

    /* Enable the DMA transfer for the receiver request by setting the DMAR bit
    in the UART CR3 register */
    huart->Instance->CR3 |= USART_CR3_DMAR;

    /* Process Unlocked */
    __HAL_UNLOCK(huart);

    return HAL_OK;
  }
  else
  {
    return HAL_BUSY;
  }
}

传送门 HAL UART DMA 环形缓冲接收不定长数据包


收藏 评论3 发布时间:2016-6-23 09:47

举报

3个回答
大林林 回答时间:2016-6-23 12:40:07
如果串口接收频率不高,不是可以使用串口自带的空中断,如果连续接收的,那就DMA收定长的数据,然后做好协议,拼接。
楼下有高见吗
ataudio 回答时间:2017-7-11 16:29:48
楼上说的对。如果不是ADC之类的自动转换后的连续收发,没必要用DMA中断的UART,用自带的中断就足够了。

用缓存存储数据,整合后发送或者处理。
一叶清风 回答时间:2018-1-5 22:57:48
谢谢分享

所属标签

相似问题

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版