From dd1171130680dbce746d49b3a71a4806e7ebce36 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 20 Dec 2017 10:31:05 -0500 Subject: [PATCH 01/49] Add support for 7-bit modes: 7N1 and 7N2 --- ports/stm32/uart.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index 0b46d4f040966..731afecbe4890 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -500,7 +500,21 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k if (!self->is_enabled) { mp_printf(print, "UART(%u)", self->uart_id); } else { - mp_int_t bits = (self->uart.Init.WordLength == UART_WORDLENGTH_8B ? 8 : 9); + mp_int_t bits; + switch(self->uart.Init.WordLength) { + case UART_WORDLENGTH_8B: + default: + bits = 8; + break; +#ifdef UART_WORDLENGTH_7B + case UART_WORDLENGTH_7B: + bits = 7; + break; +#endif + case UART_WORDLENGTH_9B: + bits = 9; + break; + } if (self->uart.Init.Parity != UART_PARITY_NONE) { bits -= 1; } @@ -580,6 +594,11 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const init->WordLength = UART_WORDLENGTH_8B; } else if (bits == 9) { init->WordLength = UART_WORDLENGTH_9B; +#ifdef UART_WORDLENGTH_7B + } else if (bits == 7) { + // possible with 7N1 or 7N2 + init->WordLength = UART_WORDLENGTH_7B; +#endif } else { mp_raise_ValueError("unsupported combination of bits and parity"); } From 31bbad3331d33ed464967c903b344f24ab8a9ead Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 20 Dec 2017 10:35:00 -0500 Subject: [PATCH 02/49] Port may specify a subset of UART's to run in half-duplex (all the time) --- ports/stm32/mpconfigport.h | 4 ++++ ports/stm32/uart.c | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 51d44256161ab..3a260477273ce 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -245,6 +245,10 @@ extern const struct _mp_obj_module_t mp_module_onewire; #define MICROPY_HW_MAX_UART (6) #endif +#ifndef MICROPY_HW_UARTn_IS_HALF_DUPLEX +#define MICROPY_HW_UARTn_IS_HALF_DUPLEX(n) (0) +#endif + #define MP_STATE_PORT MP_STATE_VM #define MICROPY_PORT_ROOT_POINTERS \ diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index 731afecbe4890..d3bfc229875bb 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -287,8 +287,13 @@ STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) { uart_obj->irqn = irqn; uart_obj->uart.Instance = UARTx; - // init UARTx - HAL_UART_Init(&uart_obj->uart); + if(MICROPY_HW_UARTn_IS_HALF_DUPLEX(uart_obj->uart_id)) { + // init in half-duplex mode + HAL_HalfDuplex_Init(&uart_obj->uart); + } else { + // init UARTx + HAL_UART_Init(&uart_obj->uart); + } uart_obj->is_enabled = true; @@ -534,6 +539,9 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k mp_printf(print, "CTS"); } } + if(MICROPY_HW_UARTn_IS_HALF_DUPLEX(self->uart_id)) { + mp_printf(print, ", half=1"); + } mp_printf(print, ", stop=%u, timeout=%u, timeout_char=%u, read_buf_len=%u)", self->uart.Init.StopBits == UART_STOPBITS_1 ? 1 : 2, self->timeout, self->timeout_char, From 6d6a1458e7d16bd8fbe1d5cabcae9649e8fc6742 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 20 Dec 2017 10:36:53 -0500 Subject: [PATCH 03/49] Bugfix: second DMA on a channel crashes because ChannelIndex gots reset (MCU_SERIES_L4 only, affects SDCard support) --- ports/stm32/dma.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ports/stm32/dma.c b/ports/stm32/dma.c index df6275d652ce9..5c91fed06ce1b 100644 --- a/ports/stm32/dma.c +++ b/ports/stm32/dma.c @@ -448,10 +448,25 @@ void dma_init(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, void *data){ } else { // only necessary initialization dma->State = HAL_DMA_STATE_READY; + #if defined(MCU_SERIES_F4) || defined(MCU_SERIES_F7) // calculate DMA base address and bitshift to be used in IRQ handler extern uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma); DMA_CalcBaseAndBitshift(dma); + +#elif defined(MCU_SERIES_L4) + // copied from stm32l4xx_hal_dma.c:dma_init() + if ((uint32_t)(dma->Instance) < (uint32_t)(DMA2_Channel1)) { + // DMA1 + dma->ChannelIndex = (((uint32_t)dma->Instance - (uint32_t)DMA1_Channel1) + / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2; + dma->DmaBaseAddress = DMA1; + } else { + // DMA2 + dma->ChannelIndex = (((uint32_t)dma->Instance - (uint32_t)DMA2_Channel1) + / ((uint32_t)DMA2_Channel2 - (uint32_t)DMA2_Channel1)) << 2; + dma->DmaBaseAddress = DMA2; + } #endif } From 992bd776cdd22d47a792be6971e808e361cfb7b3 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 20 Dec 2017 10:39:30 -0500 Subject: [PATCH 04/49] If MICROPY_HW_SPIn_MISO undefined, do not claim pin: permits output-only SPI use --- ports/stm32/spi.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ports/stm32/spi.c b/ports/stm32/spi.c index cfd9c2667f866..0216c347adb80 100644 --- a/ports/stm32/spi.c +++ b/ports/stm32/spi.c @@ -254,7 +254,9 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) { pins[0] = &MICROPY_HW_SPI1_NSS; #endif pins[1] = &MICROPY_HW_SPI1_SCK; + #if defined(MICROPY_HW_SPI1_MISO) pins[2] = &MICROPY_HW_SPI1_MISO; + #endif pins[3] = &MICROPY_HW_SPI1_MOSI; // enable the SPI clock __SPI1_CLK_ENABLE(); @@ -266,7 +268,9 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) { pins[0] = &MICROPY_HW_SPI2_NSS; #endif pins[1] = &MICROPY_HW_SPI2_SCK; + #if defined(MICROPY_HW_SPI2_MISO) pins[2] = &MICROPY_HW_SPI2_MISO; + #endif pins[3] = &MICROPY_HW_SPI2_MOSI; // enable the SPI clock __SPI2_CLK_ENABLE(); @@ -278,7 +282,9 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) { pins[0] = &MICROPY_HW_SPI3_NSS; #endif pins[1] = &MICROPY_HW_SPI3_SCK; + #if defined(MICROPY_HW_SPI3_MISO) pins[2] = &MICROPY_HW_SPI3_MISO; + #endif pins[3] = &MICROPY_HW_SPI3_MOSI; // enable the SPI clock __SPI3_CLK_ENABLE(); @@ -290,7 +296,9 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) { pins[0] = &MICROPY_HW_SPI4_NSS; #endif pins[1] = &MICROPY_HW_SPI4_SCK; + #if defined(MICROPY_HW_SPI4_MISO) pins[2] = &MICROPY_HW_SPI4_MISO; + #endif pins[3] = &MICROPY_HW_SPI4_MOSI; // enable the SPI clock __SPI4_CLK_ENABLE(); @@ -302,7 +310,9 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) { pins[0] = &MICROPY_HW_SPI5_NSS; #endif pins[1] = &MICROPY_HW_SPI5_SCK; + #if defined(MICROPY_HW_SPI5_MISO) pins[2] = &MICROPY_HW_SPI5_MISO; + #endif pins[3] = &MICROPY_HW_SPI5_MOSI; // enable the SPI clock __SPI5_CLK_ENABLE(); @@ -314,7 +324,9 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) { pins[0] = &MICROPY_HW_SPI6_NSS; #endif pins[1] = &MICROPY_HW_SPI6_SCK; + #if defined(MICROPY_HW_SPI6_MISO) pins[2] = &MICROPY_HW_SPI6_MISO; + #endif pins[3] = &MICROPY_HW_SPI6_MOSI; // enable the SPI clock __SPI6_CLK_ENABLE(); From da6f810af29b17806b06c7147fdbb1e6110a361f Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 20 Dec 2017 10:40:31 -0500 Subject: [PATCH 05/49] Correct compilier error (unused static) if no I2C ports defined by port --- ports/stm32/i2c.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/stm32/i2c.c b/ports/stm32/i2c.c index b22787cab58dd..c53b94f71e3d3 100644 --- a/ports/stm32/i2c.c +++ b/ports/stm32/i2c.c @@ -100,7 +100,10 @@ I2C_HandleTypeDef I2CHandle3 = {.Instance = NULL}; I2C_HandleTypeDef I2CHandle4 = {.Instance = NULL}; #endif +#if defined(MICROPY_HW_I2C1_SCL) || defined(MICROPY_HW_I2C2_SCL) \ + || defined(MICROPY_HW_I2C3_SCL) || defined(MICROPY_HW_I2C3_SCL) STATIC bool pyb_i2c_use_dma[4]; +#endif const pyb_i2c_obj_t pyb_i2c_obj[] = { #if defined(MICROPY_HW_I2C1_SCL) From 3874c1219fca7561b2f682fd3f3e0c9412da854f Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 20 Dec 2017 10:45:22 -0500 Subject: [PATCH 06/49] Move to corrected capitalization of HAL_SD_CardStateTypedef (not TypeDef). stm32lib has fixes in stm32_hal_legacy.h for other MCU series other than STM32L4 --- ports/stm32/sdcard.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index 484426b8468b1..a54e05011c3d8 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -251,7 +251,7 @@ STATIC HAL_StatusTypeDef sdcard_wait_finished(SD_HandleTypeDef *sd, uint32_t tim } // Wait for SD card to complete the operation for (;;) { - HAL_SD_CardStateTypeDef state = HAL_SD_GetCardState(sd); + HAL_SD_CardStateTypedef state = HAL_SD_GetCardState(sd); if (state == HAL_SD_CARD_TRANSFER) { return HAL_OK; } From d1d570f2b232c0550ff0b5c57087cdab109efdab Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Thu, 4 Jan 2018 10:58:59 -0500 Subject: [PATCH 07/49] unix/Makefile: allow override of mpconfigport.mk --- ports/unix/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/unix/Makefile b/ports/unix/Makefile index b5f9f8e7da855..cbe920f93c048 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -1,4 +1,6 @@ -include mpconfigport.mk +MPCONFIGPORT_MK ?= mpconfigport.mk +-include $(MPCONFIGPORT_MK) include ../../py/mkenv.mk FROZEN_DIR = scripts From 492859f06209648772b6ca8c72b8a689bc849a69 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Thu, 4 Jan 2018 11:36:35 -0500 Subject: [PATCH 08/49] drivers/display/ssd1306.py: call super as function --- drivers/display/ssd1306.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/display/ssd1306.py b/drivers/display/ssd1306.py index cebe10e67755f..178b4911d7125 100644 --- a/drivers/display/ssd1306.py +++ b/drivers/display/ssd1306.py @@ -32,7 +32,7 @@ def __init__(self, width, height, external_vcc): self.external_vcc = external_vcc self.pages = self.height // 8 self.buffer = bytearray(self.pages * self.width) - super.__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB) + super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB) self.init_display() def init_display(self): From 0524998ab8f0cc387df31564aefff4e646cb6ee7 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Thu, 4 Jan 2018 12:10:15 -0500 Subject: [PATCH 09/49] ports/stm32/system_stm32.c: make SystemClock_Config() a weak symbol --- ports/stm32/system_stm32.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/system_stm32.c b/ports/stm32/system_stm32.c index b71a03181a7c4..645261964e58d 100644 --- a/ports/stm32/system_stm32.c +++ b/ports/stm32/system_stm32.c @@ -317,7 +317,7 @@ void SystemInit(void) * * Timers run from APBx if APBx_PRESC=1, else 2x APBx */ -void SystemClock_Config(void) +MP_WEAK void SystemClock_Config(void) { RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; From e5c93777e29baccbb3091a8ca869c9774107d958 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 10 Jan 2018 10:16:01 -0500 Subject: [PATCH 10/49] toplevel/.gitignore: add .tags file to ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5e841a89c059d..1d940559d4116 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ # VIM Swap Files ###################### *.swp +.tags # Build directory ###################### From 790470f02f09d362a460789791687c7c542efc50 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Thu, 4 Jan 2018 11:45:36 -0500 Subject: [PATCH 11/49] ports/stm32/modmachine.c: handle case of no MICROPY_PY_MACHINE_I2C --- ports/stm32/modmachine.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index 8c59758fa309b..d9eb6383e7a5d 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -558,7 +558,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) }, { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) }, #endif +#if MICROPY_PY_MACHINE_I2C { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, +#endif { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&pyb_wdt_type) }, From 602682305c1188750a9874bffe7d4728ba266649 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 10 Jan 2018 13:55:54 -0500 Subject: [PATCH 12/49] As the last living person using ctags, no need to share this --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1d940559d4116..5e841a89c059d 100644 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,6 @@ # VIM Swap Files ###################### *.swp -.tags # Build directory ###################### From 025c3050d575a77defe318ea2b1c1833bf2f7bdc Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Thu, 1 Mar 2018 11:50:03 -0500 Subject: [PATCH 13/49] stm32/system_stm32.c: Make it easier to override VECT_TAB_OFFSET default --- ports/stm32/system_stm32.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/stm32/system_stm32.c b/ports/stm32/system_stm32.c index 645261964e58d..83695738a35c5 100644 --- a/ports/stm32/system_stm32.c +++ b/ports/stm32/system_stm32.c @@ -144,8 +144,10 @@ const uint32_t MSIRangeTable[12] = {100000, 200000, 400000, 800000, 1000000, 200 /*!< Uncomment the following line if you need to relocate your vector Table in Internal SRAM. */ /* #define VECT_TAB_SRAM */ +#ifndef VECT_TAB_OFFSET #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. This value must be a multiple of 0x200. */ +#endif /******************************************************************************/ /** From 89bd790a845058aa8e410e82488bddc9511859cf Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Thu, 29 Mar 2018 15:02:13 -0400 Subject: [PATCH 14/49] MCU_SERIES_L4 => STM32L4 --- ports/stm32/dma.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/dma.c b/ports/stm32/dma.c index c24de601ea298..632dbbfa54967 100644 --- a/ports/stm32/dma.c +++ b/ports/stm32/dma.c @@ -517,7 +517,7 @@ void dma_init(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, void *data){ extern uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma); DMA_CalcBaseAndBitshift(dma); -#elif defined(MCU_SERIES_L4) +#elif defined(STM32L4) // copied from stm32l4xx_hal_dma.c:dma_init() if ((uint32_t)(dma->Instance) < (uint32_t)(DMA2_Channel1)) { // DMA1 From 5017aa4e101a7328a0e73b12c19455e00285da1e Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Thu, 29 Mar 2018 15:03:17 -0400 Subject: [PATCH 15/49] stm32/main.c: Enable VCP+MSC in default boot.py if USB enabled for board --- ports/stm32/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index fb3e843bba4a4..b04d46892488a 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -136,8 +136,10 @@ static const char fresh_boot_py[] = "import machine\r\n" "import pyb\r\n" "#pyb.main('main.py') # main script to run after this one\r\n" -"#pyb.usb_mode('VCP+MSC') # act as a serial and a storage device\r\n" +#if MICROPY_HW_ENABLE_USB +"pyb.usb_mode('VCP+MSC') # act as a serial and a storage device\r\n" "#pyb.usb_mode('VCP+HID') # act as a serial device and a mouse\r\n" +#endif ; static const char fresh_main_py[] = From 8dca42e6e2914928fb2efc82c35a81f110a4466d Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Mon, 2 Apr 2018 10:07:42 -0400 Subject: [PATCH 16/49] Supress noise for CC project --- ports/.gitignore | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 ports/.gitignore diff --git a/ports/.gitignore b/ports/.gitignore new file mode 100644 index 0000000000000..059c3f194d12a --- /dev/null +++ b/ports/.gitignore @@ -0,0 +1,5 @@ +# this file only useful for Coldcard project +stm32/boards/COLDCARD +unix/coldcard-mpy +unix/unix_random.P + From 4bc05bf997c10be000da4d0238b64fd853f36f55 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Mon, 2 Apr 2018 11:17:00 -0400 Subject: [PATCH 17/49] stm32/main: allow override of init_flash_fs() and file contents --- ports/stm32/main.c | 69 +++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index b04d46892488a..196c79d7e85f0 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -129,44 +129,45 @@ STATIC mp_obj_t pyb_main(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_a } MP_DEFINE_CONST_FUN_OBJ_KW(pyb_main_obj, 1, pyb_main); -static const char fresh_boot_py[] = -"# boot.py -- run on boot-up\r\n" -"# can run arbitrary Python, but best to keep it minimal\r\n" -"\r\n" -"import machine\r\n" -"import pyb\r\n" -"#pyb.main('main.py') # main script to run after this one\r\n" -#if MICROPY_HW_ENABLE_USB -"pyb.usb_mode('VCP+MSC') # act as a serial and a storage device\r\n" -"#pyb.usb_mode('VCP+HID') # act as a serial device and a mouse\r\n" -#endif -; +// avoid inlining to avoid stack usage within main() +MP_WEAK MP_NOINLINE bool init_flash_fs(uint reset_mode) { + + static const char fresh_boot_py[] = + "# boot.py -- run on boot-up\r\n" + "# can run arbitrary Python, but best to keep it minimal\r\n" + "\r\n" + "import machine\r\n" + "import pyb\r\n" + "#pyb.main('main.py') # main script to run after this one\r\n" + #if MICROPY_HW_ENABLE_USB + "pyb.usb_mode('VCP+MSC') # act as a serial and a storage device\r\n" + "#pyb.usb_mode('VCP+HID') # act as a serial device and a mouse\r\n" + #endif + ; -static const char fresh_main_py[] = -"# main.py -- put your code here!\r\n" -; + static const char fresh_main_py[] = + "# main.py -- put your code here!\r\n" + ; -static const char fresh_pybcdc_inf[] = + static const char fresh_pybcdc_inf[] = #include "genhdr/pybcdc_inf.h" -; - -static const char fresh_readme_txt[] = -"This is a MicroPython board\r\n" -"\r\n" -"You can get started right away by writing your Python code in 'main.py'.\r\n" -"\r\n" -"For a serial prompt:\r\n" -" - Windows: you need to go to 'Device manager', right click on the unknown device,\r\n" -" then update the driver software, using the 'pybcdc.inf' file found on this drive.\r\n" -" Then use a terminal program like Hyperterminal or putty.\r\n" -" - Mac OS X: use the command: screen /dev/tty.usbmodem*\r\n" -" - Linux: use the command: screen /dev/ttyACM0\r\n" -"\r\n" -"Please visit http://micropython.org/help/ for further help.\r\n" -; + ; + + static const char fresh_readme_txt[] = + "This is a MicroPython board\r\n" + "\r\n" + "You can get started right away by writing your Python code in 'main.py'.\r\n" + "\r\n" + "For a serial prompt:\r\n" + " - Windows: you need to go to 'Device manager', right click on the unknown device,\r\n" + " then update the driver software, using the 'pybcdc.inf' file found on this drive.\r\n" + " Then use a terminal program like Hyperterminal or putty.\r\n" + " - Mac OS X: use the command: screen /dev/tty.usbmodem*\r\n" + " - Linux: use the command: screen /dev/ttyACM0\r\n" + "\r\n" + "Please visit http://micropython.org/help/ for further help.\r\n" + ; -// avoid inlining to avoid stack usage within main() -MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { // init the vfs object fs_user_mount_t *vfs_fat = &fs_user_mount_flash; vfs_fat->flags = 0; From b2494d4284d8e593e989bcb662ab46f250ef0381 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Tue, 10 Apr 2018 09:12:03 -0400 Subject: [PATCH 18/49] stm32/main.c: no need to enable USB in boot.py, see #3689 --- ports/stm32/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 196c79d7e85f0..d507b6033e433 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -140,7 +140,7 @@ MP_WEAK MP_NOINLINE bool init_flash_fs(uint reset_mode) { "import pyb\r\n" "#pyb.main('main.py') # main script to run after this one\r\n" #if MICROPY_HW_ENABLE_USB - "pyb.usb_mode('VCP+MSC') # act as a serial and a storage device\r\n" + "#pyb.usb_mode('VCP+MSC') # act as a serial and a storage device\r\n" "#pyb.usb_mode('VCP+HID') # act as a serial device and a mouse\r\n" #endif ; From 9c79154800c9689c56278fb20e07be08455ee313 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Tue, 10 Apr 2018 13:53:20 -0400 Subject: [PATCH 19/49] stm32/dma.c: move re-init code into dma_init_handle() as suggested by @tobbad --- ports/stm32/dma.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/ports/stm32/dma.c b/ports/stm32/dma.c index 632dbbfa54967..895f10c0b4e2e 100644 --- a/ports/stm32/dma.c +++ b/ports/stm32/dma.c @@ -475,8 +475,22 @@ void dma_init_handle(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, void dma->Init.Direction = dma_descr->transfer_direction; #if defined(STM32L4) || defined(STM32H7) dma->Init.Request = dma_descr->sub_instance; + + // Factor 4 (or <<2) for matching channel bit location with + // CSELR, IFCR, ISR .. register. + // See as well HAL_DMA_Init() + dma->ChannelIndex = ((dma_descr->id)%NSTREAMS_PER_CONTROLLER)<<2; + if (dma_descr->id < NSTREAMS_PER_CONTROLLER) { + dma->DmaBaseAddress = DMA1; + } else { + dma->DmaBaseAddress = DMA2; + } #else dma->Init.Channel = dma_descr->sub_instance; + + // calculate DMA base address and bitshift to be used in IRQ handler + extern uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma); + DMA_CalcBaseAndBitshift(dma); #endif // half of __HAL_LINKDMA(data, xxx, *dma) // caller must implement other half by doing: data->xxx = dma @@ -512,25 +526,6 @@ void dma_init(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, void *data){ } else { // only necessary initialization dma->State = HAL_DMA_STATE_READY; -#if defined(STM32F4) || defined(STM32F7) - // calculate DMA base address and bitshift to be used in IRQ handler - extern uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma); - DMA_CalcBaseAndBitshift(dma); - -#elif defined(STM32L4) - // copied from stm32l4xx_hal_dma.c:dma_init() - if ((uint32_t)(dma->Instance) < (uint32_t)(DMA2_Channel1)) { - // DMA1 - dma->ChannelIndex = (((uint32_t)dma->Instance - (uint32_t)DMA1_Channel1) - / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2; - dma->DmaBaseAddress = DMA1; - } else { - // DMA2 - dma->ChannelIndex = (((uint32_t)dma->Instance - (uint32_t)DMA2_Channel1) - / ((uint32_t)DMA2_Channel2 - (uint32_t)DMA2_Channel1)) << 2; - dma->DmaBaseAddress = DMA2; - } -#endif } HAL_NVIC_EnableIRQ(dma_irqn[dma_id]); From 0bfc7e9067cea327d3fadf5cff4840039a3bed57 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Thu, 12 Apr 2018 11:44:41 -0400 Subject: [PATCH 20/49] extmod/vfs_fat: enable FAT32 (and maybe ExFAT) in mkfs so larger cards can be formated --- extmod/vfs_fat.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 5666a6b0c1c88..de22642d1cfe2 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -111,7 +111,7 @@ STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) { // make the filesystem uint8_t working_buf[_MAX_SS]; - FRESULT res = f_mkfs(&vfs->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf)); + FRESULT res = f_mkfs(&vfs->fatfs, FM_ANY, 0, working_buf, sizeof(working_buf)); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); } @@ -383,7 +383,7 @@ STATIC mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs FRESULT res = (self->flags & FSUSER_NO_FILESYSTEM) ? FR_NO_FILESYSTEM : FR_OK; if (res == FR_NO_FILESYSTEM && mp_obj_is_true(mkfs)) { uint8_t working_buf[_MAX_SS]; - res = f_mkfs(&self->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf)); + res = f_mkfs(&self->fatfs, FM_ANY, 0, working_buf, sizeof(working_buf)); } if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); From dfbd1c468ed0fcd28eff4d6b5c1b9291cdd7eb38 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 13 Apr 2018 09:14:44 -0400 Subject: [PATCH 21/49] stm32/dma: STM32L4: always deinit/reinit DMA channels --- ports/stm32/dma.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/ports/stm32/dma.c b/ports/stm32/dma.c index 895f10c0b4e2e..067b080cc97a8 100644 --- a/ports/stm32/dma.c +++ b/ports/stm32/dma.c @@ -475,16 +475,6 @@ void dma_init_handle(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, void dma->Init.Direction = dma_descr->transfer_direction; #if defined(STM32L4) || defined(STM32H7) dma->Init.Request = dma_descr->sub_instance; - - // Factor 4 (or <<2) for matching channel bit location with - // CSELR, IFCR, ISR .. register. - // See as well HAL_DMA_Init() - dma->ChannelIndex = ((dma_descr->id)%NSTREAMS_PER_CONTROLLER)<<2; - if (dma_descr->id < NSTREAMS_PER_CONTROLLER) { - dma->DmaBaseAddress = DMA1; - } else { - dma->DmaBaseAddress = DMA2; - } #else dma->Init.Channel = dma_descr->sub_instance; @@ -512,6 +502,7 @@ void dma_init(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, void *data){ dma_enable_clock(dma_id); +#if !defined(STM32L4) // if this stream was previously configured for this channel/request then we // can skip most of the initialisation uint8_t sub_inst = DMA_SUB_INSTANCE_AS_UINT8(dma_descr->sub_instance); @@ -527,6 +518,13 @@ void dma_init(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, void *data){ // only necessary initialization dma->State = HAL_DMA_STATE_READY; } +#else + // ALWAYS reset and configure DMA peripheral + // (dma->State is set to HAL_DMA_STATE_RESET by memset above) + HAL_DMA_DeInit(dma); + HAL_DMA_Init(dma); + HAL_NVIC_SetPriority(dma_irqn[dma_id], IRQ_PRI_DMA, IRQ_SUBPRI_DMA); +#endif HAL_NVIC_EnableIRQ(dma_irqn[dma_id]); } From 4f0b68f9381bc1fca9ecd4e3205f03a3469e60f4 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 13 Apr 2018 09:25:39 -0400 Subject: [PATCH 22/49] stm32/sdcard: sdcard_wait_finished() bugfix: remove WFI for STM32L4 case --- ports/stm32/sdcard.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index 9244be5d4fa1c..a2ca007b2bb64 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -275,7 +275,9 @@ STATIC HAL_StatusTypeDef sdcard_wait_finished(SD_HandleTypeDef *sd, uint32_t tim enable_irq(irq_state); break; } +#if !defined(STM32L4) __WFI(); +#endif enable_irq(irq_state); if (HAL_GetTick() - start >= timeout) { return HAL_TIMEOUT; @@ -294,7 +296,9 @@ STATIC HAL_StatusTypeDef sdcard_wait_finished(SD_HandleTypeDef *sd, uint32_t tim if (HAL_GetTick() - start >= timeout) { return HAL_TIMEOUT; } +#if !defined(STM32L4) __WFI(); +#endif } return HAL_OK; } From b9231d9c171f5e22d711558cb3d8dc7a0892bee2 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 13 Apr 2018 09:26:58 -0400 Subject: [PATCH 23/49] stm32/sdcard.c: implement BP_IOCTL_SEC_COUNT so we can mkfs sdcards --- ports/stm32/sdcard.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index a2ca007b2bb64..9f735d2e9a79b 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -543,7 +543,7 @@ STATIC mp_obj_t pyb_sdcard_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in return MP_OBJ_NEW_SMALL_INT(0); // success case BP_IOCTL_SEC_COUNT: - return MP_OBJ_NEW_SMALL_INT(0); // TODO + return MP_OBJ_NEW_SMALL_INT(sdcard_get_capacity_in_bytes() / SDCARD_BLOCK_SIZE); case BP_IOCTL_SEC_SIZE: return MP_OBJ_NEW_SMALL_INT(SDCARD_BLOCK_SIZE); From 501b1e78c619da7f5131ad8597314e6e02003648 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 27 Apr 2018 12:54:35 +1000 Subject: [PATCH 24/49] stm32/system_stm32: Reconfigure SysTick IRQ priority for L4 MCUs. After calling HAL_SYSTICK_Config the SysTick IRQ priority is set to 15, the lowest priority. This commit reconfigures the IRQ priority to the desired TICK_INT_PRIORITY value. --- ports/stm32/system_stm32.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/system_stm32.c b/ports/stm32/system_stm32.c index 4a0bd945da96b..88cd78930efa5 100644 --- a/ports/stm32/system_stm32.c +++ b/ports/stm32/system_stm32.c @@ -600,8 +600,8 @@ MP_WEAK void SystemClock_Config(void) HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); - HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); + HAL_NVIC_SetPriority(SysTick_IRQn, TICK_INT_PRIORITY, 0); #endif } From ff286a74dde9d9741f91df46c45f837df16dc87a Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 27 Apr 2018 00:16:46 -0400 Subject: [PATCH 25/49] Undo hack regarding WFI --- ports/stm32/sdcard.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index 9f735d2e9a79b..4eb9fce6cd5b0 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -275,9 +275,7 @@ STATIC HAL_StatusTypeDef sdcard_wait_finished(SD_HandleTypeDef *sd, uint32_t tim enable_irq(irq_state); break; } -#if !defined(STM32L4) __WFI(); -#endif enable_irq(irq_state); if (HAL_GetTick() - start >= timeout) { return HAL_TIMEOUT; @@ -296,9 +294,7 @@ STATIC HAL_StatusTypeDef sdcard_wait_finished(SD_HandleTypeDef *sd, uint32_t tim if (HAL_GetTick() - start >= timeout) { return HAL_TIMEOUT; } -#if !defined(STM32L4) __WFI(); -#endif } return HAL_OK; } From 24cc1c16c8d355703d0320fd226f4c70ea2e46bf Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 11 May 2018 11:40:04 -0400 Subject: [PATCH 26/49] usbd_hid_interface.c: address possible race condition vs. interrupt --- ports/stm32/usbd_hid_interface.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/stm32/usbd_hid_interface.c b/ports/stm32/usbd_hid_interface.c index 4ee533c21cf9f..9ab5986b6680a 100644 --- a/ports/stm32/usbd_hid_interface.c +++ b/ports/stm32/usbd_hid_interface.c @@ -94,12 +94,13 @@ int usbd_hid_rx(usbd_hid_itf_t *hid, size_t len, uint8_t *buf, uint32_t timeout) } // Copy bytes from device to user buffer - memcpy(buf, hid->buffer[hid->current_read_buffer], hid->last_read_len); + int read_len = hid->last_read_len; + memcpy(buf, hid->buffer[hid->current_read_buffer], read_len); hid->current_read_buffer = !hid->current_read_buffer; // Clear NAK to indicate we are ready to read more data USBD_HID_ClearNAK(hid->usbd); // Success, return number of bytes read - return hid->last_read_len; + return read_len; } From 92b1f3ab5dccbe9e85158127cb71f94183814540 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 11 May 2018 11:42:19 -0400 Subject: [PATCH 27/49] usbd_cdc_msc_hid.c: bugfix: dont replace existing outgoing report if host hasnt read it yet --- ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c index a1a7cff6c1f3a..079002147e6b6 100644 --- a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c +++ b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c @@ -1162,9 +1162,10 @@ uint8_t USBD_HID_SendReport(usbd_cdc_msc_hid_state_t *usbd, uint8_t *report, uin if (usbd->HID_ClassData.state == HID_IDLE) { usbd->HID_ClassData.state = HID_BUSY; USBD_LL_Transmit(usbd->pdev, usbd->hid_in_ep, report, len); + return USBD_OK; } } - return USBD_OK; + return USBD_FAIL; } uint8_t USBD_HID_SetNAK(usbd_cdc_msc_hid_state_t *usbd) { From 6372f60c8f30f2e27de5a4e0064460a993ab8ee7 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Thu, 17 May 2018 09:07:06 -0400 Subject: [PATCH 28/49] Support override of USB vender/product strings --- ports/stm32/usbd_desc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/stm32/usbd_desc.c b/ports/stm32/usbd_desc.c index 4babebf64030d..6a2cfaff0167d 100644 --- a/ports/stm32/usbd_desc.c +++ b/ports/stm32/usbd_desc.c @@ -41,6 +41,7 @@ #define USBD_VID 0xf055 #define USBD_PID 0x9800 #define USBD_LANGID_STRING 0x409 +#ifndef USBD_MANUFACTURER_STRING #define USBD_MANUFACTURER_STRING "MicroPython" #define USBD_PRODUCT_HS_STRING "Pyboard Virtual Comm Port in HS Mode" #define USBD_PRODUCT_FS_STRING "Pyboard Virtual Comm Port in FS Mode" @@ -48,6 +49,7 @@ #define USBD_INTERFACE_HS_STRING "Pyboard Interface" #define USBD_CONFIGURATION_FS_STRING "Pyboard Config" #define USBD_INTERFACE_FS_STRING "Pyboard Interface" +#endif __ALIGN_BEGIN static const uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = { USB_LEN_LANGID_STR_DESC, From 9aeacb9ccf517a66878be4304acd7fe368da35e3 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 13 Jun 2018 10:10:01 -0400 Subject: [PATCH 29/49] Correct hash: 92f23d6 --- lib/lwip | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lwip b/lib/lwip index 5b8b5d459e7dd..92f23d6ca0971 160000 --- a/lib/lwip +++ b/lib/lwip @@ -1 +1 @@ -Subproject commit 5b8b5d459e7dd890724515bbfad86c705234f9ec +Subproject commit 92f23d6ca0971a32f2085b9480e738d34174417b From d2f32935286fc8f86c779e2ad2f65547427f1acc Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 13 Jun 2018 10:10:58 -0400 Subject: [PATCH 30/49] Correct hash: 1fe30d1 --- lib/stm32lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stm32lib b/lib/stm32lib index d2bcfda543d3b..1fe30d1446f2e 160000 --- a/lib/stm32lib +++ b/lib/stm32lib @@ -1 +1 @@ -Subproject commit d2bcfda543d3b99361e44112aca929225bdcc07f +Subproject commit 1fe30d1446f2eba3730dc4b05e9ac0cf03bcf9bf From a6b981a3f0e830e463b0ee3c7aa87b7b4c150124 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 13 Jun 2018 11:31:16 -0400 Subject: [PATCH 31/49] Bugfix for typo in merge --- ports/stm32/main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 59c55aedfc72c..aa9000ff215ff 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -153,8 +153,6 @@ MP_WEAK MP_NOINLINE bool init_flash_fs(uint reset_mode) { ; static const char fresh_pybcdc_inf[] = - - static const char fresh_pybcdc_inf[] = #include "genhdr/pybcdc_inf.h" ; From 31e8a48b430405474256a5b979da9b35cdc9e7c8 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 13 Jun 2018 11:31:45 -0400 Subject: [PATCH 32/49] extmod/vfs_posix.c: Add stdio.h include for rename() --- extmod/vfs_posix.c | 1 + 1 file changed, 1 insertion(+) diff --git a/extmod/vfs_posix.c b/extmod/vfs_posix.c index 6e3bb2c5bd1b0..caaa71330b5a5 100644 --- a/extmod/vfs_posix.c +++ b/extmod/vfs_posix.c @@ -34,6 +34,7 @@ #include #include #include +#include typedef struct _mp_obj_vfs_posix_t { mp_obj_base_t base; From a177941f549f70e75c4210fa88a9427d778f46a2 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 15 Jun 2018 00:13:13 -0400 Subject: [PATCH 33/49] unix/modos.c: align ilistdir type value with docs --- ports/unix/modos.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ports/unix/modos.c b/ports/unix/modos.c index d99d0d62c9a03..f2a268fb7cea2 100644 --- a/ports/unix/modos.c +++ b/ports/unix/modos.c @@ -173,10 +173,12 @@ STATIC mp_obj_t listdir_next(mp_obj_t self_in) { mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); t->items[0] = mp_obj_new_str(dirent->d_name, strlen(dirent->d_name)); #ifdef _DIRENT_HAVE_D_TYPE - t->items[1] = MP_OBJ_NEW_SMALL_INT(dirent->d_type); + t->items[1] = MP_OBJ_NEW_SMALL_INT(dirent->d_type == DT_DIR + ? 0x4000 + : (dirent->d_type == DT_REG ? 0x8000 : 0)); #else - // DT_UNKNOWN should have 0 value on any reasonable system - t->items[1] = MP_OBJ_NEW_SMALL_INT(0); + // assume only regular files if we can't tell more + t->items[1] = MP_OBJ_NEW_SMALL_INT(0x8000); #endif #ifdef _DIRENT_HAVE_D_INO t->items[2] = MP_OBJ_NEW_SMALL_INT(dirent->d_ino); From e5a86f88da272a8f3f59fbda284217d098b6376d Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 13 Jul 2018 10:20:37 -0400 Subject: [PATCH 34/49] stm32/fatfs_port.c: bugfix when MICROPY_HW_ENABLE_RTC not set --- ports/stm32/fatfs_port.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ports/stm32/fatfs_port.c b/ports/stm32/fatfs_port.c index d0e311ed77102..bc97ad7edfa71 100644 --- a/ports/stm32/fatfs_port.c +++ b/ports/stm32/fatfs_port.c @@ -28,11 +28,16 @@ #include "lib/oofatfs/ff.h" #include "rtc.h" -DWORD get_fattime(void) { +MP_WEAK DWORD get_fattime(void) { +#if MICROPY_HW_ENABLE_RTC rtc_init_finalise(); RTC_TimeTypeDef time; RTC_DateTypeDef date; HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN); HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN); return ((2000 + date.Year - 1980) << 25) | ((date.Month) << 21) | ((date.Date) << 16) | ((time.Hours) << 11) | ((time.Minutes) << 5) | (time.Seconds / 2); +#else + // Jan 1st, 2018 at midnight. Not sure what timezone. + return ((2018 - 1980) << 25) | ((1) << 21) | ((1) << 16) | ((0) << 11) | ((0) << 5) | (0 / 2); +#endif } From 743147b89e6b8f749698dedaedec323c06c771c1 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 13 Jul 2018 10:23:59 -0400 Subject: [PATCH 35/49] stm32/flashbdev.c: Bugfix: was dereferencing 0x800 rather than using as litteral --- ports/stm32/flashbdev.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ports/stm32/flashbdev.c b/ports/stm32/flashbdev.c index 5ae67d1ec2f2f..a36e3bcf74e2f 100644 --- a/ports/stm32/flashbdev.c +++ b/ports/stm32/flashbdev.c @@ -95,14 +95,15 @@ STATIC byte flash_cache_mem[0x4000] __attribute__((aligned(4))); // 16k #elif defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L496xx) +// The STM32L475/6 doesn't have CCRAM, so we use the 32K SRAM2 for this, although +// actual location and size is defined by the linker script. extern uint8_t _flash_fs_start; extern uint8_t _flash_fs_end; extern uint32_t _ram_fs_cache_start[2048 / 4]; extern uint32_t _ram_fs_cache_block_size; -// The STM32L475/6 doesn't have CCRAM, so we use the 32K SRAM2 for this. -#define CACHE_MEM_START_ADDR (&_ram_fs_cache_start) // End of SRAM2 RAM segment-2k -#define FLASH_SECTOR_SIZE_MAX (_ram_fs_cache_block_size) // 2k max +#define CACHE_MEM_START_ADDR (&_ram_fs_cache_start) +#define FLASH_SECTOR_SIZE_MAX ((uint32_t)(&_ram_fs_cache_block_size)) // 2k max #define FLASH_MEM_SEG1_START_ADDR ((long)&_flash_fs_start) #define FLASH_MEM_SEG1_NUM_BLOCKS ((&_flash_fs_end - &_flash_fs_start) / 512) From 3a138196a7e72e4ef799ee581ccf5afcfc96699a Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 13 Jul 2018 10:23:59 -0400 Subject: [PATCH 36/49] stm32/flashbdev.c: Bugfix: was dereferencing 0x800 rather than using as litteral --- ports/stm32/flashbdev.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ports/stm32/flashbdev.c b/ports/stm32/flashbdev.c index 5ae67d1ec2f2f..a36e3bcf74e2f 100644 --- a/ports/stm32/flashbdev.c +++ b/ports/stm32/flashbdev.c @@ -95,14 +95,15 @@ STATIC byte flash_cache_mem[0x4000] __attribute__((aligned(4))); // 16k #elif defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L496xx) +// The STM32L475/6 doesn't have CCRAM, so we use the 32K SRAM2 for this, although +// actual location and size is defined by the linker script. extern uint8_t _flash_fs_start; extern uint8_t _flash_fs_end; extern uint32_t _ram_fs_cache_start[2048 / 4]; extern uint32_t _ram_fs_cache_block_size; -// The STM32L475/6 doesn't have CCRAM, so we use the 32K SRAM2 for this. -#define CACHE_MEM_START_ADDR (&_ram_fs_cache_start) // End of SRAM2 RAM segment-2k -#define FLASH_SECTOR_SIZE_MAX (_ram_fs_cache_block_size) // 2k max +#define CACHE_MEM_START_ADDR (&_ram_fs_cache_start) +#define FLASH_SECTOR_SIZE_MAX ((uint32_t)(&_ram_fs_cache_block_size)) // 2k max #define FLASH_MEM_SEG1_START_ADDR ((long)&_flash_fs_start) #define FLASH_MEM_SEG1_NUM_BLOCKS ((&_flash_fs_end - &_flash_fs_start) / 512) From 302ddb579159d6c57e1385a142c928dc339963ab Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Tue, 17 Jul 2018 14:22:35 -0400 Subject: [PATCH 37/49] stm32/mphalport.c: make mp_hal_stdin_rx_chr() and mp_hal_stdout_tx_strn() weakly linked for customizations --- ports/stm32/mphalport.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32/mphalport.c b/ports/stm32/mphalport.c index a2f8e412ee25b..206221721bb80 100644 --- a/ports/stm32/mphalport.c +++ b/ports/stm32/mphalport.c @@ -19,7 +19,7 @@ NORETURN void mp_hal_raise(HAL_StatusTypeDef status) { mp_raise_OSError(mp_hal_status_to_errno_table[status]); } -int mp_hal_stdin_rx_chr(void) { +MP_WEAK int mp_hal_stdin_rx_chr(void) { for (;;) { #if 0 #ifdef USE_HOST_MODE @@ -52,7 +52,7 @@ void mp_hal_stdout_tx_str(const char *str) { mp_hal_stdout_tx_strn(str, strlen(str)); } -void mp_hal_stdout_tx_strn(const char *str, size_t len) { +MP_WEAK void mp_hal_stdout_tx_strn(const char *str, size_t len) { if (MP_STATE_PORT(pyb_stdio_uart) != NULL) { uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len); } From 2fbcf3ba6b1025de041f2ca614c32dc14b87b7dc Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Tue, 17 Jul 2018 14:32:14 -0400 Subject: [PATCH 38/49] Define ptr to end of fs cache, rather than length in linker script --- ports/stm32/boards/stm32l476xe.ld | 2 +- ports/stm32/boards/stm32l476xg.ld | 2 +- ports/stm32/boards/stm32l496xg.ld | 2 +- ports/stm32/flashbdev.c | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ports/stm32/boards/stm32l476xe.ld b/ports/stm32/boards/stm32l476xe.ld index 22c4466c66b10..31929517d77f6 100644 --- a/ports/stm32/boards/stm32l476xe.ld +++ b/ports/stm32/boards/stm32l476xe.ld @@ -25,7 +25,7 @@ _estack = ORIGIN(RAM) + LENGTH(RAM); /* RAM extents for the garbage collector */ _ram_fs_cache_start = ORIGIN(FS_CACHE); -_ram_fs_cache_block_size = LENGTH(FS_CACHE); +_ram_fs_cache_end = ORIGIN(FS_CACHE) + LENGTH(FS_CACHE); _ram_start = ORIGIN(RAM); _ram_end = ORIGIN(RAM) + LENGTH(RAM); _heap_start = _ebss; /* heap starts just after statically allocated memory */ diff --git a/ports/stm32/boards/stm32l476xg.ld b/ports/stm32/boards/stm32l476xg.ld index 40d679ac39d73..59c5d90b69aca 100644 --- a/ports/stm32/boards/stm32l476xg.ld +++ b/ports/stm32/boards/stm32l476xg.ld @@ -25,7 +25,7 @@ _estack = ORIGIN(RAM) + LENGTH(RAM); /* RAM extents for the garbage collector */ _ram_fs_cache_start = ORIGIN(FS_CACHE); -_ram_fs_cache_block_size = LENGTH(FS_CACHE); +_ram_fs_cache_end = ORIGIN(FS_CACHE) + LENGTH(FS_CACHE); _ram_start = ORIGIN(RAM); _ram_end = ORIGIN(RAM) + LENGTH(RAM); _heap_start = _ebss; /* heap starts just after statically allocated memory */ diff --git a/ports/stm32/boards/stm32l496xg.ld b/ports/stm32/boards/stm32l496xg.ld index 88221170b4207..b42087319964e 100644 --- a/ports/stm32/boards/stm32l496xg.ld +++ b/ports/stm32/boards/stm32l496xg.ld @@ -25,7 +25,7 @@ _estack = ORIGIN(RAM) + LENGTH(RAM) + LENGTH(SRAM2); /* RAM extents for the garbage collector */ _ram_fs_cache_start = ORIGIN(FS_CACHE); -_ram_fs_cache_block_size = LENGTH(FS_CACHE); +_ram_fs_cache_end = ORIGIN(FS_CACHE) + LENGTH(FS_CACHE); _ram_start = ORIGIN(RAM); _ram_end = ORIGIN(RAM) + LENGTH(RAM) + LENGTH(SRAM2); _heap_start = _ebss; /* heap starts just after statically allocated memory */ diff --git a/ports/stm32/flashbdev.c b/ports/stm32/flashbdev.c index a36e3bcf74e2f..41a97f1c844b9 100644 --- a/ports/stm32/flashbdev.c +++ b/ports/stm32/flashbdev.c @@ -99,11 +99,11 @@ STATIC byte flash_cache_mem[0x4000] __attribute__((aligned(4))); // 16k // actual location and size is defined by the linker script. extern uint8_t _flash_fs_start; extern uint8_t _flash_fs_end; -extern uint32_t _ram_fs_cache_start[2048 / 4]; -extern uint32_t _ram_fs_cache_block_size; +extern uint8_t _ram_fs_cache_start[0]; // size determined by linker file +extern uint8_t _ram_fs_cache_end[0]; #define CACHE_MEM_START_ADDR (&_ram_fs_cache_start) -#define FLASH_SECTOR_SIZE_MAX ((uint32_t)(&_ram_fs_cache_block_size)) // 2k max +#define FLASH_SECTOR_SIZE_MAX (&_ram_fs_cache_end - &_ram_fs_cache_start) // 2k max #define FLASH_MEM_SEG1_START_ADDR ((long)&_flash_fs_start) #define FLASH_MEM_SEG1_NUM_BLOCKS ((&_flash_fs_end - &_flash_fs_start) / 512) From c04dfcd5d12abb16d57984851ec7fa82d6c2cb9e Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Thu, 11 Apr 2019 14:46:56 -0400 Subject: [PATCH 39/49] ports/stm32/gccollect.c: dont assume end of stack == end of RAM --- ports/stm32/gccollect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/gccollect.c b/ports/stm32/gccollect.c index cdec2a136cf8c..273e8b80ecb9c 100644 --- a/ports/stm32/gccollect.c +++ b/ports/stm32/gccollect.c @@ -52,7 +52,7 @@ void gc_collect(void) { #if MICROPY_PY_THREAD gc_collect_root((void**)sp, ((uint32_t)MP_STATE_THREAD(stack_top) - sp) / sizeof(uint32_t)); #else - gc_collect_root((void**)sp, ((uint32_t)&_ram_end - sp) / sizeof(uint32_t)); + gc_collect_root((void**)sp, ((uint32_t)&_estack - sp) / sizeof(uint32_t)); #endif // trace root pointers from any threads From be04812db5cae6bfc33bf4397bef3e2d56a21f36 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Mon, 8 Jul 2019 10:57:59 -0400 Subject: [PATCH 40/49] More flexibility in heap size/location --- ports/stm32/main.c | 2 +- ports/stm32/mpconfigboard_common.h | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index aa9000ff215ff..d8a7260e6318f 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -557,7 +557,7 @@ void stm32_main(uint32_t reset_mode) { mp_stack_set_limit((char*)&_estack - (char*)&_heap_end - 1024); // GC init - gc_init(&_heap_start, &_heap_end); + gc_init(MICROPY_HEAP_START, MICROPY_HEAP_END); #if MICROPY_ENABLE_PYSTACK static mp_obj_t pystack[384]; diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 2cc02b77cf06d..5316113cc10ef 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -110,6 +110,14 @@ /*****************************************************************************/ // General configuration +// Heap start / end definitions +#ifndef MICROPY_HEAP_START +#define MICROPY_HEAP_START &_heap_start +#endif +#ifndef MICROPY_HEAP_END +#define MICROPY_HEAP_END &_heap_end +#endif + // Configuration for STM32F0 series #if defined(STM32F0) From 37afd6d0b2df8123aea226554cb782b2e2bd91f1 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Mon, 28 Oct 2019 14:19:56 -0400 Subject: [PATCH 41/49] lib/utils/printf: Exclude __GI_vsnprintf alias for gcc 9 and above. See issue #4457. --- lib/utils/printf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/utils/printf.c b/lib/utils/printf.c index 51dfa5b963473..09899adbb9685 100644 --- a/lib/utils/printf.c +++ b/lib/utils/printf.c @@ -103,9 +103,11 @@ STATIC void strn_print_strn(void *data, const char *str, size_t len) { strn_print_env->remain -= len; } -#if defined(__GNUC__) && !defined(__clang__) +#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 9 // uClibc requires this alias to be defined, or there may be link errors // when linkings against it statically. +// GCC 9 gives a warning about missing attributes so it's excluded until +// uClibc+GCC9 support is needed. int __GI_vsnprintf(char *str, size_t size, const char *fmt, va_list ap) __attribute__((weak, alias ("vsnprintf"))); #endif From b0d1dd08be2f11489c48f3c200d0faf617bc9fd8 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Mon, 27 Apr 2020 11:22:40 -0400 Subject: [PATCH 42/49] Extend SDCard.info() with CSD and CID values from the card itself --- ports/stm32/sdcard.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index 27e7a34b2645b..3be833e4a4583 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -463,13 +463,15 @@ STATIC mp_obj_t sd_info(mp_obj_t self) { } HAL_SD_CardInfoTypeDef cardinfo; HAL_SD_GetCardInfo(&sd_handle, &cardinfo); - // cardinfo.SD_csd and cardinfo.SD_cid have lots of info but we don't use them - mp_obj_t tuple[3] = { + // cardinfo.SD_csd and cardinfo.SD_cid have lots of info, so let's share them + mp_obj_t tuple[5] = { mp_obj_new_int_from_ull((uint64_t)cardinfo.LogBlockNbr * (uint64_t)cardinfo.LogBlockSize), mp_obj_new_int_from_uint(cardinfo.LogBlockSize), mp_obj_new_int(cardinfo.CardType), + mp_obj_new_bytes((const uint8_t *)&sd_handle.CSD, 16), + mp_obj_new_bytes((const uint8_t *)&sd_handle.CID, 16) }; - return mp_obj_new_tuple(3, tuple); + return mp_obj_new_tuple(5, tuple); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(sd_info_obj, sd_info); From 39bc837052dcd651941999f2e8e5daa4ec57c520 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 29 Apr 2020 12:46:50 -0400 Subject: [PATCH 43/49] Quite a warning w/ py37 --- tools/pydfu.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/pydfu.py b/tools/pydfu.py index a33e4971296a9..a6b76bd2cb9d0 100755 --- a/tools/pydfu.py +++ b/tools/pydfu.py @@ -62,7 +62,11 @@ __DFU_INTERFACE = 0 import inspect -if 'length' in inspect.getargspec(usb.util.get_string).args: +# Python 3 deprecated getargspec in favour of getfullargspec, but +# Python 2 doesn't have the latter, so detect which one to use +getargspec = getattr(inspect, "getfullargspec", inspect.getargspec) + +if "length" in getargspec(usb.util.get_string).args: # PyUSB 1.0.0.b1 has the length argument def get_string(dev, index): return usb.util.get_string(dev, 255, index) From 4c945ff9aa614b363ef1eb8fdfcb15f4099bb726 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 28 Dec 2019 11:54:49 +1100 Subject: [PATCH 44/49] unix/modos: Add uos.rename and uos.rmdir. The existing uos.remove cannot be used to remove directories, instead uos.rmdir is needed. And also provide uos.rename to get a good set of filesystem functionality without requiring additional Python-level os functions (eg using ffi). --- ports/unix/modos.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ports/unix/modos.c b/ports/unix/modos.c index f2a268fb7cea2..70860ff32aa24 100644 --- a/ports/unix/modos.c +++ b/ports/unix/modos.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -117,6 +118,29 @@ STATIC mp_obj_t mod_os_unlink(mp_obj_t path_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_unlink_obj, mod_os_unlink); +STATIC mp_obj_t mod_os_rename(mp_obj_t old_path_in, mp_obj_t new_path_in) { + const char *old_path = mp_obj_str_get_str(old_path_in); + const char *new_path = mp_obj_str_get_str(new_path_in); + + int r = rename(old_path, new_path); + + RAISE_ERRNO(r, errno); + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_os_rename_obj, mod_os_rename); + +STATIC mp_obj_t mod_os_rmdir(mp_obj_t path_in) { + const char *path = mp_obj_str_get_str(path_in); + + int r = rmdir(path); + + RAISE_ERRNO(r, errno); + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_rmdir_obj, mod_os_rmdir); + STATIC mp_obj_t mod_os_system(mp_obj_t cmd_in) { const char *cmd = mp_obj_str_get_str(cmd_in); @@ -220,6 +244,8 @@ STATIC const mp_rom_map_elem_t mp_module_os_globals_table[] = { #endif { MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&mod_os_system_obj) }, { MP_ROM_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&mod_os_unlink_obj) }, + { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&mod_os_rename_obj) }, + { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&mod_os_rmdir_obj) }, { MP_ROM_QSTR(MP_QSTR_getenv), MP_ROM_PTR(&mod_os_getenv_obj) }, { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mod_os_mkdir_obj) }, { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&mod_os_ilistdir_obj) }, From a094a0a55efb08101ee08edfd9712e8404ddfbd2 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 16 Nov 2018 12:38:57 +0300 Subject: [PATCH 45/49] unix/modos: Rename unlink to remove to be consistent with other ports. We standardized to provide uos.remove() as a more obvious and user-friendly name. That's what written in the docs. The Unix port implementation predates this convention, so update it now. --- ports/unix/modos.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ports/unix/modos.c b/ports/unix/modos.c index 70860ff32aa24..bf57430e4a539 100644 --- a/ports/unix/modos.c +++ b/ports/unix/modos.c @@ -107,16 +107,21 @@ STATIC mp_obj_t mod_os_statvfs(mp_obj_t path_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_statvfs_obj, mod_os_statvfs); #endif -STATIC mp_obj_t mod_os_unlink(mp_obj_t path_in) { +STATIC mp_obj_t mod_os_remove(mp_obj_t path_in) { const char *path = mp_obj_str_get_str(path_in); + // Note that POSIX requires remove() to be able to delete a directory + // too (act as rmdir()). This is POSIX extenstion to ANSI C semantics + // of that function. But Python remove() follows ANSI C, and explicitly + // required to raise exception on attempt to remove a directory. Thus, + // call POSIX unlink() here. int r = unlink(path); RAISE_ERRNO(r, errno); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_unlink_obj, mod_os_unlink); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_remove_obj, mod_os_remove); STATIC mp_obj_t mod_os_rename(mp_obj_t old_path_in, mp_obj_t new_path_in) { const char *old_path = mp_obj_str_get_str(old_path_in); @@ -243,7 +248,7 @@ STATIC const mp_rom_map_elem_t mp_module_os_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&mod_os_statvfs_obj) }, #endif { MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&mod_os_system_obj) }, - { MP_ROM_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&mod_os_unlink_obj) }, + { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&mod_os_remove_obj) }, { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&mod_os_rename_obj) }, { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&mod_os_rmdir_obj) }, { MP_ROM_QSTR(MP_QSTR_getenv), MP_ROM_PTR(&mod_os_getenv_obj) }, From db4e297ea1f40d2105cea1e2592e46fbda918dbc Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Tue, 4 Aug 2020 15:31:43 -0400 Subject: [PATCH 46/49] Oops --- drivers/dht/dht.c | 3 +++ ports/stm32/modmachine.c | 2 ++ ports/stm32/modpyb.c | 4 ++++ ports/stm32/mpconfigboard_common.h | 6 ++++++ 4 files changed, 15 insertions(+) diff --git a/drivers/dht/dht.c b/drivers/dht/dht.c index 5d92ae39a3326..96f48a538c748 100644 --- a/drivers/dht/dht.c +++ b/drivers/dht/dht.c @@ -1,3 +1,4 @@ +#ifdef MICROPY_HW_ENABLE_DHT /* * This file is part of the MicroPython project, http://micropython.org/ * @@ -87,3 +88,5 @@ STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) { mp_raise_OSError(MP_ETIMEDOUT); } MP_DEFINE_CONST_FUN_OBJ_2(dht_readinto_obj, dht_readinto); + +#endif diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index 158f5f2b34805..b9ebc56bfa78a 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -630,7 +630,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&pyb_disable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&pyb_enable_irq_obj) }, +#if MICROPY_PY_MACHINE_PULSE { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, +#endif { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) }, { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, diff --git a/ports/stm32/modpyb.c b/ports/stm32/modpyb.c index 5afbbc4842945..8681b57557265 100644 --- a/ports/stm32/modpyb.c +++ b/ports/stm32/modpyb.c @@ -30,7 +30,9 @@ #include "py/runtime.h" #include "py/mphal.h" #include "lib/utils/pyexec.h" +#if MICROPY_HW_ENABLE_DHT #include "drivers/dht/dht.h" +#endif #include "stm32_it.h" #include "irq.h" #include "led.h" @@ -164,8 +166,10 @@ STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) }, #endif +#if MICROPY_HW_ENABLE_DHT // This function is not intended to be public and may be moved elsewhere { MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) }, +#endif { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&pyb_timer_type) }, diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 5316113cc10ef..2d70f3d2099d0 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -224,3 +224,9 @@ #endif #define MICROPY_HW_USES_BOOTLOADER (MICROPY_HW_VTOR != 0x08000000) + +// drivers/dht ... a specific enviro sensor of some sort +#ifndef MICROPY_HW_ENABLE_DHT +#define MICROPY_HW_ENABLE_DHT (1) +#endif + From cfb6444a794a22375cf13322aae8fae2179b6d1c Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 5 Feb 2021 09:34:36 -0500 Subject: [PATCH 47/49] extmod/moduhashlib: allow multiple calls to digest() method --- extmod/moduhashlib.c | 6 ++++-- tests/extmod/uhashlib_sha256.py | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 195a24334b69f..372a127183219 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -98,7 +98,8 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); vstr_t vstr; vstr_init_len(&vstr, 32); - mbedtls_sha256_finish_ret((mbedtls_sha256_context *)&self->state, (unsigned char *)vstr.buf); + mbedtls_sha256_context tmp_ctx = *(mbedtls_sha256_context *)self->state; + mbedtls_sha256_finish_ret(&tmp_ctx, (unsigned char *)vstr.buf); return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } @@ -129,7 +130,8 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); vstr_t vstr; vstr_init_len(&vstr, SHA256_BLOCK_SIZE); - sha256_final((CRYAL_SHA256_CTX *)self->state, (byte *)vstr.buf); + CRYAL_SHA256_CTX tmp_ctx = *(CRYAL_SHA256_CTX *)self->state; + sha256_final(&tmp_ctx, (byte *)vstr.buf); return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } #endif diff --git a/tests/extmod/uhashlib_sha256.py b/tests/extmod/uhashlib_sha256.py index 2e6df7df13202..60c3b54ef0787 100644 --- a/tests/extmod/uhashlib_sha256.py +++ b/tests/extmod/uhashlib_sha256.py @@ -26,13 +26,13 @@ # 56 bytes is a boundary case in the algorithm print(hashlib.sha256(b"\xff" * 56).digest()) -# TODO: running .digest() several times in row is not supported() -# h = hashlib.sha256(b'123') -# print(h.digest()) -# print(h.digest()) - -# TODO: partial digests are not supported -# h = hashlib.sha256(b'123') -# print(h.digest()) -# h.update(b'456') -# print(h.digest()) +# running .digest() several times in row is now supported +h = hashlib.sha256(b'123') +print(h.digest()) +print(h.digest()) + +# partial digests are supported +h = hashlib.sha256(b'123') +print(h.digest()) +h.update(b'456') +print(h.digest()) From 79b973a91c7571dce45ea3d867af4dca3a10fb5a Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Tue, 9 Feb 2021 08:58:47 -0500 Subject: [PATCH 48/49] Improve portability and avoid 'dereferencing type-punned pointer' --- extmod/moduhashlib.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 372a127183219..8c95bde2aff0c 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -98,7 +98,8 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); vstr_t vstr; vstr_init_len(&vstr, 32); - mbedtls_sha256_context tmp_ctx = *(mbedtls_sha256_context *)self->state; + mbedtls_sha256_context tmp_ctx; + memcpy(&tmp_ctx, self->state, sizeof(tmp_ctx)); mbedtls_sha256_finish_ret(&tmp_ctx, (unsigned char *)vstr.buf); return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } @@ -130,7 +131,8 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) { mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); vstr_t vstr; vstr_init_len(&vstr, SHA256_BLOCK_SIZE); - CRYAL_SHA256_CTX tmp_ctx = *(CRYAL_SHA256_CTX *)self->state; + CRYAL_SHA256_CTX tmp_ctx; + memcpy(&tmp_ctx, self->state, sizeof(tmp_ctx)); sha256_final(&tmp_ctx, (byte *)vstr.buf); return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } From aa63cf6773267a918b52f6630187159077eb903f Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Tue, 30 Mar 2021 09:06:38 -0400 Subject: [PATCH 49/49] extmod/vfs_fat: support FAT32 in mkfs=T case on mount --- extmod/vfs_fat.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 95b7ad9944116..0166008ae9f34 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -379,6 +379,9 @@ STATIC mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs if (res == FR_NO_FILESYSTEM && mp_obj_is_true(mkfs)) { uint8_t working_buf[FF_MAX_SS]; res = f_mkfs(&self->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf)); + if (res == FR_MKFS_ABORTED) { // Probably doesn't support FAT16 + res = f_mkfs(&self->fatfs, FM_FAT32, 0, working_buf, sizeof(working_buf)); + } } if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]);