samd: Integrate latest asf4, add help, more time funcs and uPy features.

- Makefile: update to use new ASF4 files, support frozen manifest, and
  include source files in upcoming commits
- boards/manifest.py: add files to freeze
- boards/samd51p19a.ld: add linker script for this MCU
- help.c: add custom help text
- main.c: execute _boot.py, boot.py and main.py on start-up
- modules/_boot.py: startup file to freeze
- modutime.c: add gmtime, localtime, mktime, time functions
- mpconfigport.h: enabled more features for sys and io and modules
- mphalport.h: add mp_hal_pin_xxx macros
- mphalport.c: add mp_hal_stdio_poll
This commit is contained in:
Peter van der Burg 2021-05-20 18:30:08 +10:00 committed by Damien George
parent f27be2d78a
commit cd2223b8fe
10 changed files with 287 additions and 5 deletions

View File

@ -16,6 +16,10 @@ include $(BOARD_DIR)/mpconfigboard.mk
QSTR_DEFS = qstrdefsport.h
QSTR_GLOBAL_DEPENDENCIES = $(BOARD_DIR)/mpconfigboard.h
MCU_SERIES_LOWER = $(shell echo $(MCU_SERIES) | tr '[:upper:]' '[:lower:]')
FROZEN_MANIFEST ?= boards/manifest.py
# Include py core make definitions
include $(TOP)/py/py.mk
@ -26,14 +30,25 @@ INC += -I$(TOP)
INC += -I$(BUILD)
INC += -I$(BOARD_DIR)
INC += -I$(TOP)/lib/cmsis/inc
INC += -I$(TOP)/lib/asf4/$(shell echo $(MCU_SERIES) | tr '[:upper:]' '[:lower:]')/include
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hal/include
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hal/utils/include
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/config
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hri
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hpl/core
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hpl/gclk
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hpl/pm
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hpl/port
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hpl/rtc
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hpl/tc
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/include
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/include/pio
INC += -I$(TOP)/lib/tinyusb/src
CFLAGS_MCU_SAMD21 = -mtune=cortex-m0plus -mcpu=cortex-m0plus -msoft-float
CFLAGS_MCU_SAMD51 = -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard
CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib -mthumb $(CFLAGS_MCU_$(MCU_SERIES)) -fsingle-precision-constant -Wdouble-promotion
CFLAGS += -DMCU_$(MCU_SERIES) -D__$(CMSIS_MCU)__
CFLAGS += $(CFLAGS_MOD)
CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA)
LDFLAGS = -nostdlib $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref
LDFLAGS += $(LDFLAGS_MOD)
@ -59,12 +74,22 @@ endif
SRC_C = \
main.c \
help.c \
moduos.c \
modutime.c \
modmachine.c \
$(BOARD_DIR)/pins.c \
machine_pin.c \
machine_led.c \
modsamd.c \
samd_flash.c \
mphalport.c \
samd_isr.c \
samd_soc.c \
tusb_port.c \
lib/asf4/$(MCU_SERIES_LOWER)/hal/src/hal_atomic.c \
lib/asf4/$(MCU_SERIES_LOWER)/hal/src/hal_flash.c \
lib/asf4/$(MCU_SERIES_LOWER)/hpl/nvmctrl/hpl_nvmctrl.c \
lib/libm/ef_sqrt.c \
lib/libm/fmodf.c \
lib/libm/math.c \
@ -81,6 +106,8 @@ SRC_C = \
shared/runtime/gchelper_native.c \
shared/runtime/pyexec.c \
shared/runtime/stdout_helpers.c \
shared/runtime/sys_stdio_mphal.c \
shared/timeutils/timeutils.c \
SRC_C += $(SRC_MOD)
@ -93,12 +120,27 @@ SRC_S = shared/runtime/gchelper_m3.s
endif
# List of sources for qstr extraction
SRC_QSTR += modutime.c modmachine.c $(SRC_MOD) $(SRC_CXX)
SRC_QSTR += moduos.c \
modutime.c \
modmachine.c \
machine_pin.c \
machine_led.c \
modsamd.c \
samd_flash.c \
SRC_QSTR += $(SRC_MOD) $(SRC_CXX)
OBJ += $(PY_O)
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
ifneq ($(FROZEN_MANIFEST),)
CFLAGS += -DMICROPY_MODULE_FROZEN_MPY
CFLAGS += -DMICROPY_MODULE_FROZEN_STR
CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool
endif
# Workaround for bug in older gcc, warning on "static usbd_device_t _usbd_dev = { 0 };"
$(BUILD)/lib/tinyusb/src/device/usbd.o: CFLAGS += -Wno-missing-braces

View File

@ -0,0 +1 @@
freeze("$(PORT_DIR)/modules")

View File

