sem_example.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2021, Meco Jianting Man <jiantingman@foxmail.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-09-30 Meco Man first version
  9. */
  10. #include <ucos_ii.h>
  11. #define TASK_SIZE 256
  12. #define TASK1_PRIO 5
  13. #define TASK2_PRIO 6
  14. static OS_STK task1_stack [TASK_SIZE];
  15. static OS_STK task2_stack [TASK_SIZE];
  16. static OS_EVENT *pSem;
  17. //task1 tx
  18. static void task1(void *p_arg)
  19. {
  20. // INT8U err;
  21. // OSTimeDly(1000);
  22. // OSSemPendAbort(pSem, OS_PEND_OPT_BROADCAST, &err);
  23. while(1)
  24. {
  25. OSSemPost(pSem);
  26. OSTimeDly(1000);
  27. }
  28. }
  29. //task2 rx
  30. static void task2(void *p_arg)
  31. {
  32. INT8U err;
  33. while(1)
  34. {
  35. OSSemPend(pSem, 0, &err);
  36. if(err == OS_ERR_NONE)
  37. {
  38. rt_kprintf("success!\r\n");
  39. }
  40. else if (err == OS_ERR_PEND_ABORT)
  41. {
  42. rt_kprintf("abort!\r\n");
  43. }
  44. else
  45. {
  46. rt_kprintf("failure!%d\r\n",err);
  47. }
  48. }
  49. }
  50. void sem_example (void)
  51. {
  52. pSem = OSSemCreate(0);
  53. #if OS_STK_GROWTH == 0u
  54. OSTaskCreateExt(task1,0,&task1_stack[0],TASK_PRIO,0,&task1_stack[TASK_SIZE-1],TASK_SIZE,0,OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);
  55. #else
  56. OSTaskCreateExt(task1,0,&task1_stack[TASK_SIZE-1],TASK1_PRIO,0,&task1_stack[0],TASK_SIZE,0,OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);
  57. #endif
  58. #if OS_STK_GROWTH == 0u
  59. OSTaskCreateExt(task2,0,&task2_stack[0],TASK_PRIO,0,&task2_stack[TASK_SIZE-1],TASK_SIZE,0,OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);
  60. #else
  61. OSTaskCreateExt(task2,0,&task2_stack[TASK_SIZE-1],TASK2_PRIO,0,&task2_stack[0],TASK_SIZE,0,OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);
  62. #endif
  63. }