|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2024 OpenMV LLC. |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "irq.h" |
| 28 | +#include "system_tick.h" |
| 29 | + |
| 30 | +#include "utimer.h" |
| 31 | + |
| 32 | +#define MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 33 | + |
| 34 | +#define UTIMER ((UTIMER_Type *)UTIMER_BASE) |
| 35 | +#define UTIMER_CHANNEL (11) |
| 36 | + |
| 37 | +uint64_t system_core_clock_mhz; |
| 38 | +static volatile uint32_t system_tick_hi; |
| 39 | + |
| 40 | +static void system_tick_nvic_config(unsigned int index) { |
| 41 | + NVIC_ClearPendingIRQ(UTIMER_IRQ0_IRQn + UTIMER_CHANNEL * 8 + index); |
| 42 | + NVIC_SetPriority(UTIMER_IRQ0_IRQn + UTIMER_CHANNEL * 8 + index, IRQ_PRI_SYSTEM_TICK); |
| 43 | + NVIC_EnableIRQ(UTIMER_IRQ0_IRQn + UTIMER_CHANNEL * 8 + index); |
| 44 | +} |
| 45 | + |
| 46 | +void system_tick_init(void) { |
| 47 | + system_tick_hi = 0; |
| 48 | + system_core_clock_mhz = SystemCoreClock / 1000000; |
| 49 | + |
| 50 | + // Configure NVIC OVER_FLOW interrupt. |
| 51 | + system_tick_nvic_config(7); |
| 52 | + |
| 53 | + utimer_clock_enable(UTIMER, UTIMER_CHANNEL); |
| 54 | + utimer_channel_config cfg = { 0 }; |
| 55 | + cfg.fixed_buffer = false; |
| 56 | + utimer_config_direction(UTIMER, UTIMER_CHANNEL, UTIMER_COUNTER_UP, &cfg); |
| 57 | + utimer_set_count(UTIMER, UTIMER_CHANNEL, UTIMER_CNTR_PTR, 0xffffffff); |
| 58 | + utimer_unmask_interrupt(UTIMER, UTIMER_CHANNEL, CHAN_INTERRUPT_OVER_FLOW_MASK); |
| 59 | + utimer_control_enable(UTIMER, UTIMER_CHANNEL); |
| 60 | + utimer_counter_start(UTIMER, UTIMER_CHANNEL); |
| 61 | + |
| 62 | + // Set up the UTIMER compare interrupt, to be used later. |
| 63 | + system_tick_nvic_config(2); |
| 64 | + UTIMER->UTIMER_CHANNEL_CFG[UTIMER_CHANNEL].UTIMER_COMPARE_CTRL_A |= COMPARE_CTRL_DRV_COMPARE_EN; |
| 65 | +} |
| 66 | + |
| 67 | +// COMPARE_A_BUF1 |
| 68 | +void UTIMER_IRQ90Handler(void) { |
| 69 | + uint32_t chan_interrupt = UTIMER->UTIMER_CHANNEL_CFG[UTIMER_CHANNEL].UTIMER_CHAN_INTERRUPT; |
| 70 | + if (chan_interrupt & CHAN_INTERRUPT_COMPARE_A_BUF1_MASK) { |
| 71 | + utimer_clear_interrupt(UTIMER, UTIMER_CHANNEL, CHAN_INTERRUPT_COMPARE_A_BUF1_MASK); |
| 72 | + __SEV(); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// OVER_FLOW |
| 77 | +void UTIMER_IRQ95Handler(void) { |
| 78 | + uint32_t chan_interrupt = UTIMER->UTIMER_CHANNEL_CFG[UTIMER_CHANNEL].UTIMER_CHAN_INTERRUPT; |
| 79 | + if (chan_interrupt & CHAN_INTERRUPT_OVER_FLOW_MASK) { |
| 80 | + utimer_clear_interrupt(UTIMER, UTIMER_CHANNEL, CHAN_INTERRUPT_OVER_FLOW_MASK); |
| 81 | + ++system_tick_hi; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +uint32_t system_tick_get_u32(void) { |
| 86 | + return utimer_get_count(UTIMER, UTIMER_CHANNEL, UTIMER_CNTR); |
| 87 | +} |
| 88 | + |
| 89 | +uint64_t system_tick_get_u64(void) { |
| 90 | + uint32_t irq_state = disable_irq(); |
| 91 | + uint32_t ticks_lo = utimer_get_count(UTIMER, UTIMER_CHANNEL, UTIMER_CNTR); |
| 92 | + uint32_t ticks_hi = system_tick_hi; |
| 93 | + uint32_t chan_interrupt = UTIMER->UTIMER_CHANNEL_CFG[UTIMER_CHANNEL].UTIMER_CHAN_INTERRUPT; |
| 94 | + enable_irq(irq_state); |
| 95 | + |
| 96 | + if (chan_interrupt & CHAN_INTERRUPT_OVER_FLOW_MASK) { |
| 97 | + // The timer had an overflow while interrupts were disabled. |
| 98 | + if (ticks_lo < 0x80000000) { |
| 99 | + // The timer had an overflow just before we sampled it. |
| 100 | + ++ticks_hi; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // This ticks runs at SystemCoreClock. |
| 105 | + return (uint64_t)ticks_hi << 32 | ticks_lo; |
| 106 | +} |
| 107 | + |
| 108 | +void system_tick_wfe_with_timeout_us(uint32_t timeout_us) { |
| 109 | + // Maximum 10 second timeout, to not overflow signed 32-bit ticks when |
| 110 | + // system_core_clock_mhz==400. |
| 111 | + uint32_t timeout_ticks = MIN(timeout_us, 10000000) * system_core_clock_mhz; |
| 112 | + |
| 113 | + // Set up the UTIMER compare interrupt to fire after the given timeout. |
| 114 | + uint32_t cntr = utimer_get_count(UTIMER, UTIMER_CHANNEL, UTIMER_CNTR); |
| 115 | + utimer_set_count(UTIMER, UTIMER_CHANNEL, UTIMER_COMPARE_A_BUF1, cntr + timeout_ticks); |
| 116 | + utimer_clear_interrupt(UTIMER, UTIMER_CHANNEL, CHAN_INTERRUPT_COMPARE_A_BUF1_MASK); |
| 117 | + utimer_unmask_interrupt(UTIMER, UTIMER_CHANNEL, CHAN_INTERRUPT_COMPARE_A_BUF1_MASK); |
| 118 | + |
| 119 | + // Wait for an event (or compare timeout event) if the timeout hasn't expired yet |
| 120 | + // (this check handles the case of short timeouts). |
| 121 | + uint32_t cntr2 = utimer_get_count(UTIMER, UTIMER_CHANNEL, UTIMER_CNTR); |
| 122 | + if ((uint32_t)(cntr2 - cntr) < timeout_ticks) { |
| 123 | + __WFE(); |
| 124 | + } |
| 125 | + |
| 126 | + // Disable the UTIMER compare interrupt. |
| 127 | + utimer_mask_interrupt(UTIMER, UTIMER_CHANNEL, CHAN_INTERRUPT_COMPARE_A_BUF1_MASK); |
| 128 | +} |
0 commit comments