Skip to content

Commit 48b5f2a

Browse files
committed
Initial work on SAMD
1 parent affd3fc commit 48b5f2a

19 files changed

Lines changed: 139 additions & 316 deletions

File tree

ports/atmel-samd/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ SRC_ASF := \
194194
hpl/gclk/hpl_gclk.c \
195195
hpl/nvmctrl/hpl_nvmctrl.c \
196196
hpl/pm/hpl_pm.c \
197-
hpl/rtc/hpl_rtc.c \
198197
hpl/sercom/hpl_sercom.c \
199198
hpl/systick/hpl_systick.c \
200199
hal/utils/src/utils_list.c \
@@ -258,7 +257,6 @@ SRC_C = \
258257
peripherals/samd/timers.c \
259258
reset.c \
260259
supervisor/shared/memory.c \
261-
tick.c \
262260
timer_handler.c \
263261

264262

ports/atmel-samd/background.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
#include "background.h"
2727

2828
#include "audio_dma.h"
29-
#include "tick.h"
3029
#include "supervisor/filesystem.h"
3130
#include "supervisor/shared/tick.h"
3231
#include "supervisor/usb.h"
3332

3433
#include "py/runtime.h"
3534
#include "shared-module/network/__init__.h"
3635
#include "supervisor/shared/stack.h"
36+
#include "supervisor/port.h"
3737

3838
#ifdef CIRCUITPY_DISPLAYIO
3939
#include "shared-module/displayio/__init__.h"
@@ -92,10 +92,10 @@ void run_background_tasks(void) {
9292
running_background_tasks = false;
9393
assert_heap_ok();
9494

95-
last_finished_tick = supervisor_ticks_ms64();
95+
last_finished_tick = port_get_raw_ticks(NULL);
9696
finish_background_task();
9797
}
9898

9999
bool background_tasks_ok(void) {
100-
return supervisor_ticks_ms64() - last_finished_tick < 1000;
100+
return port_get_raw_ticks(NULL) - last_finished_tick < 1024;
101101
}

ports/atmel-samd/common-hal/audiobusio/PDMIn.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
#include "samd/dma.h"
5050

5151
#include "audio_dma.h"
52-
#include "tick.h"
5352

5453
#define OVERSAMPLING 64
5554
#define SAMPLES_PER_BUFFER 32

ports/atmel-samd/common-hal/displayio/ParallelBus.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
#include "shared-bindings/digitalio/DigitalInOut.h"
3434
#include "shared-bindings/microcontroller/__init__.h"
3535

36-
#include "tick.h"
37-
3836
void common_hal_displayio_parallelbus_construct(displayio_parallelbus_obj_t* self,
3937
const mcu_pin_obj_t* data0, const mcu_pin_obj_t* command, const mcu_pin_obj_t* chip_select,
4038
const mcu_pin_obj_t* write, const mcu_pin_obj_t* read, const mcu_pin_obj_t* reset) {

ports/atmel-samd/common-hal/neopixel_write/__init__.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#include "shared-bindings/neopixel_write/__init__.h"
3131

32-
#include "tick.h"
32+
#include "supervisor/port.h"
3333

3434
#ifdef SAMD51
3535
#include "hri/hri_cmcc_d51.h"
@@ -91,8 +91,7 @@ static void neopixel_send_buffer_core(volatile uint32_t *clraddr, uint32_t pinMa
9191
"");
9292
}
9393

94-
uint64_t next_start_tick_ms = 0;
95-
uint32_t next_start_tick_us = 1000;
94+
uint64_t next_start_raw_ticks = 0;
9695

9796
void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
9897
// This is adapted directly from the Adafruit NeoPixel library SAMD21G18A code:
@@ -101,9 +100,9 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout,
101100
uint32_t pinMask;
102101
PortGroup* port;
103102

104-
// This must be called while interrupts are on in case we're waiting for a
105-
// future ms tick.
106-
wait_until(next_start_tick_ms, next_start_tick_us);
103+
// Wait to make sure we don't append onto the last transmission. This should only be a tick or
104+
// two.
105+
while (port_get_raw_ticks(NULL) < next_start_raw_ticks) {}
107106

108107
// Turn off interrupts of any kind during timing-sensitive code.
109108
mp_hal_disable_all_interrupts();
@@ -144,15 +143,8 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout,
144143

