| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- /*
- * Copyright (c) 2022 OpenLuat & AirM2M
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
- * this software and associated documentation files (the "Software"), to deal in
- * the Software without restriction, including without limitation the rights to
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
- * the Software, and to permit persons to whom the Software is furnished to do so,
- * subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
- #ifndef LUAT_RTOS_H
- #define LUAT_RTOS_H
- #include "luat_base.h"
- #include "luat_rtos_legacy.h"
- /**
- * @defgroup luatos_os 操作系统接口
- * @{
- */
- /**
- * @brief LUAT_RTOS 超时时间枚举值
- */
- typedef enum LUAT_RTOS_WAIT
- {
- LUAT_NO_WAIT = 0, /**< 超时时间为0 */
- LUAT_WAIT_FOREVER = (uint32_t)0xFFFFFFFF /**< 最大超时时间0xFFFFFFFF*/
- } LUAT_RTOS_WAIT_E;
- typedef enum
- {
- LUAT_FLAG_AND = 5,
- LUAT_FLAG_AND_CLEAR = 6,
- LUAT_FLAG_OR = 7,
- LUAT_FLAG_OR_CLEAR = 8
- } LUAT_FLAG_OP_E;
- /* ------------------------------------------------ task begin------------------------------------------------ */
- /**
- * @defgroup luatos_os_Task 线程任务接口函数
- * @{
- */
- /**
- *@brief task的入口函数,函数类型
- */
- typedef void (*luat_rtos_task_entry) (void*);
- /**
- *@brief 定义task任务句柄
- */
- typedef void * luat_rtos_task_handle;
- /**
- * @brief 创建一个可以带mailbox机制的task,mailbox是message和event的基础,但是和queue无关
- *
- * @param task_handle[OUT] 返回创建的句柄
- * @param stack_size task的栈空间大小,单位byte,必须4字节对齐
- * @param priority 优先级,单位是百分比,0%~100%,100%为最高等级,由具体实现转换到底层SDK用的优先级
- * @param task_name task名字
- * @param task_fun task的入口函数
- * @param user_data task的入口参数
- * @param event_cout 如果OS允许在中断里malloc,或者不使用message和event机制的,这个参数无视,如果OS不允许在中断里malloc,则这里填写预分配的event空间用于中断里使用,如果写0会使用公共event
- * @return int =0成功,其他失败
- */
- 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);
- /**
- * @brief 删除task
- *
- * @param task_handle
- * @return int =0成功,其他失败
- */
- int luat_rtos_task_delete(luat_rtos_task_handle task_handle);
- /**
- * @brief 挂起某个task
- *
- * @param task_handle task句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_task_suspend(luat_rtos_task_handle task_handle);
- /**
- * @brief 恢复挂起的task
- *
- * @param task_handle task句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_task_resume(luat_rtos_task_handle task_handle);
- /**
- * @brief 挂起全部task
- *
- */
- void luat_rtos_task_suspend_all(void);
- /**
- * @brief 恢复全部task
- *
- */
- void luat_rtos_task_resume_all(void);
- /**
- * @brief task休眠一段时间
- *
- * @param ms 休眠时间,单位ms
- */
- void luat_rtos_task_sleep(uint32_t ms);
- /**
- * @brief 获取当前task的句柄
- *
- * @return luat_rtos_task_handle 当前task的句柄
- */
- luat_rtos_task_handle luat_rtos_get_current_handle(void);
- /**
- * @brief 获取task堆栈剩余的最小值,叫做“高水位线”
- *
- * @param luat_rtos_task_handle task的句柄
- * @return task堆栈剩余的最小值,单位为字
- */
- uint32_t luat_rtos_task_get_high_water_mark(luat_rtos_task_handle task_handle);
- uint32_t luat_rtos_task_stack_free_space(luat_rtos_task_handle task_handle);
- /**
- * @brief 启动task运行时间记录功能,必须在task调度前启用
- */
- void luat_rtos_task_run_time_record_enable(void);
- /**
- * @brief 打印所有task运行时间和占用百分比
- * @param print_ticks 打印具体ticks
- */
- void luat_rtos_task_run_time_record_print(uint8_t print_ticks);
- /** @}*/
- /* ------------------------------------------------ task end------------------------------------------------ */
- /**
- * @defgroup luatos_os_event 消息事件函数
- * @{
- */
- /* ----------------------------------------------- event begin---------------------------------------------- */
- /**
- * @brief 在等待event中,如果设置了目标event id,而到来的不是目标event id,可以通过回调函数交给用户处理
- *
- */
- typedef LUAT_RT_RET_TYPE (*luat_rtos_event_wait_callback_t)(LUAT_RT_CB_PARAM);
- /**
- * @brief 发送一个event给task的mailbox,只有设置了mailbox启用的task能接收,如果缓存了超过1024个event会断言
- *
- * @param task_handle 需要接收event的task句柄
- * @param id event id
- * @param param1 event参数1
- * @param param2 event参数2
- * @param param3 event参数3
- * @param timeout 发送超时,已经废弃了
- * @return int =0成功,其他失败
- */
- 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);
- /**
- * @brief 接收一个event,只能在task里接收
- *
- * @param task_handle 需要接收event的task句柄
- * @param wait_event_id 目标event的ID,=0表示不限制,任意event id都会返回
- * @param out_event[OUT] 接收到的event
- * @param callback_fun event的ID不是目标ID时,用户回调函数,可以为NULL,从而抛弃掉这个event
- * @param timeout 接收超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
- * @return int =0成功,其他失败
- */
- 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);
- /* ----------------------------------------------- event end---------------------------------------------- */
- /* ----------------------------------------------- message begin---------------------------------------------- */
- /**
- * @brief 发送一个message给task的mailbox,只有设置了mailbox启用的task能接收,message可以动态创建的,可以任意大小,如果缓存了超过1024个message会断言
- *
- * @param task_handle 需要接收massage的task句柄
- * @param message_id message id
- * @param p_message message内容,传入指针,如果动态创建,需要在接收时释放
- * @return int =0成功,其他失败
- */
- int luat_rtos_message_send(luat_rtos_task_handle task_handle, uint32_t message_id, void *p_message);
- /**
- * @brief 接收一个message,只能在task里接收
- *
- * @param task_handle 需要接收massage的task句柄
- * @param message_id[OUT] 接收到的message id
- * @param p_p_message[OUT] message内容,输出一个void *指针,如果是发送时动态创建的,需要释放掉
- * @param timeout 接收超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
- * @return int =0成功,其他失败
- */
- int luat_rtos_message_recv(luat_rtos_task_handle task_handle, uint32_t *message_id, void **p_p_message, uint32_t timeout);
- /** @}*/
- /* ----------------------------------------------- message end---------------------------------------------- */
- /**
- * @defgroup luatos_os_semaphore 信号量接口函数
- * @{
- */
- /* ---------------------------------------------- semaphore begin--------------------------------------------- */
- /**
- * @brief 定义信号量句柄
- */
- typedef void * luat_rtos_semaphore_t;
- /**
- * @brief 信号量创建,可以在中断中release
- *
- * @param semaphore_handle[OUT] 信号量句柄
- * @param init_count 初始值
- * @return int =0成功,其他失败
- */
- int luat_rtos_semaphore_create(luat_rtos_semaphore_t *semaphore_handle, uint32_t init_count);
- /**
- * @brief 删除信号量
- *
- * @param semaphore_handle 信号量句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_semaphore_delete(luat_rtos_semaphore_t semaphore_handle);
- /**
- * @brief 信号量等待获取
- *
- * @param semaphore_handle 信号量句柄
- * @param timeout 接收超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
- * @return int =0成功,其他失败
- */
- int luat_rtos_semaphore_take(luat_rtos_semaphore_t semaphore_handle, uint32_t timeout);
- /**
- * @brief 信号量释放发送
- *
- * @param semaphore_handle 信号量句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_semaphore_release(luat_rtos_semaphore_t semaphore_handle);
- /* ---------------------------------------------- semaphore end--------------------------------------------- */
- /** @}*/
- /**
- * @defgroup luatos_os_mutex 互斥锁接口函数
- * @{
- */
- /* ------------------------------------------------ mutex begin----------------------------------------------- */
- /**
- * @brief 定义mutex句柄
- */
- typedef void * luat_rtos_mutex_t;
- /**
- * @brief 互斥锁创建,不能在中断中unlock
- *
- * @param mutex_handle[OUT] 互斥锁句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_mutex_create(luat_rtos_mutex_t *mutex_handle);
- /**
- * @brief 获得锁
- *
- * @param mutex_handle 互斥锁句柄
- * @param timeout 超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
- * @return int =0成功,其他失败
- */
- int luat_rtos_mutex_lock(luat_rtos_mutex_t mutex_handle, uint32_t timeout);
- /**
- * @brief 释放锁
- *
- * @param mutex_handle 互斥锁句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_mutex_unlock(luat_rtos_mutex_t mutex_handle);
- /**
- * @brief 删除互斥锁
- *
- * @param mutex_handle 互斥锁句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_mutex_delete(luat_rtos_mutex_t mutex_handle);
- /* ------------------------------------------------ mutex end----------------------------------------------- */
- /** @}*/
- /**
- * @defgroup luatos_os_queue 队列接口函数
- * @{
- */
- /* ------------------------------------------------ queue begin----------------------------------------------- */
- /**
- * @brief 定义队列句柄
- */
- typedef void * luat_rtos_queue_t;
- /**
- * @brief 创建队列
- *
- * @param queue_handle[OUT] 返回的队列句柄
- * @param msgcount 队列里元素的最大数量
- * @param msgsize 队列里单个元素的大小
- * @return int =0成功,其他失败
- */
- int luat_rtos_queue_create(luat_rtos_queue_t *queue_handle, uint32_t item_count, uint32_t item_size);
- /**
- * @brief 删除队列
- *
- * @param queue_handle 队列句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_queue_delete(luat_rtos_queue_t queue_handle);
- /**
- * @brief 往队列里发送一个元素
- *
- * @param queue_handle 队列句柄
- * @param item 元素指针
- * @param item_size 元素大小,这个是兼容性参数,实际上必须于创建时的item_size一致,所以忽略
- * @param timeout 超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
- * @return int =0成功,其他失败
- */
- int luat_rtos_queue_send(luat_rtos_queue_t queue_handle, void *item, uint32_t item_size, uint32_t timeout);
- /**
- * @brief 从队列里取出一个元素
- *
- * @param queue_handle 队列句柄
- * @param item 元素指针
- * @param item_size 元素大小,这个是兼容性参数,实际上必须于创建时的item_size一致,所以忽略
- * @param timeout 超时,单位ms,特殊值见LUAT_RTOS_WAIT_E
- * @return int =0成功,其他失败
- */
- int luat_rtos_queue_recv(luat_rtos_queue_t queue_handle, void *item, uint32_t item_size, uint32_t timeout);
- /**
- * @brief 查询队列中剩余未处理的元素数量
- *
- * @param queue_handle 队列句柄
- * @param item_cnt[OUT] 返回未处理的元素数量
- * @return int =0成功,其他失败
- */
- int luat_rtos_queue_get_cnt(luat_rtos_queue_t queue_handle, uint32_t *item_cnt);
- /* ------------------------------------------------ queue end----------------------------------------------- */
- /** @}*/
- /**
- * @defgroup luatos_os_flag 事件接口函数
- * @{
- */
- /* ------------------------------------------------ flag begin----------------------------------------------- */
- /**
- * @brief 定义事件句柄
- */
- typedef void * luat_rtos_flag_t;
- /**
- * @brief 创建事件
- *
- * @param flag_handle[OUT] 返回的事件句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_flag_create(luat_rtos_flag_t *flag_handle);
- /**
- * @brief 等待事件
- * @param mask 等待的事件掩码
- * @param operation 事件触发要求(与操作需要全部满足才触发,或操作有一个就触发)和操作(是否要清除)
- * @param flags[OUT] 当前事件状态值
- * @param timeout 超时时间
- * @return int =0成功,其他失败
- */
- 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);
- /**
- * @brief 设置事件
- * @param flag_handle 事件句柄
- * @param mask 设置掩码
- * @param operation 事件判断(与或)和操作(是否要清除),freertos支持或操作LUAT_FLAG_OR
- * @return int =0成功,其他失败
- */
- int luat_rtos_flag_release(luat_rtos_flag_t flag_handle, uint32_t mask, LUAT_FLAG_OP_E operation);
- /**
- * @brief 删除事件
- *
- * @param flag_handle 事件句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_flag_delete(luat_rtos_flag_t flag_handle);
- /* ------------------------------------------------ flag end----------------------------------------------- */
- /** @}*/
- /**
- * @defgroup luatos_os_timer 软件定时器接口函数
- * @{
- */
- /* ------------------------------------------------ timer begin----------------------------------------------- */
- /**
- * @brief 定时器头数据类型
- */
- typedef void * luat_rtos_timer_t;
- /**
- * @brief 定义定时器处理函数
- */
- typedef LUAT_RT_RET_TYPE (*luat_rtos_timer_callback_t)(LUAT_RT_CB_PARAM);
- /**
- * @brief 创建软件定时器
- *
- * @param timer_handle[OUT] 返回定时器句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_timer_create(luat_rtos_timer_t *timer_handle);
- /**
- * @brief 删除软件定时器
- *
- * @param timer_handle 定时器句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_timer_delete(luat_rtos_timer_t timer_handle);
- /**
- * @brief 启动软件定时器
- *
- * @param timer_handle 定时器句柄
- * @param timeout 超时时间,单位ms,没有特殊值
- * @param repeat 0不重复,其他重复
- * @param callback_fun 定时时间到后的回调函数
- * @param user_param 回调函数时的最后一个输入参数
- * @return int =0成功,其他失败
- */
- 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);
- /**
- * @brief 停止软件定时器
- *
- * @param timer_handle 定时器句柄
- * @return int =0成功,其他失败
- */
- int luat_rtos_timer_stop(luat_rtos_timer_t timer_handle);
- /**
- * @brief 检测软件定时器是否处于激活状态
- *
- * @param timer_handle 定时器句柄
- * @return int =0未激活,1激活,其他失败
- */
- int luat_rtos_timer_is_active(luat_rtos_timer_t timer_handle);
- /*------------------------------------------------ timer end----------------------------------------------- */
- /** @}*/
- /**
- * @defgroup luatos_os_critical 临界保护接口函数
- * @{
- */
- /* ------------------------------------------------ critical begin----------------------------------------------- */
- /**
- * @brief 进入临界保护
- *
- * @return uint32_t 退出临界保护所需参数
- */
- uint32_t luat_rtos_entry_critical(void);
- /**
- * @brief 退出临界保护
- *
- * @param critical 进入临界保护时返回的参数
- */
- void luat_rtos_exit_critical(uint32_t critical);
- /**
- * @brief 获取是否为中断
- *
- * @param critical 进入临界保护时返回的参数
- * @return uint32_t 0不是中断,1是中断
- */
- uint32_t luat_rtos_get_ipsr(void);
- #if defined(CHIP_EC618) || defined(CHIP_EC716) || defined(CHIP_EC718)
- #define luat_rtos_ms2tick(ms) (ms)
- #else
- int luat_rtos_ms2tick(int ms);
- #endif
- /*------------------------------------------------ critical end----------------------------------------------- */
- /** @}*/
- /** @}*/
- #endif
|