Fix autoreload, neopixel, monotonic_ns and sleep w/o SD
This commit is contained in:
parent
6f60afe8c5
commit
418333979a
2
main.c
2
main.c
@ -428,7 +428,7 @@ int __attribute__((used)) main(void) {
|
|||||||
filesystem_init(safe_mode == NO_SAFE_MODE, false);
|
filesystem_init(safe_mode == NO_SAFE_MODE, false);
|
||||||
|
|
||||||
// displays init after filesystem, since they could share the flash SPI
|
// displays init after filesystem, since they could share the flash SPI
|
||||||
board_init();
|
board_init();
|
||||||
|
|
||||||
// Reset everything and prep MicroPython to run boot.py.
|
// Reset everything and prep MicroPython to run boot.py.
|
||||||
reset_port();
|
reset_port();
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
#include "py/mphal.h"
|
#include "py/mphal.h"
|
||||||
#include "py/mpstate.h"
|
#include "py/mpstate.h"
|
||||||
#include "shared-bindings/neopixel_write/__init__.h"
|
#include "shared-bindings/neopixel_write/__init__.h"
|
||||||
|
#include "supervisor/port.h"
|
||||||
#include "nrf_pwm.h"
|
#include "nrf_pwm.h"
|
||||||
|
|
||||||
// https://github.com/adafruit/Adafruit_NeoPixel/blob/master/Adafruit_NeoPixel.cpp
|
// https://github.com/adafruit/Adafruit_NeoPixel/blob/master/Adafruit_NeoPixel.cpp
|
||||||
@ -103,8 +104,7 @@ void neopixel_write_reset(void) {
|
|||||||
pixels_pattern_heap_size = 0;
|
pixels_pattern_heap_size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t next_start_tick_ms = 0;
|
uint64_t next_start_raw_ticks = 0;
|
||||||
uint32_t next_start_tick_us = 1000;
|
|
||||||
|
|
||||||
void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
|
void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
|
||||||
// To support both the SoftDevice + Neopixels we use the EasyDMA
|
// To support both the SoftDevice + Neopixels we use the EasyDMA
|
||||||
@ -173,8 +173,9 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait to make sure we don't append onto the last transmission.
|
// Wait to make sure we don't append onto the last transmission. This should only be a tick or
|
||||||
// wait_until(next_start_tick_ms, next_start_tick_us);
|
// two.
|
||||||
|
while (port_get_raw_ticks(NULL) < next_start_raw_ticks) {}
|
||||||
|
|
||||||
// Use the identified device to choose the implementation
|
// Use the identified device to choose the implementation
|
||||||
// If a PWM device is available and we have a buffer, use DMA.
|
// If a PWM device is available and we have a buffer, use DMA.
|
||||||
@ -321,11 +322,5 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update the next start.
|
// Update the next start.
|
||||||
// current_tick(&next_start_tick_ms, &next_start_tick_us);
|
next_start_raw_ticks = port_get_raw_ticks(NULL) + 4;
|
||||||
// if (next_start_tick_us < 100) {
|
|
||||||
// next_start_tick_ms += 1;
|
|
||||||
// next_start_tick_us = 100 - next_start_tick_us;
|
|
||||||
// } else {
|
|
||||||
// next_start_tick_us -= 100;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
@ -40,12 +40,12 @@
|
|||||||
static uint32_t rtc_offset = 0;
|
static uint32_t rtc_offset = 0;
|
||||||
|
|
||||||
void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
|
void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
|
||||||
uint64_t ticks_s = port_get_raw_ticks() / 1024;
|
uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024;
|
||||||
timeutils_seconds_since_2000_to_struct_time(rtc_offset + ticks_s, tm);
|
timeutils_seconds_since_2000_to_struct_time(rtc_offset + ticks_s, tm);
|
||||||
}
|
}
|
||||||
|
|
||||||
void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
|
void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
|
||||||
uint64_t ticks_s = port_get_raw_ticks() / 1024;
|
uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024;
|
||||||
uint32_t epoch_s = timeutils_seconds_since_2000(
|
uint32_t epoch_s = timeutils_seconds_since_2000(
|
||||||
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec
|
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec
|
||||||
);
|
);
|
||||||
|
@ -25,18 +25,18 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "py/mphal.h"
|
#include "py/mphal.h"
|
||||||
|
#include "supervisor/port.h"
|
||||||
|
|
||||||
uint64_t common_hal_time_monotonic(void) {
|
uint64_t common_hal_time_monotonic(void) {
|
||||||
return supervisor_ticks_ms64();
|
return supervisor_ticks_ms64();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t common_hal_time_monotonic_ns(void) {
|
uint64_t common_hal_time_monotonic_ns(void) {
|
||||||
uint64_t ms = 0;
|
uint8_t subticks = 0;
|
||||||
uint32_t us_until_ms = 0;
|
uint64_t ticks = port_get_raw_ticks(&subticks);
|
||||||
// FIXME! Re-implement this.
|
// A tick is 976562.5 nanoseconds so multiply it by the base and add half instead of doing float
|
||||||
// current_tick(&ms, &us_until_ms);
|
// math.
|
||||||
// us counts down.
|
return 976562 * ticks + ticks / 2 + 30518 * subticks;
|
||||||
return 1000 * (ms * 1000 + (1000 - us_until_ms));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void common_hal_time_delay_ms(uint32_t delay) {
|
void common_hal_time_delay_ms(uint32_t delay) {
|
||||||
|
@ -68,7 +68,7 @@ static void power_warning_handler(void) {
|
|||||||
const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE(2);
|
const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE(2);
|
||||||
|
|
||||||
const nrfx_rtc_config_t rtc_config = {
|
const nrfx_rtc_config_t rtc_config = {
|
||||||
.prescaler = RTC_FREQ_TO_PRESCALER(1024),
|
.prescaler = RTC_FREQ_TO_PRESCALER(0x8000),
|
||||||
.reliable = 0,
|
.reliable = 0,
|
||||||
.tick_latency = 0,
|
.tick_latency = 0,
|
||||||
.interrupt_priority = 6
|
.interrupt_priority = 6
|
||||||
@ -79,10 +79,11 @@ static volatile uint64_t overflowed_ticks = 0;
|
|||||||
void rtc_handler(nrfx_rtc_int_type_t int_type) {
|
void rtc_handler(nrfx_rtc_int_type_t int_type) {
|
||||||
if (int_type == NRFX_RTC_INT_OVERFLOW) {
|
if (int_type == NRFX_RTC_INT_OVERFLOW) {
|
||||||
overflowed_ticks += (1L<<24);
|
overflowed_ticks += (1L<<24);
|
||||||
}
|
} else if (int_type == NRFX_RTC_INT_TICK && nrfx_rtc_counter_get(&rtc_instance) % 32 == 0) {
|
||||||
// Do things common to all ports when the tick occurs
|
// Do things common to all ports when the tick occurs
|
||||||
if (int_type == NRFX_RTC_INT_TICK) {
|
|
||||||
supervisor_tick();
|
supervisor_tick();
|
||||||
|
} else if (int_type == NRFX_RTC_INT_COMPARE0) {
|
||||||
|
nrfx_rtc_cc_set(&rtc_instance, 0, 0, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,8 +196,12 @@ uint32_t port_get_saved_word(void) {
|
|||||||
return _ebss;
|
return _ebss;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t port_get_raw_ticks(void) {
|
uint64_t port_get_raw_ticks(uint8_t* subticks) {
|
||||||
return overflowed_ticks + nrfx_rtc_counter_get(&rtc_instance);
|
uint32_t rtc = nrfx_rtc_counter_get(&rtc_instance);
|
||||||
|
if (subticks != NULL) {
|
||||||
|
*subticks = (rtc % 32);
|
||||||
|
}
|
||||||
|
return overflowed_ticks + rtc / 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable 1/1024 second tick.
|
// Enable 1/1024 second tick.
|
||||||
@ -213,7 +218,10 @@ void port_interrupt_after_ticks(uint32_t ticks) {
|
|||||||
uint32_t current_ticks = nrfx_rtc_counter_get(&rtc_instance);
|
uint32_t current_ticks = nrfx_rtc_counter_get(&rtc_instance);
|
||||||
uint32_t diff = 3;
|
uint32_t diff = 3;
|
||||||
if (ticks > diff) {
|
if (ticks > diff) {
|
||||||
diff = ticks;
|
diff = ticks * 32;
|
||||||
|
}
|
||||||
|
if (diff > 0xffffff) {
|
||||||
|
diff = 0xffffff;
|
||||||
}
|
}
|
||||||
nrfx_rtc_cc_set(&rtc_instance, 0, current_ticks + diff, true);
|
nrfx_rtc_cc_set(&rtc_instance, 0, current_ticks + diff, true);
|
||||||
}
|
}
|
||||||
@ -225,7 +233,15 @@ void port_sleep_until_interrupt(void) {
|
|||||||
(void) __get_FPSCR();
|
(void) __get_FPSCR();
|
||||||
NVIC_ClearPendingIRQ(FPU_IRQn);
|
NVIC_ClearPendingIRQ(FPU_IRQn);
|
||||||
}
|
}
|
||||||
sd_app_evt_wait();
|
uint8_t sd_enabled;
|
||||||
|
|
||||||
|
sd_softdevice_is_enabled(&sd_enabled);
|
||||||
|
if (sd_enabled) {
|
||||||
|
sd_app_evt_wait();
|
||||||
|
} else {
|
||||||
|
// Call wait for interrupt ourselves if the SD isn't enabled.
|
||||||
|
__WFI();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -70,9 +70,10 @@ uint32_t *port_heap_get_top(void);
|
|||||||
void port_set_saved_word(uint32_t);
|
void port_set_saved_word(uint32_t);
|
||||||
uint32_t port_get_saved_word(void);
|
uint32_t port_get_saved_word(void);
|
||||||
|
|
||||||
// Get the raw tick count since start up. A tick is 1/32768 of a second, a common low frequency
|
// Get the raw tick count since start up. A tick is 1/1024 of a second, a common low frequency
|
||||||
// clock rate.
|
// clock rate. If subticks is not NULL then the port will fill in the number of subticks where each
|
||||||
uint64_t port_get_raw_ticks(void);
|
// tick is 32 subticks (for a resolution of 1/32768 or 30.5ish microseconds.)
|
||||||
|
uint64_t port_get_raw_ticks(uint8_t* subticks);
|
||||||
|
|
||||||
// Enable 1/1024 second tick.
|
// Enable 1/1024 second tick.
|
||||||
void port_enable_tick(void);
|
void port_enable_tick(void);
|
||||||
@ -80,7 +81,8 @@ void port_enable_tick(void);
|
|||||||
// Disable 1/1024 second tick.
|
// Disable 1/1024 second tick.
|
||||||
void port_disable_tick(void);
|
void port_disable_tick(void);
|
||||||
|
|
||||||
// Wake the CPU after the given number of ticks or sooner.
|
// Wake the CPU after the given number of ticks or sooner. Only the last call to this will apply.
|
||||||
|
// Only the common sleep routine should use it.
|
||||||
void port_interrupt_after_ticks(uint32_t ticks);
|
void port_interrupt_after_ticks(uint32_t ticks);
|
||||||
|
|
||||||
// Sleep the CPU until an interrupt is received.
|
// Sleep the CPU until an interrupt is received.
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
|
|
||||||
#include "py/mphal.h"
|
#include "py/mphal.h"
|
||||||
#include "py/reload.h"
|
#include "py/reload.h"
|
||||||
|
#include "supervisor/shared/tick.h"
|
||||||
|
|
||||||
static volatile uint32_t autoreload_delay_ms = 0;
|
static volatile uint32_t autoreload_delay_ms = 0;
|
||||||
static bool autoreload_enabled = false;
|
static bool autoreload_enabled = false;
|
||||||
@ -43,6 +44,7 @@ inline void autoreload_tick() {
|
|||||||
!autoreload_suspended && !reload_requested) {
|
!autoreload_suspended && !reload_requested) {
|
||||||
mp_raise_reload_exception();
|
mp_raise_reload_exception();
|
||||||
reload_requested = true;
|
reload_requested = true;
|
||||||
|
supervisor_disable_tick();
|
||||||
}
|
}
|
||||||
autoreload_delay_ms--;
|
autoreload_delay_ms--;
|
||||||
}
|
}
|
||||||
@ -69,6 +71,9 @@ inline bool autoreload_is_enabled() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void autoreload_start() {
|
void autoreload_start() {
|
||||||
|
if (autoreload_delay_ms == 0) {
|
||||||
|
supervisor_enable_tick();
|
||||||
|
}
|
||||||
autoreload_delay_ms = CIRCUITPY_AUTORELOAD_DELAY_MS;
|
autoreload_delay_ms = CIRCUITPY_AUTORELOAD_DELAY_MS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ void supervisor_tick(void) {
|
|||||||
autoreload_tick();
|
autoreload_tick();
|
||||||
#endif
|
#endif
|
||||||
#ifdef CIRCUITPY_GAMEPAD_TICKS
|
#ifdef CIRCUITPY_GAMEPAD_TICKS
|
||||||
if (!(port_get_raw_ticks() & CIRCUITPY_GAMEPAD_TICKS)) {
|
if (!(port_get_raw_ticks(NULL) & CIRCUITPY_GAMEPAD_TICKS)) {
|
||||||
#if CIRCUITPY_GAMEPAD
|
#if CIRCUITPY_GAMEPAD
|
||||||
gamepad_tick();
|
gamepad_tick();
|
||||||
#endif
|
#endif
|
||||||
@ -66,7 +66,7 @@ void supervisor_tick(void) {
|
|||||||
uint64_t supervisor_ticks_ms64() {
|
uint64_t supervisor_ticks_ms64() {
|
||||||
uint64_t result;
|
uint64_t result;
|
||||||
common_hal_mcu_disable_interrupts();
|
common_hal_mcu_disable_interrupts();
|
||||||
result = port_get_raw_ticks();
|
result = port_get_raw_ticks(NULL);
|
||||||
common_hal_mcu_enable_interrupts();
|
common_hal_mcu_enable_interrupts();
|
||||||
result = result * 1000 / 1024;
|
result = result * 1000 / 1024;
|
||||||
return result;
|
return result;
|
||||||
@ -79,23 +79,24 @@ uint32_t supervisor_ticks_ms32() {
|
|||||||
extern void run_background_tasks(void);
|
extern void run_background_tasks(void);
|
||||||
|
|
||||||
void PLACE_IN_ITCM(supervisor_run_background_tasks_if_tick)() {
|
void PLACE_IN_ITCM(supervisor_run_background_tasks_if_tick)() {
|
||||||
// uint64_t now = port_get_raw_ticks();
|
uint8_t subticks;
|
||||||
|
uint64_t now = port_get_raw_ticks(&subticks);
|
||||||
|
|
||||||
// if (now == background_ticks) {
|
if (now == background_ticks && (subticks & 0x3) != 0) {
|
||||||
// return;
|
return;
|
||||||
// }
|
}
|
||||||
// background_ticks = now;
|
background_ticks = now;
|
||||||
|
|
||||||
run_background_tasks();
|
run_background_tasks();
|
||||||
}
|
}
|
||||||
|
|
||||||
void supervisor_fake_tick() {
|
void supervisor_fake_tick() {
|
||||||
uint32_t now = port_get_raw_ticks();
|
uint32_t now = port_get_raw_ticks(NULL);
|
||||||
background_ticks = (now - 1);
|
background_ticks = (now - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_hal_delay_ms(mp_uint_t delay) {
|
void mp_hal_delay_ms(mp_uint_t delay) {
|
||||||
uint64_t start_tick = port_get_raw_ticks();
|
uint64_t start_tick = port_get_raw_ticks(NULL);
|
||||||
// Adjust the delay to ticks vs ms.
|
// Adjust the delay to ticks vs ms.
|
||||||
delay = delay * 1024 / 1000;
|
delay = delay * 1024 / 1000;
|
||||||
uint64_t duration = 0;
|
uint64_t duration = 0;
|
||||||
@ -110,7 +111,23 @@ void mp_hal_delay_ms(mp_uint_t delay) {
|
|||||||
// Sleep until an interrupt happens.
|
// Sleep until an interrupt happens.
|
||||||
port_sleep_until_interrupt();
|
port_sleep_until_interrupt();
|
||||||
// asm("bkpt");
|
// asm("bkpt");
|
||||||
duration = (port_get_raw_ticks() - start_tick);
|
duration = (port_get_raw_ticks(NULL) - start_tick);
|
||||||
|
port_interrupt_after_ticks(duration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
volatile size_t tick_enable_count = 0;
|
||||||
|
extern void supervisor_enable_tick(void) {
|
||||||
|
if (tick_enable_count == 0) {
|
||||||
|
port_enable_tick();
|
||||||
|
}
|
||||||
|
tick_enable_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern void supervisor_disable_tick(void) {
|
||||||
|
tick_enable_count--;
|
||||||
|
if (tick_enable_count == 0) {
|
||||||
|
port_disable_tick();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,4 +64,7 @@ extern uint64_t supervisor_ticks_ms64(void);
|
|||||||
*/
|
*/
|
||||||
extern void supervisor_run_background_if_tick(void);
|
extern void supervisor_run_background_if_tick(void);
|
||||||
|
|
||||||
|
extern void supervisor_enable_tick(void);
|
||||||
|
extern void supervisor_disable_tick(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user