8000 atmel-samd: Add time back using the SysTick counter in the core. Fixe… · adafruit/circuitpython@cab12b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit cab12b1

Browse files
committed
atmel-samd: Add time back using the SysTick counter in the core. Fixes #261
1 parent bac841d commit cab12b1

File tree

8 files changed

+61
-38
lines changed

8 files changed

+61
-38
lines changed

atmel-samd/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,12 @@ SRC_C = \
213213
214214
SRC_COMMON_HAL = \
215215
board/__init__.c \
216+
digitalio/__init__.c \
217+
digitalio/DigitalInOut.c \
216218
microcontroller/__init__.c \
217219
microcontroller/Pin.c \
218220
microcontroller/Processor.c \
219-
digitalio/__init__.c \
220-
digitalio/DigitalInOut.c
221+
time/__init__.c
221222
# analogio/__init__.c \
222223
analogio/AnalogIn.c \
223224
analogio/AnalogOut.c \
@@ -238,7 +239,6 @@ SRC_COMMON_HAL = \
238239
pulseio/PulseOut.c \
239240
pulseio/PWMOut.c \
240241
storage/__init__.c \
241-
8000 time/__init__.c \
242242
touchio/__init__.c \
243243
touchio/TouchIn.c \
244244
usb_hid/__init__.c \

atmel-samd/asf4_conf/samd21/peripheral_clk_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* \brief CPU's Clock frequency
4242
*/
4343
#ifndef CONF_CPU_FREQUENCY
44-
#define CONF_CPU_FREQUENCY 1000000
44+
#define CONF_CPU_FREQUENCY 48000000
4545
#endif
4646

4747
// <y> Core Clock Source

atmel-samd/common-hal/microcontroller/Processor.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464
#include "common-hal/microcontroller/Processor.h"
6565

66+
#include "peripheral_clk_config.h"
6667

6768
// #define ADC_TEMP_SAMPLE_LENGTH 4
6869
// #define INT1V_VALUE_FLOAT 1.0
@@ -226,6 +227,6 @@ float common_hal_mcu_processor_get_temperature(void) {
226227

227228

228229
uint32_t common_hal_mcu_processor_get_frequency(void) {
229-
return 0;
230-
//return system_cpu_clock_get_hz();
230+
// TODO(tannewt): Determine this dynamically.
231+
return CONF_CPU_FREQUENCY;
231232
}

atmel-samd/mpconfigport.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ extern const struct _mp_obj_module_t usb_hid_module;
203203
// { MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module },
204204
// { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module },
205205
// { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module },
206-
// { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module },
207206
// { MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module },
208207
// { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module },
209208
// { MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module },
@@ -213,9 +212,10 @@ extern const struct _mp_obj_module_t usb_hid_module;
213212

214213

215214
#define MICROPY_PORT_BUILTIN_MODULES \
216-
{ MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module }, \
217-
{ MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \
218215
{ MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \
216+
{ MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \
217+
{ MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module }, \
218+
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module },
219219
EXTRA_BUILTIN_MODULES
220220

221221
#define MICROPY_PORT_BUILTIN_DEBUG_MODULES \

atmel-samd/mphalport.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "mpconfigboard.h"
1919
#include "mphalport.h"
2020
#include "reset.h"
21+
#include "tick.h"
2122
#include "usb.h"
2223

2324
extern struct usart_module usart_instance;
@@ -64,7 +65,7 @@ void mp_hal_delay_ms(mp_uint_t delay) {
6465
}
6566

6667
void mp_hal_delay_us(mp_uint_t delay) {
67-
delay_us(delay);
68+
tick_delay(delay);
6869
}
6970

7071
void mp_hal_disable_all_interrupts(void) {

atmel-samd/supervisor/port.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ safe_mode_t port_init(void) {
8989

9090
init_mcu();
9191

92-
delay_init(SysTick);
93-
9492
board_init();
9593

9694
// Configure millisecond timer initialization.

atmel-samd/tick.c

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
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
1635
volatile 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,23 @@ void timer_tick(const struct timer_task *const timer_task) {
2945
}
3046

3147
void 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->LOAD = ((ticks_per_ms - 1) << SysTick_LOAD_RELOAD_Pos);
50+
NVIC_EnableIRQ(SysTick_IRQn);
51+
SysTick->CTRL = (1 << SysTick_CTRL_ENABLE_Pos) |
52+
(1 << SysTick_CTRL_TICKINT_Pos)|
53+
(1 << SysTick_CTRL_CLKSOURCE_Pos);
54+
}
3855

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);
56+
void tick_delay(uint32_t us) {
57+
uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000;
58+
uint32_t us_between_ticks = SysTick->VAL / ticks_per_us;
59+
uint64_t start_ms = ticks_ms;
60+
while (us > 1000) {
61+
while (ticks_ms == start_ms) {}
62+
us -= us_between_ticks;
63+
start_ms = ticks_ms;
64+
us_between_ticks = 1000;
65+
}
66+
while (SysTick->VAL > ((1000 - us) * ticks_per_us)) {}
4667
}

atmel-samd/tick.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ extern struct timer_descriptor ms_timer;
3434

3535
void tick_init(void);
3636

37+
void tick_delay(uint32_t us);
38+
3739
#endif // MICROPY_INCLUDED_ATMEL_SAMD_TICK_H

0 commit comments

Comments
 (0)
0