leo hendrickson
/
S
simple-mbed-cloud-client/mbed-cloud-client/sal-stack-nanostack-eventloop/source/minar_hal_timer.cpp@0:25fa8795676b, 2021-04-18 (annotated)
- Committer:
- leothedragon
- Date:
- Sun Apr 18 15:20:23 2021 +0000
- Revision:
- 0:25fa8795676b
DS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:25fa8795676b | 1 | /* |
leothedragon | 0:25fa8795676b | 2 | * Copyright (c) 2016 ARM Limited, All Rights Reserved |
leothedragon | 0:25fa8795676b | 3 | */ |
leothedragon | 0:25fa8795676b | 4 | |
leothedragon | 0:25fa8795676b | 5 | // Include before mbed.h to properly get UINT*_C() |
leothedragon | 0:25fa8795676b | 6 | |
leothedragon | 0:25fa8795676b | 7 | #include "ns_types.h" |
leothedragon | 0:25fa8795676b | 8 | |
leothedragon | 0:25fa8795676b | 9 | #include "platform/arm_hal_timer.h" |
leothedragon | 0:25fa8795676b | 10 | #include "platform/arm_hal_interrupt.h" |
leothedragon | 0:25fa8795676b | 11 | |
leothedragon | 0:25fa8795676b | 12 | #if defined(NS_EVENTLOOP_USE_TICK_TIMER) && defined(YOTTA_CFG_MINAR) |
leothedragon | 0:25fa8795676b | 13 | |
leothedragon | 0:25fa8795676b | 14 | #include "minar/minar.h" |
leothedragon | 0:25fa8795676b | 15 | #include "mbed-drivers/mbed.h" |
leothedragon | 0:25fa8795676b | 16 | #include "core-util/FunctionPointer.h" |
leothedragon | 0:25fa8795676b | 17 | #include "core-util/Event.h" |
leothedragon | 0:25fa8795676b | 18 | |
leothedragon | 0:25fa8795676b | 19 | #define TICK_TIMER_ID 1 |
leothedragon | 0:25fa8795676b | 20 | |
leothedragon | 0:25fa8795676b | 21 | using minar::Scheduler; |
leothedragon | 0:25fa8795676b | 22 | using minar::milliseconds; |
leothedragon | 0:25fa8795676b | 23 | using minar::callback_handle_t; |
leothedragon | 0:25fa8795676b | 24 | using namespace mbed::util; |
leothedragon | 0:25fa8795676b | 25 | |
leothedragon | 0:25fa8795676b | 26 | static callback_handle_t sys_timer_handle; |
leothedragon | 0:25fa8795676b | 27 | static void (*tick_timer_callback)(void); |
leothedragon | 0:25fa8795676b | 28 | |
leothedragon | 0:25fa8795676b | 29 | void timer_callback(void const *funcArgument) |
leothedragon | 0:25fa8795676b | 30 | { |
leothedragon | 0:25fa8795676b | 31 | (void)funcArgument; |
leothedragon | 0:25fa8795676b | 32 | if (NULL != tick_timer_callback) { |
leothedragon | 0:25fa8795676b | 33 | tick_timer_callback(); |
leothedragon | 0:25fa8795676b | 34 | } |
leothedragon | 0:25fa8795676b | 35 | } |
leothedragon | 0:25fa8795676b | 36 | |
leothedragon | 0:25fa8795676b | 37 | // Low precision platform tick timer |
leothedragon | 0:25fa8795676b | 38 | int8_t platform_tick_timer_register(void (*tick_timer_cb_handler)(void)) |
leothedragon | 0:25fa8795676b | 39 | { |
leothedragon | 0:25fa8795676b | 40 | tick_timer_callback = tick_timer_cb_handler; |
leothedragon | 0:25fa8795676b | 41 | return TICK_TIMER_ID; |
leothedragon | 0:25fa8795676b | 42 | } |
leothedragon | 0:25fa8795676b | 43 | |
leothedragon | 0:25fa8795676b | 44 | int8_t platform_tick_timer_start(uint32_t period_ms) |
leothedragon | 0:25fa8795676b | 45 | { |
leothedragon | 0:25fa8795676b | 46 | int8_t retval = -1; |
leothedragon | 0:25fa8795676b | 47 | if (sys_timer_handle != NULL) { |
leothedragon | 0:25fa8795676b | 48 | return 0; // Timer already started already so return success |
leothedragon | 0:25fa8795676b | 49 | } |
leothedragon | 0:25fa8795676b | 50 | Event e = FunctionPointer1<void, void const *>(timer_callback).bind(NULL); |
leothedragon | 0:25fa8795676b | 51 | if (e != NULL) { |
leothedragon | 0:25fa8795676b | 52 | sys_timer_handle = Scheduler::postCallback(e).period(milliseconds(period_ms)).getHandle(); |
leothedragon | 0:25fa8795676b | 53 | if (sys_timer_handle != NULL) { |
leothedragon | 0:25fa8795676b | 54 | retval = 0; |
leothedragon | 0:25fa8795676b | 55 | } |
leothedragon | 0:25fa8795676b | 56 | } |
leothedragon | 0:25fa8795676b | 57 | return retval; |
leothedragon | 0:25fa8795676b | 58 | } |
leothedragon | 0:25fa8795676b | 59 | |
leothedragon | 0:25fa8795676b | 60 | int8_t platform_tick_timer_stop(void) |
leothedragon | 0:25fa8795676b | 61 | { |
leothedragon | 0:25fa8795676b | 62 | int8_t retval = -1; |
leothedragon | 0:25fa8795676b | 63 | if (sys_timer_handle != NULL) { |
leothedragon | 0:25fa8795676b | 64 | Scheduler::cancelCallback(sys_timer_handle); |
leothedragon | 0:25fa8795676b | 65 | sys_timer_handle = NULL; |
leothedragon | 0:25fa8795676b | 66 | retval = 0; |
leothedragon | 0:25fa8795676b | 67 | } |
leothedragon | 0:25fa8795676b | 68 | return retval; |
leothedragon | 0:25fa8795676b | 69 | } |
leothedragon | 0:25fa8795676b | 70 | |
leothedragon | 0:25fa8795676b | 71 | #endif // defined(NS_EVENTLOOP_USE_TICK_TIMER) && defined(YOTTA_CFG) |