Skip to content

Update time to use SysTick #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions atmel-samd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,12 @@ SRC_C = \

SRC_COMMON_HAL = \
board/__init__.c \
digitalio/__init__.c \
digitalio/DigitalInOut.c \
microcontroller/__init__.c \
microcontroller/Pin.c \
microcontroller/Processor.c \
digitalio/__init__.c \
digitalio/DigitalInOut.c
time/__init__.c
# analogio/__init__.c \
analogio/AnalogIn.c \
analogio/AnalogOut.c \
Expand All @@ -238,7 +239,6 @@ SRC_COMMON_HAL = \
pulseio/PulseOut.c \
pulseio/PWMOut.c \
storage/__init__.c \
time/__init__.c \
touchio/__init__.c \
touchio/TouchIn.c \
usb_hid/__init__.c \
Expand Down
2 changes: 1 addition & 1 deletion atmel-samd/asf4_conf/samd21/peripheral_clk_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* \brief CPU's Clock frequency
*/
#ifndef CONF_CPU_FREQUENCY
#define CONF_CPU_FREQUENCY 1000000
#define CONF_CPU_FREQUENCY 48000000
#endif

// <y> Core Clock Source
Expand Down
5 changes: 3 additions & 2 deletions atmel-samd/common-hal/microcontroller/Processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

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

#include "peripheral_clk_config.h"

// #define ADC_TEMP_SAMPLE_LENGTH 4
// #define INT1V_VALUE_FLOAT 1.0
Expand Down Expand Up @@ -226,6 +227,6 @@ float common_hal_mcu_processor_get_temperature(void) {


uint32_t common_hal_mcu_processor_get_frequency(void) {
return 0;
//return system_cpu_clock_get_hz();
// TODO(tannewt): Determine this dynamically.
return CONF_CPU_FREQUENCY;
}
6 changes: 3 additions & 3 deletions atmel-samd/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_analogio), (mp_obj_t)&analogio_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_usb_hid),(mp_obj_t)&usb_hid_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module },
Expand All @@ -213,9 +212,10 @@ extern const struct _mp_obj_module_t usb_hid_module;


#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_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

#define MICROPY_PORT_BUILTIN_DEBUG_MODULES \
Expand Down
3 changes: 2 additions & 1 deletion atmel-samd/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "mpconfigboard.h"
#include "mphalport.h"
#include "reset.h"
#include "tick.h"
#include "usb.h"

extern struct usart_module usart_instance;
Expand Down Expand Up @@ -64,7 +65,7 @@ void mp_hal_delay_ms(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) {
Expand Down
2 changes: 0 additions & 2 deletions atmel-samd/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ safe_mode_t port_init(void) {

init_mcu();

delay_init(SysTick);

board_init();

// Configure millisecond timer initialization.
Expand Down
70 changes: 44 additions & 26 deletions atmel-samd/tick.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* 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 "tick.h"

#include <peripheral_clk_config.h>

#include "hal/include/hal_timer.h"
#include "hpl/pm/hpl_pm_base.h"
#include "hpl/tc/hpl_tc_base.h"
#include "hpl/gclk/hpl_gclk_base.h"
#include "include/component/gclk.h"
#include "peripheral_clk_config.h"

#include "supervisor/shared/autoreload.h"

#include "tick.h"
#include "shared-bindings/microcontroller/Processor.h"

// Global millisecond tick count
volatile uint64_t ticks_ms = 0;

struct timer_descriptor ms_timer;
//static struct timer_task task;

void timer_tick(const struct timer_task *const timer_task) {
void SysTick_Handler(void) {
// SysTick interrupt handler called when the SysTick timer reaches zero
// (every millisecond).
ticks_ms += 1;
Expand All @@ -29,18 +45,20 @@ void timer_tick(const struct timer_task *const timer_task) {
}

void tick_init() {
#ifdef SAMD21
_pm_enable_bus_clock(PM_BUS_APBC, TC5);
#endif
// _gclk_enable_channel(TC5_GCLK_ID, GCLK_SOURCE_DFLL48M);

// timer_init(&ms_timer, TC5, _tc_get_timer());
uint32_t ticks_per_ms = common_hal_mcu_processor_get_frequency() / 1000;
SysTick_Config(ticks_per_ms);
NVIC_EnableIRQ(SysTick_IRQn);
}

// timer_set_clock_cycles_per_tick(&ms_timer, 48000000 / 1000 - 1);
// task.cb = timer_tick;
// task.interval = 1;
// task.mode = TIMER_TASK_REPEAT;
// timer_add_task(&ms_timer, &task);
//
// timer_start(&ms_timer);
void tick_delay(uint32_t us) {
uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000;
uint32_t us_between_ticks = SysTick->VAL / ticks_per_us;
uint64_t start_ms = ticks_ms;
while (us > 1000) {
while (ticks_ms == start_ms) {}
us -= us_between_ticks;
start_ms = ticks_ms;
us_between_ticks = 1000;
}
while (SysTick->VAL > ((1000 - us) * ticks_per_us)) {}
}
2 changes: 2 additions & 0 deletions atmel-samd/tick.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ extern struct timer_descriptor ms_timer;

void tick_init(void);

void tick_delay(uint32_t us);

#endif // MICROPY_INCLUDED_ATMEL_SAMD_TICK_H