forked from Infineon/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticks.c
142 lines (128 loc) · 4.62 KB
/
ticks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/mphal.h"
#include "ticks.h"
#include "fsl_gpt.h"
// General purpose timer for keeping microsecond and millisecond tick values.
#define GPTx GPT2
#define GPTx_IRQn GPT2_IRQn
#define GPTx_IRQHandler GPT2_IRQHandler
static uint32_t ticks_us64_upper;
static uint32_t ticks_ms_upper;
void ticks_init(void) {
ticks_us64_upper = 0;
ticks_ms_upper = 0;
gpt_config_t config;
#if defined MIMXRT117x_SERIES
config.clockSource = kGPT_ClockSource_HighFreq;
#else
config.clockSource = kGPT_ClockSource_Osc;
#endif
config.divider = 24; // XTAL is 24MHz
config.enableFreeRun = true;
config.enableRunInWait = true;
config.enableRunInStop = true;
config.enableRunInDoze = true;
config.enableRunInDbg = false;
config.enableMode = true;
GPT_Init(GPTx, &config);
GPT_EnableInterrupts(GPTx, kGPT_RollOverFlagInterruptEnable);
NVIC_SetPriority(GPTx_IRQn, 0); // highest priority
NVIC_EnableIRQ(GPTx_IRQn);
GPT_StartTimer(GPTx);
#ifdef NDEBUG
mp_hal_ticks_cpu_enable();
#endif
}
void GPTx_IRQHandler(void) {
if (GPT_GetStatusFlags(GPTx, kGPT_OutputCompare1Flag)) {
GPT_ClearStatusFlags(GPTx, kGPT_OutputCompare1Flag);
GPT_DisableInterrupts(GPTx, kGPT_OutputCompare1InterruptEnable);
__SEV();
}
if (GPT_GetStatusFlags(GPTx, kGPT_RollOverFlag)) {
GPT_ClearStatusFlags(GPTx, kGPT_RollOverFlag);
++ticks_us64_upper;
if (++ticks_ms_upper >= 1000) {
// Wrap upper counter at a multiple of 1000 so that when mp_hal_ticks_ms()
// wraps due to overflow it wraps smoothly.
ticks_ms_upper = 0;
}
}
}
static void ticks_wake_after_us32(uint32_t us) {
if (us < 2) {
// Delay too short to guarantee that we won't miss it when setting the OCR below.
__SEV();
} else {
// Disable IRQs so setting the OCR is done without any interruption.
uint32_t irq_state = DisableGlobalIRQ();
GPT_EnableInterrupts(GPTx, kGPT_OutputCompare1InterruptEnable);
uint32_t oc = GPT_GetCurrentTimerCount(GPTx) + us;
GPT_SetOutputCompareValue(GPTx, kGPT_OutputCompare_Channel1, oc);
EnableGlobalIRQ(irq_state);
}
}
static uint64_t ticks_us64_with(uint32_t *upper_ptr) {
uint32_t irq_state = DisableGlobalIRQ();
uint32_t lower = GPT_GetCurrentTimerCount(GPTx);
uint32_t upper = *upper_ptr;
uint32_t overflow = GPT_GetStatusFlags(GPTx, kGPT_RollOverFlag);
EnableGlobalIRQ(irq_state);
if (overflow && lower < 0x80000000) {
// The timer counter overflowed before reading it but the IRQ handler
// has not yet been called, so perform the IRQ arithmetic now.
++upper;
}
return (uint64_t)upper << 32 | (uint64_t)lower;
}
uint32_t ticks_us32(void) {
return GPT_GetCurrentTimerCount(GPTx);
}
uint64_t ticks_us64(void) {
return ticks_us64_with(&ticks_us64_upper);
}
uint32_t ticks_ms32(void) {
// This will return a value that only has the lower 32-bits valid.
return ticks_us64_with(&ticks_ms_upper) / 1000;
}
void ticks_delay_us64(uint64_t us) {
uint64_t t0 = ticks_us64();
for (;;) {
uint64_t dt = ticks_us64() - t0;
if (dt >= us) {
return;
}
dt = us - dt;
if (dt > 0xffffffff) {
dt = 0xffffffff;
}
ticks_wake_after_us32((uint32_t)dt);
if (dt > 50) {
MICROPY_EVENT_POLL_HOOK
}
}
}