mirror of
https://github.com/micropython/micropython.git
synced 2026-01-28 14:50:21 +01:00
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_c2_c5_c6) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_p4) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_hardfp) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_softfp) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
qemu port / build_and_test_rv64 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / longlong (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / gil_enabled (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / repr_b (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
The implementation uses the object based capabilities, which avoids complication about different flash block sizes. The ROM partition is placed between the text and writable filesystem sections, and the latter size is unchanged. VfsRom sizes are: - NRF51x22: 12K - NRF52832: 128K - NRF52840: 256K - NRF9160: 256K Use frozen `_boot.py` to set up and mount the filesystem, replacing a mix of C-Code and Python code. The mkfs part has been simplified to save code. Tested with Microbit and Arduino Nano Connect. Signed-off-by: Damien George <damien@micropython.org> Signed-off-by: robert-hh <robert@hammelrath.com>
369 lines
9.4 KiB
C
369 lines
9.4 KiB
C
/*
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
* Copyright (c) 2015 Glenn Ruben Bakke
|
|
*
|
|
* 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 <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "py/nlr.h"
|
|
#include "py/mperrno.h"
|
|
#include "py/mphal.h"
|
|
#include "py/lexer.h"
|
|
#include "py/parse.h"
|
|
#include "py/obj.h"
|
|
#include "py/runtime.h"
|
|
#include "py/gc.h"
|
|
#include "py/compile.h"
|
|
#include "py/persistentcode.h"
|
|
#include "extmod/misc.h"
|
|
#include "extmod/modmachine.h"
|
|
#include "shared/runtime/pyexec.h"
|
|
#include "readline.h"
|
|
#include "gccollect.h"
|
|
#include "modmachine.h"
|
|
#include "modmusic.h"
|
|
#include "modules/os/microbitfs.h"
|
|
#include "led.h"
|
|
#include "uart.h"
|
|
#include "nrf.h"
|
|
#include "pin.h"
|
|
#include "spi.h"
|
|
#include "i2c.h"
|
|
#include "adc.h"
|
|
#include "rtcounter.h"
|
|
|
|
#if MICROPY_PY_MACHINE_HW_PWM
|
|
#include "pwm.h"
|
|
#endif
|
|
#include "timer.h"
|
|
|
|
#if BLUETOOTH_SD
|
|
#include "nrf_sdm.h"
|
|
#endif
|
|
|
|
#if (MICROPY_PY_BLE_NUS)
|
|
#include "ble_uart.h"
|
|
#endif
|
|
|
|
#if MICROPY_PY_MACHINE_SOFT_PWM
|
|
#include "ticker.h"
|
|
#include "softpwm.h"
|
|
#endif
|
|
|
|
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC
|
|
#include "usb_cdc.h"
|
|
#endif
|
|
|
|
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
|
|
#include "extmod/vfs_fat.h"
|
|
#include "lib/oofatfs/ff.h"
|
|
#include "extmod/vfs.h"
|
|
#include "flashbdev.h"
|
|
#endif
|
|
|
|
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
|
|
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
|
|
if (lex == NULL) {
|
|
printf("MemoryError: lexer could not allocate memory\n");
|
|
return;
|
|
}
|
|
|
|
nlr_buf_t nlr;
|
|
if (nlr_push(&nlr) == 0) {
|
|
qstr source_name = lex->source_name;
|
|
mp_parse_tree_t pn = mp_parse(lex, input_kind);
|
|
mp_obj_t module_fun = mp_compile(&pn, source_name, true);
|
|
mp_call_function_0(module_fun);
|
|
nlr_pop();
|
|
} else {
|
|
// uncaught exception
|
|
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
|
|
}
|
|
}
|
|
|
|
extern uint32_t _heap_start;
|
|
extern uint32_t _heap_end;
|
|
|
|
void MP_NORETURN _start(void) {
|
|
// Hook for a board to run code at start up, for example check if a
|
|
// bootloader should be entered instead of the main application.
|
|
MICROPY_BOARD_STARTUP();
|
|
|
|
MICROPY_BOARD_EARLY_INIT();
|
|
|
|
soft_reset:
|
|
|
|
#if MICROPY_PY_TIME_TICKS
|
|
rtc1_init_time_ticks();
|
|
#endif
|
|
|
|
led_init();
|
|
|
|
led_state(1, 1); // MICROPY_HW_LED_1 aka MICROPY_HW_LED_RED
|
|
|
|
mp_cstack_init_with_top(&_ram_end, (char *)&_ram_end - (char *)&_heap_end);
|
|
|
|
machine_init();
|
|
|
|
gc_init(&_heap_start, &_heap_end);
|
|
|
|
mp_init();
|
|
readline_init0();
|
|
|
|
#if MICROPY_PY_MACHINE_SPI
|
|
spi_init0();
|
|
#endif
|
|
|
|
#if MICROPY_PY_MACHINE_I2C
|
|
i2c_init0();
|
|
#endif
|
|
|
|
#if MICROPY_PY_MACHINE_ADC
|
|
adc_init0();
|
|
#endif
|
|
|
|
#if MICROPY_PY_MACHINE_HW_PWM
|
|
pwm_init0();
|
|
#endif
|
|
|
|
#if MICROPY_PY_MACHINE_RTCOUNTER
|
|
rtc_init0();
|
|
#endif
|
|
|
|
#if MICROPY_PY_MACHINE_TIMER_NRF
|
|
timer_init0();
|
|
#endif
|
|
|
|
#if MICROPY_PY_MACHINE_UART
|
|
uart_init0();
|
|
#endif
|
|
|
|
#if MICROPY_HW_ENABLE_UART_REPL
|
|
{
|
|
mp_obj_t args[2] = {
|
|
MP_OBJ_NEW_SMALL_INT(MICROPY_HW_UART_REPL),
|
|
MP_OBJ_NEW_SMALL_INT(MICROPY_HW_UART_REPL_BAUD),
|
|
};
|
|
mp_obj_t uart = MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, make_new)((mp_obj_t)&machine_uart_type, MP_ARRAY_SIZE(args), 0, args);
|
|
mp_os_dupterm_obj.fun.var(1, &uart);
|
|
}
|
|
#endif
|
|
|
|
pin_init0();
|
|
|
|
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
|
|
flashbdev_init();
|
|
// Execute _boot.py to set up the filesystem.
|
|
pyexec_frozen_module("_boot.py", false);
|
|
#endif
|
|
|
|
#if MICROPY_MBFS
|
|
microbit_filesystem_init();
|
|
#endif
|
|
|
|
#if MICROPY_HW_HAS_SDCARD
|
|
// if an SD card is present then mount it on /sd/
|
|
if (sdcard_is_present()) {
|
|
// create vfs object
|
|
fs_user_mount_t *vfs = m_new_obj_maybe(fs_user_mount_t);
|
|
if (vfs == NULL) {
|
|
goto no_mem_for_sd;
|
|
}
|
|
vfs->str = "/sd";
|
|
vfs->len = 3;
|
|
vfs->flags = MP_BLOCKDEV_FLAG_FREE_OBJ;
|
|
sdcard_init_vfs(vfs);
|
|
|
|
// put the sd device in slot 1 (it will be unused at this point)
|
|
MP_STATE_PORT(fs_user_mount)[1] = vfs;
|
|
|
|
FRESULT res = f_mount(&vfs->fatfs, vfs->str, 1);
|
|
if (res != FR_OK) {
|
|
printf("MPY: can't mount SD card\n");
|
|
MP_STATE_PORT(fs_user_mount)[1] = NULL;
|
|
m_del_obj(fs_user_mount_t, vfs);
|
|
} else {
|
|
// TODO these should go before the /flash entries in the path
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd));
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib));
|
|
|
|
// use SD card as current directory
|
|
f_chdrive("/sd");
|
|
}
|
|
no_mem_for_sd:;
|
|
}
|
|
#endif
|
|
|
|
// Main script is finished, so now go into REPL mode.
|
|
// The REPL mode can change, or it can request a soft reset.
|
|
int ret_code = 0;
|
|
|
|
#if MICROPY_PY_BLE_NUS
|
|
ble_uart_init0();
|
|
#endif
|
|
|
|
#if MICROPY_PY_MACHINE_SOFT_PWM
|
|
ticker_init0();
|
|
softpwm_init0();
|
|
#endif
|
|
|
|
#if MICROPY_PY_MUSIC
|
|
microbit_music_init0();
|
|
#endif
|
|
#if BOARD_SPECIFIC_MODULES
|
|
board_modules_init0();
|
|
#endif
|
|
|
|
#if MICROPY_PY_MACHINE_SOFT_PWM
|
|
ticker_start();
|
|
pwm_start();
|
|
#endif
|
|
|
|
led_state(1, 0);
|
|
|
|
#if MICROPY_VFS || MICROPY_MBFS || MICROPY_MODULE_FROZEN
|
|
ret_code = pyexec_file_if_exists("boot.py");
|
|
#endif
|
|
|
|
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC
|
|
usb_cdc_init();
|
|
#endif
|
|
|
|
#if MICROPY_VFS || MICROPY_MBFS || MICROPY_MODULE_FROZEN
|
|
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL && ret_code != 0) {
|
|
pyexec_file_if_exists("main.py");
|
|
}
|
|
#endif
|
|
|
|
for (;;) {
|
|
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
|
|
if (pyexec_raw_repl() != 0) {
|
|
break;
|
|
}
|
|
} else {
|
|
ret_code = pyexec_friendly_repl();
|
|
if (ret_code != 0) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
#if MICROPY_PY_MACHINE_HW_PWM
|
|
pwm_deinit_all();
|
|
#endif
|
|
|
|
mp_deinit();
|
|
|
|
printf("MPY: soft reboot\n");
|
|
|
|
#if BLUETOOTH_SD
|
|
sd_softdevice_disable();
|
|
#endif
|
|
|
|
goto soft_reset;
|
|
}
|
|
|
|
#if !MICROPY_VFS
|
|
#if MICROPY_MBFS
|
|
// Use micro:bit filesystem
|
|
mp_lexer_t *mp_lexer_new_from_file(qstr filename) {
|
|
return os_mbfs_new_reader(qstr_str(filename));
|
|
}
|
|
|
|
mp_import_stat_t mp_import_stat(const char *path) {
|
|
return os_mbfs_import_stat(path);
|
|
}
|
|
|
|
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
|
|
return os_mbfs_open(n_args, args);
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
|
|
|
|
#else
|
|
// use dummy functions - no filesystem available
|
|
mp_lexer_t *mp_lexer_new_from_file(qstr filename) {
|
|
mp_raise_OSError(MP_ENOENT);
|
|
}
|
|
|
|
mp_import_stat_t mp_import_stat(const char *path) {
|
|
return MP_IMPORT_STAT_NO_EXIST;
|
|
}
|
|
|
|
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
|
|
mp_raise_OSError(MP_EPERM);
|
|
}
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
void HardFault_Handler(void) {
|
|
#if defined(NRF52_SERIES) || defined(NRF91_SERIES)
|
|
static volatile uint32_t reg;
|
|
static volatile uint32_t reg2;
|
|
static volatile uint32_t bfar;
|
|
reg = SCB->HFSR;
|
|
reg2 = SCB->CFSR;
|
|
bfar = SCB->BFAR;
|
|
for (int i = 0; i < 0; i++) {
|
|
(void)reg;
|
|
(void)reg2;
|
|
(void)bfar;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void MP_NORETURN __fatal_error(const char *msg) {
|
|
while (1) {
|
|
;
|
|
}
|
|
}
|
|
|
|
void nlr_jump_fail(void *val) {
|
|
printf("FATAL: uncaught exception %p\n", val);
|
|
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)val);
|
|
__fatal_error("");
|
|
}
|
|
|
|
void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
|
|
printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
|
|
__fatal_error("Assertion failed");
|
|
}
|
|
|
|
#if MICROPY_EMIT_INLINE_THUMB || MICROPY_ENABLE_NATIVE_CODE
|
|
void *nrf_native_code_commit(void *buf, unsigned int len, void *reloc) {
|
|
(void)len;
|
|
if (reloc) {
|
|
// Native code in RAM must execute from the IRAM region at 0x00800000, and so relocations
|
|
// to text must also point to this region. The MICROPY_MAKE_POINTER_CALLABLE macro will
|
|
// adjust the `buf` address from RAM to IRAM.
|
|
mp_native_relocate(reloc, buf, (uintptr_t)MICROPY_MAKE_POINTER_CALLABLE(buf) & ~1);
|
|
}
|
|
return buf;
|
|
}
|
|
#endif
|