1 // Copyright (C) 2022 Beken Corporation 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 #include <common/bk_include.h> 18 #include <common/bk_typedef.h> 19 #include <common/bk_kernel_err.h> 20 21 #pragma once 22 23 /** 24 * @brief os enums defines 25 * @defgroup os_defines os defines 26 * @ingroup os 27 * @{ 28 */ 29 30 #define RTOS_TAG "os" /**< OS log tag*/ 31 32 #define RTOS_LOGI(...) BK_LOGI(RTOS_TAG, ##__VA_ARGS__) /**< Output OS Info log */ 33 #define RTOS_LOGW(...) BK_LOGW(RTOS_TAG, ##__VA_ARGS__) /**< Output OS Warning log */ 34 #define RTOS_LOGE(...) BK_LOGE(RTOS_TAG, ##__VA_ARGS__) /**< Output OS Error log */ 35 #define RTOS_LOGD(...) BK_LOGD(RTOS_TAG, ##__VA_ARGS__) /**< Output OS Debug log */ 36 37 38 /// os stop interrupt 39 #define GLOBAL_INT_STOP (void)rtos_disable_int 40 41 /// os declaration interrupt status 42 #define GLOBAL_INT_DECLARATION() uint32_t irq_level 43 /// os read interrupt status and disable interrupt 44 #define GLOBAL_INT_DISABLE() irq_level = rtos_disable_int() 45 /// os restore interrupt status 46 #define GLOBAL_INT_RESTORE() rtos_enable_int(irq_level) 47 48 #define RTOS_SUCCESS (1) /**< Return Success */ 49 #define RTOS_FAILURE (0) /**< Return Failure */ 50 51 #define BEKEN_DEFAULT_WORKER_PRIORITY (6) /**< Default Worker Priority */ 52 #define BEKEN_APPLICATION_PRIORITY (7) /**< Application Task Priority */ 53 54 #define NanosecondsPerSecond 1000000000UUL /**< Nanoseconds Per Second */ 55 #define MicrosecondsPerSecond 1000000UL /**< Microseconds Per Second */ 56 #define MillisecondsPerSecond 1000 /**< Milliseconds Per Second */ 57 58 59 #define BEKEN_NEVER_TIMEOUT (0xFFFFFFFF) /**< Never Timeout */ 60 #define BEKEN_WAIT_FOREVER (0xFFFFFFFF) /**< Wait Forever */ 61 #define BEKEN_NO_WAIT (0) /**< No Wait */ 62 63 #define NANOSECONDS 1000000UL /**< Nanoseconds Per Milliseconds */ 64 #define MICROSECONDS 1000 /**< Microseconds Per Milliseconds */ 65 #define MILLISECONDS (1) /**< One Milliseconds */ 66 #define SECONDS (1000) /**< Milliseconds Per Second */ 67 #define MINUTES (60 * SECONDS) /**< Milliseconds Per MINUTES */ 68 #define HOURS (60 * MINUTES) /**< Milliseconds Per HOURS */ 69 #define DAYS (24 * HOURS) /**< Milliseconds Per DAYS */ 70 71 #define BEKEN_MAGIC_WORD (0xBABA7231) /**< Beken Magic word */ 72 73 74 typedef int bk_err_t; /**< Return error code */ 75 76 typedef void *beken_thread_arg_t; /**< Thread argument pointer type */ 77 typedef uint8_t beken_bool_t; /**< Bool type */ 78 typedef uint32_t beken_time_t; /**< Time value in milliseconds */ 79 typedef uint32_t beken_utc_time_t; /**< UTC Time in seconds */ 80 typedef uint64_t beken_utc_time_ms_t; /**< UTC Time in milliseconds */ 81 typedef uint32_t beken_event_flags_t; /**< Event flag type */ 82 typedef void *beken_semaphore_t; /**< OS Semaphore handle pointer */ 83 typedef void *beken_mutex_t; /**< OS Mutex handle pointer */ 84 typedef void *beken_thread_t; /**< OS Thread handle pointer */ 85 typedef void *beken_queue_t; /**< OS Queue handle pointer */ 86 typedef void *beken_event_t; /**< OS Event handle pointer */ 87 88 /// timer callback function type with one parameter 89 typedef void (*timer_handler_t)(void *); 90 /// timer callback function type with two parameters 91 typedef void (*timer_2handler_t)(void *Larg, void *Rarg); 92 /// event callback function type with one parameter 93 typedef bk_err_t(*event_handler_t)(void *arg); 94 95 /// OS Thread entry function type 96 typedef void (*beken_thread_function_t)(beken_thread_arg_t arg); 97 98 /// OS Event wait options 99 typedef enum { 100 WAIT_FOR_ANY_EVENT, /**< Wait for any event */ 101 WAIT_FOR_ALL_EVENTS, /**< Wait for all event */ 102 } beken_event_flags_wait_option_t; 103 104 /** 105 * @} 106 */ 107 108 109 #define os_printf bk_printf /**< OS printf, will be replace with RTOS_LOGI later */ 110 #define os_null_printf bk_null_printf /**< OS drop print string */ 111 #define rtos_init_semaphore_adv rtos_init_semaphore_ex /**< To be replace with rtos_init_semaphore_ex */ 112 #define rtos_get_sema_count rtos_get_semaphore_count /**< To be replace with rtos_get_semaphore_count */ 113 114 /** 115 * @brief os struct defines 116 * @defgroup os_structs structs in os 117 * @ingroup os 118 * @{ 119 */ 120 121 /// OS timer handle struct type 122 typedef struct { 123 void *handle; /**< OS timer handle pointer */ 124 timer_handler_t function; /**< OS timer handle callback function */ 125 void *arg; /**< OS timer handle callback argument */ 126 } beken_timer_t; 127 128 /// OS worker thread handle struct type 129 typedef struct { 130 beken_thread_t thread; /**< OS thread handle */ 131 beken_queue_t event_queue; /**< OS event queue */ 132 } beken_worker_thread_t; 133 134 /// OS timer event struct type 135 typedef struct { 136 event_handler_t function; /**< OS event callback function */ 137 void *arg; /**< OS event callback argument */ 138 beken_timer_t timer; /**< OS timer handle */ 139 beken_worker_thread_t *thread; /**< OS work thread handle */ 140 } beken_timed_event_t; 141 142 /// OS timer handle struct type 143 typedef struct { 144 void *handle; /**< OS timer handle pointer */ 145 timer_2handler_t function; /**< OS timer handle callback function */ 146 void *left_arg; /**< OS timer handle callback first argument */ 147 void *right_arg; /**< OS timer handle callback second argument */ 148 uint32_t beken_magic; /**< OS timer magic word */ 149 } beken2_timer_t; 150 /** 151 * @} 152 */ 153 154 155 156 /** 157 * @brief os API 158 * @defgroup os_apis os API group 159 * @ingroup os 160 * @{ 161 */ 162 163 164 /** @brief Enter a critical session, all interrupts are disabled 165 * 166 * @return none 167 */ 168 void rtos_enter_critical(void); 169 170 /** @brief Exit a critical session, all interrupts are enabled 171 * 172 * @return none 173 */ 174 void rtos_exit_critical(void); 175 176 177 /** @brief Get system time value in milliseconds 178 * 179 * @param time_ptr : the pointer of time value in milliseconds 180 * 181 * @return kNoErr : on success. 182 * @return kGeneralErr : if an error occurred 183 */ 184 bk_err_t beken_time_get_time(beken_time_t *time_ptr); 185 186 187 188 /** @defgroup BEKEN_RTOS_Thread _BK_ RTOS Thread Management Functions 189 * @brief Provide thread creation, delete, suspend, resume, and other RTOS management API 190 * @verbatim 191 * _BK_ thread priority table 192 * 193 * +----------+-----------------+ 194 * | Priority | Thread | 195 * |----------|-----------------| 196 * | 0 | _BK_ | Highest priority 197 * | 1 | Network | 198 * | 2 | | 199 * | 3 | Network worker | 200 * | 4 | | 201 * | 5 | Default Library | 202 * | | Default worker | 203 * | 6 | | 204 * | 7 | Application | 205 * | 8 | | 206 * | 9 | Idle | Lowest priority 207 * +----------+-----------------+ 208 * @endverbatim 209 * @{ 210 */ 211 212 213 /** @brief Creates and starts a new thread 214 * 215 * @param thread : Pointer to variable that will receive the thread handle (can be null) 216 * @param priority : A priority number. 217 * @param name : a text name for the thread (can be null) 218 * @param function : the main thread function 219 * @param stack_size : stack size for this thread 220 * @param arg : argument which will be passed to thread function 221 * 222 * @return kNoErr : on success. 223 * @return kGeneralErr : if an error occurred 224 */ 225 bk_err_t rtos_create_thread(beken_thread_t *thread, uint8_t priority, const char *name, beken_thread_function_t function, uint32_t stack_size, beken_thread_arg_t arg); 226 227 /** @brief Deletes a terminated thread 228 * 229 * @param thread : the handle of the thread to delete, , NULL is the current thread 230 * 231 * @return kNoErr : on success. 232 * @return kGeneralErr : if an error occurred 233 */ 234 bk_err_t rtos_delete_thread(beken_thread_t *thread); 235 236 /** @brief Creates a worker thread 237 * 238 * Creates a worker thread 239 * A worker thread is a thread in whose context timed and asynchronous events 240 * execute. 241 * 242 * @param worker_thread : a pointer to the worker thread to be created 243 * @param priority : thread priority 244 * @param stack_size : thread's stack size in number of bytes 245 * @param event_queue_size : number of events can be pushed into the queue 246 * 247 * @return kNoErr : on success. 248 * @return kGeneralErr : if an error occurred 249 */ 250 bk_err_t rtos_create_worker_thread(beken_worker_thread_t *worker_thread, uint8_t priority, uint32_t stack_size, uint32_t event_queue_size); 251 252 253 /** @brief Deletes a worker thread 254 * 255 * @param worker_thread : a pointer to the worker thread to be created 256 * 257 * @return kNoErr : on success. 258 * @return kGeneralErr : if an error occurred 259 */ 260 bk_err_t rtos_delete_worker_thread(beken_worker_thread_t *worker_thread); 261 262 263 /** @brief Suspend a thread 264 * 265 * @param thread : the handle of the thread to suspend, NULL is the current thread 266 * 267 * @return kNoErr : on success. 268 * @return kGeneralErr : if an error occurred 269 */ 270 void rtos_suspend_thread(beken_thread_t *thread); 271 272 273 274 /** @brief Suspend all other thread 275 * 276 * @param none 277 * 278 * @return none 279 */ 280 void rtos_suspend_all_thread(void); 281 282 283 /** @brief Rresume all other thread 284 * 285 * @param none 286 * 287 * @return none 288 */ 289 long rtos_resume_all_thread(void); 290 291 292 /** @brief Sleeps until another thread has terminated 293 * 294 * @Details Causes the current thread to sleep until the specified other thread 295 * has terminated. If the processor is heavily loaded with higher priority 296 * tasks, this thread may not wake until significantly after the thread termination. 297 * 298 * @param thread : the handle of the other thread which will terminate 299 * 300 * @return kNoErr : on success. 301 * @return kGeneralErr : if an error occurred 302 */ 303 bk_err_t rtos_thread_join(beken_thread_t *thread); 304 305 306 /** @brief Forcibly wakes another thread 307 * 308 * @Details Causes the specified thread to wake from suspension. This will usually 309 * cause an error or timeout in that thread, since the task it was waiting on 310 * is not complete. 311 * 312 * @param thread : the handle of the other thread which will be woken 313 * 314 * @return kNoErr : on success. 315 * @return kGeneralErr : if an error occurred 316 */ 317 bk_err_t rtos_thread_force_awake(beken_thread_t *thread); 318 319 320 /** @brief Checks if a thread is the current thread 321 * 322 * @Details Checks if a specified thread is the currently running thread 323 * 324 * @param thread : the handle of the other thread against which the current thread 325 * will be compared 326 * 327 * @return true : specified thread is the current thread 328 * @return false : specified thread is not currently running 329 */ 330 bool rtos_is_current_thread(beken_thread_t *thread); 331 332 /** @brief Get current thread handler 333 * 334 * @return Current RTOS thread handler 335 */ 336 beken_thread_t *rtos_get_current_thread(void); 337 338 /** @brief Suspend current thread for a specific time 339 * 340 * @param seconds : A time interval (Unit: seconds) 341 * 342 * @return None. 343 */ 344 void rtos_thread_sleep(uint32_t seconds); 345 346 /** @brief Suspend current thread for a specific time 347 * 348 * @param milliseconds : A time interval (Unit: millisecond) 349 * 350 * @return None. 351 */ 352 void rtos_thread_msleep(uint32_t milliseconds); 353 354 /** @brief Suspend current thread for a specific time 355 * 356 * @param num_ms : A time interval (Unit: millisecond) 357 * 358 * @return kNoErr. 359 */ 360 bk_err_t rtos_delay_milliseconds(uint32_t num_ms); 361 362 363 /** @brief Print Thread status into buffer 364 * 365 * @param buffer, point to buffer to store thread status 366 * @param length, length of the buffer 367 * 368 * @return none 369 */ 370 bk_err_t rtos_print_thread_status(char *buffer, int length); 371 372 /** 373 * @} 374 */ 375 376 /** @defgroup BEKEN_RTOS_SEM _BK_ RTOS Semaphore Functions 377 * @brief Provide management APIs for semaphore such as init,set,get and dinit. 378 * @{ 379 */ 380 381 /** @brief Initialises a counting semaphore and set count to 0 382 * 383 * @param semaphore : a pointer to the semaphore handle to be initialised 384 * @param max_count : the max count number of this semaphore 385 * 386 * @return kNoErr : on success. 387 * @return kGeneralErr : if an error occurred 388 */ 389 bk_err_t rtos_init_semaphore(beken_semaphore_t *semaphore, int max_count); 390 391 /** @brief Initialises a counting semaphore and set count to init count 392 * 393 * @param semaphore : a pointer to the semaphore handle to be initialised 394 * @param max_count : the max count number of this semaphore 395 * @param init_count : the init count number of this semaphore 396 * 397 * @return kNoErr : on success. 398 * @return kGeneralErr : if an error occurred 399 */ 400 bk_err_t rtos_init_semaphore_ex(beken_semaphore_t *semaphore, int max_count, int init_count); 401 402 403 /** @brief Set (post/put/increment) a semaphore 404 * 405 * @param semaphore : a pointer to the semaphore handle to be set 406 * 407 * @return kNoErr : on success. 408 * @return kGeneralErr : if an error occurred 409 */ 410 bk_err_t rtos_set_semaphore(beken_semaphore_t *semaphore); 411 412 413 /** @brief Get (wait/decrement) a semaphore 414 * 415 * @Details Attempts to get (wait/decrement) a semaphore. If semaphore is at zero already, 416 * then the calling thread will be suspended until another thread sets the 417 * semaphore with @ref rtos_set_semaphore 418 * 419 * @param semaphore : a pointer to the semaphore handle 420 * @param timeout_ms: the number of milliseconds to wait before returning 421 * 422 * @return kNoErr : on success. 423 * @return kGeneralErr : if an error occurred 424 */ 425 bk_err_t rtos_get_semaphore(beken_semaphore_t *semaphore, uint32_t timeout_ms); 426 427 428 int rtos_get_semaphore_count(beken_semaphore_t *semaphore); 429 430 431 /** @brief De-initialise a semaphore 432 * 433 * @Details Deletes a semaphore created with @ref rtos_init_semaphore 434 * 435 * @param semaphore : a pointer to the semaphore handle 436 * 437 * @return kNoErr : on success. 438 * @return kGeneralErr : if an error occurred 439 */ 440 bk_err_t rtos_deinit_semaphore(beken_semaphore_t *semaphore); 441 /** 442 * @} 443 */ 444 445 /** @defgroup BEKEN_RTOS_MUTEX _BK_ RTOS Mutex Functions 446 * @brief Provide management APIs for Mutex such as init,lock,unlock and dinit. 447 * @{ 448 */ 449 450 /** @brief Initialises a mutex 451 * 452 * @Details A mutex is different to a semaphore in that a thread that already holds 453 * the lock on the mutex can request the lock again (nested) without causing 454 * it to be suspended. 455 * 456 * @param mutex : a pointer to the mutex handle to be initialised 457 * 458 * @return kNoErr : on success. 459 * @return kGeneralErr : if an error occurred 460 */ 461 bk_err_t rtos_init_mutex(beken_mutex_t *mutex); 462 463 /** @brief Obtains the lock on a mutex 464 * 465 * @Details Attempts to obtain the lock on a mutex. If the lock is already held 466 * by another thead, the calling thread will not be suspended until the mutex 467 * lock is released by the other thread. 468 * 469 * @param mutex : a pointer to the mutex handle to be locked 470 * 471 * @return kNoErr : on success. 472 * @return kGeneralErr : if an error occurred 473 */ 474 bk_err_t rtos_trylock_mutex(beken_mutex_t *mutex); 475 476 /** @brief Obtains the lock on a mutex 477 * 478 * @Details Attempts to obtain the lock on a mutex. If the lock is already held 479 * by another thead, the calling thread will be suspended until the mutex 480 * lock is released by the other thread. 481 * 482 * @param mutex : a pointer to the mutex handle to be locked 483 * 484 * @return kNoErr : on success. 485 * @return kGeneralErr : if an error occurred 486 */ 487 bk_err_t rtos_lock_mutex(beken_mutex_t *mutex); 488 489 490 /** @brief Releases the lock on a mutex 491 * 492 * @Details Releases a currently held lock on a mutex. If another thread 493 * is waiting on the mutex lock, then it will be resumed. 494 * 495 * @param mutex : a pointer to the mutex handle to be unlocked 496 * 497 * @return kNoErr : on success. 498 * @return kGeneralErr : if an error occurred 499 */ 500 bk_err_t rtos_unlock_mutex(beken_mutex_t *mutex); 501 502 503 /** @brief De-initialise a mutex 504 * 505 * @Details Deletes a mutex created with @ref rtos_init_mutex 506 * 507 * @param mutex : a pointer to the mutex handle 508 * 509 * @return kNoErr : on success. 510 * @return kGeneralErr : if an error occurred 511 */ 512 bk_err_t rtos_deinit_mutex(beken_mutex_t *mutex); 513 /** 514 * @} 515 */ 516 517 /** @defgroup BEKEN_RTOS_QUEUE _BK_ RTOS FIFO Queue Functions 518 * @brief Provide management APIs for FIFO such as init,push,pop and dinit. 519 * @{ 520 */ 521 522 /** @brief Initialises a FIFO queue 523 * 524 * @param queue : a pointer to the queue handle to be initialised 525 * @param name : a text string name for the queue (NULL is allowed) 526 * @param message_size : size in bytes of objects that will be held in the queue 527 * @param number_of_messages : depth of the queue - i.e. max number of objects in the queue 528 * 529 * @return kNoErr : on success. 530 * @return kGeneralErr : if an error occurred 531 */ 532 bk_err_t rtos_init_queue(beken_queue_t *queue, const char *name, uint32_t message_size, uint32_t number_of_messages); 533 534 535 /** @brief Pushes an object onto a queue 536 * 537 * @param queue : a pointer to the queue handle 538 * @param message : the object to be added to the queue. Size is assumed to be 539 * the size specified in @ref rtos_init_queue 540 * @param timeout_ms: the number of milliseconds to wait before returning 541 * 542 * @return kNoErr : on success. 543 * @return kGeneralErr : if an error or timeout occurred 544 */ 545 bk_err_t rtos_push_to_queue(beken_queue_t *queue, void *message, uint32_t timeout_ms); 546 547 /** @brief Pushes an object to front of the queue 548 * 549 * @param queue : a pointer to the queue handle 550 * @param message : the object to be added to the queue. Size is assumed to be 551 * the size specified in @ref rtos_init_queue 552 * @param timeout_ms: the number of milliseconds to wait before returning 553 * 554 * @return kNoErr : on success. 555 * @return kGeneralErr : if an error or timeout occurred 556 */ 557 bk_err_t rtos_push_to_queue_front(beken_queue_t *queue, void *message, uint32_t timeout_ms); 558 559 560 /** @brief Pops an object off a queue 561 * 562 * @param queue : a pointer to the queue handle 563 * @param message : pointer to a buffer that will receive the object being 564 * popped off the queue. Size is assumed to be 565 * the size specified in @ref rtos_init_queue , hence 566 * you must ensure the buffer is long enough or memory 567 * corruption will result 568 * @param timeout_ms: the number of milliseconds to wait before returning 569 * 570 * @return kNoErr : on success. 571 * @return kGeneralErr : if an error or timeout occurred 572 */ 573 bk_err_t rtos_pop_from_queue(beken_queue_t *queue, void *message, uint32_t timeout_ms); 574 575 576 /** @brief De-initialise a queue created with @ref rtos_init_queue 577 * 578 * @param queue : a pointer to the queue handle 579 * 580 * @return kNoErr : on success. 581 * @return kGeneralErr : if an error occurred 582 */ 583 bk_err_t rtos_deinit_queue(beken_queue_t *queue); 584 585 586 /** @brief Check if a queue is empty 587 * 588 * @param queue : a pointer to the queue handle 589 * 590 * @return true : queue is empty. 591 * @return false : queue is not empty. 592 */ 593 bool rtos_is_queue_empty(beken_queue_t *queue); 594 595 596 /** @brief Check if a queue is full 597 * 598 * @param queue : a pointer to the queue handle 599 * 600 * @return true : queue is empty. 601 * @return false : queue is not empty. 602 */ 603 bool rtos_is_queue_full(beken_queue_t *queue); 604 605 606 607 608 /** @defgroup BEKEN_RTOS_EVENT _BK_ RTOS Event Functions 609 * @{ 610 */ 611 612 /** 613 * @brief Sends an asynchronous event to the associated worker thread 614 * 615 * @param worker_thread :the worker thread in which context the callback should execute from 616 * @param function : the callback function to be called from the worker thread 617 * @param arg : the argument to be passed to the callback function 618 * 619 * @return kNoErr : on success. 620 * @return kGeneralErr : if an error occurred 621 */ 622 bk_err_t rtos_send_asynchronous_event(beken_worker_thread_t *worker_thread, event_handler_t function, void *arg); 623 624 /** Requests a function be called at a regular interval 625 * 626 * This function registers a function that will be called at a regular 627 * interval. Since this is based on the RTOS time-slice scheduling, the 628 * accuracy is not high, and is affected by processor load. 629 * 630 * @param event_object : pointer to a event handle which will be initialised 631 * @param worker_thread : pointer to the worker thread in whose context the 632 * callback function runs on 633 * @param function : the callback function that is to be called regularly 634 * @param time_ms : the time period between function calls in milliseconds 635 * @param arg : an argument that will be supplied to the function when 636 * it is called 637 * 638 * @return kNoErr : on success. 639 * @return kGeneralErr : if an error occurred 640 */ 641 bk_err_t rtos_register_timed_event(beken_timed_event_t *event_object, beken_worker_thread_t *worker_thread, event_handler_t function, uint32_t time_ms, void *arg); 642 643 644 /** Removes a request for a regular function execution 645 * 646 * This function de-registers a function that has previously been set-up 647 * with @ref rtos_register_timed_event. 648 * 649 * @param event_object : the event handle used with @ref rtos_register_timed_event 650 * 651 * @return kNoErr : on success. 652 * @return kGeneralErr : if an error occurred 653 */ 654 bk_err_t rtos_deregister_timed_event(beken_timed_event_t *event_object); 655 656 657 658 /** @defgroup BEKEN_RTOS_TIMER _BK_ RTOS Timer Functions 659 * @brief Provide management APIs for timer such as init,start,stop,reload and dinit. 660 * @{ 661 */ 662 663 /** 664 * @brief Gets time in miiliseconds since RTOS start 665 * 666 * @note: Since this is only 32 bits, it will roll over every 49 days, 17 hours. 667 * 668 * @returns Time in milliseconds since RTOS started. 669 */ 670 uint32_t rtos_get_time(void); 671 672 /** @defgroup BEKEN_RTOS_TIMER _BK_ RTOS Timer Functions 673 * @brief Provide management APIs for timer such as init,start,stop,reload and dinit. 674 * @{ 675 */ 676 677 /** 678 * @brief Gets time in miiliseconds since RTOS start 679 * 680 * @note: Since this is only 32 bits, it will roll over every 49 days, 17 hours. 681 * 682 * @returns Time in milliseconds since RTOS started. 683 */ 684 uint32_t beken_ms_per_tick(void); 685 686 /** 687 * @brief Initialize a RTOS timer 688 * 689 * @note Timer does not start running until @ref beken_start_timer is called 690 * 691 * @param timer : a pointer to the timer handle to be initialised 692 * @param time_ms : Timer period in milliseconds 693 * @param function : the callback handler function that is called each time the 694 * timer expires 695 * @param arg : an argument that will be passed to the callback function 696 * 697 * @return kNoErr : on success. 698 * @return kGeneralErr : if an error occurred 699 */ 700 bk_err_t rtos_init_timer(beken_timer_t *timer, uint32_t time_ms, timer_handler_t function, void *arg); 701 bk_err_t rtos_init_oneshot_timer(beken2_timer_t *timer, 702 uint32_t time_ms, 703 timer_2handler_t function, 704 void *larg, 705 void *rarg); 706 bk_err_t rtos_deinit_oneshot_timer(beken2_timer_t *timer); 707 bk_err_t rtos_stop_oneshot_timer(beken2_timer_t *timer); 708 bool rtos_is_oneshot_timer_running(beken2_timer_t *timer); 709 bk_err_t rtos_start_oneshot_timer(beken2_timer_t *timer); 710 bool rtos_is_oneshot_timer_init(beken2_timer_t *timer); 711 bk_err_t rtos_oneshot_reload_timer(beken2_timer_t *timer); 712 bk_err_t rtos_change_period(beken_timer_t *timer, uint32_t time_ms); 713 bk_err_t rtos_oneshot_reload_timer_ex(beken2_timer_t *timer, 714 uint32_t time_ms, 715 timer_2handler_t function, 716 void *larg, 717 void *rarg); 718 719 /** @brief Starts a RTOS timer running 720 * 721 * @note Timer must have been previously initialised with @ref rtos_init_timer 722 * 723 * @param timer : a pointer to the timer handle to start 724 * 725 * @return kNoErr : on success. 726 * @return kGeneralErr : if an error occurred 727 */ 728 bk_err_t rtos_start_timer(beken_timer_t *timer); 729 730 731 /** @brief Stops a running RTOS timer 732 * 733 * @note Timer must have been previously started with @ref rtos_init_timer 734 * 735 * @param timer : a pointer to the timer handle to stop 736 * 737 * @return kNoErr : on success. 738 * @return kGeneralErr : if an error occurred 739 */ 740 bk_err_t rtos_stop_timer(beken_timer_t *timer); 741 742 743 /** @brief Reloads a RTOS timer that has expired 744 * 745 * @note This is usually called in the timer callback handler, to 746 * reschedule the timer for the next period. 747 * 748 * @param timer : a pointer to the timer handle to reload 749 * 750 * @return kNoErr : on success. 751 * @return kGeneralErr : if an error occurred 752 */ 753 bk_err_t rtos_reload_timer(beken_timer_t *timer); 754 755 756 /** @brief De-initialise a RTOS timer 757 * 758 * @note Deletes a RTOS timer created with @ref rtos_init_timer 759 * 760 * @param timer : a pointer to the RTOS timer handle 761 * 762 * @return kNoErr : on success. 763 * @return kGeneralErr : if an error occurred 764 */ 765 bk_err_t rtos_deinit_timer(beken_timer_t *timer); 766 767 /** @brief Check if an RTOS timer is init 768 * 769 * @param timer : a pointer to the RTOS timer handle 770 * 771 * @return true : if init. 772 * @return false : if not init 773 */ 774 bool rtos_is_timer_init(beken_timer_t *timer); 775 776 /** @brief Check if an RTOS timer is running 777 * 778 * @param timer : a pointer to the RTOS timer handle 779 * 780 * @return true : if running. 781 * @return false : if not running 782 */ 783 bool rtos_is_timer_running(beken_timer_t *timer); 784 785 uint32_t rtos_get_timer_expiry_time(beken_timer_t *timer); 786 uint32_t rtos_get_next_expire_time(); 787 uint32_t rtos_get_current_timer_count(void); 788 789 790 void rtos_start_scheduler(void); 791 bool rtos_is_scheduler_started(void); 792 793 uint32_t rtos_get_cpsr(void); 794 795 char* rtos_get_name(void); 796 char* rtos_get_version(void); 797 798 size_t rtos_get_free_heap_size(void); 799 uint32_t rtos_get_tick_count(void); 800 801 uint32_t rtos_disable_int(void); 802 void rtos_enable_int(uint32_t int_level); 803 bool rtos_is_in_interrupt_context(void); 804 void rtos_wait_for_interrupt(void); 805 806 void rtos_shutdown(void); 807 /** @brief save the state of the MIE and then disable the plic,plmt,plic_sw in mie 808 * 809 * @param void 810 * 811 * @return the state of the MIE 812 * 813 */ 814 uint32_t rtos_disable_mie_int(void); 815 /** @brief enable the MIE using the saving the state of the mie 816 * 817 * @param int_level : the saving the state of the mie 818 * 819 * @return void 820 * 821 */ 822 void rtos_enable_mie_int(uint32_t int_level); 823 /** 824 * @} 825 */ 826 827 828 829