kernel.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #ifndef AOS_KERNEL_H
  5. #define AOS_KERNEL_H
  6. #include <stdlib.h>
  7. #include <stddef.h>
  8. #include <sys/types.h>
  9. #include <stdint.h>
  10. #include <assert.h>
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #define AOS_WAIT_FOREVER 0xffffffffu
  15. #define AOS_NO_WAIT 0x0
  16. #ifndef AOS_DEFAULT_APP_PRI
  17. #define AOS_DEFAULT_APP_PRI 32
  18. #endif
  19. #ifndef AOS_MAX_APP_PRI
  20. #define AOS_MAX_APP_PRI 60
  21. #endif
  22. #define AOS_EVENT_AND 0x02u
  23. #define AOS_EVENT_AND_CLEAR 0x03u
  24. #define AOS_EVENT_OR 0x00u
  25. #define AOS_EVENT_OR_CLEAR 0x01u
  26. /// Status code values returned by CSI-kernel functions. 0 - success, negative represents error code ,see errno.h
  27. typedef int32_t k_status_t;
  28. /// Entry point of a timer call back function.
  29. typedef void (*aos_timer_cb_t)(void *, void *);
  30. typedef struct {
  31. void *hdl;
  32. } aos_hdl_t;
  33. typedef aos_hdl_t aos_task_t;
  34. typedef aos_hdl_t aos_mutex_t;
  35. typedef aos_hdl_t aos_sem_t;
  36. typedef aos_hdl_t aos_queue_t;
  37. typedef aos_hdl_t aos_timer_t;
  38. typedef aos_hdl_t aos_work_t;
  39. typedef aos_hdl_t aos_event_t;
  40. typedef struct {
  41. void *hdl;
  42. void *stk;
  43. } aos_workqueue_t;
  44. typedef unsigned int aos_task_key_t;
  45. #define MS2TICK(ms) krhino_ms_to_ticks(ms)
  46. int32_t aos_irq_context(void);
  47. // #define aos_assert(x) assert(x)
  48. /**
  49. * Reboot AliOS.
  50. */
  51. void aos_reboot(void);
  52. /**
  53. * Get HZ(ticks per second).
  54. *
  55. * @return RHINO_CONFIG_TICKS_PER_SECOND.
  56. */
  57. int aos_get_hz(void);
  58. /**
  59. * Get kernel version.
  60. *
  61. * @return SYSINFO_KERNEL_VERSION.
  62. */
  63. const char *aos_version_get(void);
  64. /**
  65. * Create a task.
  66. *
  67. * @param[in] name task name.
  68. * @param[in] fn function to run.
  69. * @param[in] arg argument of the function.
  70. * @param[in] stacksize stack-size in bytes.
  71. *
  72. * @return 0: success.
  73. */
  74. int aos_task_new(const char *name, void (*fn)(void *), void *arg, int stack_size);
  75. /**
  76. * Create a task.
  77. *
  78. * @param[in] task handle.
  79. * @param[in] name task name.
  80. * @param[in] fn task function.
  81. * @param[in] arg argument of the function..
  82. * @param[in] stack_buf stack-buf: if stack_buf==NULL, provided by kernel.
  83. * @param[in] stack_size stack-size in bytes.
  84. * @param[in] prio priority value, the max is RHINO_CONFIG_USER_PRI_MAX(default 60).
  85. *
  86. * @return 0: success.
  87. */
  88. int aos_task_new_ext(aos_task_t *task, const char *name, void (*fn)(void *), void *arg,
  89. int stack_size, int prio);
  90. /**
  91. * show all tasks info.
  92. *
  93. */
  94. void aos_task_show_info(void);
  95. void aos_task_yield();
  96. /**
  97. * Exit a task.
  98. *
  99. * @param[in] code not used now.
  100. */
  101. void aos_task_exit(int code);
  102. void aos_task_wdt_attach(void (*will)(void *), void *args);
  103. void aos_task_wdt_detach();
  104. void aos_task_wdt_feed(int time);
  105. aos_task_t aos_task_self();
  106. /**
  107. * Get task name.
  108. *
  109. * @return the name of the task
  110. */
  111. const char *aos_task_name(void);
  112. const char *aos_task_get_name(aos_task_t *task);
  113. /**
  114. * Create a task key.
  115. *
  116. * @param[in] key pointer of key object.
  117. *
  118. * @return 0: success, -EINVAL: error.
  119. */
  120. int aos_task_key_create(aos_task_key_t *key);
  121. /**
  122. * Delete a task key.
  123. *
  124. * @param[in] key key object.
  125. */
  126. void aos_task_key_delete(aos_task_key_t key);
  127. /**
  128. * Associate a task-specific value with a key.
  129. * Support this functiong need set RHINO_CONFIG_TASK_INFO 1 at k_config.h
  130. *
  131. * @param[in] key key object.
  132. * @param[in] vp pointer of a task-specific value.
  133. *
  134. * @return the check status, 0 is OK, -1 indicates invalid.
  135. */
  136. int aos_task_setspecific(aos_task_key_t key, void *vp);
  137. /**
  138. * Get the value currently bound to the specified key.
  139. * Support this functiong need set RHINO_CONFIG_TASK_INFO 1 at k_config.h
  140. *
  141. * @param[in] key key object.
  142. */
  143. void *aos_task_getspecific(aos_task_key_t key);
  144. /**
  145. * Alloc a mutex.
  146. *
  147. * @param[in] mutex pointer of mutex object, mutex object must be alloced,
  148. * hdl pointer in aos_mutex_t will refer a kernel obj internally.
  149. *
  150. * @return 0: success.
  151. */
  152. int aos_mutex_new(aos_mutex_t *mutex);
  153. /**
  154. * Free a mutex.
  155. *
  156. * @param[in] mutex mutex object, mem refered by hdl pointer in aos_mutex_t will
  157. * be freed internally.
  158. */
  159. void aos_mutex_free(aos_mutex_t *mutex);
  160. /**
  161. * Lock a mutex.
  162. *
  163. * @param[in] mutex mutex object, it contains kernel obj pointer which aos_mutex_new alloced.
  164. * @param[in] timeout waiting until timeout in milliseconds.
  165. *
  166. * @return 0: success.
  167. */
  168. int aos_mutex_lock(aos_mutex_t *mutex, unsigned int timeout);
  169. /**
  170. * Unlock a mutex.
  171. *
  172. * @param[in] mutex mutex object, it contains kernel obj pointer which oc_mutex_new alloced.
  173. *
  174. * @return 0: success.
  175. */
  176. int aos_mutex_unlock(aos_mutex_t *mutex);
  177. /**
  178. * This function will check if mutex is valid.
  179. *
  180. * @param[in] mutex pointer to the mutex.
  181. *
  182. * @return 0: invalid, 1: valid.
  183. */
  184. int aos_mutex_is_valid(aos_mutex_t *mutex);
  185. /**
  186. * This function will check if mutex is locked.
  187. *
  188. * @param[in] mutex pointer to the mutex.
  189. *
  190. * @return 0: no lock, 1: locked.
  191. */
  192. int aos_mutex_is_locked(aos_mutex_t *mutex);
  193. /**
  194. * Alloc a semaphore.
  195. *
  196. * @param[out] sem pointer of semaphore object, semaphore object must be alloced,
  197. * hdl pointer in aos_sem_t will refer a kernel obj internally.
  198. * @param[in] count initial semaphore counter.
  199. *
  200. * @return 0:success.
  201. */
  202. int aos_sem_new(aos_sem_t *sem, int count);
  203. /**
  204. * Destroy a semaphore.
  205. *
  206. * @param[in] sem pointer of semaphore object, mem refered by hdl pointer
  207. * in aos_sem_t will be freed internally.
  208. */
  209. void aos_sem_free(aos_sem_t *sem);
  210. /**
  211. * Acquire a semaphore.
  212. *
  213. * @param[in] sem semaphore object, it contains kernel obj pointer which aos_sem_new alloced.
  214. * @param[in] timeout waiting until timeout in milliseconds.
  215. *
  216. * @return 0: success.
  217. */
  218. int aos_sem_wait(aos_sem_t *sem, unsigned int timeout);
  219. /**
  220. * Release a semaphore.
  221. *
  222. * @param[in] sem semaphore object, it contains kernel obj pointer which aos_sem_new alloced.
  223. */
  224. void aos_sem_signal(aos_sem_t *sem);
  225. /**
  226. * This function will check if semaphore is valid.
  227. *
  228. * @param[in] sem pointer to the semaphore.
  229. *
  230. * @return 0: invalid, 1: valid.
  231. */
  232. int aos_sem_is_valid(aos_sem_t *sem);
  233. /**
  234. * Release all semaphore.
  235. *
  236. * @param[in] sem semaphore object, it contains kernel obj pointer which aos_sem_new alloced.
  237. */
  238. void aos_sem_signal_all(aos_sem_t *sem);
  239. /**
  240. * This function will create an event with an initialization flag set.
  241. * This function should not be called from interrupt context.
  242. *
  243. * @param[in] event event object pointer.
  244. * @param[in] flags initialization flag set(provided by caller).
  245. *
  246. * @return 0: success.
  247. */
  248. int aos_event_new(aos_event_t *event, unsigned int flags);
  249. /**
  250. * This function will free an event.
  251. * This function shoud not be called from interrupt context.
  252. *
  253. * @param[in] event memory refered by hdl pointer in event will be freed.
  254. *
  255. * @return N/A.
  256. */
  257. void aos_event_free(aos_event_t *event);
  258. /**
  259. * This function will try to get flag set from given event, if the request flag
  260. * set is satisfied, it will return immediately, if the request flag set is not
  261. * satisfied with timeout(RHINO_WAIT_FOREVER,0xFFFFFFFF), the caller task will be
  262. * pended on event until the flag is satisfied, if the request flag is not
  263. * satisfied with timeout(RHINO_NO_WAIT, 0x0), it will also return immediately.
  264. * Note, this function should not be called from interrupt context because it has
  265. * possible to lead context switch and an interrupt has no TCB to save context.
  266. *
  267. * @param[in] event event object pointer.
  268. * @param[in] flags request flag set.
  269. * @param[in] opt operation type, such as AND,OR,AND_CLEAR,OR_CLEAR.
  270. * @param[out] actl_flags the internal flags value hold by event.
  271. * @param[in] flags request flag set.
  272. * @param[in] timeout max wait time in millisecond.
  273. *
  274. * @return 0: success.
  275. */
  276. int aos_event_get(aos_event_t *event, unsigned int flags, unsigned char opt,
  277. unsigned int *actl_flags, unsigned int timeout);
  278. /**
  279. * This function will set flag set to given event, and it will check if any task
  280. * which is pending on the event should be waken up.
  281. *
  282. * @param[in] event event object pointer.
  283. * @param[in] flags flag set to be set into event.
  284. * @param[in] opt operation type, such as AND,OR.
  285. *
  286. * @return 0: success.
  287. */
  288. int aos_event_set(aos_event_t *event, unsigned int flags, unsigned char opt);
  289. /**
  290. * This function will check if event is valid.
  291. *
  292. * @param[in] sem pointer to the semaphore.
  293. *
  294. * @return 0: invalid, 1: valid.
  295. */
  296. int aos_event_is_valid(aos_event_t *event);
  297. /**
  298. * This function will create a queue.
  299. *
  300. * @param[in] queue pointer to the queue(the space is provided by user).
  301. * @param[in] buf buf of the queue(provided by user).
  302. * @param[in] size the bytes of the buf.
  303. * @param[in] max_msg the max size of the msg.
  304. *
  305. * @return 0: success.
  306. */
  307. int aos_queue_new(aos_queue_t *queue, void *buf, unsigned int size, int max_msg);
  308. /**
  309. * This function will delete a queue.
  310. *
  311. * @param[in] queue pointer to the queue.
  312. */
  313. void aos_queue_free(aos_queue_t *queue);
  314. /**
  315. * This function will send a msg to the front of a queue.
  316. *
  317. * @param[in] queue pointer to the queue.
  318. * @param[in] msg msg to send.
  319. * @param[in] size size of the msg.
  320. *
  321. * @return 0: success.
  322. */
  323. int aos_queue_send(aos_queue_t *queue, void *msg, unsigned int size);
  324. /**
  325. * This function will receive msg from a queue.
  326. *
  327. * @param[in] queue pointer to the queue.
  328. * @param[in] ms ms to wait before receive.
  329. * @param[out] msg buf to save msg.
  330. * @param[out] size size of the msg.
  331. *
  332. * @return 0: success.
  333. */
  334. int aos_queue_recv(aos_queue_t *queue, unsigned int ms, void *msg, unsigned int *size);
  335. /**
  336. * This function will check if queue is valid.
  337. *
  338. * @param[in] queue pointer to the queue.
  339. *
  340. * @return 0: invalid, 1: valid.
  341. */
  342. int aos_queue_is_valid(aos_queue_t *queue);
  343. /**
  344. * This function will return buf ptr if queue is inited.
  345. *
  346. * @param[in] queue pointer to the queue.
  347. *
  348. * @return NULL: error.
  349. */
  350. void *aos_queue_buf_ptr(aos_queue_t *queue);
  351. /**
  352. * Get number of queued messages in a message queue.
  353. *
  354. * @param[in] queue message queue handle to operate.
  355. *
  356. * @return number of queued messages.negative indicates error code.
  357. */
  358. int aos_queue_get_count(aos_queue_t *queue);
  359. /**
  360. * This function will create a timer and run auto.
  361. *
  362. * @param[in] timer pointer to the timer.
  363. * @param[in] fn callbak of the timer.
  364. * @param[in] arg the argument of the callback.
  365. * @param[in] ms ms of the normal timer triger.
  366. * @param[in] repeat repeat or not when the timer is created.
  367. *
  368. * @return 0: success.
  369. */
  370. int aos_timer_new(aos_timer_t *timer, void (*fn)(void *, void *),
  371. void *arg, int ms, int repeat);
  372. /**
  373. * This function will create a timer.
  374. *
  375. * @param[in] timer pointer to the timer.
  376. * @param[in] fn callbak of the timer.
  377. * @param[in] arg the argument of the callback.
  378. * @param[in] ms ms of the normal timer triger.
  379. * @param[in] repeat repeat or not when the timer is created.
  380. * @param[in] auto_run run auto or not when the timer is created.
  381. *
  382. * @return 0: success.
  383. */
  384. int aos_timer_new_ext(aos_timer_t *timer, void (*fn)(void *, void *),
  385. void *arg, int ms, int repeat, unsigned char auto_run);
  386. /**
  387. * This function will delete a timer.
  388. *
  389. * @param[in] timer pointer to a timer.
  390. */
  391. void aos_timer_free(aos_timer_t *timer);
  392. /**
  393. * This function will start a timer.
  394. *
  395. * @param[in] timer pointer to the timer.
  396. *
  397. * @return 0: success.
  398. */
  399. int aos_timer_start(aos_timer_t *timer);
  400. /**
  401. * This function will stop a timer.
  402. *
  403. * @param[in] timer pointer to the timer.
  404. *
  405. * @return 0: success.
  406. */
  407. int aos_timer_stop(aos_timer_t *timer);
  408. /**
  409. * This function will change attributes of a timer.
  410. *
  411. * @param[in] timer pointer to the timer.
  412. * @param[in] ms ms of the timer triger.
  413. *
  414. * @return 0: success.
  415. */
  416. int aos_timer_change(aos_timer_t *timer, int ms);
  417. /**
  418. * This function will change attributes of a timer.
  419. *
  420. * @param[in] timer pointer to the timer.
  421. * @param[in] ms ms of the timer triger.
  422. *
  423. * @return 0: success.
  424. */
  425. int aos_timer_change_once(aos_timer_t *timer, int ms);
  426. /**
  427. * This function will check if timer is valid.
  428. *
  429. * @param[in] timer pointer to the timer.
  430. *
  431. * @return 0: success.
  432. */
  433. int aos_timer_is_valid(aos_timer_t *timer);
  434. /**
  435. * This function will creat a workqueue.
  436. *
  437. * @param[in] workqueue the workqueue to be created.
  438. * @param[in] pri the priority of the worker.
  439. * @param[in] stack_size the size of the worker-stack.
  440. *
  441. * @return 0: success.
  442. */
  443. int aos_workqueue_create(aos_workqueue_t *workqueue, int pri, int stack_size);
  444. /**
  445. * This function will delete a workqueue.
  446. *
  447. * @param[in] workqueue the workqueue to be deleted.
  448. */
  449. void aos_workqueue_del(aos_workqueue_t *workqueue);
  450. /**
  451. * This function will initialize a work.
  452. *
  453. * @param[in] work the work to be initialized.
  454. * @param[in] fn the call back function to run.
  455. * @param[in] arg the paraments of the function.
  456. * @param[in] dly ms to delay before run.
  457. *
  458. * @return 0: success.
  459. */
  460. int aos_work_init(aos_work_t *work, void (*fn)(void *), void *arg, int dly);
  461. /**
  462. * This function will destroy a work.
  463. *
  464. * @param[in] work the work to be destroied.
  465. */
  466. void aos_work_destroy(aos_work_t *work);
  467. /**
  468. * This function will run a work on a workqueue.
  469. *
  470. * @param[in] workqueue the workqueue to run work.
  471. * @param[in] work the work to run.
  472. *
  473. * @return 0: success.
  474. */
  475. int aos_work_run(aos_workqueue_t *workqueue, aos_work_t *work);
  476. /**
  477. * This function will run a work on the default workqueue.
  478. *
  479. * @param[in] work the work to run.
  480. *
  481. * @return 0: success.
  482. */
  483. int aos_work_sched(aos_work_t *work);
  484. /**
  485. * This function will cancel a work on the default workqueue.
  486. *
  487. * @param[in] work the work to cancel.
  488. *
  489. * @return 0: success.
  490. */
  491. int aos_work_cancel(aos_work_t *work);
  492. /**
  493. * Realloc memory.
  494. *
  495. * @param[in] mem current memory address point.
  496. * @param[in] size new size of the mem to remalloc.
  497. *
  498. * @return NULL: error.
  499. */
  500. void *aos_realloc(void *mem, unsigned int size);
  501. void *aos_realloc_check(void *mem, unsigned int size);
  502. /**
  503. * Alloc memory.
  504. *
  505. * @param[in] size size of the mem to malloc.
  506. *
  507. * @return NULL: error.
  508. */
  509. void *aos_malloc(unsigned int size);
  510. void *aos_malloc_check(unsigned int size);
  511. /**
  512. * Alloc memory and clear to zero.
  513. *
  514. * @param[in] size size of the mem to malloc.
  515. *
  516. * @return NULL: error.
  517. */
  518. void *aos_zalloc(unsigned int size);
  519. void *aos_zalloc_check(unsigned int size);
  520. void *aos_calloc(unsigned int size, int num);
  521. void *aos_calloc_check(unsigned int size, int num);
  522. /**
  523. * Trace alloced mems.
  524. *
  525. * @param[in] addr pointer of the mem alloced by malloc.
  526. * @param[in] allocator buildin_address.
  527. */
  528. void aos_alloc_trace(void *addr, size_t allocator);
  529. /**
  530. * Free memory.
  531. *
  532. * @param[in] ptr address point of the mem.
  533. */
  534. void aos_free(void *mem);
  535. /**
  536. * Free memory and set NULL.
  537. *
  538. * @param[in] *ptr address point of the mem.
  539. */
  540. void aos_freep(char **ptr);
  541. /**
  542. * Get current time in nano seconds.
  543. *
  544. * @return elapsed time in nano seconds from system starting.
  545. */
  546. long long aos_now(void);
  547. /**
  548. * Get current time in mini seconds.
  549. *
  550. * @return elapsed time in mini seconds from system starting.
  551. */
  552. long long aos_now_ms(void);
  553. /**
  554. * Msleep.
  555. *
  556. * @param[in] ms sleep time in milliseconds.
  557. */
  558. void aos_msleep(int ms);
  559. /**
  560. * Initialize system
  561. */
  562. void aos_init(void);
  563. /**
  564. * Start system
  565. */
  566. void aos_start(void);
  567. /// System enter interrupt status.
  568. /// \return execution status code. \ref k_status_t
  569. k_status_t aos_kernel_intrpt_enter(void);
  570. /// System exit interrupt status.
  571. /// \return execution status code. \ref k_status_t
  572. k_status_t aos_kernel_intrpt_exit(void);
  573. /**
  574. * Get aos memory used info.
  575. *
  576. * @param[out] total the total memory can be use.
  577. * @param[out] used the used memory by malloc.
  578. * @param[out] mfree the free memory can be use.
  579. * @param[out] peak the peak memory used.
  580. *
  581. * @return execution status code.
  582. */
  583. int aos_get_mminfo(int32_t *total, int32_t *used, int32_t *mfree, int32_t *peak);
  584. /**
  585. * Dump aos memory .
  586. *
  587. * @return execution status code.
  588. */
  589. int aos_mm_dump(void);
  590. /// Suspend the scheduler.
  591. void aos_kernel_sched_suspend(void);
  592. /// Resume the scheduler.
  593. void aos_kernel_sched_resume(void);
  594. uint64_t aos_kernel_tick2ms(uint32_t ticks);
  595. uint64_t aos_kernel_ms2tick(uint32_t ms);
  596. int32_t aos_kernel_suspend(void);
  597. void aos_kernel_resume(int32_t ticks);
  598. typedef void (*except_process_t)(int errno, const char *file, int line, const char *func_name, void *caller);
  599. void aos_set_except_callback(except_process_t except);
  600. void aos_set_except_default();
  601. #ifdef __cplusplus
  602. }
  603. #endif
  604. #endif /* AOS_KERNEL_H */