1+ /*
2+ * This file is part of the MicroPython project, http://micropython.org/
3+ *
4+ * The MIT License (MIT)
5+ *
6+ * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
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+ */
126
27+ #include "tick.h"
228
3- #include <peripheral_clk_config.h>
4-
5- #include "hal/include/hal_timer.h"
6- #include "hpl/pm/hpl_pm_base.h"
7- #include "hpl/tc/hpl_tc_base.h"
8- #include "hpl/gclk/hpl_gclk_base.h"
9- #include "include/component/gclk.h"
29+ #include "peripheral_clk_config.h"
1030
1131#include "supervisor/shared/autoreload.h"
12-
13- #include "tick.h"
32+ #include "shared-bindings/microcontroller/Processor.h"
1433
1534// Global millisecond tick count
1635volatile uint64_t ticks_ms = 0 ;
1736
18- struct timer_descriptor ms_timer ;
19- //static struct timer_task task;
20-
21- void timer_tick (const struct timer_task * const timer_task ) {
37+ void SysTick_Handler (void ) {
2238 // SysTick interrupt handler called when the SysTick timer reaches zero
2339 // (every millisecond).
2440 ticks_ms += 1 ;
@@ -29,18 +45,20 @@ void timer_tick(const struct timer_task *const timer_task) {
2945}
3046
3147void tick_init () {
32- #ifdef SAMD21
33- _pm_enable_bus_clock (PM_BUS_APBC , TC5 );
34- #endif
35- // _gclk_enable_channel(TC5_GCLK_ID, GCLK_SOURCE_DFLL48M);
36-
37- // timer_init(&ms_timer, TC5, _tc_get_timer());
48+ uint32_t ticks_per_ms = common_hal_mcu_processor_get_frequency () / 1000 ;
49+ SysTick_Config (ticks_per_ms );
50+ NVIC_EnableIRQ (SysTick_IRQn );
51+ }
3852
39- // timer_set_clock_cycles_per_tick(&ms_timer, 48000000 / 1000 - 1);
40- // task.cb = timer_tick;
41- // task.interval = 1;
42- // task.mode = TIMER_TASK_REPEAT;
43- // timer_add_task(&ms_timer, &task);
44- //
45- // timer_start(&ms_timer);
53+ void tick_delay (uint32_t us ) {
54+ uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency () / 1000 / 1000 ;
55+ uint32_t us_between_ticks = SysTick -> VAL / ticks_per_us ;
56+ uint64_t start_ms = ticks_ms ;
57+ while (us > 1000 ) {
58+ while (ticks_ms == start_ms ) {}
59+ us -= us_between_ticks ;
60+ start_ms = ticks_ms ;
61+ us_between_ticks = 1000 ;
62+ }
63+ while (SysTick -> VAL > ((1000 - us ) * ticks_per_us )) {}
4664}
0 commit comments