luat_rtos.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef LUAT_RTOS_H
  22. #define LUAT_RTOS_H
  23. #include "luat_base.h"
  24. #include "luat_rtos_legacy.h"
  25. /**
  26. * @defgroup luatos_os 操作系统接口
  27. * @{
  28. */
  29. /**
  30. * @brief LUAT_RTOS 超时时间枚举值
  31. */
  32. typedef enum LUAT_RTOS_WAIT
  33. {
  34. LUAT_NO_WAIT = 0, /**< 超时时间为0 */
  35. LUAT_WAIT_FOREVER = (uint32_t)0xFFFFFFFF /**< 最大超时时间0xFFFFFFFF*/
  36. } LUAT_RTOS_WAIT_E;
  37. typedef enum
  38. {
  39. LUAT_FLAG_AND = 5,
  40. LUAT_FLAG_AND_CLEAR = 6,
  41. LUAT_FLAG_OR = 7,
  42. LUAT_FLAG_OR_CLEAR = 8
  43. } LUAT_FLAG_OP_E;
  44. /* ------------------------------------------------ task begin------------------------------------------------ */
  45. /**
  46. * @defgroup luatos_os_Task 线程任务接口函数
  47. * @{
  48. */
  49. /**
  50. *@brief task的入口函数,函数类型
  51. */
  52. typedef void (*luat_rtos_task_entry) (void*);
  53. /**
  54. *@brief 定义task任务句柄
  55. */
  56. typedef void * luat_rtos_task_handle;
  57. /**
  58. * @brief 创建一个可以带mailbox机制的task,mailbox是message和event的基础,但是和queue无关
  59. *
  60. * @param task_handle[OUT] 返回创建的句柄
  61. * @param stack_size task的栈空间大小,单位byte,必须4字节对齐
  62. * @param priority 优先级,单位是百分比,0%~100%,100%为最高等级,由具体实现转换到底层SDK用的优先级
  63. * @param task_name task名字
  64. * @param task_fun task的入口函数
  65. * @param user_data task的入口参数
  66. * @param event_cout 如果OS允许在中断里malloc,或者不使用message和event机制的,这个参数无视,如果OS不允许在中断里malloc,则这里填写预分配的event空间用于中断里使用,如果写0会使用公共event
  67. * @return int =0成功,其他失败
  68. */
  69. int luat_rtos_task_create(luat_rtos_task_handle *task_handle, uint32_t stack_size, uint8_t priority, const char *task_name, luat_rtos_task_entry task_fun, void* user_data, uint16_t event_cout);
  70. /**
  71. * @brief 删除task
  72. *
  73. * @param task_handle
  74. * @return int =0成功,其他失败
  75. */
  76. int luat_rtos_task_delete(luat_rtos_task_handle task_handle);
  77. /**
  78. * @brief 挂起某个task
  79. *
  80. * @param task_handle task句柄
  81. * @return int =0成功,其他失败
  82. */
  83. int luat_rtos_task_suspend(luat_rtos_task_handle task_handle);
  84. /**
  85. * @brief 恢复挂起的task
  86. *
  87. * @param task_handle task句柄
  88. * @return int =0成功,其他失败
  89. */
  90. int luat_rtos_task_resume(luat_rtos_task_handle task_handle);
  91. /**
  92. * @brief 挂起全部task
  93. *
  94. */
  95. void luat_rtos_task_suspend_all(void);
  96. /**
  97. * @brief 恢复全部task
  98. *
  99. */
  100. void luat_rtos_task_resume_all(void);
  101. /**
  102. * @brief task休眠一段时间
  103. *
  104. * @param ms 休眠时间,单位ms
  105. */
  106. void luat_rtos_task_sleep(uint32_t ms);
  107. /**
  108. * @brief 获取当前task的句柄
  109. *
  110. * @return luat_rtos_task_handle 当前task的句柄
  111. */
  112. luat_rtos_task_handle luat_rtos_get_current_handle(void);
  113. /**
  114. * @brief 获取task堆栈剩余的最小值,叫做“高水位线”
  115. *
  116. * @param luat_rtos_task_handle task的句柄
  117. * @return task堆栈剩余的最小值,单位为字
  118. */
  119. uint32_t luat_rtos_task_get_high_water_mark(luat_rtos_task_handle task_handle);
  120. uint32_t luat_rtos_task_stack_free_space(luat_rtos_task_handle task_handle);
  121. /**
  122. * @brief 启动task运行时间记录功能,必须在task调度前启用
  123. */
  124. void luat_rtos_task_run_time_record_enable(void);
  125. /**
  126. * @brief 打印所有task运行时间和占用百分比
  127. * @param print_ticks 打印具体ticks
  128. */
  129. void luat_rtos_task_run_time_record_print(uint8_t print_ticks);
  130. /** @}*/
  131. /* ------------------------------------------------ task end------------------------------------------------ */
  132. /**
  133. * @defgroup luatos_os_event 消息事件函数
  134. * @{
  135. */
  136. /* ----------------------------------------------- event begin---------------------------------------------- */
  137. /**
  138. * @brief 在等待event中,如果设置了目标event id,而到来的不是目标event id,可以通过回调函数交给用户处理
  139. *
  140. */
  141. typedef LUAT_RT_RET_TYPE (*luat_rtos_event_wait_callback_t)(LUAT_RT_CB_PARAM);
  142. /**
  143. * @brief 发送一个event给task的mailbox,只有设置了mailbox启用的task能接收,如果缓存了超过1024个event会断言
  144. *
  145. * @param task_handle 需要接收event的task句柄
  146. * @param id event id
  147. * @param param1 event参数1
  148. * @param param2 event参数2
  149. * @param param3 event参数3
  150. * @param timeout 发送超时,已经废弃了
  151. * @return int =0成功,其他失败
  152. */
  153. int luat_rtos_event_send(luat_rtos_task_handle task_handle, uint32_t id, uint32_t param1, uint32_t param2, uint32_t param3, uint32_t timeout);
  154. /**
  155. * @brief 接收一个event,只能在task里接收
  156. *
  157. * @param task_handle 需要接收event的task句柄
  158. * @param wait_event_id 目标event的ID,=0表示不限制,任意event id都会返回
  159. * @param out_event[OUT] 接收到的event
  160. * @param callback_fun event的ID不是目标ID时,用户回调函数,可以为NULL,从而抛弃掉这个event
  161. * @param timeout 接收超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
  162. * @return int =0成功,其他失败
  163. */
  164. int luat_rtos_event_recv(luat_rtos_task_handle task_handle, uint32_t wait_event_id, luat_event_t *out_event, luat_rtos_event_wait_callback_t *callback_fun, uint32_t timeout);
  165. /* ----------------------------------------------- event end---------------------------------------------- */
  166. /* ----------------------------------------------- message begin---------------------------------------------- */
  167. /**
  168. * @brief 发送一个message给task的mailbox,只有设置了mailbox启用的task能接收,message可以动态创建的,可以任意大小,如果缓存了超过1024个message会断言
  169. *
  170. * @param task_handle 需要接收massage的task句柄
  171. * @param message_id message id
  172. * @param p_message message内容,传入指针,如果动态创建,需要在接收时释放
  173. * @return int =0成功,其他失败
  174. */
  175. int luat_rtos_message_send(luat_rtos_task_handle task_handle, uint32_t message_id, void *p_message);
  176. /**
  177. * @brief 接收一个message,只能在task里接收
  178. *
  179. * @param task_handle 需要接收massage的task句柄
  180. * @param message_id[OUT] 接收到的message id
  181. * @param p_p_message[OUT] message内容,输出一个void *指针,如果是发送时动态创建的,需要释放掉
  182. * @param timeout 接收超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
  183. * @return int =0成功,其他失败
  184. */
  185. int luat_rtos_message_recv(luat_rtos_task_handle task_handle, uint32_t *message_id, void **p_p_message, uint32_t timeout);
  186. /** @}*/
  187. /* ----------------------------------------------- message end---------------------------------------------- */
  188. /**
  189. * @defgroup luatos_os_semaphore 信号量接口函数
  190. * @{
  191. */
  192. /* ---------------------------------------------- semaphore begin--------------------------------------------- */
  193. /**
  194. * @brief 定义信号量句柄
  195. */
  196. typedef void * luat_rtos_semaphore_t;
  197. /**
  198. * @brief 信号量创建,可以在中断中release
  199. *
  200. * @param semaphore_handle[OUT] 信号量句柄
  201. * @param init_count 初始值
  202. * @return int =0成功,其他失败
  203. */
  204. int luat_rtos_semaphore_create(luat_rtos_semaphore_t *semaphore_handle, uint32_t init_count);
  205. /**
  206. * @brief 删除信号量
  207. *
  208. * @param semaphore_handle 信号量句柄
  209. * @return int =0成功,其他失败
  210. */
  211. int luat_rtos_semaphore_delete(luat_rtos_semaphore_t semaphore_handle);
  212. /**
  213. * @brief 信号量等待获取
  214. *
  215. * @param semaphore_handle 信号量句柄
  216. * @param timeout 接收超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
  217. * @return int =0成功,其他失败
  218. */
  219. int luat_rtos_semaphore_take(luat_rtos_semaphore_t semaphore_handle, uint32_t timeout);
  220. /**
  221. * @brief 信号量释放发送
  222. *
  223. * @param semaphore_handle 信号量句柄
  224. * @return int =0成功,其他失败
  225. */
  226. int luat_rtos_semaphore_release(luat_rtos_semaphore_t semaphore_handle);
  227. /* ---------------------------------------------- semaphore end--------------------------------------------- */
  228. /** @}*/
  229. /**
  230. * @defgroup luatos_os_mutex 互斥锁接口函数
  231. * @{
  232. */
  233. /* ------------------------------------------------ mutex begin----------------------------------------------- */
  234. /**
  235. * @brief 定义mutex句柄
  236. */
  237. typedef void * luat_rtos_mutex_t;
  238. /**
  239. * @brief 互斥锁创建,不能在中断中unlock
  240. *
  241. * @param mutex_handle[OUT] 互斥锁句柄
  242. * @return int =0成功,其他失败
  243. */
  244. int luat_rtos_mutex_create(luat_rtos_mutex_t *mutex_handle);
  245. /**
  246. * @brief 获得锁
  247. *
  248. * @param mutex_handle 互斥锁句柄
  249. * @param timeout 超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
  250. * @return int =0成功,其他失败
  251. */
  252. int luat_rtos_mutex_lock(luat_rtos_mutex_t mutex_handle, uint32_t timeout);
  253. /**
  254. * @brief 释放锁
  255. *
  256. * @param mutex_handle 互斥锁句柄
  257. * @return int =0成功,其他失败
  258. */
  259. int luat_rtos_mutex_unlock(luat_rtos_mutex_t mutex_handle);
  260. /**
  261. * @brief 删除互斥锁
  262. *
  263. * @param mutex_handle 互斥锁句柄
  264. * @return int =0成功,其他失败
  265. */
  266. int luat_rtos_mutex_delete(luat_rtos_mutex_t mutex_handle);
  267. /* ------------------------------------------------ mutex end----------------------------------------------- */
  268. /** @}*/
  269. /**
  270. * @defgroup luatos_os_queue 队列接口函数
  271. * @{
  272. */
  273. /* ------------------------------------------------ queue begin----------------------------------------------- */
  274. /**
  275. * @brief 定义队列句柄
  276. */
  277. typedef void * luat_rtos_queue_t;
  278. /**
  279. * @brief 创建队列
  280. *
  281. * @param queue_handle[OUT] 返回的队列句柄
  282. * @param msgcount 队列里元素的最大数量
  283. * @param msgsize 队列里单个元素的大小
  284. * @return int =0成功,其他失败
  285. */
  286. int luat_rtos_queue_create(luat_rtos_queue_t *queue_handle, uint32_t item_count, uint32_t item_size);
  287. /**
  288. * @brief 删除队列
  289. *
  290. * @param queue_handle 队列句柄
  291. * @return int =0成功,其他失败
  292. */
  293. int luat_rtos_queue_delete(luat_rtos_queue_t queue_handle);
  294. /**
  295. * @brief 往队列里发送一个元素
  296. *
  297. * @param queue_handle 队列句柄
  298. * @param item 元素指针
  299. * @param item_size 元素大小,这个是兼容性参数,实际上必须于创建时的item_size一致,所以忽略
  300. * @param timeout 超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
  301. * @return int =0成功,其他失败
  302. */
  303. int luat_rtos_queue_send(luat_rtos_queue_t queue_handle, void *item, uint32_t item_size, uint32_t timeout);
  304. /**
  305. * @brief 从队列里取出一个元素
  306. *
  307. * @param queue_handle 队列句柄
  308. * @param item 元素指针
  309. * @param item_size 元素大小,这个是兼容性参数,实际上必须于创建时的item_size一致,所以忽略
  310. * @param timeout 超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
  311. * @return int =0成功,其他失败
  312. */
  313. int luat_rtos_queue_recv(luat_rtos_queue_t queue_handle, void *item, uint32_t item_size, uint32_t timeout);
  314. /**
  315. * @brief 查询队列中剩余未处理的元素数量
  316. *
  317. * @param queue_handle 队列句柄
  318. * @param item_cnt[OUT] 返回未处理的元素数量
  319. * @return int =0成功,其他失败
  320. */
  321. int luat_rtos_queue_get_cnt(luat_rtos_queue_t queue_handle, uint32_t *item_cnt);
  322. /* ------------------------------------------------ queue end----------------------------------------------- */
  323. /** @}*/
  324. /**
  325. * @defgroup luatos_os_flag 事件接口函数
  326. * @{
  327. */
  328. /* ------------------------------------------------ flag begin----------------------------------------------- */
  329. /**
  330. * @brief 定义事件句柄
  331. */
  332. typedef void * luat_rtos_flag_t;
  333. /**
  334. * @brief 创建事件
  335. *
  336. * @param flag_handle[OUT] 返回的事件句柄
  337. * @return int =0成功,其他失败
  338. */
  339. int luat_rtos_flag_create(luat_rtos_flag_t *flag_handle);
  340. /**
  341. * @brief 等待事件
  342. * @param mask 等待的事件掩码
  343. * @param operation 事件触发要求(与操作需要全部满足才触发,或操作有一个就触发)和操作(是否要清除)
  344. * @param flags[OUT] 当前事件状态值
  345. * @param timeout 超时时间
  346. * @return int =0成功,其他失败
  347. */
  348. int luat_rtos_flag_wait(luat_rtos_flag_t flag_handle, uint32_t mask, LUAT_FLAG_OP_E operation, uint32_t *flags,uint32_t timeout);
  349. /**
  350. * @brief 设置事件
  351. * @param flag_handle 事件句柄
  352. * @param mask 设置掩码
  353. * @param operation 事件判断(与或)和操作(是否要清除),freertos支持或操作LUAT_FLAG_OR
  354. * @return int =0成功,其他失败
  355. */
  356. int luat_rtos_flag_release(luat_rtos_flag_t flag_handle, uint32_t mask, LUAT_FLAG_OP_E operation);
  357. /**
  358. * @brief 删除事件
  359. *
  360. * @param flag_handle 事件句柄
  361. * @return int =0成功,其他失败
  362. */
  363. int luat_rtos_flag_delete(luat_rtos_flag_t flag_handle);
  364. /* ------------------------------------------------ flag end----------------------------------------------- */
  365. /** @}*/
  366. /**
  367. * @defgroup luatos_os_timer 软件定时器接口函数
  368. * @{
  369. */
  370. /* ------------------------------------------------ timer begin----------------------------------------------- */
  371. /**
  372. * @brief 定时器头数据类型
  373. */
  374. typedef void * luat_rtos_timer_t;
  375. /**
  376. * @brief 定义定时器处理函数
  377. */
  378. typedef LUAT_RT_RET_TYPE (*luat_rtos_timer_callback_t)(LUAT_RT_CB_PARAM);
  379. /**
  380. * @brief 创建软件定时器
  381. *
  382. * @param timer_handle[OUT] 返回定时器句柄
  383. * @return int =0成功,其他失败
  384. */
  385. int luat_rtos_timer_create(luat_rtos_timer_t *timer_handle);
  386. /**
  387. * @brief 删除软件定时器
  388. *
  389. * @param timer_handle 定时器句柄
  390. * @return int =0成功,其他失败
  391. */
  392. int luat_rtos_timer_delete(luat_rtos_timer_t timer_handle);
  393. /**
  394. * @brief 启动软件定时器
  395. *
  396. * @param timer_handle 定时器句柄
  397. * @param timeout 超时时间,单位ms,没有特殊值
  398. * @param repeat 0不重复,其他重复
  399. * @param callback_fun 定时时间到后的回调函数
  400. * @param user_param 回调函数时的最后一个输入参数
  401. * @return int =0成功,其他失败
  402. */
  403. int luat_rtos_timer_start(luat_rtos_timer_t timer_handle, uint32_t timeout, uint8_t repeat, luat_rtos_timer_callback_t callback_fun, void *user_param);
  404. /**
  405. * @brief 停止软件定时器
  406. *
  407. * @param timer_handle 定时器句柄
  408. * @return int =0成功,其他失败
  409. */
  410. int luat_rtos_timer_stop(luat_rtos_timer_t timer_handle);
  411. /**
  412. * @brief 检测软件定时器是否处于激活状态
  413. *
  414. * @param timer_handle 定时器句柄
  415. * @return int =0未激活,1激活,其他失败
  416. */
  417. int luat_rtos_timer_is_active(luat_rtos_timer_t timer_handle);
  418. /*------------------------------------------------ timer end----------------------------------------------- */
  419. /** @}*/
  420. /**
  421. * @defgroup luatos_os_critical 临界保护接口函数
  422. * @{
  423. */
  424. /* ------------------------------------------------ critical begin----------------------------------------------- */
  425. /**
  426. * @brief 进入临界保护
  427. *
  428. * @return uint32_t 退出临界保护所需参数
  429. */
  430. uint32_t luat_rtos_entry_critical(void);
  431. /**
  432. * @brief 退出临界保护
  433. *
  434. * @param critical 进入临界保护时返回的参数
  435. */
  436. void luat_rtos_exit_critical(uint32_t critical);
  437. /**
  438. * @brief 获取是否为中断
  439. *
  440. * @param critical 进入临界保护时返回的参数
  441. * @return uint32_t 0不是中断,1是中断
  442. */
  443. uint32_t luat_rtos_get_ipsr(void);
  444. #if defined(CHIP_EC618) || defined(CHIP_EC716) || defined(CHIP_EC718)
  445. #define luat_rtos_ms2tick(ms) (ms)
  446. #else
  447. int luat_rtos_ms2tick(int ms);
  448. #endif
  449. /*------------------------------------------------ critical end----------------------------------------------- */
  450. /** @}*/
  451. /** @}*/
  452. #endif