xt-sdk 文档 v1.0.3
xt-sdk 嵌入式 SDK API 参考
载入中...
搜索中...
未找到
xt_task.h
浏览该文件的文档.
1
12
13#ifndef __XT_TASK_H__
14#define __XT_TASK_H__
15
16/* ==================== [Includes] ========================================== */
17
18#include "xtiny.h"
19#if (XT_TASK_ENABLE_OS)
20#include "cmsis_os2.h"
21#endif
22#include "xt_timer2.h"
23#include "xt_event.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
35
36/* ==================== [Defines] =========================================== */
37
39#define XT_TASK_INLIVAD_ID 0xff
40
42#define XT_TASK_WAIT_FOREVER 0xffffffff
43
45#define XT_TASK_LC_INIT_VALUE 0
46
53#define xt_task_lc_init(s) s = XT_TASK_LC_INIT_VALUE;
54
62#define xt_task_lc_resume(s) switch(s) { case XT_TASK_LC_INIT_VALUE:
63
73#define _xt_task_lc_set(s) s = __LINE__; case __LINE__:
74
82#define xt_task_lc_end(s) default:; break; }
83
84/* ==================== [Typedefs] ========================================== */
85
90typedef void (*xt_task_entry_t)(void *user_data);
91
93typedef struct xt_task xt_task_t;
94
107
108/* ==================== [Global Prototypes] ================================= */
109
114
123
134xt_task_t *xt_task_start(xt_task_entry_t entry, void *user_data);
135
143
154
162 /* 任务生命周期与控制 */
164
165/* ---- 内部 API(用户不应直接调用) ---- */
166
174void xt_task_timer_start(uint32_t timeout_ms);
175
184void xt_task_event_subscribe(xt_event_id_t event_id, uint32_t timeout_ms);
185
196
197/* ==================== [Macros] ============================================ */
198
208#define XT_TASK_BEGIN() \
209 _Pragma("GCC diagnostic push") \
210 _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"") \
211 { char XT_TASK_YIELD_FLAG = 1; (void)XT_TASK_YIELD_FLAG; xt_task_lc_resume(xt_task_running()->lc)
212
220#define XT_TASK_END() \
221 xt_task_lc_end(); xt_task_lc_init(xt_task_running()->lc);} \
222 _Pragma("GCC diagnostic pop")
223
234#define xt_task_delay(ms) \
235 do { \
236 xt_task_timer_start(ms); \
237 XT_TASK_YIELD_FLAG = 0; \
238 _xt_task_lc_set(xt_task_running()->lc); \
239 if (XT_TASK_YIELD_FLAG == 0) { \
240 return; \
241 } \
242 } while(0)
243
256#define xt_task_wait_event(event_id, event_param) \
257 do { \
258 xt_task_event_subscribe(event_id, XT_TASK_WAIT_FOREVER); \
259 XT_TASK_YIELD_FLAG = 0; \
260 _xt_task_lc_set(xt_task_running()->lc); \
261 if (XT_TASK_YIELD_FLAG == 0) { \
262 return; \
263 } \
264 event_param = xt_event_get_current_event_param(); \
265 } while(0)
266
281#define xt_task_wait_until(event_id, timeout) \
282 do { \
283 xt_task_event_subscribe(event_id, timeout); \
284 XT_TASK_YIELD_FLAG = 0; \
285 _xt_task_lc_set((xt_task_running()->lc)); \
286 if (XT_TASK_YIELD_FLAG == 0) { \
287 return; \
288 } \
289 } while(0)
290
305#define xt_task_call_subtask(entry, user_data) \
306 do { \
307 xt_task_t *__sub = xt_task_start(entry, user_data); \
308 if (__sub == NULL) { \
309 break; \
310 } \
311 XT_TASK_YIELD_FLAG = 0; \
312 _xt_task_lc_set((xt_task_running()->lc)); \
313 if (XT_TASK_YIELD_FLAG == 0) { \
314 return; \
315 } \
316 } while(0)
317 /* xt_task */
319
320#ifdef __cplusplus
321} /* extern "C" */
322#endif
323
324#endif /* __XT_TASK_H__ */
int32_t xt_err_t
错误码类型
uint32_t xt_event_id_t
事件 ID 类型,32 位无符号
void(* xt_task_entry_t)(void *user_data)
协程任务入口函数类型
void xt_task_event_handler(xt_event_id_t event_id)
事件到达分发入口
xt_task_t * xt_task_running(void)
获取当前正在运行的 task
void xt_task_event_subscribe(xt_event_id_t event_id, uint32_t timeout_ms)
为当前 task 订阅事件
xt_task_t * xt_task_start(xt_task_entry_t entry, void *user_data)
启动一个协程任务
bool xt_task_is_running(xt_task_t *task)
检查 task 是否在运行
void xt_task_timer_start(uint32_t timeout_ms)
启动当前 task 的定时器
void xt_task_sys_reset(void)
重置 task 子系统(仅测试用)
struct xt_task xt_task_t
协程任务句柄
xt_err_t xt_task_setup(void)
初始化 task 子系统
struct xt_timer2 xt_timer2_t
定时器句柄(前向声明)
任务控制块结构体
xt_task_entry_t entry
uint8_t task_id
xt_timer2_t timer
uint8_t parent_id
xt_event_id_t wait_event
void * user_data
volatile int lc
事件发布订阅模块接口(去重匹配、dispatch 上下文栈、临界区保护)
新版软件定时器接口(对象化、单链表、new/delete 生命周期)
XTINY 总头文件