Skip to content

Commit 92bb909

Browse files
committed
add a frequencyin_reset() for VM restart
1 parent 4de6c7c commit 92bb909

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

ports/atmel-samd/common-hal/frequencyio/FrequencyIn.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,23 @@
5454
#endif
5555

5656
static frequencyio_frequencyin_obj_t *active_frequencyins[TC_INST_NUM];
57-
volatile uint8_t reference_tc = 0xff;
57+
volatile uint8_t reference_tc;
5858
#ifdef SAM_D5X_E5X
5959
static uint8_t dpll_gclk;
6060
#endif
6161

62-
void frequencyin_emergency_cancel_capture(uint8_t index) {
62+
void frequencyin_reset(void) {
63+
for (uint8_t i = 0; i < TC_INST_NUM; i++) {
64+
active_frequencyins[i] = NULL;
65+
}
66+
67+
reference_tc = 0xff;
68+
#ifdef SAM_D5X_E5X
69+
dpll_gclk = 0xff;
70+
#endif
71+
}
72+
73+
static void frequencyin_emergency_cancel_capture(uint8_t index) {
6374
frequencyio_frequencyin_obj_t* self = active_frequencyins[index];
6475

6576
NVIC_DisableIRQ(self->TC_IRQ);
@@ -93,7 +104,7 @@ void frequencyin_interrupt_handler(uint8_t index) {
93104

94105
uint64_t current_ns = common_hal_time_monotonic_ns();
95106

96-
for (uint8_t i = 0; i <= (TC_INST_NUM - 1); i++) {
107+
for (uint8_t i = 0; i < TC_INST_NUM; i++) {
97108
if (active_frequencyins[i] != NULL) {
98109
frequencyio_frequencyin_obj_t* self = active_frequencyins[i];
99110
Tc* tc = tc_insts[self->tc_index];
@@ -143,7 +154,7 @@ void frequencyin_interrupt_handler(uint8_t index) {
143154
ref_tc->COUNT16.INTFLAG.reg |= TC_INTFLAG_OVF;
144155
}
145156

146-
void frequencyin_reference_tc_init() {
157+
static void frequencyin_reference_tc_init(void) {
147158
if (reference_tc == 0xff) {
148159
return;
149160
}
@@ -154,9 +165,6 @@ void frequencyin_reference_tc_init() {
154165
// use the DPLL we setup so that the reference_tc and freqin_tc(s)
155166
// are using the same clock frequency.
156167
#ifdef SAM_D5X_E5X
157-
if (dpll_gclk == 0xff) {
158-
frequencyin_samd51_start_dpll();
159-
}
160168
set_timer_handler(true, reference_tc, TC_HANDLER_FREQUENCYIN);
161169
turn_on_clocks(true, reference_tc, dpll_gclk);
162170
#endif
@@ -178,15 +186,15 @@ void frequencyin_reference_tc_init() {
178186
#endif
179187
}
180188

181-
bool frequencyin_reference_tc_enabled() {
189+
static bool frequencyin_reference_tc_enabled(void) {
182190
if (reference_tc == 0xff) {
183191
return false;
184192
}
185193
Tc *tc = tc_insts[reference_tc];
186194
return tc->COUNT16.CTRLA.bit.ENABLE;
187195
}
188196

189-
void frequencyin_reference_tc_enable(bool enable) {
197+
static void frequencyin_reference_tc_enable(bool enable) {
190198
if (reference_tc == 0xff) {
191199
return;
192200
}
@@ -195,15 +203,15 @@ void frequencyin_reference_tc_enable(bool enable) {
195203
}
196204

197205
#ifdef SAM_D5X_E5X
198-
void frequencyin_samd51_start_dpll() {
206+
static bool frequencyin_samd51_start_dpll(void) {
199207
if (clock_get_enabled(0, GCLK_SOURCE_DPLL1)) {
200-
return;
208+
return true;
201209
}
202210

203211
uint8_t free_gclk = find_free_gclk(1);
204212
if (free_gclk == 0xff) {
205213
dpll_gclk = 0xff;
206-
return;
214+
return false;
207215
}
208216

209217
GCLK->PCHCTRL[OSCCTRL_GCLK_ID_FDPLL1].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN(free_gclk);
@@ -228,23 +236,25 @@ void frequencyin_samd51_start_dpll() {
228236
while (!(OSCCTRL->Dpll[1].DPLLSTATUS.bit.LOCK || OSCCTRL->Dpll[1].DPLLSTATUS.bit.CLKRDY)) {}
229237
enable_clock_generator(free_gclk, GCLK_GENCTRL_SRC_DPLL1_Val, 1);
230238
dpll_gclk = free_gclk;
239+
return true;
231240
}
232241

233-
void frequencyin_samd51_stop_dpll() {
242+
static void frequencyin_samd51_stop_dpll(void) {
234243
if (!clock_get_enabled(0, GCLK_SOURCE_DPLL1)) {
235244
return;
236245
}
237246

238-
disable_clock_generator(dpll_gclk);
247+
if (dpll_gclk != 0xff) {
248+
disable_clock_generator(dpll_gclk);
249+
dpll_gclk = 0xff;
250+
}
239251

240252
GCLK->PCHCTRL[OSCCTRL_GCLK_ID_FDPLL1].reg = 0;
241253
OSCCTRL->Dpll[1].DPLLCTRLA.reg = 0;
242254
OSCCTRL->Dpll[1].DPLLRATIO.reg = 0;
243255
OSCCTRL->Dpll[1].DPLLCTRLB.reg = 0;
244-
245256
while (OSCCTRL->Dpll[1].DPLLSYNCBUSY.bit.ENABLE) {
246257
}
247-
dpll_gclk = 0xff;
248258
}
249259
#endif
250260

@@ -421,7 +431,7 @@ void common_hal_frequencyio_frequencyin_deinit(frequencyio_frequencyin_obj_t* se
421431
self->pin = NO_PIN;
422432

423433
bool check_active = false;
424-
for (uint8_t i = 0; i <= (TC_INST_NUM - 1); i++) {
434+
for (uint8_t i = 0; i < TC_INST_NUM; i++) {
425435
if (active_frequencyins[i] != NULL) {
426436
check_active = true;
427437
}

ports/atmel-samd/common-hal/frequencyio/FrequencyIn.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,7 @@ typedef struct {
4646
} frequencyio_frequencyin_obj_t;
4747

4848
void frequencyin_interrupt_handler(uint8_t index);
49-
void frequencyin_emergency_cancel_capture(uint8_t index);
50-
void frequencyin_reference_tc_init(void);
51-
void frequencyin_reference_tc_enable(bool enable);
52-
bool frequencyin_reference_tc_enabled(void);
53-
#ifdef SAM_D5X_E5X
54-
void frequencyin_samd51_start_dpll(void);
55-
void frequencyin_samd51_stop_dpll(void);
56-
#endif
49+
void frequencyin_reset(void);
5750

5851

5952
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_FREQUENCYIO_FREQUENCYIN_H

ports/atmel-samd/supervisor/port.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
#include "common-hal/busio/__init__.h"
7272
#endif
7373

74+
#if CIRCUITPY_FREQUENCYIO
75+
#include "common-hal/frequencyio/FrequencyIn.h"
76+
#endif
77+
7478
#include "common-hal/microcontroller/Pin.h"
7579

7680
#if CIRCUITPY_PULSEIO
@@ -388,6 +392,10 @@ void reset_port(void) {
388392
i2sout_reset();
389393
#endif
390394

395+
#if CIRCUITPY_FREQUENCYIO
396+
frequencyin_reset();
397+
#endif
398+
391399
#if CIRCUITPY_TOUCHIO && CIRCUITPY_TOUCHIO_USE_NATIVE
392400
touchin_reset();
393401
#endif
@@ -399,7 +407,7 @@ void reset_port(void) {
399407
#if CIRCUITPY_PWMIO
400408
pwmout_reset();
401409
#endif
402-
#if CIRCUITPY_PWMIO || CIRCUITPY_AUDIOIO
410+
#if CIRCUITPY_PWMIO || CIRCUITPY_AUDIOIO || CIRCUITPY_FREQUENCYIO
403411
reset_timers();
404412
#endif
405413

0 commit comments

Comments
 (0)