zoukankan      html  css  js  c++  java
  • FreeRTOS---互斥量在printf的使用

     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!
    ");
    17       //printf("Task1 is running,will be in the ready state!
    ");
    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!
    ");
    31       //printf("Task2 is running!
    ");
    32       osDelay(50);
    33   }
    34   /* USER CODE END Task2 */
    35 }

    直接使用printf输出不做临界保护输出的打印信息如下,有时出现输出打印信息不完整。

    使用Debug_Printf打印信息如下:

  • 相关阅读:
    webpack to package typescript & scss
    start use webpack
    use selenium+chromedriver to taobao automatically
    Use Hexo to Build My Gitee Blog
    Promise调用方式
    导航守卫用法
    VueCli路由配置
    webpack安装vue-loader
    webpack用npm进行局部安装
    JavaScript里的语句用分号结尾是个选项吗
  • 原文地址:https://www.cnblogs.com/mickey-double/p/11430572.html
Copyright © 2011-2022 走看看