rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015 Eric Holland |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <err.h> |
| 24 | #include <debug.h> |
| 25 | #include <trace.h> |
| 26 | #include <arch/arm/cm.h> |
| 27 | #include <platform.h> |
| 28 | #include <platform/nrf51.h> |
| 29 | #include <platform/system_nrf51.h> |
| 30 | #include <platform/timer.h> |
| 31 | #include <sys/types.h> |
| 32 | |
| 33 | |
| 34 | static volatile uint64_t ticks; |
| 35 | static uint32_t tick_rate = 0; |
| 36 | static uint32_t tick_rate_mhz = 0; |
| 37 | static lk_time_t tick_interval_ms; |
| 38 | |
| 39 | static platform_timer_callback cb; |
| 40 | static void *cb_args; |
| 41 | |
| 42 | typedef enum handler_return (*platform_timer_callback)(void *arg, lk_time_t now); |
| 43 | |
| 44 | status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, lk_time_t interval) |
| 45 | { |
| 46 | |
| 47 | cb = callback; |
| 48 | cb_args = arg; |
| 49 | |
| 50 | tick_interval_ms = interval; |
| 51 | |
| 52 | uint32_t ticks = tick_rate / ( 1000 / interval ); |
| 53 | |
| 54 | NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos; |
| 55 | NRF_CLOCK->TASKS_LFCLKSTART = 1; |
| 56 | |
| 57 | NRF_RTC1->PRESCALER = ticks; |
| 58 | NRF_RTC1->INTENSET = RTC_INTENSET_TICK_Enabled << RTC_INTENSET_TICK_Pos; |
| 59 | |
| 60 | NRF_RTC1->EVENTS_TICK = 0; |
| 61 | NRF_RTC1->TASKS_START = 1; |
| 62 | NVIC_EnableIRQ(RTC1_IRQn); |
| 63 | |
| 64 | return NO_ERROR; |
| 65 | } |
| 66 | |
| 67 | lk_time_t current_time(void) |
| 68 | { |
| 69 | uint64_t t; |
| 70 | |
| 71 | do { |
| 72 | t = ticks; |
| 73 | } while (ticks != t); |
| 74 | |
| 75 | return t * tick_interval_ms; |
| 76 | } |
| 77 | |
| 78 | lk_bigtime_t current_time_hires(void) |
| 79 | { |
| 80 | return current_time() * 1000; |
| 81 | } |
| 82 | |
| 83 | void nrf51_RTC1_IRQ(void) |
| 84 | { |
| 85 | ticks++; |
| 86 | arm_cm_irq_entry(); |
| 87 | |
| 88 | NRF_RTC1->EVENTS_TICK = 0; |
| 89 | |
| 90 | bool resched = false; |
| 91 | if (cb) { |
| 92 | lk_time_t now = current_time(); |
| 93 | if (cb(cb_args, now) == INT_RESCHEDULE) |
| 94 | resched = true; |
| 95 | } |
| 96 | arm_cm_irq_exit(resched); |
| 97 | } |
| 98 | |
| 99 | void arm_cm_systick_init(uint32_t mhz) |
| 100 | { |
| 101 | tick_rate = mhz; |
| 102 | tick_rate_mhz = mhz / 1000000; |
| 103 | } |
| 104 | |