|
32 | 32 | * for the low power timer, which should be good enough for a low power use
|
33 | 33 | * case.
|
34 | 34 | *
|
| 35 | + * Mapping of mbed APIs to Silicon Labs peripherals: |
| 36 | + * ---: Does not meet mbed API requirements |
| 37 | + * X : Implemented to provide mbed API functionality |
| 38 | + * |
| 39 | + * -------------------------------------------- |
| 40 | + * | ------------- | RTCC | BURTC | RTC | TIMER | |
| 41 | + * | rtc_api | X | X | --- | ----- | |
| 42 | + * | lp_ticker_api | X | | X | ----- | |
| 43 | + * | us_ticker_api | --- | ----- | --- | X | |
| 44 | + * -------------------------------------------- |
| 45 | + * |
35 | 46 | * On Silicon Labs devices, the lowest width RTC implementation has a 24-bit
|
36 | 47 | * counter, which gets extended with a further 32-bit software counter. This
|
37 | 48 | * gives 56 bits of actual width, which with the default speed maps to
|
|
40 | 51 | * (At max speed the wraparound is at 69730 years, which is unlikely as well)
|
41 | 52 | ******************************************************************************/
|
42 | 53 |
|
43 |
| -#include "rtc_api.h" |
44 |
| -#include "rtc_api_HAL.h" |
| 54 | +#if defined(RTC_PRESENT) |
| 55 | +#include "em_rtc.h" |
| 56 | +#include "em_cmu.h" |
45 | 57 | #include "lp_ticker_api.h"
|
46 | 58 | #include "mbed_critical.h"
|
47 | 59 |
|
48 |
| -static int rtc_reserved = 0; |
| 60 | +#if RTC_CLOCKDIV_INT > 16 |
| 61 | +#error invalid prescaler value RTC_CLOCKDIV_INT, since LP ticker resolution will exceed 1ms. |
| 62 | +#endif |
| 63 | + |
| 64 | +#define RTC_BITS (24U) |
| 65 | +#define RTC_MAX_VALUE (0xFFFFFFUL) |
| 66 | + |
| 67 | +static bool rtc_inited = false; |
| 68 | + |
| 69 | +const ticker_info_t* lp_ticker_get_info(void) |
| 70 | +{ |
| 71 | + static const ticker_info_t rtc_info = { |
| 72 | + LOW_ENERGY_CLOCK_FREQUENCY, |
| 73 | + RTC_BITS |
| 74 | + }; |
| 75 | + return &rtc_info; |
| 76 | +} |
| 77 | + |
| 78 | +void RTC_IRQHandler(void) |
| 79 | +{ |
| 80 | + uint32_t flags; |
| 81 | + flags = RTC_IntGet(); |
| 82 | + if ((flags & RTC_IF_COMP0) && rtc_inited) { |
| 83 | + RTC_IntClear(RTC_IF_COMP0); |
| 84 | + lp_ticker_irq_handler(); |
| 85 | + } |
| 86 | +} |
49 | 87 |
|
50 | 88 | void lp_ticker_init()
|
51 | 89 | {
|
52 |
| - if(!rtc_reserved) { |
53 |
| - core_util_critical_section_enter(); |
54 |
| - rtc_init_real(RTC_INIT_LPTIMER); |
55 |
| - rtc_set_comp0_handler((uint32_t)lp_ticker_irq_handler); |
56 |
| - rtc_reserved = 1; |
57 |
| - core_util_critical_section_exit(); |
| 90 | + core_util_critical_section_enter(); |
| 91 | + if (!rtc_inited) { |
| 92 | + CMU_ClockEnable(cmuClock_RTC, true); |
| 93 | + |
| 94 | + /* Initialize RTC */ |
| 95 | + RTC_Init_TypeDef init = RTC_INIT_DEFAULT; |
| 96 | + init.enable = 1; |
| 97 | + /* Don't use compare register 0 as top value */ |
| 98 | + init.comp0Top = 0; |
| 99 | + |
| 100 | + /* Initialize */ |
| 101 | + RTC_Init(&init); |
| 102 | + RTC_CounterSet(20); |
| 103 | + |
| 104 | + /* Enable Interrupt from RTC */ |
| 105 | + RTC_IntDisable(RTC_IF_COMP0); |
| 106 | + RTC_IntClear(RTC_IF_COMP0); |
| 107 | + NVIC_SetVector(RTC_IRQn, (uint32_t)RTC_IRQHandler); |
| 108 | + NVIC_EnableIRQ(RTC_IRQn); |
| 109 | + |
| 110 | + rtc_inited = true; |
| 111 | + } else { |
| 112 | + /* Cancel current interrupt by virtue of calling init again */ |
| 113 | + RTC_IntDisable(RTC_IF_COMP0); |
| 114 | + RTC_IntClear(RTC_IF_COMP0); |
58 | 115 | }
|
| 116 | + core_util_critical_section_exit(); |
59 | 117 | }
|
60 | 118 |
|
61 | 119 | void lp_ticker_free()
|
62 | 120 | {
|
63 |
| - if(rtc_reserved) { |
64 |
| - core_util_critical_section_enter(); |
65 |
| - rtc_free_real(RTC_INIT_LPTIMER); |
66 |
| - rtc_reserved = 0; |
67 |
| - core_util_critical_section_exit(); |
| 121 | + /* Disable the RTC if it was inited and is no longer in use by anyone. */ |
| 122 | + if (rtc_inited) { |
| 123 | + NVIC_DisableIRQ(RTC_IRQn); |
| 124 | + RTC_Reset(); |
| 125 | + CMU_ClockEnable(cmuClock_RTC, false); |
| 126 | + rtc_inited = false; |
68 | 127 | }
|
69 | 128 | }
|
70 | 129 |
|
71 | 130 | void lp_ticker_set_interrupt(timestamp_t timestamp)
|
72 | 131 | {
|
73 |
| - uint64_t rtc_compare_value; |
74 |
| - uint64_t current_ticks = rtc_get_full(); |
75 |
| - timestamp_t current_time = lp_ticker_read(); |
76 |
| - |
77 |
| - /* calculate offset value */ |
78 |
| - timestamp_t offset = timestamp - current_time; |
79 |
| - |
80 |
| - /* If the requested timestamp is too far in the future, we might not be able |
81 |
| - * to set the interrupt accurately due to potentially having ticked between |
82 |
| - * calculating the timestamp to set and us calculating the offset. */ |
83 |
| - if(offset > 0xFFFF0000) offset = 100; |
84 |
| - |
85 |
| - /* map offset to RTC value */ |
86 |
| - // ticks = offset * RTC frequency div 1000000 |
87 |
| - rtc_compare_value = ((uint64_t)offset * (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT)) / 1000000; |
88 |
| - |
89 |
| - /* If RTC offset is less then 2 RTC ticks, the interrupt won't fire */ |
90 |
| - if(rtc_compare_value < 2) { |
91 |
| - rtc_compare_value = 2; |
92 |
| - } |
93 |
| - |
94 |
| - rtc_compare_value += current_ticks; |
95 |
| - |
96 |
| - rtc_set_comp0_value(rtc_compare_value, true); |
| 132 | + RTC_IntDisable(RTC_IF_COMP0); |
| 133 | + RTC_IntClear(RTC_IF_COMP0); |
| 134 | + RTC_FreezeEnable(true); |
| 135 | + RTC_CompareSet(0, (uint32_t) (timestamp & RTC_MAX_VALUE)); |
| 136 | + RTC_FreezeEnable(false); |
| 137 | + RTC_IntEnable(RTC_IF_COMP0); |
97 | 138 | }
|
98 | 139 |
|
99 |
| -inline void lp_ticker_fire_interrupt(void) |
| 140 | +void lp_ticker_fire_interrupt(void) |
100 | 141 | {
|
101 |
| - rtc_force_comp0(); |
| 142 | + RTC_IntEnable(RTC_IF_COMP0); |
| 143 | + RTC_IntSet(RTC_IF_COMP0); |
102 | 144 | }
|
103 | 145 |
|
104 |
| -inline void lp_ticker_disable_interrupt() |
| 146 | +void lp_ticker_disable_interrupt() |
105 | 147 | {
|
106 |
| - rtc_enable_comp0(false); |
| 148 | + RTC_IntDisable(RTC_IF_COMP0); |
107 | 149 | }
|
108 | 150 |
|
109 |
| -inline void lp_ticker_clear_interrupt() |
| 151 | +void lp_ticker_clear_interrupt() |
110 | 152 | {
|
111 |
| - /* No need to clear interrupt flag, since that already happens at RTC level */ |
| 153 | + RTC_IntClear(RTC_IF_COMP0); |
112 | 154 | }
|
113 | 155 |
|
114 | 156 | timestamp_t lp_ticker_read()
|
115 | 157 | {
|
116 |
| - lp_ticker_init(); |
117 |
| - |
118 |
| - uint64_t ticks_temp; |
119 |
| - uint64_t ticks = rtc_get_full(); |
120 |
| - |
121 |
| - /* ticks = counter tick value |
122 |
| - * timestamp = value in microseconds |
123 |
| - * timestamp = ticks * 1.000.000 / RTC frequency |
124 |
| - */ |
125 |
| - |
126 |
| - ticks_temp = (ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT); |
127 |
| - return (timestamp_t) (ticks_temp & 0xFFFFFFFF); |
| 158 | + return (timestamp_t) RTC_CounterGet(); |
128 | 159 | }
|
129 | 160 |
|
130 |
| -#endif |
| 161 | +#elif defined(RTCC_PRESENT) |
| 162 | +/* lp_ticker api is implemented in rtc_rtcc.c */ |
| 163 | +#endif /* RTC_PRESENT */ |
| 164 | +#endif /* DEVICE_LPTICKER */ |
0 commit comments