diff --git a/ports/litex/.gitignore b/ports/litex/.gitignore new file mode 100644 index 0000000000..414487d53e --- /dev/null +++ b/ports/litex/.gitignore @@ -0,0 +1 @@ +build-*/ diff --git a/ports/litex/Makefile b/ports/litex/Makefile index 51cb186356..596eaf2bb3 100644 --- a/ports/litex/Makefile +++ b/ports/litex/Makefile @@ -116,7 +116,6 @@ SRC_C += \ background.c \ fatfs_port.c \ mphalport.c \ - tick.c \ boards/$(BOARD)/board.c \ boards/$(BOARD)/pins.c \ lib/libc/string0.c \ diff --git a/ports/litex/README.rst b/ports/litex/README.rst new file mode 100644 index 0000000000..d3c66b22cd --- /dev/null +++ b/ports/litex/README.rst @@ -0,0 +1,13 @@ +You'll need `dfu-util` to install CircuitPython on the Fomu. + +Make sure the foboot bootloader is updated. Instructions are here: https://github.com/im-tomu/fomu-workshop/blob/master/docs/bootloader.rst + +Once you've updated the bootloader, you should know how to use `dfu-util`. It's pretty easy! + +To install CircuitPython do: + +.. code-block:: shell + + dfu-util -D adafruit-circuitpython-fomu-en_US-.dfu + +It will install and then restart. CIRCUITPY should appear as it usually does and work the same. diff --git a/ports/litex/mphalport.c b/ports/litex/mphalport.c index 005a65aac4..12eaaac3cc 100644 --- a/ports/litex/mphalport.c +++ b/ports/litex/mphalport.c @@ -40,26 +40,6 @@ void hal_dcd_isr(uint8_t rhport); #endif -/*------------------------------------------------------------------*/ -/* delay - *------------------------------------------------------------------*/ -void mp_hal_delay_ms(mp_uint_t delay) { - uint64_t start_tick = supervisor_ticks_ms64(); - uint64_t duration = 0; - while (duration < delay) { - #ifdef MICROPY_VM_HOOK_LOOP - MICROPY_VM_HOOK_LOOP - #endif - // Check to see if we've been CTRL-Ced by autoreload or the user. - if(MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)) || - MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) { - break; - } - duration = (supervisor_ticks_ms64() - start_tick); - // TODO(tannewt): Go to sleep for a little while while we wait. - } -} - extern void SysTick_Handler(void); __attribute__((section(".ramtext"))) diff --git a/ports/litex/supervisor/internal_flash.c b/ports/litex/supervisor/internal_flash.c index 93aeda8cbd..2dbf46b3a6 100644 --- a/ports/litex/supervisor/internal_flash.c +++ b/ports/litex/supervisor/internal_flash.c @@ -37,6 +37,7 @@ #include "py/runtime.h" #include "lib/oofatfs/ff.h" +#include "supervisor/flash.h" #include "supervisor/usb.h" #include "csr.h" @@ -270,7 +271,7 @@ uint32_t supervisor_flash_get_block_count(void) { } __attribute__((section(".ramtext"))) -void supervisor_flash_flush(void) { +void port_internal_flash_flush(void) { // Skip if data is the same, or if there is no data in the cache if (_flash_page_addr == NO_CACHE) return; diff --git a/ports/litex/supervisor/port.c b/ports/litex/supervisor/port.c index 9688c7baef..c2af0d2a93 100644 --- a/ports/litex/supervisor/port.c +++ b/ports/litex/supervisor/port.c @@ -27,11 +27,43 @@ #include #include "supervisor/port.h" +#include "supervisor/shared/tick.h" #include "boards/board.h" -#include "tick.h" #include "irq.h" #include "csr.h" +// Global millisecond tick count. 1024 per second because most RTCs are clocked with 32.768khz +// crystals. +volatile uint64_t raw_ticks = 0; +volatile int subsecond = 0; +__attribute__((section(".ramtext"))) +void SysTick_Handler(void) { + timer0_ev_pending_write(1); + raw_ticks += 1; + subsecond += 1; + // We track subsecond ticks so that we can increment raw_ticks one extra every 40 ms. We do this + // every 40 except 0 to make it 24 increments and not 25. + if (subsecond == 1000) { + subsecond = 0; + } else if (subsecond % 40 == 0) { + raw_ticks += 1; + } + supervisor_tick(); +} + +static void tick_init(void) { + int t; + + timer0_en_write(0); + t = CONFIG_CLOCK_FREQUENCY / 1000; // 1000 kHz tick + timer0_reload_write(t); + timer0_load_write(t); + timer0_en_write(1); + timer0_ev_enable_write(1); + timer0_ev_pending_write(1); + irq_setmask(irq_getmask() | (1 << TIMER0_INTERRUPT)); +} + safe_mode_t port_init(void) { irq_setmask(0); irq_setie(1); @@ -83,3 +115,21 @@ void port_set_saved_word(uint32_t value) { uint32_t port_get_saved_word(void) { return _ebss; } + +uint64_t port_get_raw_ticks(uint8_t* subticks) { + return raw_ticks; +} + +// Enable 1/1024 second tick. +void port_enable_tick(void) { +} + +// Disable 1/1024 second tick. +void port_disable_tick(void) { +} + +void port_interrupt_after_ticks(uint32_t ticks) { +} + +void port_sleep_until_interrupt(void) { +} diff --git a/ports/litex/supervisor/usb.c b/ports/litex/supervisor/usb.c index 182360b713..b626aaf496 100644 --- a/ports/litex/supervisor/usb.c +++ b/ports/litex/supervisor/usb.c @@ -25,8 +25,6 @@ * THE SOFTWARE. */ - -#include "tick.h" #include "supervisor/usb.h" #include "lib/utils/interrupt_char.h" #include "lib/mp-readline/readline.h" diff --git a/ports/litex/tick.c b/ports/litex/tick.c deleted file mode 100644 index 8ba06044ac..0000000000 --- a/ports/litex/tick.c +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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 "csr.h" -#include "tick.h" -#include "irq.h" - -#include "supervisor/shared/autoreload.h" -#include "supervisor/filesystem.h" -#include "supervisor/shared/tick.h" -#include "shared-module/gamepad/__init__.h" -#include "shared-bindings/microcontroller/Processor.h" - -// Global millisecond tick count -// volatile uint64_t ticks_ms = 0; - -__attribute__((section(".ramtext"))) -void SysTick_Handler(void) { - timer0_ev_pending_write(1); - supervisor_tick(); -} - -void tick_init() { - int t; - - timer0_en_write(0); - t = CONFIG_CLOCK_FREQUENCY / 1000; // 1000 kHz tick - timer0_reload_write(t); - timer0_load_write(t); - timer0_en_write(1); - timer0_ev_enable_write(1); - timer0_ev_pending_write(1); - irq_setmask(irq_getmask() | (1 << TIMER0_INTERRUPT)); -} - -void tick_delay(uint32_t us) { - // uint32_t ticks_per_us = SystemCoreClock / 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 > ((us_between_ticks - us) * ticks_per_us)) {} -} - -// us counts down! -void current_tick(uint64_t* ms, uint32_t* us_until_ms) { - // uint32_t ticks_per_us = SystemCoreClock / 1000 / 1000; - // *ms = ticks_ms; - // *us_until_ms = SysTick->VAL / ticks_per_us; -} - -void wait_until(uint64_t ms, uint32_t us_until_ms) { - // uint32_t ticks_per_us = SystemCoreClock / 1000 / 1000; - // while(ticks_ms <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {} -} diff --git a/ports/litex/tick.h b/ports/litex/tick.h deleted file mode 100644 index b4d27b8416..0000000000 --- a/ports/litex/tick.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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. - */ -#ifndef MICROPY_INCLUDED_LITEX_TICK_H -#define MICROPY_INCLUDED_LITEX_TICK_H - -#include "py/mpconfig.h" - -#include - -extern volatile uint64_t ticks_ms; - -extern struct timer_descriptor ms_timer; - -void tick_init(void); - -void tick_delay(uint32_t us); - -void current_tick(uint64_t* ms, uint32_t* us_until_ms); -// Do not call this with interrupts disabled because it may be waiting for -// ticks_ms to increment. -void wait_until(uint64_t ms, uint32_t us_until_ms); - -#endif // MICROPY_INCLUDED_LITEX_TICK_H