1 #ifdef __GNUC__ 2 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) 3 #else 4 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) 5 #endif 6 7 /*retargets the C library printf function to the USART*/ 8 PUTCHAR_PROTOTYPE 9 { 10 HAL_UART_Transmit(&huart1,(uint8_t*)&ch, 1, 0xFFFF); 11 return ch; 12 }
1 void Debug_Printf(char *format, ...) 2 { 3 char buf_str[128]; 4 va_list v_args; 5 6 7 va_start(v_args, format); 8 (void)vsnprintf((char *)&buf_str[0], 9 (size_t ) sizeof(buf_str), 10 (char const *) format, 11 v_args); 12 va_end(v_args); 13 14 /* 互斥信号量 */ 15 osMutexWait(Mutex_printfHandle,osWaitForever); 16 printf("%s", buf_str); 17 osMutexRelease(Mutex_printfHandle); 18 }
1 /* definition and creation of vTask1 */ 2 osThreadDef(vTask1, Task1, osPriorityNormal, 0, 256); 3 vTask1Handle = osThreadCreate(osThread(vTask1), NULL); 4 5 /* definition and creation of vTask2 */ 6 osThreadDef(vTask2, Task2, osPriorityNormal, 0, 256); 7 vTask2Handle = osThreadCreate(osThread(vTask2), NULL); 8 9 void Task1(void const * argument) 10 { 11 12 /* USER CODE BEGIN Task1 */ 13 /* Infinite loop */ 14 for(;;) 15 { 16 Debug_Printf("Task1 is running,will be in the ready state!\n"); 17 //printf("Task1 is running,will be in the ready state!\n"); 18 osDelay(50); 19 } 20 /* USER CODE END Task1 */ 21 } 22 23 /* Task2 function */ 24 void Task2(void const * argument) 25 { 26 /* USER CODE BEGIN Task2 */ 27 /* Infinite loop */ 28 for(;;) 29 { 30 Debug_Printf("Task2 is running!\n"); 31 //printf("Task2 is running!\n"); 32 osDelay(50); 33 } 34 /* USER CODE END Task2 */ 35 }
直接使用printf输出不做临界保护输出的打印信息如下,有时出现输出打印信息不完整。
使用Debug_Printf打印信息如下: