|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2018 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include <stddef.h> |
| 17 | +#include "lp_ticker_api.h" |
| 18 | +#include "PeripheralNames.h" |
| 19 | + |
| 20 | +#define LP_TICKER_INTERRUPT CMSDK_TIMER0 |
| 21 | +#define LP_TICKER_COUNTER CMSDK_TIMER1 |
| 22 | +#define LP_TICKER_TIMER_IRQn TIMER0_IRQn |
| 23 | + |
| 24 | + |
| 25 | +/* mbed OS HAL API defined lp_ticker as an increment ticker |
| 26 | + * MPS2 platform provided in SSE-200 are decrement tickers |
| 27 | + * with interrupt fired counter reaches 0. |
| 28 | + * |
| 29 | + * So 2 Timers are used to construct mbed OS HAL low power ticker. |
| 30 | + * |
| 31 | + * TIMER0 is for generating interrupts |
| 32 | + * and TIMER0 will turned off when it is generating interrupts |
| 33 | + * |
| 34 | + * TIMER1 is for counting, and returns inverted binary when read from it |
| 35 | + * Because TIMER1 is running at the speed of 25Mhz, it need to be shift by 10, |
| 36 | + * in order to meet mbed HAL lp_ticker definitions |
| 37 | + * |
| 38 | + */ |
| 39 | + |
| 40 | +static int lp_ticker_inited = 0; |
| 41 | + |
| 42 | +void lp_ticker_internal_handler(void) |
| 43 | +{ |
| 44 | + LP_TICKER_INTERRUPT->CTRL &= ~CMSDK_TIMER_CTRL_IRQEN_Msk; |
| 45 | + LP_TICKER_INTERRUPT->CTRL &= ~CMSDK_TIMER_CTRL_EN_Msk; |
| 46 | + lp_ticker_irq_handler(); |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +void lp_ticker_init(void) |
| 51 | +{ |
| 52 | + if (lp_ticker_inited) { |
| 53 | + lp_ticker_disable_interrupt(); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + LP_TICKER_COUNTER->CTRL = 0x0ul; |
| 58 | + LP_TICKER_INTERRUPT->CTRL = 0x0ul; |
| 59 | + |
| 60 | + LP_TICKER_COUNTER->RELOAD = 0xFFFFFFFFul; |
| 61 | + LP_TICKER_INTERRUPT->RELOAD = 0xFFFFFFFFul; |
| 62 | + |
| 63 | + LP_TICKER_COUNTER->CTRL |= CMSDK_TIMER_CTRL_EN_Msk; |
| 64 | + |
| 65 | + NVIC_SetVector(LP_TICKER_TIMER_IRQn, (uint32_t)lp_ticker_internal_handler); |
| 66 | + lp_ticker_inited = 1; |
| 67 | +} |
| 68 | + |
| 69 | +void lp_ticker_free(void) |
| 70 | +{ |
| 71 | + LP_TICKER_COUNTER->CTRL &= ~CMSDK_TIMER_CTRL_EN_Msk; |
| 72 | + lp_ticker_disable_interrupt(); |
| 73 | + lp_ticker_inited = 0; |
| 74 | +} |
| 75 | + |
| 76 | +uint32_t lp_ticker_read() |
| 77 | +{ |
| 78 | + return (~LP_TICKER_COUNTER->VALUE) >> 10; |
| 79 | +} |
| 80 | + |
| 81 | +void lp_ticker_set_interrupt(timestamp_t timestamp) |
| 82 | +{ |
| 83 | + uint32_t delta = (timestamp << 10) - (~LP_TICKER_COUNTER->VALUE); |
| 84 | + |
| 85 | + LP_TICKER_INTERRUPT->CTRL &= ~CMSDK_TIMER_CTRL_EN_Msk; |
| 86 | + LP_TICKER_INTERRUPT->RELOAD = delta; |
| 87 | + LP_TICKER_INTERRUPT->CTRL |= CMSDK_TIMER_CTRL_IRQEN_Msk; |
| 88 | + LP_TICKER_INTERRUPT->CTRL |= CMSDK_TIMER_CTRL_EN_Msk; |
| 89 | + |
| 90 | + NVIC_EnableIRQ(LP_TICKER_TIMER_IRQn); |
| 91 | +} |
| 92 | + |
| 93 | +void lp_ticker_fire_interrupt(void) |
| 94 | +{ |
| 95 | + NVIC_EnableIRQ(LP_TICKER_TIMER_IRQn); |
| 96 | + NVIC_SetPendingIRQ(LP_TICKER_TIMER_IRQn); |
| 97 | +} |
| 98 | + |
| 99 | +void lp_ticker_disable_interrupt(void) |
| 100 | +{ |
| 101 | + LP_TICKER_INTERRUPT->CTRL &= ~CMSDK_TIMER_CTRL_IRQEN_Msk; |
| 102 | + LP_TICKER_INTERRUPT->CTRL &= ~CMSDK_TIMER_CTRL_EN_Msk; |
| 103 | + NVIC_DisableIRQ(LP_TICKER_TIMER_IRQn); |
| 104 | +} |
| 105 | + |
| 106 | +void lp_ticker_clear_interrupt(void) |
| 107 | +{ |
| 108 | + LP_TICKER_INTERRUPT->INTCLEAR = CMSDK_TIMER_INTCLEAR_Msk; |
| 109 | +} |
| 110 | + |
| 111 | +const ticker_info_t *lp_ticker_get_info(void) |
| 112 | +{ |
| 113 | + static const ticker_info_t info = { |
| 114 | + 24414, // 10 stages scaled from 25MHz (dived by 1024) |
| 115 | + 22 // 22 bit counter |
| 116 | + }; |
| 117 | + return &info; |
| 118 | +} |
| 119 | + |
| 120 | + |
0 commit comments