t
Fork of mbed-dev by
targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_us_ticker_api.c@169:e3b6fe271b81, 2017-07-19 (annotated)
- Committer:
- Kojto
- Date:
- Wed Jul 19 17:31:21 2017 +0100
- Revision:
- 169:e3b6fe271b81
- Parent:
- 167:e84263d55307
- Child:
- 174:b96e65c34a4d
This updates the lib to the mbed lib v 147
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /** |
<> | 149:156823d33999 | 2 | ****************************************************************************** |
<> | 149:156823d33999 | 3 | * @file us_ticker_api.h |
<> | 149:156823d33999 | 4 | * @brief Implementation of a Timer driver |
<> | 149:156823d33999 | 5 | * @internal |
<> | 149:156823d33999 | 6 | * @author ON Semiconductor |
<> | 149:156823d33999 | 7 | * $Rev: $ |
<> | 149:156823d33999 | 8 | * $Date: 2015-11-15 $ |
<> | 149:156823d33999 | 9 | ****************************************************************************** |
<> | 149:156823d33999 | 10 | * Copyright 2016 Semiconductor Components Industries LLC (d/b/a ON Semiconductor). |
<> | 149:156823d33999 | 11 | * All rights reserved. This software and/or documentation is licensed by ON Semiconductor |
<> | 149:156823d33999 | 12 | * under limited terms and conditions. The terms and conditions pertaining to the software |
<> | 149:156823d33999 | 13 | * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf |
<> | 149:156823d33999 | 14 | * (ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software) and |
<> | 149:156823d33999 | 15 | * if applicable the software license agreement. Do not use this software and/or |
<> | 149:156823d33999 | 16 | * documentation unless you have carefully read and you agree to the limited terms and |
<> | 149:156823d33999 | 17 | * conditions. By using this software and/or documentation, you agree to the limited |
<> | 149:156823d33999 | 18 | * terms and conditions. |
<> | 149:156823d33999 | 19 | * |
<> | 149:156823d33999 | 20 | * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED |
<> | 149:156823d33999 | 21 | * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF |
<> | 149:156823d33999 | 22 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. |
<> | 149:156823d33999 | 23 | * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, |
<> | 149:156823d33999 | 24 | * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
<> | 149:156823d33999 | 25 | * @endinternal |
<> | 149:156823d33999 | 26 | * |
<> | 149:156823d33999 | 27 | * @ingroup timer |
<> | 149:156823d33999 | 28 | */ |
<> | 149:156823d33999 | 29 | |
<> | 149:156823d33999 | 30 | #include <stddef.h> |
AnnaBridge | 167:e84263d55307 | 31 | #include "timer_ncs36510.h" |
<> | 149:156823d33999 | 32 | |
<> | 149:156823d33999 | 33 | #define US_TIMER TIMER0 |
<> | 149:156823d33999 | 34 | #define US_TICKER TIMER1 |
<> | 149:156823d33999 | 35 | |
<> | 149:156823d33999 | 36 | static int us_ticker_inited = 0; |
<> | 149:156823d33999 | 37 | |
<> | 149:156823d33999 | 38 | static void us_timer_init(void); |
<> | 149:156823d33999 | 39 | |
<> | 159:612c381a210f | 40 | static uint32_t us_ticker_target = 0; |
Kojto | 169:e3b6fe271b81 | 41 | static volatile uint16_t msb_counter = 0; |
<> | 149:156823d33999 | 42 | |
<> | 149:156823d33999 | 43 | void us_ticker_init(void) |
<> | 149:156823d33999 | 44 | { |
<> | 149:156823d33999 | 45 | if (!us_ticker_inited) { |
<> | 149:156823d33999 | 46 | us_timer_init(); |
<> | 149:156823d33999 | 47 | } |
<> | 149:156823d33999 | 48 | } |
<> | 149:156823d33999 | 49 | |
<> | 149:156823d33999 | 50 | /******************************************************************************* |
<> | 149:156823d33999 | 51 | * Timer for us timing reference |
<> | 149:156823d33999 | 52 | * |
<> | 149:156823d33999 | 53 | * Uptime counter for scheduling reference. It uses TIMER0. |
<> | 149:156823d33999 | 54 | * The NCS36510 does not have a 32 bit timer nor the option to chain timers, |
<> | 149:156823d33999 | 55 | * which is why a software timer is required to get 32-bit word length. |
<> | 149:156823d33999 | 56 | ******************************************************************************/ |
<> | 149:156823d33999 | 57 | /* TODO - Need some sort of load value/prescale calculation for non-32MHz clock */ |
<> | 149:156823d33999 | 58 | /* TODO - How is overflow handled? */ |
<> | 149:156823d33999 | 59 | |
<> | 149:156823d33999 | 60 | /* Timer 0 for free running time */ |
<> | 149:156823d33999 | 61 | extern void us_timer_isr(void) |
<> | 149:156823d33999 | 62 | { |
<> | 149:156823d33999 | 63 | TIM0REG->CLEAR = 0; |
<> | 149:156823d33999 | 64 | msb_counter++; |
<> | 149:156823d33999 | 65 | } |
<> | 149:156823d33999 | 66 | |
<> | 149:156823d33999 | 67 | /* Initializing TIMER 0(TImer) and TIMER 1(Ticker) */ |
<> | 149:156823d33999 | 68 | static void us_timer_init(void) |
<> | 149:156823d33999 | 69 | { |
<> | 149:156823d33999 | 70 | /* Enable the timer0 periphery clock */ |
<> | 149:156823d33999 | 71 | CLOCK_ENABLE(CLOCK_TIMER0); |
<> | 149:156823d33999 | 72 | /* Enable the timer0 periphery clock */ |
<> | 149:156823d33999 | 73 | CLOCK_ENABLE(CLOCK_TIMER1); |
<> | 149:156823d33999 | 74 | |
<> | 149:156823d33999 | 75 | /* Timer init */ |
<> | 149:156823d33999 | 76 | /* load timer value */ |
<> | 149:156823d33999 | 77 | TIM0REG->LOAD = 0xFFFF; |
<> | 149:156823d33999 | 78 | |
<> | 149:156823d33999 | 79 | /* set timer prescale 32 (1 us), mode & enable */ |
<> | 149:156823d33999 | 80 | TIM0REG->CONTROL.WORD = ((CLK_DIVIDER_32 << TIMER_PRESCALE_BIT_POS) | |
<> | 149:156823d33999 | 81 | (TIME_MODE_PERIODIC << TIMER_MODE_BIT_POS) | |
<> | 149:156823d33999 | 82 | (TIMER_ENABLE_BIT << TIMER_ENABLE_BIT_POS)); |
<> | 149:156823d33999 | 83 | |
<> | 149:156823d33999 | 84 | /* Ticker init */ |
<> | 149:156823d33999 | 85 | /* load timer value */ |
<> | 149:156823d33999 | 86 | TIM1REG->LOAD = 0xFFFF; |
<> | 149:156823d33999 | 87 | |
<> | 149:156823d33999 | 88 | /* set timer prescale 32 (1 us), mode & enable */ |
<> | 149:156823d33999 | 89 | TIM1REG->CONTROL.WORD = ((CLK_DIVIDER_32 << TIMER_PRESCALE_BIT_POS) | |
<> | 149:156823d33999 | 90 | (TIME_MODE_PERIODIC << TIMER_MODE_BIT_POS)); |
<> | 149:156823d33999 | 91 | |
<> | 149:156823d33999 | 92 | /* Register & enable interrupt associated with the timer */ |
<> | 149:156823d33999 | 93 | NVIC_SetVector(Tim0_IRQn,(uint32_t)us_timer_isr); |
<> | 149:156823d33999 | 94 | NVIC_SetVector(Tim1_IRQn,(uint32_t)us_ticker_isr); |
<> | 149:156823d33999 | 95 | |
<> | 149:156823d33999 | 96 | /* Clear pending irqs */ |
<> | 149:156823d33999 | 97 | NVIC_ClearPendingIRQ(Tim0_IRQn); |
<> | 149:156823d33999 | 98 | NVIC_ClearPendingIRQ(Tim1_IRQn); |
<> | 149:156823d33999 | 99 | |
<> | 149:156823d33999 | 100 | /* Setup NVIC for timer */ |
<> | 149:156823d33999 | 101 | NVIC_EnableIRQ(Tim0_IRQn); |
<> | 149:156823d33999 | 102 | NVIC_EnableIRQ(Tim1_IRQn); |
<> | 149:156823d33999 | 103 | |
<> | 149:156823d33999 | 104 | us_ticker_inited = 1; |
<> | 149:156823d33999 | 105 | } |
<> | 149:156823d33999 | 106 | |
<> | 149:156823d33999 | 107 | /* Reads 32 bit timer's current value (16 bit s/w timer | 16 bit h/w timer) */ |
<> | 149:156823d33999 | 108 | uint32_t us_ticker_read() |
<> | 149:156823d33999 | 109 | { |
<> | 149:156823d33999 | 110 | |
<> | 149:156823d33999 | 111 | if (!us_ticker_inited) { |
<> | 149:156823d33999 | 112 | us_timer_init(); |
<> | 149:156823d33999 | 113 | } |
<> | 149:156823d33999 | 114 | |
Kojto | 169:e3b6fe271b81 | 115 | NVIC_DisableIRQ(Tim0_IRQn); |
Kojto | 169:e3b6fe271b81 | 116 | uint32_t retval, tim0cval; |
<> | 149:156823d33999 | 117 | /* Get the current tick from the hw and sw timers */ |
<> | 149:156823d33999 | 118 | tim0cval = TIM0REG->VALUE; /* read current time */ |
<> | 149:156823d33999 | 119 | retval = (0xFFFF - tim0cval); /* subtract down count */ |
<> | 149:156823d33999 | 120 | |
<> | 149:156823d33999 | 121 | if (TIM0REG->CONTROL.BITS.INT) { |
Kojto | 169:e3b6fe271b81 | 122 | us_timer_isr(); /* handle ISR again */ |
Kojto | 169:e3b6fe271b81 | 123 | NVIC_ClearPendingIRQ(Tim0_IRQn); |
Kojto | 169:e3b6fe271b81 | 124 | retval = (0xFFFF - TIM0REG->VALUE); |
<> | 149:156823d33999 | 125 | } |
<> | 149:156823d33999 | 126 | retval |= msb_counter << 16; /* add software bits */ |
<> | 149:156823d33999 | 127 | NVIC_EnableIRQ(Tim0_IRQn); |
<> | 149:156823d33999 | 128 | return retval; |
<> | 149:156823d33999 | 129 | } |
<> | 149:156823d33999 | 130 | |
<> | 149:156823d33999 | 131 | /******************************************************************************* |
<> | 149:156823d33999 | 132 | * Event Timer |
<> | 149:156823d33999 | 133 | * |
<> | 149:156823d33999 | 134 | * Schedules interrupts at given (32bit)us interval of time. It uses TIMER1. |
<> | 149:156823d33999 | 135 | * The NCS36510 does not have a 32 bit timer nor the option to chain timers, |
<> | 149:156823d33999 | 136 | * which is why a software timer is required to get 32-bit word length. |
<> | 149:156823d33999 | 137 | *******************************************************************************/ |
<> | 149:156823d33999 | 138 | /* TODO - Need some sort of load value/prescale calculation for non-32MHz clock */ |
<> | 149:156823d33999 | 139 | |
<> | 149:156823d33999 | 140 | /* TImer 1 disbale interrupt */ |
<> | 149:156823d33999 | 141 | void us_ticker_disable_interrupt(void) |
<> | 149:156823d33999 | 142 | { |
<> | 149:156823d33999 | 143 | /* Disable the TIMER1 interrupt */ |
<> | 149:156823d33999 | 144 | TIM1REG->CONTROL.BITS.ENABLE = 0x0; |
<> | 149:156823d33999 | 145 | } |
<> | 149:156823d33999 | 146 | |
<> | 149:156823d33999 | 147 | /* TImer 1 clear interrupt */ |
<> | 149:156823d33999 | 148 | void us_ticker_clear_interrupt(void) |
<> | 149:156823d33999 | 149 | { |
<> | 149:156823d33999 | 150 | /* Clear the Ticker (TIMER1) interrupt */ |
<> | 149:156823d33999 | 151 | TIM1REG->CLEAR = 0; |
<> | 149:156823d33999 | 152 | } |
<> | 149:156823d33999 | 153 | |
<> | 149:156823d33999 | 154 | /* Setting TImer 1 (ticker) */ |
<> | 149:156823d33999 | 155 | inline static void ticker_set(uint32_t count) |
<> | 149:156823d33999 | 156 | { |
<> | 149:156823d33999 | 157 | /* Disable TIMER1, load the new value, and re-enable */ |
<> | 149:156823d33999 | 158 | TIM1REG->CONTROL.BITS.ENABLE = 0; |
<> | 149:156823d33999 | 159 | TIM1REG->LOAD = count; |
<> | 149:156823d33999 | 160 | TIM1REG->CONTROL.BITS.ENABLE = 1; |
<> | 149:156823d33999 | 161 | } |
<> | 149:156823d33999 | 162 | |
<> | 149:156823d33999 | 163 | /* TImer 1 - ticker ISR */ |
<> | 149:156823d33999 | 164 | extern void us_ticker_isr(void) |
<> | 149:156823d33999 | 165 | { |
<> | 149:156823d33999 | 166 | /* Clear IRQ flag */ |
<> | 149:156823d33999 | 167 | TIM1REG->CLEAR = 0; |
<> | 149:156823d33999 | 168 | |
Kojto | 169:e3b6fe271b81 | 169 | if (us_ticker_target > 0) { |
Kojto | 169:e3b6fe271b81 | 170 | --us_ticker_target; |
Kojto | 169:e3b6fe271b81 | 171 | ticker_set(0xFFFF); |
<> | 159:612c381a210f | 172 | } else { |
Kojto | 169:e3b6fe271b81 | 173 | us_ticker_irq_handler(); |
<> | 149:156823d33999 | 174 | } |
<> | 149:156823d33999 | 175 | } |
<> | 149:156823d33999 | 176 | |
<> | 149:156823d33999 | 177 | /* Set timer 1 ticker interrupt */ |
<> | 149:156823d33999 | 178 | void us_ticker_set_interrupt(timestamp_t timestamp) |
<> | 149:156823d33999 | 179 | { |
Kojto | 169:e3b6fe271b81 | 180 | int32_t delta = timestamp - us_ticker_read(); |
Kojto | 169:e3b6fe271b81 | 181 | // we got 16 bit timer, use upper 16bit as a simple counter how many times |
Kojto | 169:e3b6fe271b81 | 182 | // we need to schedule full range ticker count |
Kojto | 169:e3b6fe271b81 | 183 | us_ticker_target = (uint32_t)delta >> 16; |
<> | 149:156823d33999 | 184 | |
<> | 149:156823d33999 | 185 | if (delta <= 0) { |
<> | 149:156823d33999 | 186 | /* This event was in the past */ |
<> | 149:156823d33999 | 187 | //us_ticker_irq_handler(); |
<> | 149:156823d33999 | 188 | // This event was in the past. |
<> | 149:156823d33999 | 189 | // Set the interrupt as pending, but don't process it here. |
<> | 149:156823d33999 | 190 | // This prevents a recurive loop under heavy load |
<> | 149:156823d33999 | 191 | // which can lead to a stack overflow. |
<> | 149:156823d33999 | 192 | NVIC_SetPendingIRQ(Tim1_IRQn); |
<> | 149:156823d33999 | 193 | |
<> | 149:156823d33999 | 194 | return; |
<> | 149:156823d33999 | 195 | } |
<> | 149:156823d33999 | 196 | |
Kojto | 169:e3b6fe271b81 | 197 | // we set the full reminder of 16 bit, the next ISR will do the upper part |
Kojto | 169:e3b6fe271b81 | 198 | ticker_set(delta & 0xFFFF); |
<> | 149:156823d33999 | 199 | } |