luat_rtos.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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
  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 =0表示不需要使用mailbox机制,>0表示启用mailbox,可以使用下列event和massage api,同时如果底层SDK不支持mailbox,会创建一个queue模拟mailbox,queue里元素为luat_event_t,数量为event_cout
  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. /** @}*/
  121. /* ------------------------------------------------ task end------------------------------------------------ */
  122. /**
  123. * @defgroup luatos_os_event 消息事件函数
  124. * @{
  125. */
  126. /* ----------------------------------------------- event begin---------------------------------------------- */
  127. /**
  128. * @brief 在等待event中,如果设置了目标event id,而到来的不是目标event id,可以通过回调函数交给用户处理
  129. *
  130. */
  131. typedef LUAT_RT_RET_TYPE (*luat_rtos_event_wait_callback_t)(LUAT_RT_CB_PARAM);
  132. /**
  133. * @brief 发送一个event给task的mailbox,只有设置了mailbox启用的task能接收
  134. *
  135. * @param task_handle 需要接收event的task句柄
  136. * @param id event id
  137. * @param param1 event参数1
  138. * @param param2 event参数2
  139. * @param param3 event参数3
  140. * @param timeout 发送超时,在task发送才有,单位ms,特殊值见LUAT_RTOS_WAIT_E
  141. * @return int =0成功,其他失败
  142. */
  143. 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);
  144. /**
  145. * @brief 接收一个event,只能在task里接收
  146. *
  147. * @param task_handle 需要接收event的task句柄
  148. * @param wait_event_id 目标event的ID,=0表示不限制,任意event id都会返回
  149. * @param out_event[OUT] 接收到的event
  150. * @param callback_fun event的ID不是目标ID时,用户回调函数,可以为NULL,从而抛弃掉这个event
  151. * @param timeout 接收超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
  152. * @return int =0成功,其他失败
  153. */
  154. 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);
  155. /* ----------------------------------------------- event end---------------------------------------------- */
  156. /* ----------------------------------------------- message begin---------------------------------------------- */
  157. /**
  158. * @brief 发送一个message给task的mailbox,只有设置了mailbox启用的task能接收,message可以动态创建的,可以任意大小
  159. *
  160. * @param task_handle 需要接收massage的task句柄
  161. * @param message_id message id
  162. * @param p_message message内容,传入指针,如果动态创建,需要在接收时释放
  163. * @return int =0成功,其他失败
  164. */
  165. int luat_rtos_message_send(luat_rtos_task_handle task_handle, uint32_t message_id, void *p_message);
  166. /**
  167. * @brief 接收一个message,只能在task里接收
  168. *
  169. * @param task_handle 需要接收massage的task句柄
  170. * @param message_id[OUT] 接收到的message id
  171. * @param p_p_message[OUT] message内容,输出一个void *指针,如果是发送时动态创建的,需要释放掉
  172. * @param timeout 接收超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
  173. * @return int =0成功,其他失败
  174. */
  175. int luat_rtos_message_recv(luat_rtos_task_handle task_handle, uint32_t *message_id, void **p_p_message, uint32_t timeout);
  176. /** @}*/
  177. /* ----------------------------------------------- message end---------------------------------------------- */
  178. /**
  179. * @defgroup luatos_os_semaphore 信号量接口函数
  180. * @{
  181. */
  182. /* ---------------------------------------------- semaphore begin--------------------------------------------- */
  183. /**
  184. * @brief 定义信号量句柄
  185. */
  186. typedef void * luat_rtos_semaphore_t;
  187. /**
  188. * @brief 信号量创建,可以在中断中release
  189. *
  190. * @param semaphore_handle[OUT] 信号量句柄
  191. * @param init_count 初始值
  192. * @return int =0成功,其他失败
  193. */
  194. int luat_rtos_semaphore_create(luat_rtos_semaphore_t *semaphore_handle, uint32_t init_count);
  195. /**
  196. * @brief 删除信号量
  197. *
  198. * @param semaphore_handle 信号量句柄
  199. * @return int =0成功,其他失败
  200. */
  201. int luat_rtos_semaphore_delete(luat_rtos_semaphore_t semaphore_handle);
  202. /**
  203. * @brief 信号量等待获取
  204. *
  205. * @param semaphore_handle 信号量句柄
  206. * @param timeout 接收超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
  207. * @return int =0成功,其他失败
  208. */
  209. int luat_rtos_semaphore_take(luat_rtos_semaphore_t semaphore_handle, uint32_t timeout);
  210. /**
  211. * @brief 信号量释放发送
  212. *
  213. * @param semaphore_handle 信号量句柄
  214. * @return int =0成功,其他失败
  215. */
  216. int luat_rtos_semaphore_release(luat_rtos_semaphore_t semaphore_handle);
  217. /* ---------------------------------------------- semaphore end--------------------------------------------- */
  218. /** @}*/
  219. /**
  220. * @defgroup luatos_os_mutex 互斥锁接口函数
  221. * @{
  222. */
  223. /* ------------------------------------------------ mutex begin----------------------------------------------- */
  224. /**
  225. * @brief 定义mutex句柄
  226. */
  227. typedef void * luat_rtos_mutex_t;
  228. /**
  229. * @brief 互斥锁创建,不能在中断中unlock
  230. *
  231. * @param mutex_handle[OUT] 互斥锁句柄
  232. * @return int =0成功,其他失败
  233. */
  234. int luat_rtos_mutex_create(luat_rtos_mutex_t *mutex_handle);
  235. /**
  236. * @brief 获得锁
  237. *
  238. * @param mutex_handle 互斥锁句柄
  239. * @param timeout 超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
  240. * @return int =0成功,其他失败
  241. */
  242. int luat_rtos_mutex_lock(luat_rtos_mutex_t mutex_handle, uint32_t timeout);
  243. /**
  244. * @brief 释放锁
  245. *
  246. * @param mutex_handle 互斥锁句柄
  247. * @return int =0成功,其他失败
  248. */
  249. int luat_rtos_mutex_unlock(luat_rtos_mutex_t mutex_handle);
  250. /**
  251. * @brief 删除互斥锁
  252. *
  253. * @param mutex_handle 互斥锁句柄
  254. * @return int =0成功,其他失败
  255. */
  256. int luat_rtos_mutex_delete(luat_rtos_mutex_t mutex_handle);
  257. /* ------------------------------------------------ mutex end----------------------------------------------- */
  258. /** @}*/
  259. /**
  260. * @defgroup luatos_os_queue 队列接口函数
  261. * @{
  262. */
  263. /* ------------------------------------------------ queue begin----------------------------------------------- */
  264. /**
  265. * @brief 定义队列句柄
  266. */
  267. typedef void * luat_rtos_queue_t;
  268. /**
  269. * @brief 创建队列
  270. *
  271. * @param queue_handle[OUT] 返回的队列句柄
  272. * @param msgcount 队列里元素的最大数量
  273. * @param msgsize 队列里单个元素的大小
  274. * @return int =0成功,其他失败
  275. */
  276. int luat_rtos_queue_create(luat_rtos_queue_t *queue_handle, uint32_t item_count, uint32_t item_size);
  277. /**
  278. * @brief 删除队列
  279. *
  280. * @param queue_handle 队列句柄
  281. * @return int =0成功,其他失败
  282. */
  283. int luat_rtos_queue_delete(luat_rtos_queue_t queue_handle);
  284. /**
  285. * @brief 往队列里发送一个元素
  286. *
  287. * @param queue_handle 队列句柄
  288. * @param item 元素指针
  289. * @param item_size 元素大小,这个是兼容性参数,实际上必须于创建时的item_size一致,所以忽略
  290. * @param timeout 超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
  291. * @return int =0成功,其他失败
  292. */
  293. int luat_rtos_queue_send(luat_rtos_queue_t queue_handle, void *item, uint32_t item_size, uint32_t timeout);
  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_recv(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_cnt[OUT] 返回未处理的元素数量
  309. * @return int =0成功,其他失败
  310. */
  311. int luat_rtos_queue_get_cnt(luat_rtos_queue_t queue_handle, uint32_t *item_cnt);
  312. /* ------------------------------------------------ queue end----------------------------------------------- */
  313. /** @}*/
  314. /**
  315. * @defgroup luatos_os_flag 事件接口函数
  316. * @{
  317. */
  318. /* ------------------------------------------------ flag begin----------------------------------------------- */
  319. /**
  320. * @brief 定义事件句柄
  321. */
  322. typedef void * luat_rtos_flag_t;
  323. /**
  324. * @brief 创建事件
  325. *
  326. * @param flag_handle[OUT] 返回的事件句柄
  327. * @return int =0成功,其他失败
  328. */
  329. int luat_rtos_flag_create(luat_rtos_flag_t *flag_handle);
  330. /**
  331. * @brief 等待事件
  332. * @param mask 等待的事件掩码
  333. * @param operation 事件触发要求(与操作需要全部满足才触发,或操作有一个就触发)和操作(是否要清除)
  334. * @param flags[OUT] 当前事件状态值
  335. * @param timeout 超时时间
  336. * @return int =0成功,其他失败
  337. */
  338. 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);
  339. /**
  340. * @brief 设置事件
  341. * @param flag_handle 事件句柄
  342. * @param mask 设置掩码
  343. * @param operation 事件判断(与或)和操作(是否要清除),freertos支持或操作LUAT_FLAG_OR
  344. * @return int =0成功,其他失败
  345. */
  346. int luat_rtos_flag_release(luat_rtos_flag_t flag_handle, uint32_t mask, LUAT_FLAG_OP_E operation);
  347. /**
  348. * @brief 删除事件
  349. *
  350. * @param flag_handle 事件句柄
  351. * @return int =0成功,其他失败
  352. */
  353. int luat_rtos_flag_delete(luat_rtos_flag_t flag_handle);
  354. /* ------------------------------------------------ flag end----------------------------------------------- */
  355. /** @}*/
  356. /**
  357. * @defgroup luatos_os_timer 软件定时器接口函数
  358. * @{
  359. */
  360. /* ------------------------------------------------ timer begin----------------------------------------------- */
  361. /**
  362. * @brief 定时器头数据类型
  363. */
  364. typedef void * luat_rtos_timer_t;
  365. /**
  366. * @brief 定义定时器处理函数
  367. */
  368. typedef LUAT_RT_RET_TYPE (*luat_rtos_timer_callback_t)(LUAT_RT_CB_PARAM);
  369. /**
  370. * @brief 创建软件定时器
  371. *
  372. * @param timer_handle[OUT] 返回定时器句柄
  373. * @return int =0成功,其他失败
  374. */
  375. int luat_rtos_timer_create(luat_rtos_timer_t *timer_handle);
  376. /**
  377. * @brief 删除软件定时器
  378. *
  379. * @param timer_handle 定时器句柄
  380. * @return int =0成功,其他失败
  381. */
  382. int luat_rtos_timer_delete(luat_rtos_timer_t timer_handle);
  383. /**
  384. * @brief 启动软件定时器
  385. *
  386. * @param timer_handle 定时器句柄
  387. * @param timeout 超时时间,单位ms,没有特殊值
  388. * @param repeat 0不重复,其他重复
  389. * @param callback_fun 定时时间到后的回调函数
  390. * @param user_param 回调函数时的最后一个输入参数
  391. * @return int =0成功,其他失败
  392. */
  393. 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);
  394. /**
  395. * @brief 停止软件定时器
  396. *
  397. * @param timer_handle 定时器句柄
  398. * @return int =0成功,其他失败
  399. */
  400. int luat_rtos_timer_stop(luat_rtos_timer_t timer_handle);
  401. /**
  402. * @brief 检测软件定时器是否处于激活状态
  403. *
  404. * @param timer_handle 定时器句柄
  405. * @return int =0未激活,1激活,其他失败
  406. */
  407. int luat_rtos_timer_is_active(luat_rtos_timer_t timer_handle);
  408. /*------------------------------------------------ timer end----------------------------------------------- */
  409. /** @}*/
  410. /**
  411. * @defgroup luatos_os_critical 临界保护接口函数
  412. * @{
  413. */
  414. /* ------------------------------------------------ critical begin----------------------------------------------- */
  415. /**
  416. * @brief 进入临界保护
  417. *
  418. * @return uint32_t 退出临界保护所需参数
  419. */
  420. uint32_t luat_rtos_entry_critical(void);
  421. /**
  422. * @brief 退出临界保护
  423. *
  424. * @param critical 进入临界保护时返回的参数
  425. */
  426. void luat_rtos_exit_critical(uint32_t critical);
  427. uint32_t luat_rtos_get_ipsr(void);
  428. /*------------------------------------------------ critical end----------------------------------------------- */
  429. /** @}*/
  430. /** @}*/
  431. #endif