Update time to use SysTick (#274)

* atmel-samd: Add time back using the SysTick counter in the core. Fixes #261

* Switch to SysTick_Config
This commit is contained in:
Scott Shawcroft 2017-09-29 13:58:13 -07:00 committed by Dan Halbert
parent bac841df61
commit 211b44e630
8 changed files with 61 additions and 41 deletions

View File

@ -213,11 +213,12 @@ SRC_C = \
SRC_COMMON_HAL = \ SRC_COMMON_HAL = \
board/__init__.c \ board/__init__.c \
digitalio/__init__.c \
digitalio/DigitalInOut.c \
microcontroller/__init__.c \ microcontroller/__init__.c \
microcontroller/Pin.c \ microcontroller/Pin.c \
microcontroller/Processor.c \ microcontroller/Processor.c \
digitalio/__init__.c \ time/__init__.c
digitalio/DigitalInOut.c
# analogio/__init__.c \ # analogio/__init__.c \
analogio/AnalogIn.c \ analogio/AnalogIn.c \
analogio/AnalogOut.c \ analogio/AnalogOut.c \
@ -238,7 +239,6 @@ SRC_COMMON_HAL = \
pulseio/PulseOut.c \ pulseio/PulseOut.c \
pulseio/PWMOut.c \ pulseio/PWMOut.c \
storage/__init__.c \ storage/__init__.c \
time/__init__.c \
touchio/__init__.c \ touchio/__init__.c \
touchio/TouchIn.c \ touchio/TouchIn.c \
usb_hid/__init__.c \ usb_hid/__init__.c \

View File

@ -41,7 +41,7 @@
* \brief CPU's Clock frequency * \brief CPU's Clock frequency
*/ */
#ifndef CONF_CPU_FREQUENCY #ifndef CONF_CPU_FREQUENCY
#define CONF_CPU_FREQUENCY 1000000 #define CONF_CPU_FREQUENCY 48000000
#endif #endif
// <y> Core Clock Source // <y> Core Clock Source

View File

@ -63,6 +63,7 @@
#include "common-hal/microcontroller/Processor.h" #include "common-hal/microcontroller/Processor.h"
#include "peripheral_clk_config.h"
// #define ADC_TEMP_SAMPLE_LENGTH 4 // #define ADC_TEMP_SAMPLE_LENGTH 4
// #define INT1V_VALUE_FLOAT 1.0 // #define INT1V_VALUE_FLOAT 1.0
@ -226,6 +227,6 @@ float common_hal_mcu_processor_get_temperature(void) {
uint32_t common_hal_mcu_processor_get_frequency(void) { uint32_t common_hal_mcu_processor_get_frequency(void) {
return 0; // TODO(tannewt): Determine this dynamically.
//return system_cpu_clock_get_hz(); return CONF_CPU_FREQUENCY;
} }

View File

@ -203,7 +203,6 @@ extern const struct _mp_obj_module_t usb_hid_module;
// { MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, // { 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;
#define MICROPY_PORT_BUILTIN_MODULES \ #define MICROPY_PORT_BUILTIN_MODULES \
{ MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module },
EXTRA_BUILTIN_MODULES EXTRA_BUILTIN_MODULES
#define MICROPY_PORT_BUILTIN_DEBUG_MODULES \ #define MICROPY_PORT_BUILTIN_DEBUG_MODULES \

View File

@ -18,6 +18,7 @@
#include "mpconfigboard.h" #include "mpconfigboard.h"
#include "mphalport.h" #include "mphalport.h"
#include "reset.h" #include "reset.h"
#include "tick.h"
#include "usb.h" #include "usb.h"
extern struct usart_module usart_instance; extern struct usart_module usart_instance;
@ -64,7 +65,7 @@ void mp_hal_delay_ms(mp_uint_t delay) {
} }
void mp_hal_delay_us(mp_uint_t delay) { void mp_hal_delay_us(mp_uint_t delay) {
delay_us(delay); tick_delay(delay);
} }
void mp_hal_disable_all_interrupts(void) { void mp_hal_disable_all_interrupts(void) {

View File

@ -89,8 +89,6 @@ safe_mode_t port_init(void) {
init_mcu(); init_mcu();
delay_init(SysTick);
board_init(); board_init();
// Configure millisecond timer initialization. // Configure millisecond timer initialization.

View File

@ -1,24 +1,40 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
#include <peripheral_clk_config.h> *
* The MIT License (MIT)
#include "hal/include/hal_timer.h" *
#include "hpl/pm/hpl_pm_base.h" * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
#include "hpl/tc/hpl_tc_base.h" *
#include "hpl/gclk/hpl_gclk_base.h" * Permission is hereby granted, free of charge, to any person obtaining a copy
#include "include/component/gclk.h" * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
#include "supervisor/shared/autoreload.h" * 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 "tick.h" #include "tick.h"
#include "peripheral_clk_config.h"
#include "supervisor/shared/autoreload.h"
#include "shared-bindings/microcontroller/Processor.h"
// Global millisecond tick count // Global millisecond tick count
volatile uint64_t ticks_ms = 0; volatile uint64_t ticks_ms = 0;
struct timer_descriptor ms_timer; void SysTick_Handler(void) {
//static struct timer_task task;
void timer_tick(const struct timer_task *const timer_task) {
// SysTick interrupt handler called when the SysTick timer reaches zero // SysTick interrupt handler called when the SysTick timer reaches zero
// (every millisecond). // (every millisecond).
ticks_ms += 1; ticks_ms += 1;
@ -29,18 +45,20 @@ void timer_tick(const struct timer_task *const timer_task) {
} }
void tick_init() { void tick_init() {
#ifdef SAMD21 uint32_t ticks_per_ms = common_hal_mcu_processor_get_frequency() / 1000;
_pm_enable_bus_clock(PM_BUS_APBC, TC5); SysTick_Config(ticks_per_ms);
#endif NVIC_EnableIRQ(SysTick_IRQn);
// _gclk_enable_channel(TC5_GCLK_ID, GCLK_SOURCE_DFLL48M); }
// timer_init(&ms_timer, TC5, _tc_get_timer()); void tick_delay(uint32_t us) {
uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000;
// timer_set_clock_cycles_per_tick(&ms_timer, 48000000 / 1000 - 1); uint32_t us_between_ticks = SysTick->VAL / ticks_per_us;
// task.cb = timer_tick; uint64_t start_ms = ticks_ms;
// task.interval = 1; while (us > 1000) {
// task.mode = TIMER_TASK_REPEAT; while (ticks_ms == start_ms) {}
// timer_add_task(&ms_timer, &task); us -= us_between_ticks;
// start_ms = ticks_ms;
// timer_start(&ms_timer); us_between_ticks = 1000;
}
while (SysTick->VAL > ((1000 - us) * ticks_per_us)) {}
} }

View File

@ -34,4 +34,6 @@ extern struct timer_descriptor ms_timer;
void tick_init(void); void tick_init(void);
void tick_delay(uint32_t us);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_TICK_H #endif // MICROPY_INCLUDED_ATMEL_SAMD_TICK_H