@ -0,0 +1,17 @@
/*
GNU linker script for SAMD51
*/
/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x00004000, LENGTH = 512K - 16K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K
}
/* Top end of the stack, with room for double-tap variable */
_estack = ORIGIN(RAM) + LENGTH(RAM) - 8;
_sstack = _estack - 16K;
_sheap = _ebss;
_eheap = _sstack;

42
ports/samd/help.c Normal file
View File

@ -0,0 +1,42 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013-2016 Damien P. George
*
* 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 "py/builtin.h"
const char samd_help_text[] =
"Welcome to MicroPython!\n"
"\n"
"For online docs please visit http://docs.micropython.org/en/latest/samd/ .\n"
"\n"
"Control commands:\n"
" CTRL-A -- on a blank line, enter raw REPL mode\n"
" CTRL-B -- on a blank line, enter normal REPL mode\n"
" CTRL-C -- interrupt a running program\n"
" CTRL-D -- on a blank line, do a soft reset of the board\n"
" CTRL-E -- on a blank line, enter paste mode\n"
"\n"
"For further help on a specific object, type help('obj')\n"
;

View File

@ -45,6 +45,13 @@ void samd_main(void) {
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
// Execute _boot.py to set up the filesystem.
pyexec_frozen_module("_boot.py");
// Execute user scripts.
pyexec_file_if_exists("boot.py");
pyexec_file_if_exists("main.py");
for (;;) {
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
if (pyexec_raw_repl() != 0) {
@ -69,10 +76,13 @@ void gc_collect(void) {
gc_collect_end();
}
/*
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
mp_raise_OSError(MP_ENOENT);
}
*/
#if !MICROPY_VFS
mp_import_stat_t mp_import_stat(const char *path) {
return MP_IMPORT_STAT_NO_EXIST;
}
@ -81,6 +91,7 @@ mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs)
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
#endif
void nlr_jump_fail(void *val) {
for (;;) {

View File

@ -0,0 +1,17 @@
import gc
import uos
import samd
samd.Flash.flash_init()
bdev = samd.Flash()
# Try to mount the filesystem, and format the flash if it doesn't exist.
try:
vfs = uos.VfsLfs1(bdev)
except:
uos.VfsLfs1.mkfs(bdev)
vfs = uos.VfsLfs1(bdev)
uos.mount(vfs, "/")
gc.collect()
del uos, vfs, gc

View File

@ -24,11 +24,64 @@
* THE SOFTWARE.
*/
#include "py/runtime.h"
#include "extmod/utime_mphal.h"
#include "shared/timeutils/timeutils.h"
// localtime([secs])
STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
timeutils_struct_time_t tm;
mp_int_t seconds;
if (n_args == 0 || args[0] == mp_const_none) {
// seconds = pyb_rtc_get_us_since_epoch() / 1000 / 1000;
seconds = mp_obj_get_int(args[0]);
} else {
seconds = mp_obj_get_int(args[0]);
}
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
mp_obj_t tuple[8] = {
tuple[0] = mp_obj_new_int(tm.tm_year),
tuple[1] = mp_obj_new_int(tm.tm_mon),
tuple[2] = mp_obj_new_int(tm.tm_mday),
tuple[3] = mp_obj_new_int(tm.tm_hour),
tuple[4] = mp_obj_new_int(tm.tm_min),
tuple[5] = mp_obj_new_int(tm.tm_sec),
tuple[6] = mp_obj_new_int(tm.tm_wday),
tuple[7] = mp_obj_new_int(tm.tm_yday),
};
return mp_obj_new_tuple(8, tuple);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
// mktime()
STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
size_t len;
mp_obj_t *elem;
mp_obj_get_array(tuple, &len, &elem);
// localtime generates a tuple of len 8. CPython uses 9, so we accept both.
if (len < 8 || len > 9) {
mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("mktime needs a tuple of length 8 or 9 (%d given)"), len);
}
return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
// time()
STATIC mp_obj_t time_time(void) {
mp_raise_NotImplementedError("time");
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
{ MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) },
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
{ MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
{ MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
@ -37,6 +90,7 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
};
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);

View File

@ -48,7 +48,12 @@
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
#define MICROPY_CPYTHON_COMPAT (0)
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
#define MICROPY_PY_BUILTINS_HELP (1)
#define MICROPY_PY_BUILTINS_HELP_TEXT samd_help_text
#define MICROPY_PY_BUILTINS_HELP_MODULES (1)
// fixes sys/usys import issue
#define MICROPY_MODULE_WEAK_LINKS (1)
// Control over Python builtins
#define MICROPY_PY_ASYNC_AWAIT (0)
#define MICROPY_PY_BUILTINS_STR_COUNT (0)
@ -59,17 +64,44 @@
#define MICROPY_PY_BUILTINS_ENUMERATE (0)
#define MICROPY_PY_BUILTINS_FILTER (0)
#define MICROPY_PY_BUILTINS_REVERSED (0)
#define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1)
#define MICROPY_PY_BUILTINS_MIN_MAX (0)
#define MICROPY_PY___FILE__ (0)
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
#define MICROPY_PY_ATTRTUPLE (0)
#define MICROPY_PY_COLLECTIONS (0)
#define MICROPY_PY_SYS (1)
#define MICROPY_PY_SYS_PLATFORM "samd"
#define MICROPY_PY_SYS_EXIT (1)
#define MICROPY_PY_SYS_STDFILES (1)
#define MICROPY_PY_SYS_MAXSIZE (1)
#define MICROPY_PY_IO_FILEIO (1)
#define MICROPY_PY_IO (1)
#define MICROPY_PY_IO_IOBASE (1)
// Extended modules
#define MICROPY_PY_UTIME_MP_HAL (1)
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_UOS (1)
#define MICROPY_READER_VFS (1)
#define MICROPY_VFS (1)
#define MICROPY_PY_UJSON (1)
#define MICROPY_PY_URE (1)
#define MICROPY_PY_UBINASCII (1)
#define MICROPY_PY_UCTYPES (1)
#define MICROPY_PY_UHEAPQ (1)
#define MICROPY_PY_URANDOM (1)
#define MICROPY_PY_UZLIB (1)
#define MICROPY_PY_UASYNCIO (1)
// Use VfsLfs's types for fileio/textio
#define mp_type_fileio mp_type_vfs_lfs1_fileio
#define mp_type_textio mp_type_vfs_lfs1_textio
// Use VFS's functions for import stat and builtin open
#define mp_import_stat mp_vfs_import_stat
#define mp_builtin_open_obj mp_vfs_open_obj
// Hooks to add builtins
@ -77,10 +109,12 @@
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
extern const struct _mp_obj_module_t mp_module_machine;
extern const struct _mp_obj_module_t mp_module_samd;
extern const struct _mp_obj_module_t mp_module_utime;
#define MICROPY_PORT_BUILTIN_MODULES \
{ MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&mp_module_machine) }, \
{ MP_ROM_QSTR(MP_QSTR_samd), MP_ROM_PTR(&mp_module_samd) }, \
{ MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&mp_module_utime) }, \
#define MICROPY_PORT_ROOT_POINTERS \

View File

@ -26,6 +26,7 @@
#include "py/runtime.h"
#include "py/mphal.h"
#include "py/stream.h"
#include "samd_soc.h"
#include "tusb.h"
@ -60,6 +61,14 @@ void mp_hal_delay_us(mp_uint_t us) {
}
}
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
if (tud_cdc_connected() && tud_cdc_available()) {
ret |= MP_STREAM_POLL_RD;
}
return ret;
}
int mp_hal_stdin_rx_chr(void) {
for (;;) {
if (USARTx->USART.INTFLAG.bit.RXC) {

View File

@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Damien P. George
* Copyright (c) 2019-2021 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -26,8 +26,14 @@
#ifndef MICROPY_INCLUDED_SAMD_MPHALPORT_H
#define MICROPY_INCLUDED_SAMD_MPHALPORT_H
#include <stdint.h>
#include "py/mpconfig.h"
#include "py/ringbuf.h"
// ASF4
#include "hal_gpio.h"
extern int mp_interrupt_char;
extern ringbuf_t stdin_ringbuf;
extern volatile uint32_t systick_ms;
void mp_hal_set_interrupt_char(int c);
@ -42,4 +48,53 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
return 0;
}
// C-level pin HAL
#include "py/obj.h"
#define MP_HAL_PIN_FMT "%u"
#define mp_hal_pin_obj_t uint
extern uint32_t machine_pin_open_drain_mask;
mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t pin_in);
static inline unsigned int mp_hal_pin_name(mp_hal_pin_obj_t pin) {
return pin;
}
static inline void mp_hal_pin_input(mp_hal_pin_obj_t pin) {
gpio_set_pin_direction(pin, GPIO_DIRECTION_IN);
machine_pin_open_drain_mask &= ~(1 << pin);
}
static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) {
gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT);
machine_pin_open_drain_mask &= ~(1 << pin);
}
static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) {
gpio_set_pin_direction(pin, GPIO_DIRECTION_IN);
gpio_set_pin_level(pin, 0);
machine_pin_open_drain_mask |= 1 << pin;
}
static inline int mp_hal_pin_read(mp_hal_pin_obj_t pin) {
return gpio_get_pin_level(pin);
}
static inline void mp_hal_pin_write(mp_hal_pin_obj_t pin, int v) {
gpio_set_pin_level(pin, v);
}
/*
static inline void mp_hal_pin_od_low(mp_hal_pin_obj_t pin) {
gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT);
}
static inline void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) {
gpio_set_pin_direction(pin, GPIO_DIRECTION_IN);
}
*/
#endif // MICROPY_INCLUDED_SAMD_MPHALPORT_H