Lancaster University / mbed-src

Fork of mbed-src by mbed official

Committer:
LancasterUniversity
Date:
Wed Jul 13 12:52:54 2016 +0100
Revision:
641:be9b2017785a
Parent:
554:edd95c0879f8
Synchronized with git rev 1fb8ab4c
Author: James Devine
mbed-classic: BUGFIX for timer when using wait_ms from interrupt context

Previously if a user used wait[_ms,_us] in interrupt context the device would
hang indefinitely. This was due to incrementing overflowCount from
interrupt context only.

This meant that if a user used wait[_ms,_us] in an ISR with
the same or greater interrupt priority, it would result in an infinite
loop as the overflowCount variable would never be incremented, and
wait[_ms,_us] would never return.

This patch simply applies a better solution for the race condition
mentioned in the previous commit. It instead disables the timer1
interrupt and increments the overflowCount variable, preventing
the race condition whilst supporting wait[_ms,_us] in interrupt
context.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 554:edd95c0879f8 1 /* mbed Microcontroller Library
mbed_official 554:edd95c0879f8 2 * Copyright (c) 2006-2015 ARM Limited
mbed_official 554:edd95c0879f8 3 *
mbed_official 554:edd95c0879f8 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 554:edd95c0879f8 5 * you may not use this file except in compliance with the License.
mbed_official 554:edd95c0879f8 6 * You may obtain a copy of the License at
mbed_official 554:edd95c0879f8 7 *
mbed_official 554:edd95c0879f8 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 554:edd95c0879f8 9 *
mbed_official 554:edd95c0879f8 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 554:edd95c0879f8 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 554:edd95c0879f8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 554:edd95c0879f8 13 * See the License for the specific language governing permissions and
mbed_official 554:edd95c0879f8 14 * limitations under the License.
mbed_official 554:edd95c0879f8 15 */
mbed_official 554:edd95c0879f8 16 #include <stddef.h>
mbed_official 554:edd95c0879f8 17 #include "us_ticker_api.h"
mbed_official 554:edd95c0879f8 18 #include "PeripheralNames.h"
mbed_official 554:edd95c0879f8 19
mbed_official 554:edd95c0879f8 20 #define US_TICKER_TIMER ((LPC_TIM_TypeDef *)LPC_TIM3_BASE)
mbed_official 554:edd95c0879f8 21 #define US_TICKER_TIMER_IRQn TIMER3_IRQn
mbed_official 554:edd95c0879f8 22
mbed_official 554:edd95c0879f8 23 int us_ticker_inited = 0;
mbed_official 554:edd95c0879f8 24
mbed_official 554:edd95c0879f8 25 void us_ticker_init(void) {
mbed_official 554:edd95c0879f8 26 if (us_ticker_inited) return;
mbed_official 554:edd95c0879f8 27 us_ticker_inited = 1;
mbed_official 554:edd95c0879f8 28
mbed_official 554:edd95c0879f8 29 LPC_SC->PCONP |= 1 << PCTIM3; // Clock TIMER_3
mbed_official 554:edd95c0879f8 30
mbed_official 554:edd95c0879f8 31 US_TICKER_TIMER->CTCR = 0x0; // timer mode
mbed_official 554:edd95c0879f8 32 uint32_t PCLK = SystemCoreClock / 4;
mbed_official 554:edd95c0879f8 33
mbed_official 554:edd95c0879f8 34 US_TICKER_TIMER->TCR = 0x2; // reset
mbed_official 554:edd95c0879f8 35
mbed_official 554:edd95c0879f8 36 uint32_t prescale = PCLK / 1000000; // default to 1MHz (1 us ticks)
mbed_official 554:edd95c0879f8 37 US_TICKER_TIMER->PR = prescale - 1;
mbed_official 554:edd95c0879f8 38 US_TICKER_TIMER->TCR = 1; // enable = 1, reset = 0
mbed_official 554:edd95c0879f8 39
mbed_official 554:edd95c0879f8 40 NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
mbed_official 554:edd95c0879f8 41 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
mbed_official 554:edd95c0879f8 42 }
mbed_official 554:edd95c0879f8 43
mbed_official 554:edd95c0879f8 44 uint32_t us_ticker_read() {
mbed_official 554:edd95c0879f8 45 if (!us_ticker_inited)
mbed_official 554:edd95c0879f8 46 us_ticker_init();
mbed_official 554:edd95c0879f8 47
mbed_official 554:edd95c0879f8 48 return US_TICKER_TIMER->TC;
mbed_official 554:edd95c0879f8 49 }
mbed_official 554:edd95c0879f8 50
mbed_official 554:edd95c0879f8 51 void us_ticker_set_interrupt(timestamp_t timestamp) {
mbed_official 554:edd95c0879f8 52 // set match value
mbed_official 554:edd95c0879f8 53 US_TICKER_TIMER->MR0 = (uint32_t)timestamp;
mbed_official 554:edd95c0879f8 54 // enable match interrupt
mbed_official 554:edd95c0879f8 55 US_TICKER_TIMER->MCR |= 1;
mbed_official 554:edd95c0879f8 56 }
mbed_official 554:edd95c0879f8 57
mbed_official 554:edd95c0879f8 58 void us_ticker_disable_interrupt(void) {
mbed_official 554:edd95c0879f8 59 US_TICKER_TIMER->MCR &= ~1;
mbed_official 554:edd95c0879f8 60 }
mbed_official 554:edd95c0879f8 61
mbed_official 554:edd95c0879f8 62 void us_ticker_clear_interrupt(void) {
mbed_official 554:edd95c0879f8 63 US_TICKER_TIMER->IR = 1;
mbed_official 554:edd95c0879f8 64 }