mbed w/ spi bug fig
Fork of mbed-src by
targets/hal/TARGET_NORDIC/TARGET_NRF51822/us_ticker.c@224:e7c230c6cb31, 2014-06-11 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jun 11 08:45:06 2014 +0100
- Revision:
- 224:e7c230c6cb31
- Parent:
- 127:ce7cebc0511f
Synchronized with git revision aba739c87ab2f5818b5d93891d83ff87538d55e5
Full URL: https://github.com/mbedmicro/mbed/commit/aba739c87ab2f5818b5d93891d83ff87538d55e5/
[NRF51822] Re-init NRF Timer if powered down
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 85:e1a8e879a6a9 | 1 | /* mbed Microcontroller Library |
mbed_official | 104:a6a92e2e5a92 | 2 | * Copyright (c) 2013 Nordic Semiconductor |
mbed_official | 85:e1a8e879a6a9 | 3 | * |
mbed_official | 85:e1a8e879a6a9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 85:e1a8e879a6a9 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 85:e1a8e879a6a9 | 6 | * You may obtain a copy of the License at |
mbed_official | 85:e1a8e879a6a9 | 7 | * |
mbed_official | 85:e1a8e879a6a9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 85:e1a8e879a6a9 | 9 | * |
mbed_official | 85:e1a8e879a6a9 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 85:e1a8e879a6a9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 85:e1a8e879a6a9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 85:e1a8e879a6a9 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 85:e1a8e879a6a9 | 14 | * limitations under the License. |
mbed_official | 85:e1a8e879a6a9 | 15 | */ |
mbed_official | 85:e1a8e879a6a9 | 16 | #include <stddef.h> |
mbed_official | 85:e1a8e879a6a9 | 17 | #include "us_ticker_api.h" |
mbed_official | 85:e1a8e879a6a9 | 18 | #include "cmsis.h" |
mbed_official | 85:e1a8e879a6a9 | 19 | #include "PeripheralNames.h" |
mbed_official | 85:e1a8e879a6a9 | 20 | |
mbed_official | 85:e1a8e879a6a9 | 21 | #define US_TICKER_TIMER NRF_TIMER1 |
mbed_official | 85:e1a8e879a6a9 | 22 | #define US_TICKER_TIMER_IRQn TIMER1_IRQn |
mbed_official | 85:e1a8e879a6a9 | 23 | |
mbed_official | 85:e1a8e879a6a9 | 24 | int us_ticker_inited = 0; |
mbed_official | 85:e1a8e879a6a9 | 25 | volatile uint16_t overflow=0; //overflow value that forms the upper 16 bits of the counter |
mbed_official | 85:e1a8e879a6a9 | 26 | volatile uint16_t timeStamp=0; |
mbed_official | 85:e1a8e879a6a9 | 27 | |
mbed_official | 85:e1a8e879a6a9 | 28 | #ifdef __cplusplus |
mbed_official | 85:e1a8e879a6a9 | 29 | extern "C" { |
mbed_official | 85:e1a8e879a6a9 | 30 | #endif |
mbed_official | 127:ce7cebc0511f | 31 | void TIMER1_IRQHandler(void){ |
mbed_official | 127:ce7cebc0511f | 32 | |
mbed_official | 127:ce7cebc0511f | 33 | if ((US_TICKER_TIMER->EVENTS_COMPARE[1] != 0) && |
mbed_official | 85:e1a8e879a6a9 | 34 | ((US_TICKER_TIMER->INTENSET & TIMER_INTENSET_COMPARE1_Msk) != 0)) |
mbed_official | 85:e1a8e879a6a9 | 35 | { |
mbed_official | 85:e1a8e879a6a9 | 36 | US_TICKER_TIMER->EVENTS_COMPARE[1] = 0; |
mbed_official | 85:e1a8e879a6a9 | 37 | overflow++; |
mbed_official | 85:e1a8e879a6a9 | 38 | US_TICKER_TIMER->CC[1] =0xFFFF; |
mbed_official | 85:e1a8e879a6a9 | 39 | if(timeStamp>0) |
mbed_official | 85:e1a8e879a6a9 | 40 | { |
mbed_official | 85:e1a8e879a6a9 | 41 | timeStamp--; |
mbed_official | 85:e1a8e879a6a9 | 42 | if(timeStamp==0) |
mbed_official | 85:e1a8e879a6a9 | 43 | { |
mbed_official | 85:e1a8e879a6a9 | 44 | us_ticker_clear_interrupt(); |
mbed_official | 85:e1a8e879a6a9 | 45 | us_ticker_disable_interrupt(); |
mbed_official | 85:e1a8e879a6a9 | 46 | us_ticker_irq_handler(); |
mbed_official | 85:e1a8e879a6a9 | 47 | return; |
mbed_official | 85:e1a8e879a6a9 | 48 | } |
mbed_official | 85:e1a8e879a6a9 | 49 | } |
mbed_official | 85:e1a8e879a6a9 | 50 | } |
mbed_official | 85:e1a8e879a6a9 | 51 | if ((US_TICKER_TIMER->EVENTS_COMPARE[0] != 0) && |
mbed_official | 85:e1a8e879a6a9 | 52 | ((US_TICKER_TIMER->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0)) |
mbed_official | 85:e1a8e879a6a9 | 53 | { |
mbed_official | 85:e1a8e879a6a9 | 54 | us_ticker_clear_interrupt(); |
mbed_official | 85:e1a8e879a6a9 | 55 | us_ticker_disable_interrupt(); |
mbed_official | 85:e1a8e879a6a9 | 56 | if(timeStamp==0) |
mbed_official | 85:e1a8e879a6a9 | 57 | us_ticker_irq_handler(); |
mbed_official | 85:e1a8e879a6a9 | 58 | } |
mbed_official | 85:e1a8e879a6a9 | 59 | |
mbed_official | 85:e1a8e879a6a9 | 60 | } |
mbed_official | 85:e1a8e879a6a9 | 61 | #ifdef __cplusplus |
mbed_official | 85:e1a8e879a6a9 | 62 | } |
mbed_official | 85:e1a8e879a6a9 | 63 | #endif |
mbed_official | 85:e1a8e879a6a9 | 64 | void us_ticker_init(void){ |
mbed_official | 224:e7c230c6cb31 | 65 | if (us_ticker_inited && US_TICKER_TIMER->POWER){ |
mbed_official | 85:e1a8e879a6a9 | 66 | return; |
mbed_official | 85:e1a8e879a6a9 | 67 | } |
mbed_official | 85:e1a8e879a6a9 | 68 | |
mbed_official | 85:e1a8e879a6a9 | 69 | us_ticker_inited = 1; |
mbed_official | 85:e1a8e879a6a9 | 70 | |
mbed_official | 85:e1a8e879a6a9 | 71 | US_TICKER_TIMER->POWER = 0; |
mbed_official | 85:e1a8e879a6a9 | 72 | US_TICKER_TIMER->POWER = 1; |
mbed_official | 85:e1a8e879a6a9 | 73 | |
mbed_official | 85:e1a8e879a6a9 | 74 | US_TICKER_TIMER->MODE = TIMER_MODE_MODE_Timer; |
mbed_official | 85:e1a8e879a6a9 | 75 | |
mbed_official | 85:e1a8e879a6a9 | 76 | US_TICKER_TIMER->PRESCALER = 4; |
mbed_official | 85:e1a8e879a6a9 | 77 | US_TICKER_TIMER->BITMODE = TIMER_BITMODE_BITMODE_16Bit; |
mbed_official | 224:e7c230c6cb31 | 78 | US_TICKER_TIMER->TASKS_CLEAR =1; |
mbed_official | 224:e7c230c6cb31 | 79 | US_TICKER_TIMER->CC[1] = 0xFFFF; |
mbed_official | 224:e7c230c6cb31 | 80 | US_TICKER_TIMER->INTENSET = TIMER_INTENSET_COMPARE1_Set << TIMER_INTENSET_COMPARE1_Pos; |
mbed_official | 85:e1a8e879a6a9 | 81 | |
mbed_official | 224:e7c230c6cb31 | 82 | NVIC_SetPriority(US_TICKER_TIMER_IRQn, 3); |
mbed_official | 85:e1a8e879a6a9 | 83 | NVIC_EnableIRQ(US_TICKER_TIMER_IRQn); |
mbed_official | 85:e1a8e879a6a9 | 84 | |
mbed_official | 85:e1a8e879a6a9 | 85 | US_TICKER_TIMER->TASKS_START = 0x01; |
mbed_official | 85:e1a8e879a6a9 | 86 | } |
mbed_official | 85:e1a8e879a6a9 | 87 | |
mbed_official | 85:e1a8e879a6a9 | 88 | uint32_t us_ticker_read(){ |
mbed_official | 224:e7c230c6cb31 | 89 | if (!us_ticker_inited || US_TICKER_TIMER->POWER==0){ |
mbed_official | 85:e1a8e879a6a9 | 90 | us_ticker_init(); |
mbed_official | 85:e1a8e879a6a9 | 91 | } |
mbed_official | 85:e1a8e879a6a9 | 92 | |
mbed_official | 85:e1a8e879a6a9 | 93 | uint16_t bufferedOverFlow = overflow; |
mbed_official | 85:e1a8e879a6a9 | 94 | US_TICKER_TIMER->TASKS_CAPTURE[2] = 1; |
mbed_official | 85:e1a8e879a6a9 | 95 | |
mbed_official | 85:e1a8e879a6a9 | 96 | if(overflow!=bufferedOverFlow){ |
mbed_official | 85:e1a8e879a6a9 | 97 | bufferedOverFlow = overflow; |
mbed_official | 85:e1a8e879a6a9 | 98 | US_TICKER_TIMER->TASKS_CAPTURE[2] = 1; |
mbed_official | 85:e1a8e879a6a9 | 99 | } |
mbed_official | 85:e1a8e879a6a9 | 100 | return (((uint32_t)bufferedOverFlow<<16) | US_TICKER_TIMER->CC[2]); |
mbed_official | 85:e1a8e879a6a9 | 101 | } |
mbed_official | 85:e1a8e879a6a9 | 102 | |
mbed_official | 85:e1a8e879a6a9 | 103 | void us_ticker_set_interrupt(unsigned int timestamp){ |
mbed_official | 224:e7c230c6cb31 | 104 | if (!us_ticker_inited || US_TICKER_TIMER->POWER==0) |
mbed_official | 85:e1a8e879a6a9 | 105 | { |
mbed_official | 85:e1a8e879a6a9 | 106 | us_ticker_init(); |
mbed_official | 85:e1a8e879a6a9 | 107 | } |
mbed_official | 85:e1a8e879a6a9 | 108 | |
mbed_official | 85:e1a8e879a6a9 | 109 | US_TICKER_TIMER->TASKS_CAPTURE[0] = 1; |
mbed_official | 85:e1a8e879a6a9 | 110 | uint16_t tsUpper16 = (uint16_t)((timestamp-us_ticker_read())>>16); |
mbed_official | 85:e1a8e879a6a9 | 111 | if(tsUpper16>0){ |
mbed_official | 85:e1a8e879a6a9 | 112 | if(timeStamp ==0 || timeStamp> tsUpper16){ |
mbed_official | 85:e1a8e879a6a9 | 113 | timeStamp = tsUpper16; |
mbed_official | 85:e1a8e879a6a9 | 114 | } |
mbed_official | 85:e1a8e879a6a9 | 115 | } |
mbed_official | 85:e1a8e879a6a9 | 116 | else{ |
mbed_official | 85:e1a8e879a6a9 | 117 | US_TICKER_TIMER->INTENSET |= TIMER_INTENSET_COMPARE0_Set << TIMER_INTENSET_COMPARE0_Pos; |
mbed_official | 85:e1a8e879a6a9 | 118 | US_TICKER_TIMER->CC[0] += timestamp-us_ticker_read(); |
mbed_official | 85:e1a8e879a6a9 | 119 | } |
mbed_official | 85:e1a8e879a6a9 | 120 | } |
mbed_official | 85:e1a8e879a6a9 | 121 | |
mbed_official | 85:e1a8e879a6a9 | 122 | void us_ticker_disable_interrupt(void){ |
mbed_official | 85:e1a8e879a6a9 | 123 | US_TICKER_TIMER->INTENCLR = TIMER_INTENCLR_COMPARE0_Clear << TIMER_INTENCLR_COMPARE0_Pos; |
mbed_official | 85:e1a8e879a6a9 | 124 | } |
mbed_official | 85:e1a8e879a6a9 | 125 | void us_ticker_clear_interrupt(void){ |
mbed_official | 85:e1a8e879a6a9 | 126 | US_TICKER_TIMER->EVENTS_COMPARE[0] = 0; |
mbed_official | 85:e1a8e879a6a9 | 127 | } |