CubeMX DMA重定向printf

选择串口1 选择为异步模式 参数默认,dma增加串口发送,参数默认即可

 然后在usart.h里包含<stdio.h>头文件。在usart.c文件里加入以下代码:

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  HAL_UART_Transmit_DMA(&huart1,(uint8_t *)&ch,1);
  return ch;
}

然后调用printf即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用STM32CubeMX配置DMA来实现串口通信中的printf功能。下面是一个示例代码,可以将printf输出通过DMA发送到串口: 1. 首先,确保你已经在STM32CubeMX中启用了相关的USART和DMA功能,并正确配置了串口通信的参数。 2. 在生成的代码中找到USART相关的初始化函数,一般位于`usart.c`文件中。在该函数中,找到`HAL_UART_MspInit()`函数的实现。 3. 在`HAL_UART_MspInit()`函数的开头添加以下代码,用于初始化DMA传输相关的资源: ```c /* DMA interrupt init */ /* DMA1_Channel4_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn); ``` 4. 接下来,在`HAL_UART_MspInit()`函数中添加以下代码,用于配置DMA传输: ```c /* Enable the DMA transfer for transmit */ __HAL_RCC_DMA1_CLK_ENABLE(); hdma_usart2_tx.Instance = DMA1_Channel4; hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart2_tx.Init.Mode = DMA_NORMAL; hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW; if (HAL_DMA_Init(&hdma_usart2_tx) != HAL_OK) { Error_Handler(); } /* Associate the initialized DMA handle to the UART handle */ __HAL_LINKDMA(huart, hdmatx, hdma_usart2_tx); /* Enable the DMA transfer complete interrupt */ __HAL_UART_ENABLE_IT(huart, UART_IT_TC); ``` 注意:上述代码中,使用的是USART2和DMA1_Channel4,你需要根据实际情况进行调整。 5. 在生成的代码中找到`USART_Transmit()`函数,一般位于`usart.c`文件中。在该函数中添加以下代码,用于启动DMA传输: ```c /* Set the DMA transfer complete callback */ huart->hdmatx->XferCpltCallback = UART_DMA_TransmitCplt; /* Start the DMA transfer */ HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)str, strlen(str)); ``` 6. 最后,在生成的代码中找到`USART_IRQHandler()`函数,一般位于`stm32f4xx_it.c`文件中。在该函数中添加以下代码,用于处理DMA传输完成的中断: ```c /* USART2 DMA Transmit Complete Interrupt */ void DMA1_Channel4_IRQHandler(void) { /* Check the DMA interrupt flag */ if (__HAL_DMA_GET_IT_SOURCE(huart2.hdmatx, DMA_IT_TC) != RESET) { /* Clear the DMA interrupt flag */ __HAL_DMA_CLEAR_FLAG(huart2.hdmatx, DMA_IT_TC); /* Disable the DMA transfer complete interrupt */ __HAL_DMA_DISABLE_IT(huart2.hdmatx, DMA_IT_TC); /* Call the DMA transmit complete callback function */ HAL_UART_TxCpltCallback(&huart2); } } ``` 完成以上步骤后,你就可以在代码中使用标准的`printf`函数,输出的内容会通过DMA传输到串口中。 希望这对你有帮助!如有更多问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IoT Egr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值