qq_41295014
求索探知
采纳率100%
2018-10-27 02:06

STM32 HAL库DMA第二次输入不能从buffer头存储

5
已采纳

现在STM32的DMA成功的将接受所得的字符存入buffer,并通过IDIE中断保证了接受数据能够处理,但是当第二次输入的时候,DMA默认将数据存储在第一次输入的数据后面。请问有什么函数能让DMA将第二次(及以后)的输入从buffer头开始存储?
下面是我的代码

main.c

 uint8_t uart2Rx[UART2_BUF_MAX];      
uint16_t uart2RxLength;

int main(void)
{
  /* USER CODE BEGIN 1 */
    int i;
  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
    USR_UartInit();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
    HAL_Delay(1000);
    HAL_UART_Transmit(&huart1, (uint8_t*)"UART1\r\n", 9, 1000);

    for(i=0;i<uart1RxLength;i++)
    {
        HAL_UART_Transmit(&huart1, &uart1Rx[i], 1, 100);
    }

    HAL_Delay(1000);
    HAL_UART_Transmit(&huart1, (uint8_t*)"UART2\r\n", 9, 1000);

    for(i=0;i<uart2RxLength;i++)
    {
        HAL_UART_Transmit(&huart1, &uart2Rx[i], 1, 100);
    }

  }
  /* USER CODE END 3 */

}


void USR_UartInit(void)
{
    uart1RxLength = 0;     
    HAL_UART_Receive_DMA(&huart1, uart1Rx, UART1_BUF_MAX);  
    __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);    

        uart2RxLength = 0;     
    HAL_UART_Receive_DMA(&huart2, uart2Rx, UART2_BUF_MAX);  
    __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE); 
}

st32_f1xxit.c


void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */

  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */
    if((__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE)!=RESET)) 
    {          
          __HAL_UART_DISABLE_IT(&huart1,UART_IT_IDLE); 
            HAL_UART_DMAStop(&huart1);

            uart1RxLength = UART1_BUF_MAX-__HAL_DMA_GET_COUNTER(&hdma_usart1_rx);   

            __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);    
            HAL_UART_Receive_DMA(&huart1, uart1Rx, UART1_BUF_MAX); 
    }
  /* USER CODE END USART1_IRQn 1 */
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

相关推荐