145144
#endif
146145

147-
// ticks_ms may be out of date at this point because we stopped the
148-
// interrupt. We'll risk it anyway.
149-
current_tick(&next_start_tick_ms, &next_start_tick_us);
150-
if (next_start_tick_us < 100) {
151-
next_start_tick_ms += 1;
152-
next_start_tick_us = 100 - next_start_tick_us;
153-
} else {
154-
next_start_tick_us -= 100;
155-
}
146+
// Update the next start.
147+
next_start_raw_ticks = port_get_raw_ticks(NULL) + 4;
156148

157149
// Turn on interrupts after timing-sensitive code.
158150
mp_hal_enable_all_interrupts();

ports/atmel-samd/common-hal/ps2io/Ps2.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@
4141
#include "samd/pins.h"
4242
#include "shared-bindings/microcontroller/__init__.h"
4343
#include "shared-bindings/ps2io/Ps2.h"
44+
#include "supervisor/port.h"
4445
#include "supervisor/shared/translate.h"
4546

46-
#include "tick.h"
47-
4847
#define STATE_IDLE 0
4948
#define STATE_RECV 1
5049
#define STATE_RECV_PARITY 2
@@ -168,24 +167,21 @@ static void delay_us(uint32_t t) {
168167

169168
void ps2_interrupt_handler(uint8_t channel) {
170169
// Grab the current time first.
171-
uint32_t current_us;
172-
uint64_t current_ms;
173-
current_tick(&current_ms, &current_us);
170+
uint64_t current_tick = port_get_raw_ticks(NULL);
174171

175172
ps2io_ps2_obj_t* self = get_eic_channel_data(channel);
176173
int data_bit = gpio_get_pin_level(self->data_pin) ? 1 : 0;
177174

178175
// test for timeout
179176
if (self->state != STATE_IDLE) {
180-
int64_t diff_ms = current_ms - self->last_int_ms;
181-
if (diff_ms >= 2) { // a.k.a. > 1.001ms
177+
int64_t diff_ms = current_tick - self->last_raw_ticks;
178+
if (diff_ms > 1) { // a.k.a. > 1.001ms
182179
self->last_errors |= ERROR_TIMEOUT;
183180
self->state = STATE_IDLE;
184181
}
185182
}
186183

187-
self->last_int_us = current_us;
188-
self->last_int_ms = current_ms;
184+
self->last_raw_ticks = current_tick;
189185

190186
if (self->state == STATE_IDLE) {
191187
self->bits = 0;

ports/atmel-samd/common-hal/ps2io/Ps2.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ typedef struct {
3939
uint8_t data_pin;
4040

4141
uint8_t state;
42-
uint64_t last_int_ms;
43-
uint32_t last_int_us;
42+
uint64_t last_raw_ticks;
4443

4544
uint16_t bits;
4645
bool parity;

ports/atmel-samd/common-hal/pulseio/PulseIn.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
#include "shared-bindings/pulseio/PulseIn.h"
4343
#include "supervisor/shared/translate.h"
4444

45-
#include "tick.h"
46-
4745
static void pulsein_set_config(pulseio_pulsein_obj_t* self, bool first_edge) {
4846
uint32_t sense_setting;
4947
if (!first_edge) {
@@ -61,9 +59,9 @@ static void pulsein_set_config(pulseio_pulsein_obj_t* self, bool first_edge) {
6159

6260
void pulsein_interrupt_handler(uint8_t channel) {
6361
// Grab the current time first.
64-
uint32_t current_us;
65-
uint64_t current_ms;
66-
current_tick(&current_ms, &current_us);
62+
uint32_t current_us = 0;
63+
uint64_t current_ms = 0;
64+
// current_tick(&current_ms, &current_us);
6765

6866
// current_tick gives us the remaining us until the next tick but we want the number since the
6967
// last ms.

ports/atmel-samd/common-hal/rtc/RTC.c

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,76 +30,46 @@
3030
#include <hal_init.h>
3131
#include <hpl_gclk_base.h>
3232
#include <hpl_pm_base.h>
33-
#include <hal_calendar.h>
3433

3534
#include "py/obj.h"
3635
#include "py/runtime.h"
3736
#include "lib/timeutils/timeutils.h"
3837
#include "shared-bindings/rtc/__init__.h"
38+
#include "supervisor/port.h"
3939
#include "supervisor/shared/translate.h"
4040

41-
static struct calendar_descriptor calendar;
42-
43-
void rtc_init(void) {
44-
#ifdef SAMD21
45-
_gclk_enable_channel(RTC_GCLK_ID, CONF_GCLK_RTC_SRC);
46-
#endif
47-
#ifdef SAMD51
48-
hri_mclk_set_APBAMASK_RTC_bit(MCLK);
49-
#endif
50-
calendar_init(&calendar, RTC);
51-
calendar_set_baseyear(&calendar, 2000);
52-
calendar_enable(&calendar);
53-
}
41+
// This is the time in seconds since 2000 that the RTC was started.
42+
static uint32_t rtc_offset = 0;
5443

5544
void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
56-
struct calendar_date_time datetime;
57-
calendar_get_date_time(&calendar, &datetime);
58-
59-
tm->tm_year = datetime.date.year;
60-
tm->tm_mon = datetime.date.month;
61-
tm->tm_mday = datetime.date.day;
62-
tm->tm_hour = datetime.time.hour;
63-
tm->tm_min = datetime.time.min;
64-
tm->tm_sec = datetime.time.sec;
45+
uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024;
46+
timeutils_seconds_since_2000_to_struct_time(rtc_offset + ticks_s, tm);
6547
}
6648

6749
void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
68-
// Reset prescaler to increase initial precision. Otherwise we can be up to 1 second off already.
69-
uint32_t freqcorr = hri_rtcmode0_read_FREQCORR_reg(calendar.device.hw);
70-
calendar_deinit(&calendar);
71-
rtc_init();
72-
hri_rtcmode0_write_FREQCORR_reg(calendar.device.hw, freqcorr);
73-
74-
struct calendar_date date = {
75-
.year = tm->tm_year,
76-
.month = tm->tm_mon,
77-
.day = tm->tm_mday,
78-
};
79-
calendar_set_date(&calendar, &date);
80-
81-
struct calendar_time time = {
82-
.hour = tm->tm_hour,
83-
.min = tm->tm_min,
84-
.sec = tm->tm_sec,
85-
};
86-
calendar_set_time(&calendar, &time);
50+
uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024;
51+
uint32_t epoch_s = timeutils_seconds_since_2000(
52+
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec
53+
);
54+
rtc_offset = epoch_s - ticks_s;
8755
}
8856

8957
// A positive value speeds up the clock by removing clock cycles.
9058
int common_hal_rtc_get_calibration(void) {
91-
int calibration = hri_rtcmode0_read_FREQCORR_VALUE_bf(calendar.device.hw);
59+
int calibration = hri_rtcmode0_read_FREQCORR_VALUE_bf(RTC);
9260

93-
if (!hri_rtcmode0_get_FREQCORR_SIGN_bit(calendar.device.hw))
61+
if (!hri_rtcmode0_get_FREQCORR_SIGN_bit(RTC)) {
9462
calibration = -calibration;
63+
}
9564

9665
return calibration;
9766
}
9867

9968
void common_hal_rtc_set_calibration(int calibration) {
100-
if (calibration > 127 || calibration < -127)
69+
if (calibration > 127 || calibration < -127) {
10170
mp_raise_ValueError(translate("calibration value out of range +/-127"));
71+
}
10272

103-
hri_rtcmode0_write_FREQCORR_SIGN_bit(calendar.device.hw, calibration < 0 ? 0 : 1);
104-
hri_rtcmode0_write_FREQCORR_VALUE_bf(calendar.device.hw, abs(calibration));
73+
hri_rtcmode0_write_FREQCORR_SIGN_bit(RTC, calibration < 0 ? 0 : 1);
74+
hri_rtcmode0_write_FREQCORR_VALUE_bf(RTC, abs(calibration));
10575
}

ports/atmel-samd/common-hal/rtc/RTC.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,4 @@
2727
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_RTC_RTC_H
2828
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_RTC_RTC_H
2929

30-
extern void rtc_init(void);
31-
3230
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_RTC_RTC_H

0 commit comments

Comments
 (0)