osi_demo.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*================================================================
  2. Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
  3. Quectel Wireless Solutions Proprietary and Confidential.
  4. =================================================================*/
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "ql_api_osi.h"
  9. #include "ql_log.h"
  10. #include "osi_demo.h"
  11. #define QL_OSI_DEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  12. #define QL_OSI_DEMO_LOG(msg, ...) QL_LOG(QL_OSI_DEMO_LOG_LEVEL, "ql_osi_demo", msg, ##__VA_ARGS__)
  13. #define FEED_DOG_MAX_MISS_CNT 5
  14. #define EVENT_BIT_1 (0x01 << 0)
  15. #define EVENT_BIT_2 (0x01 << 1)
  16. #define EVENT_BIT_3 (0x01 << 2)
  17. #define EVENT_BIT_4 (0x01 << 3)
  18. ql_task_t demo_task = NULL;
  19. ql_timer_t demo_timer = NULL;
  20. ql_egroup_t egroupRef = NULL;
  21. void demo_task_callback(void *ctx)
  22. {
  23. ql_event_t test_event = {0};
  24. while(1)
  25. {
  26. if(ql_event_try_wait(&test_event) != 0)
  27. {
  28. continue;
  29. }
  30. if(test_event.id == QUEC_KERNEL_FEED_DOG)
  31. {
  32. QL_OSI_DEMO_LOG("demo task receive feed dog event");
  33. if(ql_rtos_feed_dog() != QL_OSI_SUCCESS)
  34. {
  35. QL_OSI_DEMO_LOG("feed dog failed");
  36. }
  37. }
  38. }
  39. }
  40. void demo_task_A_callback(void *ctx)
  41. {
  42. ql_event_bits_t curr_bits = 0;
  43. while(1)
  44. {
  45. if(ql_rtos_event_group_wait(egroupRef,EVENT_BIT_1|EVENT_BIT_2|EVENT_BIT_3|EVENT_BIT_4,TRUE,FALSE,&curr_bits,QL_WAIT_FOREVER) != 0)
  46. {
  47. QL_OSI_DEMO_LOG("group_wait err,curr_bits=0x%x",curr_bits);
  48. continue;
  49. }
  50. QL_OSI_DEMO_LOG("event group bits=0x%x",curr_bits);
  51. if (curr_bits & EVENT_BIT_1)
  52. {
  53. QL_OSI_DEMO_LOG("EVENT_BIT_1");
  54. }
  55. if (curr_bits & EVENT_BIT_2)
  56. {
  57. QL_OSI_DEMO_LOG("EVENT_BIT_2");
  58. }
  59. if (curr_bits & EVENT_BIT_3)
  60. {
  61. QL_OSI_DEMO_LOG("EVENT_BIT_3");
  62. }
  63. if (curr_bits & EVENT_BIT_4)
  64. {
  65. QL_OSI_DEMO_LOG("EVENT_BIT_4");
  66. }
  67. ql_rtos_task_sleep_ms(50);
  68. }
  69. }
  70. void demo_task_B_callback(void *ctx)
  71. {
  72. ql_event_bits_t curr_bits = 0;
  73. while(1)
  74. {
  75. ql_rtos_event_group_set(egroupRef, EVENT_BIT_1, &curr_bits);
  76. ql_rtos_task_sleep_ms(500);
  77. ql_rtos_event_group_set(egroupRef, EVENT_BIT_2, &curr_bits);
  78. ql_rtos_task_sleep_ms(500);
  79. ql_rtos_event_group_set(egroupRef, EVENT_BIT_3|EVENT_BIT_4, &curr_bits);
  80. ql_rtos_task_sleep_ms(500);
  81. }
  82. }
  83. void feed_dog_callback(uint32 id_type, void *ctx)
  84. {
  85. ql_event_t event;
  86. if(id_type == QUEC_KERNEL_FEED_DOG)
  87. {
  88. QL_OSI_DEMO_LOG("feed dog callback run");
  89. event.id = QUEC_KERNEL_FEED_DOG;
  90. if(ql_rtos_event_send(demo_task, &event) != QL_OSI_SUCCESS)
  91. {
  92. QL_OSI_DEMO_LOG("send feed_dog event to demo task failed");
  93. }
  94. else
  95. {
  96. QL_OSI_DEMO_LOG("send feed dog event to demo task ok");
  97. }
  98. }
  99. }
  100. void timer_callback(void *ctx)
  101. {
  102. static int cnt = 0;
  103. QL_OSI_DEMO_LOG("timer run %d times", ++cnt);
  104. return;
  105. }
  106. QlOSStatus ql_osi_demo_init(void)
  107. {
  108. QlOSStatus err = QL_OSI_SUCCESS;
  109. err = ql_rtos_event_group_create(&egroupRef);
  110. if(err != QL_OSI_SUCCESS)
  111. {
  112. QL_OSI_DEMO_LOG("demo_event_group created failed");
  113. goto exit;
  114. }
  115. err = ql_rtos_task_create(&demo_task, DEMO_TASK_STACK_SIZE, DEMO_TASK_PRIO, "DEMO_TASK", demo_task_callback, NULL, DEMO_TASK_EVENT_CNT);
  116. if(err != QL_OSI_SUCCESS)
  117. {
  118. QL_OSI_DEMO_LOG("demo_task created failed");
  119. goto exit;
  120. }
  121. err = ql_rtos_task_create(&demo_task, DEMO_TASK_STACK_SIZE, DEMO_TASK_PRIO, "DEMO_TKA", demo_task_A_callback, NULL, DEMO_TASK_EVENT_CNT+8);
  122. if(err != QL_OSI_SUCCESS)
  123. {
  124. QL_OSI_DEMO_LOG("demo_task created failed");
  125. goto exit;
  126. }
  127. err = ql_rtos_task_create(&demo_task, DEMO_TASK_STACK_SIZE, DEMO_TASK_PRIO, "DEMO_TKB", demo_task_B_callback, NULL, DEMO_TASK_EVENT_CNT+8);
  128. if(err != QL_OSI_SUCCESS)
  129. {
  130. QL_OSI_DEMO_LOG("demo_task created failed");
  131. goto exit;
  132. }
  133. err = ql_rtos_timer_create(&demo_timer, demo_task, timer_callback, NULL);
  134. if(err != QL_OSI_SUCCESS)
  135. {
  136. QL_OSI_DEMO_LOG("demo_timer created failed");
  137. goto exit;
  138. }
  139. err = ql_rtos_timer_start(demo_timer, 1000, 0);
  140. if(err != QL_OSI_SUCCESS)
  141. {
  142. QL_OSI_DEMO_LOG("demo_timer start failed");
  143. goto exit;
  144. }
  145. err = ql_rtos_swdog_register((ql_swdog_callback)feed_dog_callback, demo_task);
  146. if(err != QL_OSI_SUCCESS)
  147. {
  148. QL_OSI_DEMO_LOG("demo_task register sw dog failed");
  149. goto exit;
  150. }
  151. err = ql_rtos_sw_dog_enable(5000, 3); //配置软件看门狗每 5 秒调用一次 feed_dog_callback ,如果连续 3 次调用且没有进行喂狗操作,则判定线程已陷入死循环,触发重启
  152. if(err != QL_OSI_SUCCESS)
  153. {
  154. QL_OSI_DEMO_LOG("sw dog enable failed");
  155. goto exit;
  156. }
  157. return QL_OSI_SUCCESS;
  158. exit:
  159. if(demo_task != NULL)
  160. {
  161. ql_rtos_task_delete(demo_task);
  162. demo_task = NULL;
  163. }
  164. if(demo_timer != NULL)
  165. {
  166. ql_rtos_timer_delete(demo_timer);
  167. demo_timer = NULL;
  168. }
  169. return err;
  170. }