From 80654779e1ad98f5818186746e00b6b642dca3db Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 10 Oct 2017 11:36:00 -0700 Subject: [PATCH] atmel-samd: Add support for internal filesystems. (#311) * atmel-samd: Add support for internal filesystems. This allows us to re-enable `os`. `random` is also enabled because it solely depends on `os`. Fixes #266. Its also a pre-requisite for #260. * atmel-samd: Update SAMD51 linker script comments and MICROPY_MAX_STACK_USAGE enabling. --- atmel-samd/Makefile | 26 ++++-- .../asf4/patches/samd21/cast-align.patch | 24 ++++++ .../asf4/patches/samd21/flash-const.patch | 61 ++++++++++++++ .../asf4/patches/samd51/array-bounds.patch | 13 +++ .../asf4/patches/samd51/cast-align.patch | 37 +++++++++ .../asf4/patches/samd51/flash-const.patch | 63 ++++++++++++++ .../asf4/samd21/hal/include/hal_flash.h | 2 +- .../asf4/samd21/hal/include/hpl_flash.h | 4 +- atmel-samd/asf4/samd21/hal/src/hal_flash.c | 8 +- .../asf4/samd21/hpl/nvmctrl/hpl_nvmctrl.c | 4 +- .../asf4/samd51/CMSIS/Include/core_cm4.h | 4 + .../asf4/samd51/hal/include/hal_flash.h | 2 +- .../asf4/samd51/hal/include/hpl_flash.h | 4 +- atmel-samd/asf4/samd51/hal/src/hal_flash.c | 8 +- .../asf4/samd51/hpl/nvmctrl/hpl_nvmctrl.c | 7 +- .../asf4_conf/samd21/hpl_nvmctrl_config.h | 38 +++++++++ .../asf4_conf/samd51/hpl_nvmctrl_config.h | 36 ++++++++ atmel-samd/asf4_conf/samd51/hpl_trng_config.h | 27 ++++++ .../circuitplayground_express/mpconfigboard.h | 3 +- .../mpconfigboard.mk | 5 +- .../boards/feather_m0_express/mpconfigboard.h | 3 +- .../feather_m0_express/mpconfigboard.mk | 5 +- .../boards/metro_m0_express/mpconfigboard.h | 3 +- .../boards/metro_m0_express/mpconfigboard.mk | 5 +- .../boards/metro_m4_express/mpconfigboard.mk | 5 +- .../samd51x18-bootloader-external-flash.ld | 2 +- .../samd51x19-bootloader-external-flash.ld | 2 +- .../samd51x20-bootloader-external-flash.ld | 2 +- atmel-samd/boards/samd51x20-external-flash.ld | 2 +- atmel-samd/boards/samd51x20.ld | 81 ++++++++++++++++++ atmel-samd/common-hal/os/__init__.c | 24 ++++++ atmel-samd/internal_flash.c | 63 ++++++-------- atmel-samd/internal_flash.h | 12 ++- atmel-samd/mpconfigport.h | 12 +-- atmel-samd/supervisor/filesystem.c | 83 +++++++++++-------- atmel-samd/usb.c | 1 + supervisor/supervisor.mk | 2 +- 37 files changed, 568 insertions(+), 115 deletions(-) create mode 100644 atmel-samd/asf4/patches/samd21/flash-const.patch create mode 100644 atmel-samd/asf4/patches/samd51/array-bounds.patch create mode 100644 atmel-samd/asf4/patches/samd51/flash-const.patch create mode 100644 atmel-samd/asf4_conf/samd21/hpl_nvmctrl_config.h create mode 100644 atmel-samd/asf4_conf/samd51/hpl_nvmctrl_config.h create mode 100644 atmel-samd/asf4_conf/samd51/hpl_trng_config.h create mode 100644 atmel-samd/boards/samd51x20.ld diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 49bd087225..2c51342d52 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -79,18 +79,25 @@ BASE_CFLAGS = \ -fshort-enums \ --param max-inline-insns-single=500 +# NDEBUG disables assert() statements. This reduces code size pretty dramatically, per tannewt. +ifeq ($(CHIP_FAMILY), samd21) +CFLAGS = -Os -DNDEBUG -finline-limit=49 +endif +ifeq ($(CHIP_FAMILY), samd51) +CFLAGS = -O2 -DNDEBUG +endif + #Debugging/Optimization ifeq ($(DEBUG), 1) -# NDEBUG disables assert() statements. This reduces code size pretty dramatically, per tannewt. # Turn on Python modules useful for debugging (e.g. uheap, ustack). -CFLAGS = -O1 -ggdb -DNDEBUG +CFLAGS += -ggdb ifeq ($(CHIP_FAMILY), samd21) -CFLAGS += -DENABLE_MICRO_TRACE_BUFFER +CFLAGS += -DNDEBUG -DENABLE_MICRO_TRACE_BUFFER endif else # -finline-limit can shrink the image size. -finline-limit=80 or so is similar to not having it on. # There is no simple default value, though. -CFLAGS = -Os -DNDEBUG -flto -finline-limit=49 +CFLAGS += -DNDEBUG -flto -finline-limit=49 endif CFLAGS += $(INC) -Wall -Werror -std=gnu11 -nostdlib $(BASE_CFLAGS) $(CFLAGS_MOD) $(COPT) @@ -109,8 +116,9 @@ CFLAGS += \ -mthumb \ -mabi=aapcs-linux \ -mlong-calls \ + -mtune=cortex-m4 \ -mcpu=cortex-m4 \ - -mfloat-abi=softfp \ + -mfloat-abi=hard \ -mfpu=fpv4-sp-d16 \ -DSAMD51 endif @@ -150,11 +158,13 @@ SRC_ASF := \ gcc/system_$(CHIP_FAMILY).c \ hal/src/hal_atomic.c \ hal/src/hal_delay.c \ + hal/src/hal_flash.c \ hal/src/hal_sleep.c \ hal/src/hal_timer.c \ hal/src/hal_usb_device.c \ hpl/core/hpl_init.c \ hpl/gclk/hpl_gclk.c \ + hpl/nvmctrl/hpl_nvmctrl.c \ hpl/pm/hpl_pm.c \ hpl/rtc/hpl_rtc.c \ hpl/systick/hpl_systick.c \ @@ -172,10 +182,12 @@ SRC_ASF += \ else ifeq ($(CHIP_FAMILY), samd51) SRC_ASF += \ + hal/src/hal_rand_sync.c \ hpl/core/hpl_core_m4.c \ hpl/mclk/hpl_mclk.c \ hpl/osc32kctrl/hpl_osc32kctrl.c \ hpl/oscctrl/hpl_oscctrl.c \ + hpl/trng/hpl_trng.c \ endif @@ -184,7 +196,6 @@ SRC_ASF := $(addprefix asf4/$(CHIP_FAMILY)/, $(SRC_ASF)) # Skip this source for now. # access_vfs.c \ shared_dma.c \ - $(FLASH_IMPL) \ SRC_C = \ background.c \ @@ -195,6 +206,7 @@ SRC_C = \ $(CHIP_FAMILY)_pins.c \ tick.c \ usb.c \ + $(FLASH_IMPL) \ bindings/samd/__init__.c \ boards/$(BOARD)/board.c \ boards/$(BOARD)/pins.c \ @@ -218,6 +230,7 @@ SRC_COMMON_HAL = \ microcontroller/__init__.c \ microcontroller/Pin.c \ microcontroller/Processor.c \ + os/__init__.c \ time/__init__.c # analogio/__init__.c \ analogio/AnalogIn.c \ @@ -233,7 +246,6 @@ SRC_COMMON_HAL = \ neopixel_write/__init__.c \ nvm/__init__.c \ nvm/ByteArray.c \ - os/__init__.c \ pulseio/__init__.c \ pulseio/PulseIn.c \ pulseio/PulseOut.c \ diff --git a/atmel-samd/asf4/patches/samd21/cast-align.patch b/atmel-samd/asf4/patches/samd21/cast-align.patch index 04ddf9bf53..2059867e49 100644 --- a/atmel-samd/asf4/patches/samd21/cast-align.patch +++ b/atmel-samd/asf4/patches/samd21/cast-align.patch @@ -10,3 +10,27 @@ + #pragma GCC diagnostic pop struct timer_task * it = (struct timer_task *)list_get_head(&timer->tasks); uint32_t time = ++timer->time; +--- a/hal/src/hal_flash.c ++++ b/hal/src/hal_flash.c +@@ -304,7 +304,10 @@ static int32_t flash_is_address_aligned(struct flash_descriptor *flash, const ui + */ + static void flash_ready(struct _flash_device *device) + { ++ #pragma GCC diagnostic push ++ #pragma GCC diagnostic ignored "-Wcast-align" + struct flash_descriptor *const descr = CONTAINER_OF(device, struct flash_descriptor, dev); ++ #pragma GCC diagnostic pop + if (descr->callbacks.cb_ready) { + descr->callbacks.cb_ready(descr); + } +@@ -317,7 +320,10 @@ static void flash_ready(struct _flash_device *device) + */ + static void flash_error(struct _flash_device *device) + { ++ #pragma GCC diagnostic push ++ #pragma GCC diagnostic ignored "-Wcast-align" + struct flash_descriptor *const descr = CONTAINER_OF(device, struct flash_descriptor, dev); ++ #pragma GCC diagnostic pop + if (descr->callbacks.cb_error) { + descr->callbacks.cb_error(descr); + } diff --git a/atmel-samd/asf4/patches/samd21/flash-const.patch b/atmel-samd/asf4/patches/samd21/flash-const.patch new file mode 100644 index 0000000000..cac428ab3d --- /dev/null +++ b/atmel-samd/asf4/patches/samd21/flash-const.patch @@ -0,0 +1,61 @@ +--- a/hal/include/hal_flash.h ++++ b/hal/include/hal_flash.h +@@ -132,7 +132,7 @@ int32_t flash_write(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t * + * \param[in] length Number of bytes to append + * \return Append status. + */ +-int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t *buffer, uint32_t length); ++int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, const uint8_t *buffer, uint32_t length); + + /** \brief Reads a number of bytes to a page in the internal Flash + * +--- a/hal/include/hpl_flash.h ++++ b/hal/include/hpl_flash.h +@@ -125,7 +125,7 @@ void _flash_read(struct _flash_device *const device, const uint32_t src_addr, ui + * write is stored + * \param[in] length Number of bytes to write + */ +-void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length); ++void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length); + + /** + * \brief Appends a number of bytes in the internal Flash. +@@ -135,7 +135,7 @@ void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, u + * \param[in] buffer Pointer to buffer with data to write to flash + * \param[in] length Number of bytes to write + */ +-void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length); ++void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length); + + /** \brief Execute lock in the internal flash + * \param[in] device The pointer to FLASH device instance +--- a/hal/src/hal_flash.c ++++ b/hal/src/hal_flash.c +@@ -135,7 +135,7 @@ int32_t flash_write(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t * + /** + * \brief Appends a number of bytes to a page in the internal Flash + */ +-int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t *buffer, uint32_t length) ++int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, const uint8_t *buffer, uint32_t length) + { + ASSERT(flash && buffer && length); +--- a/hpl/nvmctrl/hpl_nvmctrl.c ++++ b/hpl/nvmctrl/hpl_nvmctrl.c +@@ -175,7 +175,7 @@ void _flash_read(struct _flash_device *const device, const uint32_t src_addr, ui + /** + * \brief Writes a number of bytes to a page in the internal Flash. + */ +-void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length) ++void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length) + { + uint8_t tmp_buffer[NVMCTRL_ROW_PAGES][NVMCTRL_PAGE_SIZE]; + uint32_t row_start_addr, row_end_addr; +@@ -219,7 +219,7 @@ void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, u + /** + * \brief Appends a number of bytes in the internal Flash. + */ +-void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length) ++void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length) + { + uint32_t page_start_addr = dst_addr & ~(NVMCTRL_PAGE_SIZE - 1); + uint32_t size; diff --git a/atmel-samd/asf4/patches/samd51/array-bounds.patch b/atmel-samd/asf4/patches/samd51/array-bounds.patch new file mode 100644 index 0000000000..30f40cb79c --- /dev/null +++ b/atmel-samd/asf4/patches/samd51/array-bounds.patch @@ -0,0 +1,13 @@ +--- a/CMSIS/Include/core_cm4.h ++++ b/CMSIS/Include/core_cm4.h +@@ -1493,8 +1493,12 @@ __STATIC_INLINE uint32_t NVIC_GetPriorityGrouping(void) + */ + __STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn) + { ++ ++ #pragma GCC diagnostic push ++ #pragma GCC diagnostic ignored "-Warray-bounds" + /* NVIC->ISER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); enable interrupt */ + NVIC->ISER[(uint32_t)((int32_t)IRQn) >> 5] = (uint32_t)(1 << ((uint32_t)((int32_t)IRQn) & (uint32_t)0x1F)); /* enable interrupt */ ++ #pragma GCC diagnostic pop + } diff --git a/atmel-samd/asf4/patches/samd51/cast-align.patch b/atmel-samd/asf4/patches/samd51/cast-align.patch index 1418c9fb1e..399c1ff809 100644 --- a/atmel-samd/asf4/patches/samd51/cast-align.patch +++ b/atmel-samd/asf4/patches/samd51/cast-align.patch @@ -10,3 +10,40 @@ + #pragma GCC diagnostic pop struct timer_task * it = (struct timer_task *)list_get_head(&timer->tasks); uint32_t time = ++timer->time; + +--- a/hal/src/hal_flash.c ++++ b/hal/src/hal_flash.c +@@ -304,7 +304,10 @@ static int32_t flash_is_address_aligned(struct flash_descriptor *flash, const ui + */ + static void flash_ready(struct _flash_device *device) + { ++ #pragma GCC diagnostic push ++ #pragma GCC diagnostic ignored "-Wcast-align" + struct flash_descriptor *const descr = CONTAINER_OF(device, struct flash_descriptor, dev); ++ #pragma GCC diagnostic pop + if (descr->callbacks.cb_ready) { + descr->callbacks.cb_ready(descr); + } +@@ -317,7 +320,10 @@ static void flash_ready(struct _flash_device *device) + */ + static void flash_error(struct _flash_device *device) + { ++ #pragma GCC diagnostic push ++ #pragma GCC diagnostic ignored "-Wcast-align" + struct flash_descriptor *const descr = CONTAINER_OF(device, struct flash_descriptor, dev); ++ #pragma GCC diagnostic pop + if (descr->callbacks.cb_error) { + descr->callbacks.cb_error(descr); + } +--- a/hpl/nvmctrl/hpl_nvmctrl.c ++++ b/hpl/nvmctrl/hpl_nvmctrl.c +@@ -386,7 +386,10 @@ static void _flash_erase_block(void *const hw, const uint32_t dst_addr) + */ + static void _flash_program(void *const hw, const uint32_t dst_addr, const uint8_t *buffer, const uint16_t size) + { ++ #pragma GCC diagnostic push ++ #pragma GCC diagnostic ignored "-Wcast-align" + uint32_t *ptr_read = (uint32_t *)buffer; ++ #pragma GCC diagnostic pop + uint32_t nvm_address = dst_addr / 4; + uint16_t i; diff --git a/atmel-samd/asf4/patches/samd51/flash-const.patch b/atmel-samd/asf4/patches/samd51/flash-const.patch new file mode 100644 index 0000000000..087581ff0c --- /dev/null +++ b/atmel-samd/asf4/patches/samd51/flash-const.patch @@ -0,0 +1,63 @@ +--- a/hal/include/hal_flash.h ++++ b/hal/include/hal_flash.h +@@ -132,7 +132,7 @@ int32_t flash_write(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t * + * \param[in] length Number of bytes to append + * \return Append status. + */ +-int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t *buffer, uint32_t length); ++int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, const uint8_t *buffer, uint32_t length); + + /** \brief Reads a number of bytes to a page in the internal Flash + * +--- a/hal/include/hpl_flash.h ++++ b/hal/include/hpl_flash.h +@@ -125,7 +125,7 @@ void _flash_read(struct _flash_device *const device, const uint32_t src_addr, ui + * write is stored + * \param[in] length Number of bytes to write + */ +-void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length); ++void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length); + + /** + * \brief Appends a number of bytes in the internal Flash. +@@ -135,7 +135,7 @@ void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, u + * \param[in] buffer Pointer to buffer with data to write to flash + * \param[in] length Number of bytes to write + */ +-void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length); ++void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length); + + /** \brief Execute lock in the internal flash + * \param[in] device The pointer to FLASH device instance + +--- a/hal/src/hal_flash.c ++++ b/hal/src/hal_flash.c +@@ -135,7 +135,7 @@ int32_t flash_write(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t * + /** + * \brief Appends a number of bytes to a page in the internal Flash + */ +-int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t *buffer, uint32_t length) ++int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, const uint8_t *buffer, uint32_t length) + { + ASSERT(flash && buffer && length); + +--- a/hpl/nvmctrl/hpl_nvmctrl.c ++++ b/hpl/nvmctrl/hpl_nvmctrl.c +@@ -167,7 +167,7 @@ void _flash_read(struct _flash_device *const device, const uint32_t src_addr, ui + /** + * \brief Writes a number of bytes to a page in the internal Flash. + */ +-void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length) ++void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length) + { + uint8_t tmp_buffer[NVMCTRL_BLOCK_PAGES][NVMCTRL_PAGE_SIZE]; + uint32_t block_start_addr, block_end_addr; +@@ -212,7 +212,7 @@ void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, u + /** + * \brief Appends a number of bytes in the internal Flash. + */ +-void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length) ++void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length) + { + uint32_t page_start_addr = dst_addr & ~(NVMCTRL_PAGE_SIZE - 1); + uint32_t size; diff --git a/atmel-samd/asf4/samd21/hal/include/hal_flash.h b/atmel-samd/asf4/samd21/hal/include/hal_flash.h index 2e6286389f..ffba519de7 100644 --- a/atmel-samd/asf4/samd21/hal/include/hal_flash.h +++ b/atmel-samd/asf4/samd21/hal/include/hal_flash.h @@ -132,7 +132,7 @@ int32_t flash_write(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t * * \param[in] length Number of bytes to append * \return Append status. */ -int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t *buffer, uint32_t length); +int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, const uint8_t *buffer, uint32_t length); /** \brief Reads a number of bytes to a page in the internal Flash * diff --git a/atmel-samd/asf4/samd21/hal/include/hpl_flash.h b/atmel-samd/asf4/samd21/hal/include/hpl_flash.h index 774bb5e0b2..6a59f757c7 100644 --- a/atmel-samd/asf4/samd21/hal/include/hpl_flash.h +++ b/atmel-samd/asf4/samd21/hal/include/hpl_flash.h @@ -125,7 +125,7 @@ void _flash_read(struct _flash_device *const device, const uint32_t src_addr, ui * write is stored * \param[in] length Number of bytes to write */ -void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length); +void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length); /** * \brief Appends a number of bytes in the internal Flash. @@ -135,7 +135,7 @@ void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, u * \param[in] buffer Pointer to buffer with data to write to flash * \param[in] length Number of bytes to write */ -void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length); +void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length); /** \brief Execute lock in the internal flash * \param[in] device The pointer to FLASH device instance diff --git a/atmel-samd/asf4/samd21/hal/src/hal_flash.c b/atmel-samd/asf4/samd21/hal/src/hal_flash.c index f3cc4bbfe0..165d46d899 100644 --- a/atmel-samd/asf4/samd21/hal/src/hal_flash.c +++ b/atmel-samd/asf4/samd21/hal/src/hal_flash.c @@ -135,7 +135,7 @@ int32_t flash_write(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t * /** * \brief Appends a number of bytes to a page in the internal Flash */ -int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t *buffer, uint32_t length) +int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, const uint8_t *buffer, uint32_t length) { ASSERT(flash && buffer && length); @@ -304,7 +304,10 @@ static int32_t flash_is_address_aligned(struct flash_descriptor *flash, const ui */ static void flash_ready(struct _flash_device *device) { + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wcast-align" struct flash_descriptor *const descr = CONTAINER_OF(device, struct flash_descriptor, dev); + #pragma GCC diagnostic pop if (descr->callbacks.cb_ready) { descr->callbacks.cb_ready(descr); } @@ -317,7 +320,10 @@ static void flash_ready(struct _flash_device *device) */ static void flash_error(struct _flash_device *device) { + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wcast-align" struct flash_descriptor *const descr = CONTAINER_OF(device, struct flash_descriptor, dev); + #pragma GCC diagnostic pop if (descr->callbacks.cb_error) { descr->callbacks.cb_error(descr); } diff --git a/atmel-samd/asf4/samd21/hpl/nvmctrl/hpl_nvmctrl.c b/atmel-samd/asf4/samd21/hpl/nvmctrl/hpl_nvmctrl.c index 2c9131a1a8..3e751e3d57 100644 --- a/atmel-samd/asf4/samd21/hpl/nvmctrl/hpl_nvmctrl.c +++ b/atmel-samd/asf4/samd21/hpl/nvmctrl/hpl_nvmctrl.c @@ -175,7 +175,7 @@ void _flash_read(struct _flash_device *const device, const uint32_t src_addr, ui /** * \brief Writes a number of bytes to a page in the internal Flash. */ -void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length) +void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length) { uint8_t tmp_buffer[NVMCTRL_ROW_PAGES][NVMCTRL_PAGE_SIZE]; uint32_t row_start_addr, row_end_addr; @@ -219,7 +219,7 @@ void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, u /** * \brief Appends a number of bytes in the internal Flash. */ -void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length) +void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length) { uint32_t page_start_addr = dst_addr & ~(NVMCTRL_PAGE_SIZE - 1); uint32_t size; diff --git a/atmel-samd/asf4/samd51/CMSIS/Include/core_cm4.h b/atmel-samd/asf4/samd51/CMSIS/Include/core_cm4.h index bb6be1305d..2ebde319de 100644 --- a/atmel-samd/asf4/samd51/CMSIS/Include/core_cm4.h +++ b/atmel-samd/asf4/samd51/CMSIS/Include/core_cm4.h @@ -1493,8 +1493,12 @@ __STATIC_INLINE uint32_t NVIC_GetPriorityGrouping(void) */ __STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn) { + + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Warray-bounds" /* NVIC->ISER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); enable interrupt */ NVIC->ISER[(uint32_t)((int32_t)IRQn) >> 5] = (uint32_t)(1 << ((uint32_t)((int32_t)IRQn) & (uint32_t)0x1F)); /* enable interrupt */ + #pragma GCC diagnostic pop } diff --git a/atmel-samd/asf4/samd51/hal/include/hal_flash.h b/atmel-samd/asf4/samd51/hal/include/hal_flash.h index 2e6286389f..ffba519de7 100644 --- a/atmel-samd/asf4/samd51/hal/include/hal_flash.h +++ b/atmel-samd/asf4/samd51/hal/include/hal_flash.h @@ -132,7 +132,7 @@ int32_t flash_write(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t * * \param[in] length Number of bytes to append * \return Append status. */ -int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t *buffer, uint32_t length); +int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, const uint8_t *buffer, uint32_t length); /** \brief Reads a number of bytes to a page in the internal Flash * diff --git a/atmel-samd/asf4/samd51/hal/include/hpl_flash.h b/atmel-samd/asf4/samd51/hal/include/hpl_flash.h index 774bb5e0b2..6a59f757c7 100644 --- a/atmel-samd/asf4/samd51/hal/include/hpl_flash.h +++ b/atmel-samd/asf4/samd51/hal/include/hpl_flash.h @@ -125,7 +125,7 @@ void _flash_read(struct _flash_device *const device, const uint32_t src_addr, ui * write is stored * \param[in] length Number of bytes to write */ -void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length); +void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length); /** * \brief Appends a number of bytes in the internal Flash. @@ -135,7 +135,7 @@ void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, u * \param[in] buffer Pointer to buffer with data to write to flash * \param[in] length Number of bytes to write */ -void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length); +void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length); /** \brief Execute lock in the internal flash * \param[in] device The pointer to FLASH device instance diff --git a/atmel-samd/asf4/samd51/hal/src/hal_flash.c b/atmel-samd/asf4/samd51/hal/src/hal_flash.c index f3cc4bbfe0..165d46d899 100644 --- a/atmel-samd/asf4/samd51/hal/src/hal_flash.c +++ b/atmel-samd/asf4/samd51/hal/src/hal_flash.c @@ -135,7 +135,7 @@ int32_t flash_write(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t * /** * \brief Appends a number of bytes to a page in the internal Flash */ -int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t *buffer, uint32_t length) +int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, const uint8_t *buffer, uint32_t length) { ASSERT(flash && buffer && length); @@ -304,7 +304,10 @@ static int32_t flash_is_address_aligned(struct flash_descriptor *flash, const ui */ static void flash_ready(struct _flash_device *device) { + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wcast-align" struct flash_descriptor *const descr = CONTAINER_OF(device, struct flash_descriptor, dev); + #pragma GCC diagnostic pop if (descr->callbacks.cb_ready) { descr->callbacks.cb_ready(descr); } @@ -317,7 +320,10 @@ static void flash_ready(struct _flash_device *device) */ static void flash_error(struct _flash_device *device) { + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wcast-align" struct flash_descriptor *const descr = CONTAINER_OF(device, struct flash_descriptor, dev); + #pragma GCC diagnostic pop if (descr->callbacks.cb_error) { descr->callbacks.cb_error(descr); } diff --git a/atmel-samd/asf4/samd51/hpl/nvmctrl/hpl_nvmctrl.c b/atmel-samd/asf4/samd51/hpl/nvmctrl/hpl_nvmctrl.c index 2f7f01d5ed..3d9df6db45 100644 --- a/atmel-samd/asf4/samd51/hpl/nvmctrl/hpl_nvmctrl.c +++ b/atmel-samd/asf4/samd51/hpl/nvmctrl/hpl_nvmctrl.c @@ -167,7 +167,7 @@ void _flash_read(struct _flash_device *const device, const uint32_t src_addr, ui /** * \brief Writes a number of bytes to a page in the internal Flash. */ -void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length) +void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length) { uint8_t tmp_buffer[NVMCTRL_BLOCK_PAGES][NVMCTRL_PAGE_SIZE]; uint32_t block_start_addr, block_end_addr; @@ -212,7 +212,7 @@ void _flash_write(struct _flash_device *const device, const uint32_t dst_addr, u /** * \brief Appends a number of bytes in the internal Flash. */ -void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, uint8_t *buffer, uint32_t length) +void _flash_append(struct _flash_device *const device, const uint32_t dst_addr, const uint8_t *buffer, uint32_t length) { uint32_t page_start_addr = dst_addr & ~(NVMCTRL_PAGE_SIZE - 1); uint32_t size; @@ -386,7 +386,10 @@ static void _flash_erase_block(void *const hw, const uint32_t dst_addr) */ static void _flash_program(void *const hw, const uint32_t dst_addr, const uint8_t *buffer, const uint16_t size) { + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wcast-align" uint32_t *ptr_read = (uint32_t *)buffer; + #pragma GCC diagnostic pop uint32_t nvm_address = dst_addr / 4; uint16_t i; diff --git a/atmel-samd/asf4_conf/samd21/hpl_nvmctrl_config.h b/atmel-samd/asf4_conf/samd21/hpl_nvmctrl_config.h new file mode 100644 index 0000000000..88d6bb93ff --- /dev/null +++ b/atmel-samd/asf4_conf/samd21/hpl_nvmctrl_config.h @@ -0,0 +1,38 @@ +/* Auto-generated config file hpl_nvmctrl_config.h */ +#ifndef HPL_NVMCTRL_CONFIG_H +#define HPL_NVMCTRL_CONFIG_H + +// <<< Use Configuration Wizard in Context Menu >>> + +// Basic Settings + +// Read Mode Selection +// <0x00=> No Miss Penalty +// <0x01=> Low Power +// <0x02=> Deterministic +// nvm_arch_read_mode +#ifndef CONF_NVM_READ_MODE +#define CONF_NVM_READ_MODE 0 +#endif + +// Power Reduction Mode During Sleep +// <0x00=> Wake On Access +// <0x01=> Wake Up Instant +// <0x03=> Disabled +// nvm_arch_sleepprm +#ifndef CONF_NVM_SLEEPPRM +#define CONF_NVM_SLEEPPRM 0 +#endif + +// Cache Disable +// Indicate whether cache is disable or not +// nvm_arch_cache +#ifndef CONF_NVM_CACHE +#define CONF_NVM_CACHE 0 +#endif + +// + +// <<< end of configuration section >>> + +#endif // HPL_NVMCTRL_CONFIG_H diff --git a/atmel-samd/asf4_conf/samd51/hpl_nvmctrl_config.h b/atmel-samd/asf4_conf/samd51/hpl_nvmctrl_config.h new file mode 100644 index 0000000000..daaba85d40 --- /dev/null +++ b/atmel-samd/asf4_conf/samd51/hpl_nvmctrl_config.h @@ -0,0 +1,36 @@ +/* Auto-generated config file hpl_nvmctrl_config.h */ +#ifndef HPL_NVMCTRL_CONFIG_H +#define HPL_NVMCTRL_CONFIG_H + +// <<< Use Configuration Wizard in Context Menu >>> + +// Basic Settings + +// Power Reduction Mode During Sleep +// <0x00=> Wake On Access +// <0x01=> Wake Up Instant +// <0x03=> Disabled +// nvm_arch_sleepprm +#ifndef CONF_NVM_SLEEPPRM +#define CONF_NVM_SLEEPPRM 0 +#endif + +// AHB0 Cache Disable +// Indicate whether AHB0 cache is disable or not +// nvm_arch_cache0 +#ifndef CONF_NVM_CACHE0 +#define CONF_NVM_CACHE0 0 +#endif + +// AHB1 Cache Disable +// Indicate whether AHB1 cache is disable or not +// nvm_arch_cache1 +#ifndef CONF_NVM_CACHE1 +#define CONF_NVM_CACHE1 0 +#endif + +// + +// <<< end of configuration section >>> + +#endif // HPL_NVMCTRL_CONFIG_H diff --git a/atmel-samd/asf4_conf/samd51/hpl_trng_config.h b/atmel-samd/asf4_conf/samd51/hpl_trng_config.h new file mode 100644 index 0000000000..ba9014989a --- /dev/null +++ b/atmel-samd/asf4_conf/samd51/hpl_trng_config.h @@ -0,0 +1,27 @@ +/* Auto-generated config file hpl_trng_config.h */ +#ifndef HPL_TRNG_CONFIG_H +#define HPL_TRNG_CONFIG_H + +// <<< Use Configuration Wizard in Context Menu >>> + +// Advanced configurations + +// Run In Standby +// Indicates whether the TRNG works in standby mode +// trng_runstdby +#ifndef CONF_TRNG_RUNSTDBY +#define CONF_TRNG_RUNSTDBY 0 +#endif + +// Data Ready Event Output Enable +// Indicates whether the TRNG generates event on Data Ready +// trng_datardyeo +#ifndef CONF_TRNG_DATARDYEO +#define CONF_TRNG_DATARDYEO 0 +#endif + +// + +// <<< end of configuration section >>> + +#endif // HPL_TRNG_CONFIG_H diff --git a/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index a12ac51a0a..e1056ae94d 100644 --- a/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -24,7 +24,8 @@ #define SPEAKER_ENABLE_PIN (&pin_PA30) -#include "spi_flash.h" +#include "internal_flash.h" +//#include "spi_flash.h" // If you change this, then make sure to update the linker scripts as well to // make sure you don't overwrite code. diff --git a/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk b/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk index 0a93ccddcd..538a737db6 100644 --- a/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk +++ b/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk @@ -1,8 +1,9 @@ -LD_FILE = boards/samd21x18-bootloader-external-flash-crystalless.ld +LD_FILE = boards/samd21x18-bootloader-crystalless.ld USB_VID = 0x239A USB_PID = 0x8019 -FLASH_IMPL = spi_flash.c +#FLASH_IMPL = spi_flash.c +FLASH_IMPL = internal_flash.c CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/atmel-samd/boards/feather_m0_express/mpconfigboard.h b/atmel-samd/boards/feather_m0_express/mpconfigboard.h index 9f9c25a7d3..39706e2606 100644 --- a/atmel-samd/boards/feather_m0_express/mpconfigboard.h +++ b/atmel-samd/boards/feather_m0_express/mpconfigboard.h @@ -20,7 +20,8 @@ #define MICROPY_PORT_B ( 0 ) #define MICROPY_PORT_C ( 0 ) -#include "spi_flash.h" +#include "internal_flash.h" +//#include "spi_flash.h" // If you change this, then make sure to update the linker scripts as well to // make sure you don't overwrite code. diff --git a/atmel-samd/boards/feather_m0_express/mpconfigboard.mk b/atmel-samd/boards/feather_m0_express/mpconfigboard.mk index 5840a5cf90..cdcfd4108b 100644 --- a/atmel-samd/boards/feather_m0_express/mpconfigboard.mk +++ b/atmel-samd/boards/feather_m0_express/mpconfigboard.mk @@ -1,8 +1,9 @@ -LD_FILE = boards/samd21x18-bootloader-external-flash.ld +LD_FILE = boards/samd21x18-bootloader.ld USB_VID = 0x239A USB_PID = 0x801b -FLASH_IMPL = spi_flash.c +#FLASH_IMPL = spi_flash.c +FLASH_IMPL = internal_flash.c CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/atmel-samd/boards/metro_m0_express/mpconfigboard.h b/atmel-samd/boards/metro_m0_express/mpconfigboard.h index 291967c885..de3cf29da2 100644 --- a/atmel-samd/boards/metro_m0_express/mpconfigboard.h +++ b/atmel-samd/boards/metro_m0_express/mpconfigboard.h @@ -24,7 +24,8 @@ #define MICROPY_PORT_B (PORT_PB03 | PORT_PB22 | PORT_PB23) #define MICROPY_PORT_C (0) -#include "spi_flash.h" +#include "internal_flash.h" +//#include "spi_flash.h" // If you change this, then make sure to update the linker scripts as well to // make sure you don't overwrite code. diff --git a/atmel-samd/boards/metro_m0_express/mpconfigboard.mk b/atmel-samd/boards/metro_m0_express/mpconfigboard.mk index 7637387482..82d673f026 100644 --- a/atmel-samd/boards/metro_m0_express/mpconfigboard.mk +++ b/atmel-samd/boards/metro_m0_express/mpconfigboard.mk @@ -1,8 +1,9 @@ -LD_FILE = boards/samd21x18-bootloader-external-flash.ld +LD_FILE = boards/samd21x18-bootloader.ld USB_VID = 0x239A USB_PID = 0x8014 -FLASH_IMPL = spi_flash.c +#FLASH_IMPL = spi_flash.c +FLASH_IMPL = internal_flash.c CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 diff --git a/atmel-samd/boards/metro_m4_express/mpconfigboard.mk b/atmel-samd/boards/metro_m4_express/mpconfigboard.mk index fd299c7f90..a21be51389 100644 --- a/atmel-samd/boards/metro_m4_express/mpconfigboard.mk +++ b/atmel-samd/boards/metro_m4_express/mpconfigboard.mk @@ -1,8 +1,9 @@ -LD_FILE = boards/samd51x20-bootloader-external-flash.ld +LD_FILE = boards/samd51x20.ld USB_VID = 0x239A USB_PID = 0x8015 -FLASH_IMPL = spi_flash.c +#FLASH_IMPL = spi_flash.c +FLASH_IMPL = internal_flash.c CHIP_VARIANT = SAMD51J20A CHIP_FAMILY = samd51 diff --git a/atmel-samd/boards/samd51x18-bootloader-external-flash.ld b/atmel-samd/boards/samd51x18-bootloader-external-flash.ld index b009de640a..f4ad37c20f 100644 --- a/atmel-samd/boards/samd51x18-bootloader-external-flash.ld +++ b/atmel-samd/boards/samd51x18-bootloader-external-flash.ld @@ -1,5 +1,5 @@ /* - GNU linker script for SAMD21 + GNU linker script for SAMD51 */ /* Specify the memory areas */ diff --git a/atmel-samd/boards/samd51x19-bootloader-external-flash.ld b/atmel-samd/boards/samd51x19-bootloader-external-flash.ld index bea575797f..4e4e8b002d 100644 --- a/atmel-samd/boards/samd51x19-bootloader-external-flash.ld +++ b/atmel-samd/boards/samd51x19-bootloader-external-flash.ld @@ -1,5 +1,5 @@ /* - GNU linker script for SAMD21 + GNU linker script for SAMD51 */ /* Specify the memory areas */ diff --git a/atmel-samd/boards/samd51x20-bootloader-external-flash.ld b/atmel-samd/boards/samd51x20-bootloader-external-flash.ld index e9c67de97b..57bf299fb4 100644 --- a/atmel-samd/boards/samd51x20-bootloader-external-flash.ld +++ b/atmel-samd/boards/samd51x20-bootloader-external-flash.ld @@ -1,5 +1,5 @@ /* - GNU linker script for SAMD21 + GNU linker script for SAMD51 */ /* Specify the memory areas */ diff --git a/atmel-samd/boards/samd51x20-external-flash.ld b/atmel-samd/boards/samd51x20-external-flash.ld index 701e24762a..b0a118507a 100644 --- a/atmel-samd/boards/samd51x20-external-flash.ld +++ b/atmel-samd/boards/samd51x20-external-flash.ld @@ -1,5 +1,5 @@ /* - GNU linker script for SAMD21 + GNU linker script for SAMD51 */ /* Specify the memory areas */ diff --git a/atmel-samd/boards/samd51x20.ld b/atmel-samd/boards/samd51x20.ld new file mode 100644 index 0000000000..4d2da5d13d --- /dev/null +++ b/atmel-samd/boards/samd51x20.ld @@ -0,0 +1,81 @@ +/* + GNU linker script for SAMD51 +*/ + +/* Specify the memory areas */ +MEMORY +{ + FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x00100000 - 0x80000 /* 1024 KiB mius 512KiB for the internal file system. */ + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x00040000 /* 256 KiB */ +} + +/* top end of the stack */ +_estack = ORIGIN(RAM) + LENGTH(RAM); + +/* define output sections */ +SECTIONS +{ + /* The program code and other data goes into FLASH */ + .text : + { + . = ALIGN(4); + _sfixed = .; + KEEP(*(.vectors)) /* isr vector table */ + *(.text) /* .text sections (code) */ + *(.text*) /* .text* sections (code) */ + *(.rodata) /* .rodata sections (constants, strings, etc.) */ + *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ + + . = ALIGN(4); + _etext = .; /* define a global symbol at end of code */ + } >FLASH + + .ARM.exidx : + { + *(.ARM.exidx*) + *(.gnu.linkonce.armexidx.*) + _sidata = .; /* This is used by the startup in order to initialize the .data section */ + } > FLASH + + /* This is the initialized data section + The program executes knowing that the data is in the RAM + but the loader puts the initial values in the FLASH (inidata). + It is one task of the startup to copy the initial values from FLASH to RAM. */ + .data : AT ( _sidata ) + { + . = ALIGN(4); + _srelocate = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */ + *(.ramfunc) + *(.ramfunc*) + *(.data) /* .data sections */ + *(.data*) /* .data* sections */ + + . = ALIGN(4); + _erelocate = .; /* define a global symbol at data end; used by startup code in order to initialise the .data section in RAM */ + } >RAM + + /* Uninitialized data section */ + .bss : + { + . = ALIGN(4); + _sbss = .; + _szero = .; /* define a global symbol at bss start; used by startup code */ + *(.bss) + *(.bss*) + *(COMMON) + + . = ALIGN(4); + _ezero = .; /* define a global symbol at bss end; used by startup code */ + _ebss = .; + } >RAM + + /* this just checks there is enough RAM for a minimal stack */ + .stack : + { + . = ALIGN(4); + . = . + 0x800; /* Reserve a minimum of 2K for the stack. */ + . = ALIGN(4); + } >RAM + + .ARM.attributes 0 : { *(.ARM.attributes) } +} diff --git a/atmel-samd/common-hal/os/__init__.c b/atmel-samd/common-hal/os/__init__.c index 2df4e40ffd..eeba29dfaf 100644 --- a/atmel-samd/common-hal/os/__init__.c +++ b/atmel-samd/common-hal/os/__init__.c @@ -30,12 +30,22 @@ #include "py/objtuple.h" #include "py/qstr.h" +#ifdef SAMD51 +#include "hal/include/hal_rand_sync.h" +#endif + STATIC const qstr os_uname_info_fields[] = { MP_QSTR_sysname, MP_QSTR_nodename, MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine }; +#ifdef SAMD21 STATIC const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, "samd21"); STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, "samd21"); +#endif +#ifdef SAMD51 +STATIC const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, "samd51"); +STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, "samd51"); +#endif STATIC const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, MICROPY_VERSION_STRING); STATIC const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE); STATIC const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME); @@ -57,5 +67,19 @@ mp_obj_t common_hal_os_uname(void) { } bool common_hal_os_urandom(uint8_t* buffer, uint32_t length) { + #ifdef SAMD51 + hri_mclk_set_APBCMASK_TRNG_bit(MCLK); + struct rand_sync_desc random; + rand_sync_init(&random, TRNG); + rand_sync_enable(&random); + + rand_sync_read_buf8(&random, buffer, length); + + rand_sync_disable(&random); + rand_sync_deinit(&random); + hri_mclk_clear_APBCMASK_TRNG_bit(MCLK); + return true; + #else return false; + #endif } diff --git a/atmel-samd/internal_flash.c b/atmel-samd/internal_flash.c index 4447f15d61..53a0a0c241 100644 --- a/atmel-samd/internal_flash.c +++ b/atmel-samd/internal_flash.c @@ -35,10 +35,14 @@ #include "py/runtime.h" #include "lib/oofatfs/ff.h" -#include "asf/sam0/drivers/nvm/nvm.h" -#include "asf/sam0/drivers/port/port.h" +#ifdef SAMD21 +#include "hpl/pm/hpl_pm_base.h" +#endif +#include "hal/include/hal_flash.h" -#include "rgb_led_status.h" +#include "supervisor/shared/rgb_led_status.h" + +static struct flash_descriptor internal_flash_desc; void internal_flash_init(void) { // Activity LED for flash writes. @@ -50,6 +54,14 @@ void internal_flash_init(void) { port_pin_set_config(MICROPY_HW_LED_MSC, &pin_conf); port_pin_set_output_level(MICROPY_HW_LED_MSC, false); #endif + + #ifdef SAMD51 + hri_mclk_set_AHBMASK_NVMCTRL_bit(MCLK); + #endif + #ifdef SAMD21 + _pm_enable_bus_clock(PM_BUS_APBB, NVMCTRL); + #endif + flash_init(&internal_flash_desc, NVMCTRL); } uint32_t internal_flash_get_block_size(void) { @@ -138,18 +150,8 @@ bool internal_flash_read_block(uint8_t *dest, uint32_t block) { // bad block number return false; } - enum status_code error_code; - // A block is made up of multiple pages. Read each page - // sequentially. - for (int i = 0; i < FILESYSTEM_BLOCK_SIZE / NVMCTRL_PAGE_SIZE; i++) { - do - { - error_code = nvm_read_buffer(src + i * NVMCTRL_PAGE_SIZE, - dest + i * NVMCTRL_PAGE_SIZE, - NVMCTRL_PAGE_SIZE); - } while (error_code == STATUS_BUSY); - } - return true; + int32_t error_code = flash_read(&internal_flash_desc, src, dest, FILESYSTEM_BLOCK_SIZE); + return error_code == ERR_NONE; } } @@ -169,36 +171,21 @@ bool internal_flash_write_block(const uint8_t *src, uint32_t block) { // bad block number return false; } - enum status_code error_code; + int32_t error_code; // A block is formed by two rows of flash. We must erase each row // before we write back to it. - do - { - error_code = nvm_erase_row(dest); - } while (error_code == STATUS_BUSY); - if (error_code != STATUS_OK) { - return false; - } - do - { - error_code = nvm_erase_row(dest + NVMCTRL_ROW_SIZE); - } while (error_code == STATUS_BUSY); - if (error_code != STATUS_OK) { + error_code = flash_erase(&internal_flash_desc, + dest, + FILESYSTEM_BLOCK_SIZE / flash_get_page_size(&internal_flash_desc)); + if (error_code != ERR_NONE) { return false; } // A block is made up of multiple pages. Write each page // sequentially. - for (int i = 0; i < FILESYSTEM_BLOCK_SIZE / NVMCTRL_PAGE_SIZE; i++) { - do - { - error_code = nvm_write_buffer(dest + i * NVMCTRL_PAGE_SIZE, - src + i * NVMCTRL_PAGE_SIZE, - NVMCTRL_PAGE_SIZE); - } while (error_code == STATUS_BUSY); - if (error_code != STATUS_OK) { - return false; - } + error_code = flash_append(&internal_flash_desc, dest, src, FILESYSTEM_BLOCK_SIZE); + if (error_code != ERR_NONE) { + return false; } clear_temp_status(); #ifdef MICROPY_HW_LED_MSC diff --git a/atmel-samd/internal_flash.h b/atmel-samd/internal_flash.h index 3de831f635..88c105386f 100644 --- a/atmel-samd/internal_flash.h +++ b/atmel-samd/internal_flash.h @@ -30,11 +30,19 @@ #include "mpconfigport.h" +#include "sam.h" + #define FLASH_ROOT_POINTERS -#define TOTAL_INTERNAL_FLASH_SIZE 0x010000 +#ifdef SAMD51 +#define TOTAL_INTERNAL_FLASH_SIZE (FLASH_SIZE / 2) +#endif -#define INTERNAL_FLASH_MEM_SEG1_START_ADDR (0x00040000 - TOTAL_INTERNAL_FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE) +#ifdef SAMD21 +#define TOTAL_INTERNAL_FLASH_SIZE 0x010000 +#endif + +#define INTERNAL_FLASH_MEM_SEG1_START_ADDR (FLASH_SIZE - TOTAL_INTERNAL_FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE) #define INTERNAL_FLASH_PART1_START_BLOCK (0x1) #define INTERNAL_FLASH_PART1_NUM_BLOCKS (TOTAL_INTERNAL_FLASH_SIZE / FILESYSTEM_BLOCK_SIZE) diff --git a/atmel-samd/mpconfigport.h b/atmel-samd/mpconfigport.h index 8881803e4e..3bdf3bfbdc 100644 --- a/atmel-samd/mpconfigport.h +++ b/atmel-samd/mpconfigport.h @@ -3,10 +3,6 @@ #ifndef __INCLUDED_MPCONFIGPORT_H #define __INCLUDED_MPCONFIGPORT_H -#define PORT_HEAP_SIZE (16384 + 4096) - -#define MICROPY_PY_SYS_PLATFORM "Atmel SAMD21" - #define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_C) // options to control how MicroPython is built @@ -138,10 +134,14 @@ typedef long mp_off_t; #ifdef SAMD21 #define CIRCUITPY_MCU_FAMILY samd21 +#define MICROPY_PY_SYS_PLATFORM "Atmel SAMD21" +#define PORT_HEAP_SIZE (16384 + 4096) #endif #ifdef SAMD51 #define CIRCUITPY_MCU_FAMILY samd51 +#define MICROPY_PY_SYS_PLATFORM "MicroChip SAMD51" +#define PORT_HEAP_SIZE (0x20000) // 128KiB #endif // extra built in modules to add to the list of known ones @@ -205,8 +205,6 @@ extern const struct _mp_obj_module_t usb_hid_module; // { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, -// { MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, -// { MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_samd),(mp_obj_t)&samd_module }, @@ -215,6 +213,8 @@ extern const struct _mp_obj_module_t usb_hid_module; { MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)µcontroller_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, EXTRA_BUILTIN_MODULES diff --git a/atmel-samd/supervisor/filesystem.c b/atmel-samd/supervisor/filesystem.c index a25a4b9e5b..2dfb2bfd38 100644 --- a/atmel-samd/supervisor/filesystem.c +++ b/atmel-samd/supervisor/filesystem.c @@ -28,6 +28,10 @@ #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" +#include "py/mpstate.h" + +#include "flash_api.h" + #ifdef EXPRESS_BOARD // #include "common-hal/touchio/TouchIn.h" #define INTERNAL_CIRCUITPY_CONFIG_START_ADDR (0x00040000 - 0x100 - CIRCUITPY_INTERNAL_NVM_SIZE) @@ -41,38 +45,49 @@ mp_vfs_mount_t mp_vfs_mount_flash; // we don't make this function static because it needs a lot of stack and we // want it to be executed without using stack within main() function void filesystem_init(void) { - // // init the vfs object - // fs_user_mount_t *vfs_fat = &fs_user_mount_flash; - // vfs_fat->flags = 0; - // flash_init_vfs(vfs_fat); - // - // // try to mount the flash - // FRESULT res = f_mount(&vfs_fat->fatfs); - // - // if (res == FR_NO_FILESYSTEM) { - // // no filesystem so create a fresh one - // - // uint8_t working_buf[_MAX_SS]; - // res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf)); - // // Flush the new file system to make sure its repaired immediately. - // flash_flush(); - // if (res != FR_OK) { - // return; - // } - // - // // set label - // f_setlabel(&vfs_fat->fatfs, "CIRCUITPY"); - // } else if (res != FR_OK) { - // return; - // } - // mp_vfs_mount_t *vfs = &mp_vfs_mount_flash; - // vfs->str = "/"; - // vfs->len = 1; - // vfs->obj = MP_OBJ_FROM_PTR(vfs_fat); - // vfs->next = NULL; - // MP_STATE_VM(vfs_mount_table) = vfs; - // - // // The current directory is used as the boot up directory. - // // It is set to the internal flash filesystem by default. - // MP_STATE_PORT(vfs_cur) = vfs; + // init the vfs object + fs_user_mount_t *vfs_fat = &fs_user_mount_flash; + vfs_fat->flags = 0; + flash_init_vfs(vfs_fat); + + // try to mount the flash + FRESULT res = f_mount(&vfs_fat->fatfs); + + if (res == FR_NO_FILESYSTEM) { + // no filesystem so create a fresh one + uint8_t working_buf[_MAX_SS]; + res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf)); + // Flush the new file system to make sure its repaired immediately. + flash_flush(); + if (res != FR_OK) { + return; + } + + // set label + f_setlabel(&vfs_fat->fatfs, "CIRCUITPY"); + } else if (res != FR_OK) { + return; + } + mp_vfs_mount_t *vfs = &mp_vfs_mount_flash; + vfs->str = "/"; + vfs->len = 1; + vfs->obj = MP_OBJ_FROM_PTR(vfs_fat); + vfs->next = NULL; + MP_STATE_VM(vfs_mount_table) = vfs; + + // The current directory is used as the boot up directory. + // It is set to the internal flash filesystem by default. + MP_STATE_PORT(vfs_cur) = vfs; +} + +void filesystem_flush(void) { + flash_flush(); +} + +void filesystem_default_writeable(bool writeable) { + flash_set_usb_writeable(writeable); +} + +bool filesystem_present(void) { + return true; } diff --git a/atmel-samd/usb.c b/atmel-samd/usb.c index 8d7870a2af..11a3d4eb94 100644 --- a/atmel-samd/usb.c +++ b/atmel-samd/usb.c @@ -123,6 +123,7 @@ static bool usb_device_cb_bulk_out(const uint8_t ep, const enum usb_xfer_code rc uint8_t buf[count]; int32_t result = cdcdf_acm_read(buf, count); if (result != ERR_NONE) { + atomic_leave_critical(&flags); return true; } diff --git a/supervisor/supervisor.mk b/supervisor/supervisor.mk index f67439a97e..c56557197c 100644 --- a/supervisor/supervisor.mk +++ b/supervisor/supervisor.mk @@ -4,7 +4,7 @@ SRC_SUPERVISOR = \ supervisor/shared/autoreload.c \ supervisor/shared/rgb_led_status.c -ifeq ($(wildcard supervisor/filesystem.c),) +ifeq ($(wildcard atmel-samd/supervisor/filesystem.c),) SRC_SUPERVISOR += supervisor/filesystem.c else SRC_SUPERVISOR += supervisor/stub/filesystem.c