DMA打印输出函数如下:
#include "stdarg.h"
uint8_t _dbg_TXBuff[150];
void uart1_printf(const char *format,...)
{
uint32_t length;
va_list args;
va_start(args, format);
length = vsnprintf((char*)_dbg_TXBuff, sizeof(_dbg_TXBuff)+1, (char*)format, args);
va_end(args);
HAL_UART_Transmit_DMA(&huart1,_dbg_TXBuff,length);
}
printf打印输出常规模式函数:
#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(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
DMA方式输出的优势
使用DMA方式输出最直接的好处就是可以大大提高CPU利用率,不会由于向外发送数据而对整个程序的运行周期产期变化。