|
目前正在学习ucos,使用的代码版本为ucos 2.92,现在使用多任务单串口打印的时候,发现容易丢字符的现象,即使使用互斥信号量依旧存在该问题,话不多说,上代码给大家看看
main函数代码
- int main(void)
- {
- CPU_INT08U err;
- Bsp_Init();
-
- OSInit(); /* Initialize "uC/OS-II, The Real-Time Kernel" */
- OSMutexCreate(3, &err);
- OSTaskCreateExt(LED1TaskStart, /* Create the start task */
- (void *)0,
- (OS_STK *)&LED1TaskStartStk[TASK_START_STK_SIZE - 1],
- LED1_TASK_START_PRIO,
- LED1_TASK_START_PRIO,
- (OS_STK *)&LED1TaskStartStk[0],
- TASK_START_STK_SIZE,
- (void *)0,
- OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
- OSTaskNameSet(LED1_TASK_START_PRIO, "LED1 Task", &err);
- OSTaskCreateExt(LED2TaskStart, /* Create the start task */
- (void *)0,
- (OS_STK *)&LED2TaskStartStk[TASK_START_STK_SIZE - 1],
- LED2_TASK_START_PRIO,
- LED2_TASK_START_PRIO,
- (OS_STK *)&LED2TaskStartStk[0],
- TASK_START_STK_SIZE,
- (void *)0,
- OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
- OSTaskNameSet(LED2_TASK_START_PRIO, "LED2 Task", &err);
- OSTaskCreateExt(LED3TaskStart, /* Create the start task */
- (void *)0,
- (OS_STK *)&LED3TaskStartStk[TASK_START_STK_SIZE - 1],
- LED3_TASK_START_PRIO,
- LED3_TASK_START_PRIO,
- (OS_STK *)&LED3TaskStartStk[0],
- TASK_START_STK_SIZE,
- (void *)0,
- OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
- OSTaskNameSet(LED3_TASK_START_PRIO, "LED3 Task", &err);
- OSTaskCreateExt(LED4TaskStart, /* Create the start task */
- (void *)0,
- (OS_STK *)&LED4TaskStartStk[TASK_START_STK_SIZE - 1],
- LED4_TASK_START_PRIO,
- LED4_TASK_START_PRIO,
- (OS_STK *)&LED4TaskStartStk[0],
- TASK_START_STK_SIZE,
- (void *)0,
- OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
- OSTaskNameSet(LED4_TASK_START_PRIO, "LED4 Task", &err);
- OSTaskCreateExt(LED5TaskStart, /* Create the start task */
- (void *)0,
- (OS_STK *)&LED5TaskStartStk[TASK_START_STK_SIZE - 1],
- LED5_TASK_START_PRIO,
- LED5_TASK_START_PRIO,
- (OS_STK *)&LED5TaskStartStk[0],
- TASK_START_STK_SIZE,
- (void *)0,
- OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
- OSTaskNameSet(LED5_TASK_START_PRIO, "LED5 Task", &err);
- OSTaskCreateExt(LED6TaskStart, /* Create the start task */
- (void *)0,
- (OS_STK *)&LED6TaskStartStk[TASK_START_STK_SIZE - 1],
- LED6_TASK_START_PRIO,
- LED6_TASK_START_PRIO,
- (OS_STK *)&LED6TaskStartStk[0],
- TASK_START_STK_SIZE,
- (void *)0,
- OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
- OSTaskNameSet(LED6_TASK_START_PRIO, "LED6 Task", &err);
- OSTaskCreateExt(LED7TaskStart, /* Create the start task */
- (void *)0,
- (OS_STK *)&LED7TaskStartStk[TASK_START_STK_SIZE - 1],
- LED7_TASK_START_PRIO,
- LED7_TASK_START_PRIO,
- (OS_STK *)&LED7TaskStartStk[0],
- TASK_START_STK_SIZE,
- (void *)0,
- OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
- OSTaskNameSet(LED7_TASK_START_PRIO, "LED7 Task", &err);
- OSTaskCreateExt(LED8TaskStart, /* Create the start task */
- (void *)0,
- (OS_STK *)&LED8TaskStartStk[TASK_START_STK_SIZE - 1],
- LED8_TASK_START_PRIO,
- LED8_TASK_START_PRIO,
- (OS_STK *)&LED8TaskStartStk[0],
- TASK_START_STK_SIZE,
- (void *)0,
- OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
- OSTaskNameSet(LED8_TASK_START_PRIO, "LED8 Task", &err);
- OSStart();
- }
复制代码
任务代码
- static void LED1TaskStart(void * p_arg)
- {
- INT8U err;
- (void)p_arg;
- while (1)
- { /* Task body, always written as an infinite loop. */
- BSP_LED_Toggle(0);
- OSMutexPend(ResourceMutex, 0, &err);
- UsartPrintf(USART1, "LED1TaskStart\n");
- OSMutexPost(ResourceMutex);
- OSTimeDlyHMSM(0, 0, 1, 0);
- }
- }
- static void LED2TaskStart(void * p_arg)
- {
- INT8U err;
- (void)p_arg;
- while (1)
- { /* Task body, always written as an infinite loop. */
- BSP_LED_Toggle(1);
- OSMutexPend(ResourceMutex, 0, &err);
- UsartPrintf(USART1, "LED2TaskStart\n");
- OSMutexPost(ResourceMutex);
- OSTimeDlyHMSM(0, 0, 2, 0);
- }
- }
- static void LED3TaskStart(void * p_arg)
- {
- INT8U err;
- (void)p_arg;
- while (1)
- { /* Task body, always written as an infinite loop. */
- BSP_LED_Toggle(2);
- OSMutexPend(ResourceMutex, 0, &err);
- UsartPrintf(USART1, "LED3TaskStart\n");
- OSMutexPost(ResourceMutex);
- OSTimeDlyHMSM(0, 0, 3, 0);
- }
- }
- static void LED4TaskStart(void * p_arg)
- {
- INT8U err;
- (void)p_arg;
- while (1)
- { /* Task body, always written as an infinite loop. */
- BSP_LED_Toggle(3);
- OSMutexPend(ResourceMutex, 0, &err);
- UsartPrintf(USART1, "LED4TaskStart\n");
- OSMutexPost(ResourceMutex);
- OSTimeDlyHMSM(0, 0, 4, 0);
- }
- }
- static void LED5TaskStart(void * p_arg)
- {
- INT8U err;
- (void)p_arg;
- while (1)
- { /* Task body, always written as an infinite loop. */
- BSP_LED_Toggle(4);
- OSMutexPend(ResourceMutex, 0, &err);
- UsartPrintf(USART1, "LED5TaskStart\n");
- OSMutexPost(ResourceMutex);
- OSTimeDlyHMSM(0, 0, 5, 0);
- }
- }
- static void LED6TaskStart(void * p_arg)
- {
- INT8U err;
- (void)p_arg;
- while (1)
- { /* Task body, always written as an infinite loop. */
- BSP_LED_Toggle(5);
- OSMutexPend(ResourceMutex, 0, &err);
- UsartPrintf(USART1, "LED6TaskStart\n");
- OSMutexPost(ResourceMutex);
- OSTimeDlyHMSM(0, 0, 6, 0);
- }
- }
- static void LED7TaskStart(void * p_arg)
- {
- INT8U err;
- (void)p_arg;
- while (1)
- { /* Task body, always written as an infinite loop. */
- BSP_LED_Toggle(6);
- OSMutexPend(ResourceMutex, 0, &err);
- UsartPrintf(USART1, "LED7TaskStart\n");
- OSMutexPost(ResourceMutex);
- OSTimeDlyHMSM(0, 0, 7, 0);
- }
- }
- static void LED8TaskStart(void * p_arg)
- {
- INT8U err;
- (void)p_arg;
- while (1)
- { /* Task body, always written as an infinite loop. */
- BSP_LED_Toggle(8);
- OSMutexPend(ResourceMutex, 0, &err);
- UsartPrintf(USART1, "LED8TaskStart\n");
- OSMutexPost(ResourceMutex);
- OSTimeDlyHMSM(0, 0, 8, 0);
- }
- }
复制代码
打印函数
- void UsartPrintf(USART_TypeDef * USARTx, const char * fmt, ...)
- {
- va_list args;
- char printf_buffer[256];
- char * ptr = printf_buffer;
- va_start(args, fmt);
- vsprintf(printf_buffer, fmt, args);
- va_end(args);
- /**查询发送************************************************************************/
- while (*ptr != '\0')
- {
- USART_SendData(USARTx, *(ptr++));
- while (!(USARTx->SR & USART_FLAG_TXE));
- }
- }
复制代码
我的疑问如下:
1、我加入了互斥型信号量,为何打印任务仍然被打断了;
2、字符为何丢失,有什么方法可以解决;
请各位高手指点迷津............ |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|