|
27 | 27 | #include "irq.h" |
28 | 28 | #include "system_tick.h" |
29 | 29 |
|
30 | | -#include "utimer.h" |
31 | | - |
32 | 30 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
33 | 31 |
|
| 32 | +#if MICROPY_HW_SYSTEM_TICK_USE_LPTIMER |
| 33 | + |
| 34 | +#include "lptimer.h" |
| 35 | +#include "sys_ctrl_lptimer.h" |
| 36 | + |
| 37 | +// Channel 0 and 1 are cascaded to make a 64-bit counter. |
| 38 | +// Channel 2 is used for system_tick_wfe_with_timeout_us. |
| 39 | +// Channel 3 is used for system_tick_schedule_after_us. |
| 40 | +#define LPTIMER ((LPTIMER_Type *)LPTIMER_BASE) |
| 41 | +#define LPTIMER_CH_A (0) |
| 42 | +#define LPTIMER_CH_B (1) |
| 43 | +#define LPTIMER_CH_C (2) |
| 44 | +#define LPTIMER_CH_D (3) |
| 45 | + |
| 46 | +uint64_t system_tick_source_hz; |
| 47 | + |
| 48 | +void system_tick_init(void) { |
| 49 | + lptimer_disable_counter(LPTIMER, LPTIMER_CH_A); |
| 50 | + lptimer_disable_counter(LPTIMER, LPTIMER_CH_B); |
| 51 | + |
| 52 | + ANA_REG->MISC_CTRL |= 1 << 0; // SEL_32K, select LXFO |
| 53 | + |
| 54 | + select_lptimer_clk(LPTIMER_CLK_SOURCE_32K, LPTIMER_CH_A); |
| 55 | + select_lptimer_clk(LPTIMER_CLK_SOURCE_CASCADE, LPTIMER_CH_B); |
| 56 | + select_lptimer_clk(LPTIMER_CLK_SOURCE_32K, LPTIMER_CH_C); |
| 57 | + select_lptimer_clk(LPTIMER_CLK_SOURCE_32K, LPTIMER_CH_D); |
| 58 | + |
| 59 | + lptimer_load_max_count(LPTIMER, LPTIMER_CH_A); |
| 60 | + lptimer_set_mode_freerunning(LPTIMER, LPTIMER_CH_A); |
| 61 | + |
| 62 | + lptimer_load_max_count(LPTIMER, LPTIMER_CH_B); |
| 63 | + lptimer_set_mode_freerunning(LPTIMER, LPTIMER_CH_B); |
| 64 | + |
| 65 | + lptimer_enable_counter(LPTIMER, LPTIMER_CH_B); |
| 66 | + lptimer_enable_counter(LPTIMER, LPTIMER_CH_A); |
| 67 | + |
| 68 | + system_tick_source_hz = 32768; |
| 69 | + |
| 70 | + NVIC_ClearPendingIRQ(LPTIMER2_IRQ_IRQn); |
| 71 | + NVIC_SetPriority(LPTIMER2_IRQ_IRQn, IRQ_PRI_SYSTEM_TICK); |
| 72 | + NVIC_EnableIRQ(LPTIMER2_IRQ_IRQn); |
| 73 | + |
| 74 | + NVIC_ClearPendingIRQ(LPTIMER3_IRQ_IRQn); |
| 75 | + NVIC_SetPriority(LPTIMER3_IRQ_IRQn, IRQ_PRI_SYSTEM_TICK); |
| 76 | + NVIC_EnableIRQ(LPTIMER3_IRQ_IRQn); |
| 77 | +} |
| 78 | + |
| 79 | +void LPTIMER2_IRQHandler(void) { |
| 80 | + lptimer_clear_interrupt(LPTIMER, LPTIMER_CH_C); |
| 81 | + __SEV(); |
| 82 | +} |
| 83 | + |
| 84 | +void LPTIMER3_IRQHandler(void) { |
| 85 | + lptimer_clear_interrupt(LPTIMER, LPTIMER_CH_D); |
| 86 | + lptimer_mask_interrupt(LPTIMER, LPTIMER_CH_D); |
| 87 | + lptimer_disable_counter(LPTIMER, LPTIMER_CH_D); |
| 88 | + system_tick_schedule_callback(); |
| 89 | + __SEV(); |
| 90 | +} |
| 91 | + |
| 92 | +uint32_t system_tick_get_u32(void) { |
| 93 | + return 0xffffffff - lptimer_get_count(LPTIMER, LPTIMER_CH_A); |
| 94 | +} |
| 95 | + |
| 96 | +uint64_t system_tick_get_u64(void) { |
| 97 | + // Get 64-bit counter value from the hardware timer. |
| 98 | + // Sample it twice in case the low counter wraps around while sampling. |
| 99 | + uint32_t irq_state = disable_irq(); |
| 100 | + uint32_t lo0 = lptimer_get_count(LPTIMER, LPTIMER_CH_A); |
| 101 | + uint32_t hi0 = lptimer_get_count(LPTIMER, LPTIMER_CH_B); |
| 102 | + uint32_t lo1 = lptimer_get_count(LPTIMER, LPTIMER_CH_A); |
| 103 | + uint32_t hi1 = lptimer_get_count(LPTIMER, LPTIMER_CH_B); |
| 104 | + enable_irq(irq_state); |
| 105 | + |
| 106 | + if (hi0 == hi1) { |
| 107 | + // Low counter may have wrapped around between sampling of lo0 and hi0, so prefer second sampling. |
| 108 | + lo0 = lo1; |
| 109 | + hi0 = hi1; |
| 110 | + } else { |
| 111 | + // Low counter wrapped around either between sampling of hi0 and lo1, or sampling of lo1 and hi1. |
| 112 | + // In either case use the first sampling. |
| 113 | + } |
| 114 | + |
| 115 | + // Convert from descending count to ascending. |
| 116 | + lo0 = 0xffffffff - lo0; |
| 117 | + hi0 = 0xffffffff - hi0; |
| 118 | + |
| 119 | + // Return a 64-bit value. |
| 120 | + return ((uint64_t)hi0 << 32) | (uint64_t)lo0; |
| 121 | +} |
| 122 | + |
| 123 | +void system_tick_wfe_with_timeout_us(uint32_t timeout_us) { |
| 124 | + // Maximum 131 second timeout, to not overflow 32-bit ticks when |
| 125 | + // LPTIMER is clocked at 32768Hz. |
| 126 | + uint32_t timeout_ticks = (uint64_t)MIN(timeout_us, 131000000) * system_tick_source_hz / 1000000; |
| 127 | + |
| 128 | + // Set up the LPTIMER interrupt to fire after the given timeout. |
| 129 | + lptimer_disable_counter(LPTIMER, LPTIMER_CH_C); |
| 130 | + lptimer_set_mode_userdefined(LPTIMER, LPTIMER_CH_C); |
| 131 | + lptimer_load_count(LPTIMER, LPTIMER_CH_C, &timeout_ticks); |
| 132 | + lptimer_clear_interrupt(LPTIMER, LPTIMER_CH_C); |
| 133 | + lptimer_unmask_interrupt(LPTIMER, LPTIMER_CH_C); |
| 134 | + lptimer_enable_counter(LPTIMER, LPTIMER_CH_C); |
| 135 | + |
| 136 | + // Wait for an event. |
| 137 | + __WFE(); |
| 138 | + |
| 139 | + // Disable the LPTIMER interrupt (in case a different interrupt woke the WFE). |
| 140 | + lptimer_mask_interrupt(LPTIMER, LPTIMER_CH_C); |
| 141 | + lptimer_disable_counter(LPTIMER, LPTIMER_CH_C); |
| 142 | +} |
| 143 | + |
| 144 | +void system_tick_schedule_after_us(uint32_t ticks_us) { |
| 145 | + // Disable the interrupt in case it's still active. |
| 146 | + lptimer_mask_interrupt(LPTIMER, LPTIMER_CH_D); |
| 147 | + |
| 148 | + // Maximum 131 second timeout, to not overflow 32-bit ticks when |
| 149 | + // LPTIMER is clocked at 32768Hz. |
| 150 | + uint32_t timeout_ticks = (uint64_t)MIN(ticks_us, 131000000) * system_tick_source_hz / 1000000; |
| 151 | + |
| 152 | + // Set up the LPTIMER interrupt to fire after the given timeout. |
| 153 | + lptimer_disable_counter(LPTIMER, LPTIMER_CH_D); |
| 154 | + lptimer_set_mode_userdefined(LPTIMER, LPTIMER_CH_D); |
| 155 | + lptimer_load_count(LPTIMER, LPTIMER_CH_D, &timeout_ticks); |
| 156 | + lptimer_clear_interrupt(LPTIMER, LPTIMER_CH_D); |
| 157 | + lptimer_unmask_interrupt(LPTIMER, LPTIMER_CH_D); |
| 158 | + lptimer_enable_counter(LPTIMER, LPTIMER_CH_D); |
| 159 | +} |
| 160 | + |
| 161 | +#elif MICROPY_HW_SYSTEM_TICK_USE_UTIMER |
| 162 | + |
| 163 | +#include "utimer.h" |
| 164 | + |
34 | 165 | #define UTIMER ((UTIMER_Type *)UTIMER_BASE) |
35 | 166 | #define UTIMER_CHANNEL (11) |
36 | 167 |
|
@@ -166,3 +297,5 @@ void system_tick_schedule_after_us(uint32_t ticks_us) { |
166 | 297 | } |
167 | 298 | } |
168 | 299 | } |
| 300 | + |
| 301 | +#endif |
0 commit comments