added prescaler for 16 bit pwm in LPC1347 target

Fork of mbed-dev by mbed official

Committer:
bogdanm
Date:
Thu Oct 01 15:25:22 2015 +0300
Revision:
0:9b334a45a8ff
Child:
144:ef7eb2e8f9f7
Initial commit on mbed-dev

Replaces mbed-src (now inactive)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 0:9b334a45a8ff 1 /*******************************************************************************
bogdanm 0:9b334a45a8ff 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
bogdanm 0:9b334a45a8ff 3 *
bogdanm 0:9b334a45a8ff 4 * Permission is hereby granted, free of charge, to any person obtaining a
bogdanm 0:9b334a45a8ff 5 * copy of this software and associated documentation files (the "Software"),
bogdanm 0:9b334a45a8ff 6 * to deal in the Software without restriction, including without limitation
bogdanm 0:9b334a45a8ff 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
bogdanm 0:9b334a45a8ff 8 * and/or sell copies of the Software, and to permit persons to whom the
bogdanm 0:9b334a45a8ff 9 * Software is furnished to do so, subject to the following conditions:
bogdanm 0:9b334a45a8ff 10 *
bogdanm 0:9b334a45a8ff 11 * The above copyright notice and this permission notice shall be included
bogdanm 0:9b334a45a8ff 12 * in all copies or substantial portions of the Software.
bogdanm 0:9b334a45a8ff 13 *
bogdanm 0:9b334a45a8ff 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
bogdanm 0:9b334a45a8ff 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
bogdanm 0:9b334a45a8ff 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
bogdanm 0:9b334a45a8ff 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
bogdanm 0:9b334a45a8ff 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
bogdanm 0:9b334a45a8ff 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
bogdanm 0:9b334a45a8ff 20 * OTHER DEALINGS IN THE SOFTWARE.
bogdanm 0:9b334a45a8ff 21 *
bogdanm 0:9b334a45a8ff 22 * Except as contained in this notice, the name of Maxim Integrated
bogdanm 0:9b334a45a8ff 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
bogdanm 0:9b334a45a8ff 24 * Products, Inc. Branding Policy.
bogdanm 0:9b334a45a8ff 25 *
bogdanm 0:9b334a45a8ff 26 * The mere transfer of this software does not imply any licenses
bogdanm 0:9b334a45a8ff 27 * of trade secrets, proprietary technology, copyrights, patents,
bogdanm 0:9b334a45a8ff 28 * trademarks, maskwork rights, or any other form of intellectual
bogdanm 0:9b334a45a8ff 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
bogdanm 0:9b334a45a8ff 30 * ownership rights.
bogdanm 0:9b334a45a8ff 31 *******************************************************************************
bogdanm 0:9b334a45a8ff 32 */
bogdanm 0:9b334a45a8ff 33
bogdanm 0:9b334a45a8ff 34 #include "mbed_error.h"
bogdanm 0:9b334a45a8ff 35 #include "us_ticker_api.h"
bogdanm 0:9b334a45a8ff 36 #include "PeripheralNames.h"
bogdanm 0:9b334a45a8ff 37 #include "tmr_regs.h"
bogdanm 0:9b334a45a8ff 38
bogdanm 0:9b334a45a8ff 39 #define US_TIMER MXC_TMR0
bogdanm 0:9b334a45a8ff 40 #define US_TIMER_IRQn TMR0_IRQn
bogdanm 0:9b334a45a8ff 41
bogdanm 0:9b334a45a8ff 42 static int us_ticker_inited = 0;
bogdanm 0:9b334a45a8ff 43 static uint32_t ticks_per_us;
bogdanm 0:9b334a45a8ff 44 static uint32_t tick_win;
bogdanm 0:9b334a45a8ff 45 static volatile uint64_t current_cnt; // Hold the current ticks
bogdanm 0:9b334a45a8ff 46 static volatile uint64_t event_cnt; // Holds the value of the next event
bogdanm 0:9b334a45a8ff 47
bogdanm 0:9b334a45a8ff 48 #define ticks_to_us(ticks) ((ticks) / ticks_per_us);
bogdanm 0:9b334a45a8ff 49 #define MAX_TICK_VAL ((uint64_t)0xFFFFFFFF * ticks_per_us)
bogdanm 0:9b334a45a8ff 50
bogdanm 0:9b334a45a8ff 51 //******************************************************************************
bogdanm 0:9b334a45a8ff 52 static inline void inc_current_cnt(uint32_t inc) {
bogdanm 0:9b334a45a8ff 53
bogdanm 0:9b334a45a8ff 54 // Overflow the ticker when the us ticker overflows
bogdanm 0:9b334a45a8ff 55 current_cnt += inc;
bogdanm 0:9b334a45a8ff 56 if (current_cnt > MAX_TICK_VAL) {
bogdanm 0:9b334a45a8ff 57 current_cnt -= (MAX_TICK_VAL + 1);
bogdanm 0:9b334a45a8ff 58 }
bogdanm 0:9b334a45a8ff 59 }
bogdanm 0:9b334a45a8ff 60
bogdanm 0:9b334a45a8ff 61 //******************************************************************************
bogdanm 0:9b334a45a8ff 62 static inline int event_passed(uint64_t current, uint64_t event) {
bogdanm 0:9b334a45a8ff 63
bogdanm 0:9b334a45a8ff 64 // Determine if the event has already happened.
bogdanm 0:9b334a45a8ff 65 // If the event is behind the current ticker, within a window,
bogdanm 0:9b334a45a8ff 66 // then the event has already happened.
bogdanm 0:9b334a45a8ff 67 if (((current < tick_win) && ((event < current) ||
bogdanm 0:9b334a45a8ff 68 (event > (MAX_TICK_VAL - (tick_win - current))))) ||
bogdanm 0:9b334a45a8ff 69 ((event < current) && (event > (current - tick_win)))) {
bogdanm 0:9b334a45a8ff 70 return 1;
bogdanm 0:9b334a45a8ff 71 }
bogdanm 0:9b334a45a8ff 72
bogdanm 0:9b334a45a8ff 73 return 0;
bogdanm 0:9b334a45a8ff 74 }
bogdanm 0:9b334a45a8ff 75
bogdanm 0:9b334a45a8ff 76 //******************************************************************************
bogdanm 0:9b334a45a8ff 77 static inline uint64_t event_diff(uint64_t current, uint64_t event) {
bogdanm 0:9b334a45a8ff 78
bogdanm 0:9b334a45a8ff 79 // Check to see if the ticker will overflow before the event
bogdanm 0:9b334a45a8ff 80 if(current <= event) {
bogdanm 0:9b334a45a8ff 81 return (event - current);
bogdanm 0:9b334a45a8ff 82 }
bogdanm 0:9b334a45a8ff 83
bogdanm 0:9b334a45a8ff 84 return ((MAX_TICK_VAL - current) + event);
bogdanm 0:9b334a45a8ff 85 }
bogdanm 0:9b334a45a8ff 86
bogdanm 0:9b334a45a8ff 87 //******************************************************************************
bogdanm 0:9b334a45a8ff 88 static void tmr_handler(void)
bogdanm 0:9b334a45a8ff 89 {
bogdanm 0:9b334a45a8ff 90 uint32_t term_cnt32 = US_TIMER->term_cnt32;
bogdanm 0:9b334a45a8ff 91 US_TIMER->term_cnt32 = 0xFFFFFFFF; // reset to max value to prevent further interrupts
bogdanm 0:9b334a45a8ff 92 US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear interrupt
bogdanm 0:9b334a45a8ff 93 NVIC_ClearPendingIRQ(US_TIMER_IRQn);
bogdanm 0:9b334a45a8ff 94
bogdanm 0:9b334a45a8ff 95 inc_current_cnt(term_cnt32);
bogdanm 0:9b334a45a8ff 96
bogdanm 0:9b334a45a8ff 97 if (event_passed(current_cnt + US_TIMER->count32, event_cnt )) {
bogdanm 0:9b334a45a8ff 98 // the timestamp has expired
bogdanm 0:9b334a45a8ff 99 event_cnt = 0xFFFFFFFFFFFFFFFFULL; // reset to max value
bogdanm 0:9b334a45a8ff 100 us_ticker_irq_handler();
bogdanm 0:9b334a45a8ff 101 } else {
bogdanm 0:9b334a45a8ff 102
bogdanm 0:9b334a45a8ff 103 uint64_t diff = event_diff(current_cnt, event_cnt);
bogdanm 0:9b334a45a8ff 104 if (diff < (uint64_t)0xFFFFFFFF) {
bogdanm 0:9b334a45a8ff 105 // the event occurs before the next overflow
bogdanm 0:9b334a45a8ff 106 US_TIMER->term_cnt32 = diff;
bogdanm 0:9b334a45a8ff 107
bogdanm 0:9b334a45a8ff 108 // Since the timer keeps counting after the terminal value is reached, it is possible that the new
bogdanm 0:9b334a45a8ff 109 // terminal value is in the past.
bogdanm 0:9b334a45a8ff 110 if (US_TIMER->term_cnt32 < US_TIMER->count32) {
bogdanm 0:9b334a45a8ff 111 // the timestamp has expired
bogdanm 0:9b334a45a8ff 112 US_TIMER->term_cnt32 = 0xFFFFFFFF; // reset to max value to prevent further interrupts
bogdanm 0:9b334a45a8ff 113 US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear interrupt
bogdanm 0:9b334a45a8ff 114 NVIC_ClearPendingIRQ(US_TIMER_IRQn);
bogdanm 0:9b334a45a8ff 115 event_cnt = 0xFFFFFFFFFFFFFFFFULL; // reset to max value
bogdanm 0:9b334a45a8ff 116 us_ticker_irq_handler();
bogdanm 0:9b334a45a8ff 117 }
bogdanm 0:9b334a45a8ff 118 }
bogdanm 0:9b334a45a8ff 119 }
bogdanm 0:9b334a45a8ff 120 }
bogdanm 0:9b334a45a8ff 121
bogdanm 0:9b334a45a8ff 122 //******************************************************************************
bogdanm 0:9b334a45a8ff 123 void us_ticker_init(void)
bogdanm 0:9b334a45a8ff 124 {
bogdanm 0:9b334a45a8ff 125 if (us_ticker_inited)
bogdanm 0:9b334a45a8ff 126 return;
bogdanm 0:9b334a45a8ff 127 us_ticker_inited = 1;
bogdanm 0:9b334a45a8ff 128
bogdanm 0:9b334a45a8ff 129 current_cnt = 0;
bogdanm 0:9b334a45a8ff 130 event_cnt = 0xFFFFFFFFFFFFFFFFULL; // reset to max value
bogdanm 0:9b334a45a8ff 131
bogdanm 0:9b334a45a8ff 132 if (SystemCoreClock <= 1000000) {
bogdanm 0:9b334a45a8ff 133 error("us_ticker cannot operate at this SystemCoreClock");
bogdanm 0:9b334a45a8ff 134 return;
bogdanm 0:9b334a45a8ff 135 }
bogdanm 0:9b334a45a8ff 136
bogdanm 0:9b334a45a8ff 137 // Configure timer for 32-bit continuous mode with /1 prescaler
bogdanm 0:9b334a45a8ff 138 US_TIMER->ctrl = MXC_E_TMR_MODE_CONTINUOUS << MXC_F_TMR_CTRL_MODE_POS | (0 << MXC_F_TMR_CTRL_PRESCALE_POS);
bogdanm 0:9b334a45a8ff 139 ticks_per_us = SystemCoreClock / 1000000;
bogdanm 0:9b334a45a8ff 140
bogdanm 0:9b334a45a8ff 141 // Set the tick window to 10ms
bogdanm 0:9b334a45a8ff 142 tick_win = SystemCoreClock/100;
bogdanm 0:9b334a45a8ff 143
bogdanm 0:9b334a45a8ff 144 // Set timer overflow to the max
bogdanm 0:9b334a45a8ff 145 US_TIMER->term_cnt32 = 0xFFFFFFFF;
bogdanm 0:9b334a45a8ff 146 US_TIMER->pwm_cap32 = 0xFFFFFFFF;
bogdanm 0:9b334a45a8ff 147 US_TIMER->count32 = 0;
bogdanm 0:9b334a45a8ff 148
bogdanm 0:9b334a45a8ff 149 US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear pending interrupts
bogdanm 0:9b334a45a8ff 150
bogdanm 0:9b334a45a8ff 151 NVIC_SetVector(US_TIMER_IRQn, (uint32_t)tmr_handler);
bogdanm 0:9b334a45a8ff 152 NVIC_EnableIRQ(US_TIMER_IRQn);
bogdanm 0:9b334a45a8ff 153
bogdanm 0:9b334a45a8ff 154 US_TIMER->inten |= MXC_F_TMR_INTEN_TIMER0; // enable interrupts
bogdanm 0:9b334a45a8ff 155 US_TIMER->ctrl |= MXC_F_TMR_CTRL_ENABLE0; // enable timer
bogdanm 0:9b334a45a8ff 156 }
bogdanm 0:9b334a45a8ff 157
bogdanm 0:9b334a45a8ff 158 //******************************************************************************
bogdanm 0:9b334a45a8ff 159 void us_ticker_deinit(void)
bogdanm 0:9b334a45a8ff 160 {
bogdanm 0:9b334a45a8ff 161 US_TIMER->ctrl = 0; // disable timer
bogdanm 0:9b334a45a8ff 162 US_TIMER->inten = 0; // disable interrupts
bogdanm 0:9b334a45a8ff 163 US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear interrupts
bogdanm 0:9b334a45a8ff 164 us_ticker_inited = 0;
bogdanm 0:9b334a45a8ff 165 }
bogdanm 0:9b334a45a8ff 166
bogdanm 0:9b334a45a8ff 167 //******************************************************************************
bogdanm 0:9b334a45a8ff 168 uint32_t us_ticker_read(void)
bogdanm 0:9b334a45a8ff 169 {
bogdanm 0:9b334a45a8ff 170 uint64_t current_cnt1, current_cnt2;
bogdanm 0:9b334a45a8ff 171 uint32_t term_cnt, tmr_cnt;
bogdanm 0:9b334a45a8ff 172 uint32_t intfl1, intfl2;
bogdanm 0:9b334a45a8ff 173
bogdanm 0:9b334a45a8ff 174 if (!us_ticker_inited)
bogdanm 0:9b334a45a8ff 175 us_ticker_init();
bogdanm 0:9b334a45a8ff 176
bogdanm 0:9b334a45a8ff 177 // Ensure coherency between current_cnt and US_TIMER->count32
bogdanm 0:9b334a45a8ff 178 do {
bogdanm 0:9b334a45a8ff 179 current_cnt1 = current_cnt;
bogdanm 0:9b334a45a8ff 180 intfl1 = US_TIMER->intfl;
bogdanm 0:9b334a45a8ff 181 term_cnt = US_TIMER->term_cnt32;
bogdanm 0:9b334a45a8ff 182 tmr_cnt = US_TIMER->count32;
bogdanm 0:9b334a45a8ff 183 intfl2 = US_TIMER->intfl;
bogdanm 0:9b334a45a8ff 184 current_cnt2 = current_cnt;
bogdanm 0:9b334a45a8ff 185 } while ((current_cnt1 != current_cnt2) || (intfl1 != intfl2));
bogdanm 0:9b334a45a8ff 186
bogdanm 0:9b334a45a8ff 187 // Account for an unserviced interrupt
bogdanm 0:9b334a45a8ff 188 if (intfl1) {
bogdanm 0:9b334a45a8ff 189 current_cnt1 += term_cnt;
bogdanm 0:9b334a45a8ff 190 }
bogdanm 0:9b334a45a8ff 191
bogdanm 0:9b334a45a8ff 192 current_cnt1 += tmr_cnt;
bogdanm 0:9b334a45a8ff 193
bogdanm 0:9b334a45a8ff 194 return (current_cnt1 / ticks_per_us);
bogdanm 0:9b334a45a8ff 195 }
bogdanm 0:9b334a45a8ff 196
bogdanm 0:9b334a45a8ff 197 //******************************************************************************
bogdanm 0:9b334a45a8ff 198 void us_ticker_set_interrupt(timestamp_t timestamp)
bogdanm 0:9b334a45a8ff 199 {
bogdanm 0:9b334a45a8ff 200 // Note: interrupts are disabled before this function is called.
bogdanm 0:9b334a45a8ff 201
bogdanm 0:9b334a45a8ff 202 US_TIMER->ctrl &= ~MXC_F_TMR_CTRL_ENABLE0; // disable timer
bogdanm 0:9b334a45a8ff 203
bogdanm 0:9b334a45a8ff 204 if (US_TIMER->intfl) {
bogdanm 0:9b334a45a8ff 205 US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear interrupt
bogdanm 0:9b334a45a8ff 206 NVIC_ClearPendingIRQ(US_TIMER_IRQn);
bogdanm 0:9b334a45a8ff 207 inc_current_cnt(US_TIMER->term_cnt32);
bogdanm 0:9b334a45a8ff 208 }
bogdanm 0:9b334a45a8ff 209
bogdanm 0:9b334a45a8ff 210 // add and reset the current count value
bogdanm 0:9b334a45a8ff 211 inc_current_cnt(US_TIMER->count32);
bogdanm 0:9b334a45a8ff 212 US_TIMER->count32 = 0;
bogdanm 0:9b334a45a8ff 213
bogdanm 0:9b334a45a8ff 214 // add the number of cycles that the timer is disabled here for
bogdanm 0:9b334a45a8ff 215 inc_current_cnt(200);
bogdanm 0:9b334a45a8ff 216
bogdanm 0:9b334a45a8ff 217 event_cnt = (uint64_t)timestamp * ticks_per_us;
bogdanm 0:9b334a45a8ff 218
bogdanm 0:9b334a45a8ff 219 // Check to see if the event has already passed
bogdanm 0:9b334a45a8ff 220 if (!event_passed(current_cnt, event_cnt)) {
bogdanm 0:9b334a45a8ff 221 uint64_t diff = event_diff(current_cnt, event_cnt);
bogdanm 0:9b334a45a8ff 222 if (diff < (uint64_t)0xFFFFFFFF) {
bogdanm 0:9b334a45a8ff 223 // the event occurs before the next overflow
bogdanm 0:9b334a45a8ff 224 US_TIMER->term_cnt32 = diff;
bogdanm 0:9b334a45a8ff 225 } else {
bogdanm 0:9b334a45a8ff 226 // the event occurs after the next overflow
bogdanm 0:9b334a45a8ff 227 US_TIMER->term_cnt32 = 0xFFFFFFFF; // set to max
bogdanm 0:9b334a45a8ff 228 }
bogdanm 0:9b334a45a8ff 229 } else {
bogdanm 0:9b334a45a8ff 230 // the requested timestamp occurs in the past
bogdanm 0:9b334a45a8ff 231 // set the timer up to immediately expire
bogdanm 0:9b334a45a8ff 232 US_TIMER->term_cnt32 = 1;
bogdanm 0:9b334a45a8ff 233 }
bogdanm 0:9b334a45a8ff 234 US_TIMER->ctrl |= MXC_F_TMR_CTRL_ENABLE0; // enable timer
bogdanm 0:9b334a45a8ff 235 }
bogdanm 0:9b334a45a8ff 236
bogdanm 0:9b334a45a8ff 237 //******************************************************************************
bogdanm 0:9b334a45a8ff 238 void us_ticker_disable_interrupt(void)
bogdanm 0:9b334a45a8ff 239 {
bogdanm 0:9b334a45a8ff 240 // There are no more events, set timer overflow to the max
bogdanm 0:9b334a45a8ff 241 US_TIMER->term_cnt32 = 0xFFFFFFFF;
bogdanm 0:9b334a45a8ff 242 }
bogdanm 0:9b334a45a8ff 243
bogdanm 0:9b334a45a8ff 244 //******************************************************************************
bogdanm 0:9b334a45a8ff 245 void us_ticker_clear_interrupt(void)
bogdanm 0:9b334a45a8ff 246 {
bogdanm 0:9b334a45a8ff 247 // cleared in the local handler
bogdanm 0:9b334a45a8ff 248 }
bogdanm 0:9b334a45a8ff 249
bogdanm 0:9b334a45a8ff 250 //******************************************************************************
bogdanm 0:9b334a45a8ff 251 void us_ticker_set(timestamp_t timestamp)
bogdanm 0:9b334a45a8ff 252 {
bogdanm 0:9b334a45a8ff 253 US_TIMER->ctrl &= ~MXC_F_TMR_CTRL_ENABLE0; // disable timer
bogdanm 0:9b334a45a8ff 254 current_cnt = (uint64_t)timestamp * ticks_per_us;
bogdanm 0:9b334a45a8ff 255 US_TIMER->count32 = 0;
bogdanm 0:9b334a45a8ff 256 US_TIMER->term_cnt32 = 0xFFFFFFFF;
bogdanm 0:9b334a45a8ff 257 US_TIMER->ctrl |= MXC_F_TMR_CTRL_ENABLE0; // enable timer
bogdanm 0:9b334a45a8ff 258
bogdanm 0:9b334a45a8ff 259 if (((uint64_t)timestamp * ticks_per_us) >= event_cnt) {
bogdanm 0:9b334a45a8ff 260 // The next timestamp has elapsed. Trigger the interrupt to handle it.
bogdanm 0:9b334a45a8ff 261 NVIC_SetPendingIRQ(US_TIMER_IRQn);
bogdanm 0:9b334a45a8ff 262 }
bogdanm 0:9b334a45a8ff 263 }