ql_api_osi.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*================================================================
  2. Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
  3. Quectel Wireless Solutions Proprietary and Confidential.
  4. =================================================================*/
  5. #ifndef _QL_API_OSI_H_
  6. #define _QL_API_OSI_H_
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include "ql_osi_def.h"
  11. #include "stdio.h"
  12. #include "stdbool.h"
  13. #include "stdint.h"
  14. #define QL_TIMER_IN_SERVICE ((void *)0xffffffff)
  15. typedef int QlOSStatus;
  16. typedef void * ql_task_t;
  17. typedef void * ql_sem_t;
  18. typedef void * ql_mutex_t;
  19. typedef void * ql_queue_t;
  20. typedef void * ql_timer_t;
  21. typedef void * ql_flag_t;
  22. typedef void * ql_egroup_t;
  23. typedef int QuecOSStatus;
  24. typedef uint32 ql_event_bits_t;
  25. typedef enum
  26. {
  27. QL_WAIT_FOREVER = 0xFFFFFFFFUL,
  28. QL_NO_WAIT = 0
  29. } ql_wait_e;
  30. typedef enum
  31. {
  32. Running = 0, /* A task is querying the state of itself, so must be running. */
  33. Ready, /* The task being queried is in a read or pending ready list. */
  34. Blocked, /* The task being queried is in the Blocked state. */
  35. Suspended, /* The task being queried is in the Suspended state, or is in the Blocked state with an infinite time out. */
  36. Deleted, /* The task being queried has been deleted, but its TCB has not yet been freed. */
  37. Invalid /* Used as an 'invalid state' value. */
  38. } ql_task_state_e;
  39. typedef struct
  40. {
  41. ql_task_t xHandle; /* The handle of the task to which the rest of the information in the structure relates. */
  42. const char * pcTaskName; /* A pointer to the task's name. This value will be invalid if the task was deleted since the structure was populated! */
  43. ql_task_state_e eCurrentState; /* The state in which the task existed when the structure was populated. */
  44. unsigned long uxCurrentPriority; /* The priority at which the task was running (may be inherited) when the structure was populated. */
  45. uint16 usStackHighWaterMark; /* The minimum amount of stack space that has remained for the task since the task was created. The closer this value is to zero the closer the task has come to overflowing its stack. */
  46. } ql_task_status_t;
  47. typedef struct
  48. {
  49. uint32 id; ///< event identifier
  50. uint32 param1; ///< 1st parameter
  51. uint32 param2; ///< 2nd parameter
  52. uint32 param3; ///< 3rd parameter
  53. } ql_event_t;
  54. /* Note: The (ql_timeval_t) and (struct timeval) data structures are independent of each other. Please do not equate them. */
  55. typedef struct
  56. {
  57. uint32 sec;
  58. uint32 usec;
  59. }ql_timeval_t;
  60. typedef struct
  61. {
  62. uint total_idle_tick;
  63. uint sys_tick_old;
  64. uint cpu_using;
  65. uint idle_in_tick;
  66. uint idle_out_tick;
  67. }ql_cpu_using_info_t;
  68. /*========================================================================
  69. * function Definition
  70. *========================================================================*/
  71. typedef void (*ql_swdog_callback)(uint32 id_type, void *ctx);
  72. typedef void (*lvgl_Callback_t)(void *ctx);
  73. /**
  74. * \brief Call malloc()
  75. * size - The size of the memory block, in bytes
  76. * Attention: The allocated memory has been memset()
  77. *
  78. * \return This function returns a pointer. If the request fails, NULL is returned.
  79. */
  80. void* ql_malloc(int size);
  81. /**
  82. * \brief Call free()
  83. * Free the memory space allocated by ql_malloc
  84. *
  85. * Attention: Contains null pointer judgments
  86. *
  87. */
  88. void ql_free(void *ptr);
  89. #define QL_FREE_PTR(p) {if(p != NULL) {ql_free(p);p = NULL;}}
  90. extern QlOSStatus ql_rtos_task_create
  91. (
  92. ql_task_t * taskRef, /* OS task reference */
  93. uint32 stackSize, /* number of bytes in task stack area */
  94. uint8 priority, /* task priority, users should use the defination in APP_ThreadPriority_e,
  95. and not recommand beyond APP_PRIORITY_ABOVE_NORMAL */
  96. char * taskName, /* task name, no more than 16 bytes, exceed 16 bytes will only save 16 bytes */
  97. void ( * taskStart)(void*), /* pointer to task entry point */
  98. void* argv, /* task entry argument pointer */
  99. uint32 event_count, /* the max event count that the task can waiting to process */
  100. ...
  101. );
  102. extern QlOSStatus ql_rtos_task_create_default
  103. (
  104. ql_task_t * taskRef, /* OS task reference */
  105. uint32 stackSize, /* number of bytes in task stack area */
  106. uint8 priority, /* task priority, users should use the defination in APP_ThreadPriority_e,
  107. and not recommand beyond APP_PRIORITY_ABOVE_NORMAL */
  108. char * taskName, /* task name, no more than 16 bytes, exceed 16 bytes will only save 16 bytes */
  109. void ( * taskStart)(void*), /* pointer to task entry point */
  110. void* argv, /* task entry argument pointer */
  111. ...
  112. );
  113. //for app task, the event ID of the 2nd param, should between QL_EVENT_APP_START and QL_EVEN_MAX
  114. //if target task has no space to store event(event is full), this api will not wait it, and will return QL_OSI_EVENT_SEND_FAIL
  115. extern QlOSStatus ql_rtos_event_send
  116. (
  117. ql_task_t task_ref,
  118. ql_event_t *event
  119. );
  120. //for app task, the event ID of the 2nd param, should between QL_EVENT_APP_START and QL_EVEN_MAX
  121. //if target task has no space to store event(event is full), this api will not wait it, and will return QL_OSI_EVENT_SEND_FAIL
  122. QlOSStatus ql_rtos_event_send_ex
  123. (
  124. ql_task_t task_ref,
  125. ql_event_t *event,
  126. uint32 timeout_ms
  127. );
  128. extern QlOSStatus ql_event_wait
  129. (
  130. ql_event_t *event_strc, /* event structure */
  131. uint32 timeout /* event wait timeout */
  132. );
  133. extern QlOSStatus ql_event_try_wait
  134. (
  135. ql_event_t *event_strc /* event structure */
  136. );
  137. QlOSStatus ql_rtos_event_group_create
  138. (
  139. ql_egroup_t * egroupRef /* OS event group reference */
  140. );
  141. QlOSStatus ql_rtos_event_group_wait
  142. (
  143. ql_egroup_t egroupRef, /* OS event group reference */
  144. const ql_event_bits_t uxBitsToWaitFor, /* A bitwise value that indicates the bit or bits to test inside the event group.
  145. ex:to wait for bit 0 and/or bit 2 set uxBitsToWaitFor to 0x05. */
  146. const bool ClearOnExit, /* Whether the event group is cleared after the conditions are met. TRUE -- clear; FALSE -- not clear */
  147. const bool WaitForAllBits, /* Whether to wait until all bits set by the uxBitsToWaitFor parameter are set or the timeout period expires */
  148. ql_event_bits_t *curr_bits, /* The value of the event group at the time either the bits being waited for became set, or the block time expired */
  149. uint32 timeout_ms /* event group wait timeout */
  150. );
  151. QlOSStatus ql_rtos_event_group_clear
  152. (
  153. ql_egroup_t egroupRef, /* OS event group reference */
  154. const ql_event_bits_t uxBitsToClear, /* The event group in which the bits are to be cleared*/
  155. ql_event_bits_t *curr_bits /* The value of the current event group */
  156. );
  157. QlOSStatus ql_rtos_event_group_set
  158. (
  159. ql_egroup_t egroupRef, /* OS event group reference */
  160. const ql_event_bits_t uxBitsToSet, /* The event group in which the bits are to be set */
  161. ql_event_bits_t *curr_bits /* The value of the current event group */
  162. );
  163. /*
  164. Atomically set bits within an event group, then wait for a combination of bits to be set within the same event group.
  165. This functionality is typically used to synchronise multiple tasks, where each task has to wait for the other tasks to reach a synchronisation point before proceeding.
  166. The function will return before its block time expires if the bits specified by the uxBitsToWait parameter are set, or become set within that time.
  167. In this case all the bits specified by uxBitsToWait will be automatically cleared before the function returns.
  168. */
  169. QlOSStatus ql_rtos_event_group_sync
  170. (
  171. ql_egroup_t egroupRef, /* OS event group reference */
  172. const ql_event_bits_t uxBitsToSet, /* The bits to set in the event group before determining if, and possibly waiting for,
  173. all the bits specified by the uxBitsToWait parameter are set */
  174. const ql_event_bits_t uxBitsToWaitFor, /* A bitwise value that indicates the bit or bits to test inside the event group. */
  175. ql_event_bits_t *curr_bits, /* The value of the current event group */
  176. uint32 timeout_ms /* event group wait timeout */
  177. );
  178. QlOSStatus ql_rtos_event_group_get
  179. (
  180. ql_egroup_t egroupRef, /* OS event group reference */
  181. ql_event_bits_t *curr_bits /* The value of the current event group */
  182. );
  183. QlOSStatus ql_rtos_event_group_delete
  184. (
  185. ql_egroup_t egroupRef /* OS event group reference */
  186. );
  187. //if you delete the current task itself, the code behind ql_rtos_task_delete will have no chance to run
  188. extern QlOSStatus ql_rtos_task_delete
  189. (
  190. ql_task_t taskRef /* OS task reference */
  191. );
  192. extern QlOSStatus ql_rtos_task_suspend
  193. (
  194. ql_task_t taskRef /* OS task reference */
  195. );
  196. extern QlOSStatus ql_rtos_task_resume
  197. (
  198. ql_task_t taskRef /* OS task reference */
  199. );
  200. extern void ql_rtos_task_yield(void);
  201. extern QlOSStatus ql_rtos_task_get_current_ref
  202. (
  203. ql_task_t * taskRef /* OS task reference */
  204. );
  205. extern QlOSStatus ql_rtos_task_change_priority
  206. (
  207. ql_task_t taskRef, /* OS task reference */
  208. uint8 new_priority, /* OS task new priority for in */
  209. uint8 *old_priority /* OS task old priority for out */
  210. );
  211. extern QlOSStatus ql_rtos_task_get_priority
  212. (
  213. ql_task_t taskRef, /* OS task reference */
  214. uint8 * priority_ptr /* OS task priority for out */
  215. );
  216. extern QlOSStatus ql_rtos_task_get_status
  217. (
  218. ql_task_t task_ref,
  219. ql_task_status_t *status
  220. );
  221. extern void ql_rtos_task_sleep_ms
  222. (
  223. uint32 ms /* OS task sleep time for ms */
  224. );
  225. extern void ql_rtos_task_sleep_s
  226. (
  227. uint32 s /* OS task sleep time for s */
  228. );
  229. QlOSStatus ql_rtos_task_set_userdata
  230. (
  231. ql_task_t taskRef, /* OS task reference */
  232. void *userData /* The user data of pointer type */
  233. );
  234. QlOSStatus ql_rtos_task_get_userdata
  235. (
  236. ql_task_t taskRef, /* OS task reference */
  237. void **userData /* The user data of pointer type */
  238. );
  239. /*
  240. 临界区中需注意:
  241. * 1. 临界区中不可有阻塞,延迟动作
  242. * 2. 临界区中不可使用操作系统带阻塞类接口(事件发送且带timeout,信号量等待且带timeout等)
  243. * 3. 临界区中不可调用Audio停止/开始相关接口, TTS停止/开始接口,文件读写接口
  244. * 4. 临界区中不可调用CFW(RPC相关)接口
  245. */
  246. extern uint32_t ql_rtos_enter_critical(void);
  247. //the parameter is the return value from ql_rtos_enter_critical()
  248. extern void ql_rtos_exit_critical(uint32_t critical);
  249. extern QlOSStatus ql_rtos_semaphore_create
  250. (
  251. ql_sem_t *semaRef, /* OS semaphore reference */
  252. uint32 initialCount /* initial count of the semaphore */
  253. );
  254. extern QlOSStatus ql_rtos_semaphore_create_ex
  255. (
  256. ql_sem_t *semaRef, /* OS semaphore reference */
  257. uint32 initialCount, /* initial count of the semaphore */
  258. uint32 max_cnt /* max count of the semaphore */
  259. );
  260. extern QlOSStatus ql_rtos_semaphore_wait
  261. (
  262. ql_sem_t semaRef, /* OS semaphore reference */
  263. uint32 timeout /* QL_WAIT_FOREVER, QL_NO_WAIT, or timeout */
  264. );
  265. extern QlOSStatus ql_rtos_semaphore_release
  266. (
  267. ql_sem_t semaRef /* OS semaphore reference */
  268. );
  269. extern QlOSStatus ql_rtos_semaphore_get_cnt
  270. (
  271. ql_sem_t semaRef, /* OS semaphore reference */
  272. uint32 * cnt_ptr /* out-parm to save the cnt of semaphore */
  273. );
  274. extern QlOSStatus ql_rtos_semaphore_delete
  275. (
  276. ql_sem_t semaRef /* OS semaphore reference */
  277. );
  278. extern QlOSStatus ql_rtos_mutex_create
  279. (
  280. ql_mutex_t *mutexRef /* OS mutex reference */
  281. );
  282. extern QlOSStatus ql_rtos_mutex_lock
  283. (
  284. ql_mutex_t mutexRef, /* OS mutex reference */
  285. uint32 timeout /* mutex wait timeout */
  286. );
  287. extern QlOSStatus ql_rtos_mutex_try_lock
  288. (
  289. ql_mutex_t mutexRef /* OS mutex reference */
  290. );
  291. extern QlOSStatus ql_rtos_mutex_unlock
  292. (
  293. ql_mutex_t mutexRef /* OS mutex reference */
  294. );
  295. extern QlOSStatus ql_rtos_mutex_delete
  296. (
  297. ql_mutex_t mutexRef /* OS mutex reference */
  298. );
  299. extern QlOSStatus ql_rtos_queue_create
  300. (
  301. ql_queue_t *msgQRef, /* OS message queue reference */
  302. uint32 maxSize, /* max message size the queue supports */
  303. uint32 maxNumber /* max # of messages in the queue */
  304. );
  305. extern QlOSStatus ql_rtos_queue_wait
  306. (
  307. ql_queue_t msgQRef, /* message queue reference */
  308. uint8 *recvMsg, /* pointer to the message received */
  309. uint32 size, /* size of the message */
  310. uint32 timeout /* QL_WAIT_FOREVER, QL_NO_WAIT, or timeout */
  311. );
  312. extern QlOSStatus ql_rtos_queue_release
  313. (
  314. ql_queue_t msgQRef, /* message queue reference */
  315. uint32 size, /* size of the message */
  316. uint8 *msgPtr, /* start address of the data to be sent */
  317. uint32 timeout /* QL_WAIT_FOREVER, QL_NO_WAIT, or timeout */
  318. );
  319. extern QlOSStatus ql_rtos_queue_get_cnt
  320. (
  321. ql_queue_t msgQRef, /* message queue reference */
  322. uint32 *cnt_ptr /* out-parm to save the cnt of message queue */
  323. );
  324. extern QlOSStatus ql_rtos_queue_get_free_cnt
  325. (
  326. ql_queue_t msgQRef, /* message queue reference */
  327. uint32 *cnt_ptr /* out-parm to save the cnt of message queue */
  328. );
  329. extern QlOSStatus ql_rtos_queue_reset
  330. (
  331. ql_queue_t msgQRef /* message queue reference */
  332. );
  333. extern QlOSStatus ql_rtos_queue_delete
  334. (
  335. ql_queue_t msgQRef /* message queue reference */
  336. );
  337. /*
  338. * taskRef取值:
  339. * 若指定task去运行定时器回调函数,则填入任务句柄;若在系统service中运行定时器回调函数,填入QL_TIMER_IN_SERVICE
  340. * 若需要在中断中运行定时器回调函数,则填入NULL, 定时器超时时将会产生中断并在中断中调用回调函数,但以下几点需注意:
  341. * 1. 中断中不可有阻塞,延迟动作,否则可能会引起未知后果
  342. * 2. 中断中不可进临界区(调用critical相关接口),操作系统带阻塞类接口(事件发送且带timeout,信号量等待且带timeout等)
  343. * 3. 中断中不可调用Audio停止/开始相关接口, TTS停止/开始接口,文件读写接口
  344. * 4. 中断中不可调用CFW(RPC相关)接口
  345. * 建议中断中只做置标致位,做简单运算,或者中断中调用无阻塞(Timeout设为0)的线程通信函数,通知自己的线程去处理
  346. */
  347. extern QlOSStatus ql_rtos_timer_create
  348. (
  349. ql_timer_t * timerRef, /* OS supplied timer reference */
  350. ql_task_t taskRef,
  351. void (*callBackRoutine)(void *), /* timer call-back routine */
  352. void *timerArgc /* argument to be passed to call-back on expiration */
  353. );
  354. extern QlOSStatus ql_rtos_timer_start
  355. (
  356. ql_timer_t timerRef, /* OS supplied timer reference */
  357. uint32 set_Time, /* timer set value */
  358. unsigned char cyclicalEn /* wether to enable the cyclical mode or not */
  359. );
  360. extern QlOSStatus ql_rtos_timer_start_relaxed /* for sleep mode, if timer finished but system is sleeping, timer will wakeup system after relax_time */
  361. (
  362. ql_timer_t timerRef, /* OS supplied timer reference */
  363. uint32 set_Time, /* timer set value */
  364. unsigned char cyclicalEn, /* wether to enable the cyclical mode or not */
  365. uint32 relax_time /* the timer wakeup time, if not want timer to wake up system, input QL_WAIT_FOREVER */
  366. );
  367. QlOSStatus ql_rtos_timer_start_us
  368. (
  369. ql_timer_t timerRef, /* OS supplied timer reference */
  370. uint32 set_Time_us /* timer set value */
  371. );
  372. extern QlOSStatus ql_rtos_timer_stop
  373. (
  374. ql_timer_t timerRef /* OS supplied timer reference */
  375. );
  376. extern QlOSStatus ql_rtos_timer_is_running
  377. (
  378. ql_timer_t timerRef /* OS supplied timer reference */
  379. );
  380. extern QlOSStatus ql_rtos_timer_delete
  381. (
  382. ql_timer_t timerRef /* OS supplied timer reference */
  383. );
  384. QlOSStatus ql_rtos_swdog_register
  385. (
  386. ql_swdog_callback callback, /* software watch dog callback */
  387. ql_task_t taskRef /* OS task reference */
  388. );
  389. QlOSStatus ql_rtos_swdog_unregister
  390. (
  391. ql_swdog_callback callback /* software watch dog callback */
  392. );
  393. QlOSStatus ql_rtos_feed_dog(void);
  394. QlOSStatus ql_rtos_sw_dog_enable
  395. (
  396. uint32 period_ms,
  397. uint32 missed_cnt
  398. );
  399. QlOSStatus ql_rtos_sw_dog_disable(void);
  400. QlOSStatus ql_gettimeofday
  401. (
  402. ql_timeval_t *timeval
  403. );
  404. uint32 ql_rtos_get_system_tick(void);
  405. void ql_assert(void);
  406. /**
  407. * \brief monoclinic system time
  408. *
  409. * It is a relative time from system boot. Even after suspend and resume,
  410. * the monoclinic system time will be contiguous.
  411. * littlevgl need this time synchronization information
  412. *
  413. * \return monoclinic system time in milliseconds
  414. */
  415. int64_t ql_rtos_up_time_ms();
  416. /**
  417. * \brief get cpu utilization
  418. *
  419. * Starting from the first call of this function, the system will count
  420. * the CPU utilization rate in the past 1s every 1s
  421. *
  422. *
  423. * Attention: only this function be called once, then system can start count
  424. * the CPU utilization
  425. * \return cpu utilization, 0~100; and <0 means system error
  426. */
  427. int ql_rtos_get_cpu_using(void);
  428. /**
  429. * \brief microsecond delay, range is 1~1000000, and will not release CPU unless interruped by higher priority task
  430. *
  431. */
  432. QlOSStatus ql_delay_us(uint32_t us);
  433. /*****************************************************************
  434. * Function: ql_app_image_from_flash
  435. *
  436. * Description:
  437. * APPIMG jump function loaded by flash, used to jump between multiple independent apps
  438. * 通过flash加载方式的APPIMG跳转函数,用于多个独立APP之间跳转
  439. * Parameters:
  440. * flash_address [in] APPIMG flash partition starting address,4KB aligned. APPIMG flash分区的起始地址,4KB对齐
  441. * flash_size [in] APPIMG flash partition size,4KB aligned. APPIMG flash分区的大小,4KB对齐
  442. * ram_address [in] APPIMG ram starting address. APPIMG ram空间的起始地址
  443. * ram_size [in] APPIMG ram size. APPIMG ram空间的大小
  444. * handler [in] appImageHandler_t APPIMG handle returned after parsing. APPIMG 解析后返回的句柄
  445. * Return:
  446. * FASLE, APPIMG 信息检查或则解析出错
  447. * TRUE , APPIMG 信息校验和解析成功
  448. *
  449. *****************************************************************/
  450. bool ql_app_image_from_flash(const unsigned flash_address,const unsigned flash_size,const unsigned ram_address,const unsigned ram_size, void *handler);
  451. /*****************************************************************
  452. * Function: ql_app_image_from_file
  453. *
  454. * Description:
  455. * APPIMG jump function loaded by file, used to jump between multiple independent apps
  456. * 通过file加载方式的APPIMG跳转函数,用于多个独立APP之间跳转
  457. * Parameters:
  458. * fname [in] The pathname of the appimg file.APPIMG 文件所在的路径名.
  459. * ex:CONFIG_APPIMG_LOAD_FILE_NAME \ CONFIG_APPIMG2_LOAD_FILE_NAME
  460. * ram_address [in] APPIMG ram starting address. APPIMG ram空间的起始地址
  461. * ram_size [in] APPIMG ram size. APPIMG ram空间的大小
  462. * handler [in] appImageHandler_t APPIMG handle returned after parsing. APPIMG 解析后返回的句柄
  463. * Return:
  464. * FASLE, APPIMG 信息检查或则解析出错
  465. * TRUE , APPIMG 信息校验和解析成功
  466. *
  467. *****************************************************************/
  468. bool ql_app_image_from_file(const char *fname,const unsigned ram_address,const unsigned ram_size, void *handler);
  469. #ifdef __cplusplus
  470. } /*"C" */
  471. #endif
  472. #endif