added prescaler for 16 bit pwm in LPC1347 target
Fork of mbed-dev by
targets/hal/TARGET_Silicon_Labs/TARGET_EFM32/lp_ticker.c@50:a417edff4437, 2016-01-15 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Jan 15 07:45:16 2016 +0000
- Revision:
- 50:a417edff4437
- Parent:
- 0:9b334a45a8ff
- Child:
- 144:ef7eb2e8f9f7
Synchronized with git revision 6010f32619bfcbb01cc73747d4ff9040863482d9
Full URL: https://github.com/mbedmicro/mbed/commit/6010f32619bfcbb01cc73747d4ff9040863482d9/
Remove doubling of buffer size in realiseEndpoint()
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /***************************************************************************//** |
bogdanm | 0:9b334a45a8ff | 2 | * @file lp_ticker.c |
bogdanm | 0:9b334a45a8ff | 3 | ******************************************************************************* |
bogdanm | 0:9b334a45a8ff | 4 | * @section License |
bogdanm | 0:9b334a45a8ff | 5 | * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> |
bogdanm | 0:9b334a45a8ff | 6 | ******************************************************************************* |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * Permission is granted to anyone to use this software for any purpose, |
bogdanm | 0:9b334a45a8ff | 9 | * including commercial applications, and to alter it and redistribute it |
bogdanm | 0:9b334a45a8ff | 10 | * freely, subject to the following restrictions: |
bogdanm | 0:9b334a45a8ff | 11 | * |
bogdanm | 0:9b334a45a8ff | 12 | * 1. The origin of this software must not be misrepresented; you must not |
bogdanm | 0:9b334a45a8ff | 13 | * claim that you wrote the original software. |
bogdanm | 0:9b334a45a8ff | 14 | * 2. Altered source versions must be plainly marked as such, and must not be |
bogdanm | 0:9b334a45a8ff | 15 | * misrepresented as being the original software. |
bogdanm | 0:9b334a45a8ff | 16 | * 3. This notice may not be removed or altered from any source distribution. |
bogdanm | 0:9b334a45a8ff | 17 | * |
bogdanm | 0:9b334a45a8ff | 18 | * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no |
bogdanm | 0:9b334a45a8ff | 19 | * obligation to support this Software. Silicon Labs is providing the |
bogdanm | 0:9b334a45a8ff | 20 | * Software "AS IS", with no express or implied warranties of any kind, |
bogdanm | 0:9b334a45a8ff | 21 | * including, but not limited to, any implied warranties of merchantability |
bogdanm | 0:9b334a45a8ff | 22 | * or fitness for any particular purpose or warranties against infringement |
bogdanm | 0:9b334a45a8ff | 23 | * of any proprietary rights of a third party. |
bogdanm | 0:9b334a45a8ff | 24 | * |
bogdanm | 0:9b334a45a8ff | 25 | * Silicon Labs will not be liable for any consequential, incidental, or |
bogdanm | 0:9b334a45a8ff | 26 | * special damages, or any other relief, or for any claim by any third party, |
bogdanm | 0:9b334a45a8ff | 27 | * arising from your use of this Software. |
bogdanm | 0:9b334a45a8ff | 28 | * |
bogdanm | 0:9b334a45a8ff | 29 | ******************************************************************************/ |
bogdanm | 0:9b334a45a8ff | 30 | |
bogdanm | 0:9b334a45a8ff | 31 | #include "device.h" |
bogdanm | 0:9b334a45a8ff | 32 | #if DEVICE_LOWPOWERTIMER |
bogdanm | 0:9b334a45a8ff | 33 | |
bogdanm | 0:9b334a45a8ff | 34 | #include "rtc_api.h" |
bogdanm | 0:9b334a45a8ff | 35 | #include "rtc_api_HAL.h" |
bogdanm | 0:9b334a45a8ff | 36 | #include "lp_ticker_api.h" |
bogdanm | 0:9b334a45a8ff | 37 | |
mbed_official | 50:a417edff4437 | 38 | #include "em_int.h" |
mbed_official | 50:a417edff4437 | 39 | #if (defined RTCC_COUNT) && (RTCC_COUNT > 0) |
mbed_official | 50:a417edff4437 | 40 | #include "em_rtcc.h" |
mbed_official | 50:a417edff4437 | 41 | #endif |
mbed_official | 50:a417edff4437 | 42 | |
mbed_official | 50:a417edff4437 | 43 | static int rtc_reserved = 0; |
mbed_official | 50:a417edff4437 | 44 | |
bogdanm | 0:9b334a45a8ff | 45 | void lp_ticker_init() |
bogdanm | 0:9b334a45a8ff | 46 | { |
mbed_official | 50:a417edff4437 | 47 | if(!rtc_reserved) { |
mbed_official | 50:a417edff4437 | 48 | INT_Disable(); |
mbed_official | 50:a417edff4437 | 49 | rtc_init_real(RTC_INIT_LPTIMER); |
mbed_official | 50:a417edff4437 | 50 | rtc_set_comp0_handler((uint32_t)lp_ticker_irq_handler); |
mbed_official | 50:a417edff4437 | 51 | rtc_reserved = 1; |
mbed_official | 50:a417edff4437 | 52 | INT_Enable(); |
mbed_official | 50:a417edff4437 | 53 | } |
bogdanm | 0:9b334a45a8ff | 54 | } |
bogdanm | 0:9b334a45a8ff | 55 | |
mbed_official | 50:a417edff4437 | 56 | void lp_ticker_free() |
mbed_official | 50:a417edff4437 | 57 | { |
mbed_official | 50:a417edff4437 | 58 | if(rtc_reserved) { |
mbed_official | 50:a417edff4437 | 59 | INT_Disable(); |
mbed_official | 50:a417edff4437 | 60 | rtc_free_real(RTC_INIT_LPTIMER); |
mbed_official | 50:a417edff4437 | 61 | rtc_reserved = 0; |
mbed_official | 50:a417edff4437 | 62 | INT_Enable(); |
mbed_official | 50:a417edff4437 | 63 | } |
mbed_official | 50:a417edff4437 | 64 | } |
mbed_official | 50:a417edff4437 | 65 | |
mbed_official | 50:a417edff4437 | 66 | #ifndef RTCC_COUNT |
mbed_official | 50:a417edff4437 | 67 | |
mbed_official | 50:a417edff4437 | 68 | /* RTC API */ |
mbed_official | 50:a417edff4437 | 69 | |
bogdanm | 0:9b334a45a8ff | 70 | void lp_ticker_set_interrupt(timestamp_t timestamp) |
bogdanm | 0:9b334a45a8ff | 71 | { |
bogdanm | 0:9b334a45a8ff | 72 | uint64_t timestamp_ticks; |
bogdanm | 0:9b334a45a8ff | 73 | uint64_t current_ticks = RTC_CounterGet(); |
bogdanm | 0:9b334a45a8ff | 74 | timestamp_t current_time = ((uint64_t)(current_ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT)); |
bogdanm | 0:9b334a45a8ff | 75 | |
mbed_official | 50:a417edff4437 | 76 | /* Initialize RTC */ |
mbed_official | 50:a417edff4437 | 77 | lp_ticker_init(); |
bogdanm | 0:9b334a45a8ff | 78 | |
bogdanm | 0:9b334a45a8ff | 79 | /* calculate offset value */ |
bogdanm | 0:9b334a45a8ff | 80 | timestamp_t offset = timestamp - current_time; |
bogdanm | 0:9b334a45a8ff | 81 | if(offset > 0xEFFFFFFF) offset = 100; |
bogdanm | 0:9b334a45a8ff | 82 | |
bogdanm | 0:9b334a45a8ff | 83 | /* map offset to RTC value */ |
bogdanm | 0:9b334a45a8ff | 84 | // ticks = offset * RTC frequency div 1000000 |
bogdanm | 0:9b334a45a8ff | 85 | timestamp_ticks = ((uint64_t)offset * (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT)) / 1000000; |
bogdanm | 0:9b334a45a8ff | 86 | timestamp_ticks += current_ticks; |
bogdanm | 0:9b334a45a8ff | 87 | |
bogdanm | 0:9b334a45a8ff | 88 | /* RTC has 24 bit resolution */ |
bogdanm | 0:9b334a45a8ff | 89 | timestamp_ticks &= 0xFFFFFF; |
bogdanm | 0:9b334a45a8ff | 90 | |
bogdanm | 0:9b334a45a8ff | 91 | /* check for RTC limitation */ |
bogdanm | 0:9b334a45a8ff | 92 | if((timestamp_ticks - RTC_CounterGet()) >= 0x800000) timestamp_ticks = RTC_CounterGet() + 2; |
bogdanm | 0:9b334a45a8ff | 93 | |
bogdanm | 0:9b334a45a8ff | 94 | /* Set callback */ |
bogdanm | 0:9b334a45a8ff | 95 | RTC_FreezeEnable(true); |
bogdanm | 0:9b334a45a8ff | 96 | RTC_CompareSet(0, (uint32_t)timestamp_ticks); |
bogdanm | 0:9b334a45a8ff | 97 | RTC_IntEnable(RTC_IF_COMP0); |
bogdanm | 0:9b334a45a8ff | 98 | RTC_FreezeEnable(false); |
bogdanm | 0:9b334a45a8ff | 99 | } |
bogdanm | 0:9b334a45a8ff | 100 | |
bogdanm | 0:9b334a45a8ff | 101 | inline void lp_ticker_disable_interrupt() |
bogdanm | 0:9b334a45a8ff | 102 | { |
bogdanm | 0:9b334a45a8ff | 103 | RTC_IntDisable(RTC_IF_COMP0); |
mbed_official | 50:a417edff4437 | 104 | lp_ticker_free(); |
bogdanm | 0:9b334a45a8ff | 105 | } |
bogdanm | 0:9b334a45a8ff | 106 | |
bogdanm | 0:9b334a45a8ff | 107 | inline void lp_ticker_clear_interrupt() |
bogdanm | 0:9b334a45a8ff | 108 | { |
bogdanm | 0:9b334a45a8ff | 109 | RTC_IntClear(RTC_IF_COMP0); |
bogdanm | 0:9b334a45a8ff | 110 | } |
bogdanm | 0:9b334a45a8ff | 111 | |
bogdanm | 0:9b334a45a8ff | 112 | timestamp_t lp_ticker_read() |
bogdanm | 0:9b334a45a8ff | 113 | { |
mbed_official | 50:a417edff4437 | 114 | lp_ticker_init(); |
mbed_official | 50:a417edff4437 | 115 | |
bogdanm | 0:9b334a45a8ff | 116 | uint64_t ticks_temp; |
bogdanm | 0:9b334a45a8ff | 117 | uint64_t ticks = RTC_CounterGet(); |
bogdanm | 0:9b334a45a8ff | 118 | |
bogdanm | 0:9b334a45a8ff | 119 | /* ticks = counter tick value |
bogdanm | 0:9b334a45a8ff | 120 | * timestamp = value in microseconds |
bogdanm | 0:9b334a45a8ff | 121 | * timestamp = ticks * 1.000.000 / RTC frequency |
bogdanm | 0:9b334a45a8ff | 122 | */ |
bogdanm | 0:9b334a45a8ff | 123 | |
bogdanm | 0:9b334a45a8ff | 124 | ticks_temp = (ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT); |
bogdanm | 0:9b334a45a8ff | 125 | return (timestamp_t) (ticks_temp & 0xFFFFFFFF); |
bogdanm | 0:9b334a45a8ff | 126 | } |
bogdanm | 0:9b334a45a8ff | 127 | |
mbed_official | 50:a417edff4437 | 128 | #else |
mbed_official | 50:a417edff4437 | 129 | |
mbed_official | 50:a417edff4437 | 130 | /* RTCC API */ |
mbed_official | 50:a417edff4437 | 131 | |
mbed_official | 50:a417edff4437 | 132 | void lp_ticker_set_interrupt(timestamp_t timestamp) |
mbed_official | 50:a417edff4437 | 133 | { |
mbed_official | 50:a417edff4437 | 134 | uint64_t timestamp_ticks; |
mbed_official | 50:a417edff4437 | 135 | uint64_t current_ticks = RTCC_CounterGet(); |
mbed_official | 50:a417edff4437 | 136 | timestamp_t current_time = ((uint64_t)(current_ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT)); |
mbed_official | 50:a417edff4437 | 137 | |
mbed_official | 50:a417edff4437 | 138 | /* Initialize RTC */ |
mbed_official | 50:a417edff4437 | 139 | lp_ticker_init(); |
mbed_official | 50:a417edff4437 | 140 | |
mbed_official | 50:a417edff4437 | 141 | /* calculate offset value */ |
mbed_official | 50:a417edff4437 | 142 | timestamp_t offset = timestamp - current_time; |
mbed_official | 50:a417edff4437 | 143 | if(offset > 0xEFFFFFFF) offset = 100; |
mbed_official | 50:a417edff4437 | 144 | |
mbed_official | 50:a417edff4437 | 145 | /* map offset to RTC value */ |
mbed_official | 50:a417edff4437 | 146 | // ticks = offset * RTC frequency div 1000000 |
mbed_official | 50:a417edff4437 | 147 | timestamp_ticks = ((uint64_t)offset * (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT)) / 1000000; |
mbed_official | 50:a417edff4437 | 148 | // checking the rounding. If timeout is wanted between RTCC ticks, irq should be configured to |
mbed_official | 50:a417edff4437 | 149 | // trigger in the latter RTCC-tick. Otherwise ticker-api fails to send timer event to its client |
mbed_official | 50:a417edff4437 | 150 | if(((timestamp_ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT)) < offset){ |
mbed_official | 50:a417edff4437 | 151 | timestamp_ticks++; |
mbed_official | 50:a417edff4437 | 152 | } |
mbed_official | 50:a417edff4437 | 153 | |
mbed_official | 50:a417edff4437 | 154 | timestamp_ticks += current_ticks; |
mbed_official | 50:a417edff4437 | 155 | |
mbed_official | 50:a417edff4437 | 156 | /* RTCC has 32 bit resolution */ |
mbed_official | 50:a417edff4437 | 157 | timestamp_ticks &= 0xFFFFFFFF; |
mbed_official | 50:a417edff4437 | 158 | |
mbed_official | 50:a417edff4437 | 159 | /* check for RTCC limitation */ |
mbed_official | 50:a417edff4437 | 160 | if((timestamp_ticks - RTCC_CounterGet()) >= 0x80000000) timestamp_ticks = RTCC_CounterGet() + 2; |
mbed_official | 50:a417edff4437 | 161 | |
mbed_official | 50:a417edff4437 | 162 | /* init channel */ |
mbed_official | 50:a417edff4437 | 163 | RTCC_CCChConf_TypeDef ccchConf = RTCC_CH_INIT_COMPARE_DEFAULT; |
mbed_official | 50:a417edff4437 | 164 | RTCC_ChannelInit(0,&ccchConf); |
mbed_official | 50:a417edff4437 | 165 | /* Set callback */ |
mbed_official | 50:a417edff4437 | 166 | RTCC_ChannelCCVSet(0, (uint32_t)timestamp_ticks); |
mbed_official | 50:a417edff4437 | 167 | RTCC_IntEnable(RTCC_IF_CC0); |
mbed_official | 50:a417edff4437 | 168 | } |
mbed_official | 50:a417edff4437 | 169 | |
mbed_official | 50:a417edff4437 | 170 | inline void lp_ticker_disable_interrupt() |
mbed_official | 50:a417edff4437 | 171 | { |
mbed_official | 50:a417edff4437 | 172 | RTCC_IntDisable(RTCC_IF_CC0); |
mbed_official | 50:a417edff4437 | 173 | lp_ticker_free(); |
mbed_official | 50:a417edff4437 | 174 | } |
mbed_official | 50:a417edff4437 | 175 | |
mbed_official | 50:a417edff4437 | 176 | inline void lp_ticker_clear_interrupt() |
mbed_official | 50:a417edff4437 | 177 | { |
mbed_official | 50:a417edff4437 | 178 | RTCC_IntClear(RTCC_IF_CC0); |
mbed_official | 50:a417edff4437 | 179 | } |
mbed_official | 50:a417edff4437 | 180 | |
mbed_official | 50:a417edff4437 | 181 | timestamp_t lp_ticker_read() |
mbed_official | 50:a417edff4437 | 182 | { |
mbed_official | 50:a417edff4437 | 183 | lp_ticker_init(); |
mbed_official | 50:a417edff4437 | 184 | |
mbed_official | 50:a417edff4437 | 185 | uint64_t ticks_temp; |
mbed_official | 50:a417edff4437 | 186 | uint64_t ticks = RTCC_CounterGet(); |
mbed_official | 50:a417edff4437 | 187 | |
mbed_official | 50:a417edff4437 | 188 | /* ticks = counter tick value |
mbed_official | 50:a417edff4437 | 189 | * timestamp = value in microseconds |
mbed_official | 50:a417edff4437 | 190 | * timestamp = ticks * 1.000.000 / RTC frequency |
mbed_official | 50:a417edff4437 | 191 | */ |
mbed_official | 50:a417edff4437 | 192 | |
mbed_official | 50:a417edff4437 | 193 | ticks_temp = (ticks * 1000000) / (LOW_ENERGY_CLOCK_FREQUENCY / RTC_CLOCKDIV_INT); |
mbed_official | 50:a417edff4437 | 194 | return (timestamp_t) (ticks_temp & 0xFFFFFFFF); |
mbed_official | 50:a417edff4437 | 195 | } |
mbed_official | 50:a417edff4437 | 196 | |
mbed_official | 50:a417edff4437 | 197 | #endif /* RTCC */ |
mbed_official | 50:a417edff4437 | 198 | |
bogdanm | 0:9b334a45a8ff | 199 | #endif |