stm32cube生成的驱动代码用printf发送uart的方法
md,每次发都会自动减去好多#**,这个论坛程序不行啊。
//////////////////////////////////////////////////////////////
#include "stdio.h"
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif / __GNUC__ /
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
//////////////////////////////////////////////////////////////
上面的是完整的,
下面的是以前写的
在头部添加代码
#include "stdio.h"#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(&huart3 , (uint8_t *)&ch, 1, 0xFFFF); return ch;}
完整的代码例子如下:
#include "stm32f4xx_hal.h"#include "stdio.h"/ Private variables ---------------------------------------------------------/UART_HandleTypeDef huart3;#define BUFFER_SIZE 0x0009//1024uint8_t TxBuffer[BUFFER_SIZE], RxBuffer[BUFFER_SIZE];#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__ // USER CODE BEGIN 0 /void Fill_Buffer(uint8_t *pBuffer, uint16_t BufferLenght, uint32_t Offset);/ USER CODE END 0 // Private function prototypes -----------------------------------------------/static void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_USART3_UART_Init(void);int main(void){ / USER CODE BEGIN 1 / uint8_t message[]="hello world."; / USER CODE END 1 / / MCU Configuration----------------------------------------------------------/ / Reset of all peripherals, Initializes the Flash interface and the Systick. / HAL_Init(); / Configure the system clock / SystemClock_Config(); / Initialize all configured peripherals / MX_GPIO_Init(); MX_USART3_UART_Init(); / USER CODE BEGIN 2 / / USER CODE END 2 / Fill_Buffer (TxBuffer ,BUFFER_SIZE ,0x40);// HAL_UART_Transmit_IT(&huart3 ,(uint8_t *)message,sizeof (message)); / Output a message on Hyperterminal using printf function / //printf("\n\r UART Printf Example: retarget the C library printf function to the UART\n\r");for(uint8_t i=0;i<BUFFER_SIZE ;i++){ printf ("%d \n\r",TxBuffer );} / USER CODE BEGIN 3 / / Infinite loop / while (1) { } / USER CODE END 3 /}/** System Clock Configuration*/static void SystemClock_Config(void){ RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; __PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 25; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; HAL_RCC_OscConfig(&RCC_OscInitStruct); RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1 |RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);}/ USART3 init function /void MX_USART3_UART_Init(void){ huart3.Instance = USART3; huart3.Init.BaudRate = 9600; huart3.Init.WordLength = UART_WORDLENGTH_8B; huart3.Init.StopBits = UART_STOPBITS_1; huart3.Init.Parity = UART_PARITY_NONE; huart3.Init.Mode = UART_MODE_TX_RX; huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart3.Init.OverSampling = UART_OVERSAMPLING_16; HAL_UART_Init(&huart3);}/** Configure pins as * Analog * Input * Output * EVENT_OUT * EXTI*/void MX_GPIO_Init(void){ / GPIO Ports Clock Enable / __GPIOH_CLK_ENABLE(); __GPIOC_CLK_ENABLE();}/ USER CODE BEGIN 4 //** * Function name : Fill_Buffer * @brief Fill the buffer * @param pBuffer: pointer on the Buffer to fill * @param BufferSize: size of the buffer to fill * @param Offset: first value to fill on the Buffer */void Fill_Buffer(uint8_t *pBuffer, uint16_t BufferLenght, uint32_t Offset){ uint16_t IndexTmp = 0; / Put in global buffer same values / for (IndexTmp = 0; IndexTmp < BufferLenght; IndexTmp++ ) { pBuffer[IndexTmp] = IndexTmp + Offset; }}/** * @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(&huart3 , (uint8_t *)&ch, 1, 0xFFFF); return ch;}/ USER CODE END 4 /#ifdef USE_FULL_ASSERT/** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */void assert_failed(uint8_t* file, uint32_t line){ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */}#endif
|