From 5a2f82095a99700cfbcc5b9bede2c9d56786311e Mon Sep 17 00:00:00 2001 From: Hierophect Date: Wed, 2 Oct 2019 15:17:49 -0400 Subject: [PATCH 1/7] Implement SPI flash settings, bugfix for SPI pin assignment --- ports/stm32f4/boards/feather_f405/mpconfigboard.h | 8 +++++++- ports/stm32f4/boards/feather_f405/mpconfigboard.mk | 6 ++++-- ports/stm32f4/common-hal/busio/SPI.c | 10 +++++----- ports/stm32f4/peripherals/stm32f4/stm32f405xx/gpio.c | 6 +++--- ports/stm32f4/peripherals/stm32f4/stm32f412zx/gpio.c | 6 +++--- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/ports/stm32f4/boards/feather_f405/mpconfigboard.h b/ports/stm32f4/boards/feather_f405/mpconfigboard.h index edbc79ea92..831a17573a 100644 --- a/ports/stm32f4/boards/feather_f405/mpconfigboard.h +++ b/ports/stm32f4/boards/feather_f405/mpconfigboard.h @@ -34,4 +34,10 @@ #define FLASH_PAGE_SIZE (0x4000) #define AUTORESET_DELAY_MS 500 -#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000) \ No newline at end of file +#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000) + +// On-board flash +#define SPI_FLASH_MOSI_PIN &pin_PB05 +#define SPI_FLASH_MISO_PIN &pin_PB04 +#define SPI_FLASH_SCK_PIN &pin_PB03 +#define SPI_FLASH_CS_PIN &pin_PA15 diff --git a/ports/stm32f4/boards/feather_f405/mpconfigboard.mk b/ports/stm32f4/boards/feather_f405/mpconfigboard.mk index 5ea1ab4501..789dc0f05d 100644 --- a/ports/stm32f4/boards/feather_f405/mpconfigboard.mk +++ b/ports/stm32f4/boards/feather_f405/mpconfigboard.mk @@ -4,8 +4,10 @@ USB_PRODUCT = "Feather F405" USB_MANUFACTURER = "Adafruit Industries LLC" USB_DEVICES = "CDC,MSC" -INTERNAL_FLASH_FILESYSTEM = 1 -LONGINT_IMPL = NONE +SPI_FLASH_FILESYSTEM = 1 +EXTERNAL_FLASH_DEVICE_COUNT = 1 +EXTERNAL_FLASH_DEVICES = GD25Q16C +LONGINT_IMPL = MPZ MCU_SERIES = m4 MCU_VARIANT = stm32f4 diff --git a/ports/stm32f4/common-hal/busio/SPI.c b/ports/stm32f4/common-hal/busio/SPI.c index 50b15978c5..ec2bf7ef1b 100644 --- a/ports/stm32f4/common-hal/busio/SPI.c +++ b/ports/stm32f4/common-hal/busio/SPI.c @@ -107,7 +107,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, continue; } //store pins if not - self->sck = &mcu_spi_sck_list[j]; + self->sck = &mcu_spi_sck_list[i]; self->mosi = &mcu_spi_mosi_list[j]; self->miso = &mcu_spi_miso_list[k]; break; @@ -196,7 +196,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, self->handle.Init.CLKPolarity = SPI_POLARITY_LOW; self->handle.Init.CLKPhase = SPI_PHASE_1EDGE; self->handle.Init.NSS = SPI_NSS_SOFT; - self->handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; + self->handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; self->handle.Init.FirstBit = SPI_FIRSTBIT_MSB; self->handle.Init.TIMode = SPI_TIMODE_DISABLE; self->handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; @@ -383,20 +383,20 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) { bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size_t len) { - HAL_StatusTypeDef result = HAL_SPI_Transmit (&self->handle, (uint8_t *)data, (uint16_t)len, 2); + HAL_StatusTypeDef result = HAL_SPI_Transmit (&self->handle, (uint8_t *)data, (uint16_t)len, 5); return result == HAL_OK ? 1 : 0; } bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value) { - HAL_StatusTypeDef result = HAL_SPI_Receive (&self->handle, data, (uint16_t)len, 2); + HAL_StatusTypeDef result = HAL_SPI_Receive (&self->handle, data, (uint16_t)len, 5); return result == HAL_OK ? 1 : 0; } bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, uint8_t *data_out, uint8_t *data_in, size_t len) { HAL_StatusTypeDef result = HAL_SPI_TransmitReceive (&self->handle, - data_out, data_in, (uint16_t)len,2); + data_out, data_in, (uint16_t)len,5); return result == HAL_OK ? 1 : 0; } diff --git a/ports/stm32f4/peripherals/stm32f4/stm32f405xx/gpio.c b/ports/stm32f4/peripherals/stm32f4/stm32f405xx/gpio.c index 3c9af0e438..f9be3b4ec2 100644 --- a/ports/stm32f4/peripherals/stm32f4/stm32f405xx/gpio.c +++ b/ports/stm32f4/peripherals/stm32f4/stm32f405xx/gpio.c @@ -40,9 +40,9 @@ void stm32f4_peripherals_gpio_init(void) { never_reset_pin_number(2,15); //PC15 OSC32_OUT never_reset_pin_number(0,13); //PA13 SWDIO never_reset_pin_number(0,14); //PA14 SWCLK - never_reset_pin_number(0,15); //PA15 JTDI - never_reset_pin_number(1,3); //PB3 JTDO - never_reset_pin_number(1,4); //PB4 JTRST + // never_reset_pin_number(0,15); //PA15 JTDI + // never_reset_pin_number(1,3); //PB3 JTDO + // never_reset_pin_number(1,4); //PB4 JTRST // Port H is not included in GPIO port array // never_reset_pin_number(5,0); //PH0 JTDO diff --git a/ports/stm32f4/peripherals/stm32f4/stm32f412zx/gpio.c b/ports/stm32f4/peripherals/stm32f4/stm32f412zx/gpio.c index d839a8f9ee..d17ab4e018 100644 --- a/ports/stm32f4/peripherals/stm32f4/stm32f412zx/gpio.c +++ b/ports/stm32f4/peripherals/stm32f4/stm32f412zx/gpio.c @@ -215,9 +215,9 @@ void stm32f4_peripherals_gpio_init(void) { never_reset_pin_number(2,15); //PC15 OSC32_OUT never_reset_pin_number(0,13); //PA13 SWDIO never_reset_pin_number(0,14); //PA14 SWCLK - never_reset_pin_number(0,15); //PA15 JTDI - never_reset_pin_number(1,3); //PB3 JTDO - never_reset_pin_number(1,4); //PB4 JTRST + //never_reset_pin_number(0,15); //PA15 JTDI + //never_reset_pin_number(1,3); //PB3 JTDO + //never_reset_pin_number(1,4); //PB4 JTRST // Port H is not included in GPIO port array // never_reset_pin_number(5,0); //PH0 JTDO From 9aa6d215fda42d2b4151e05dbc3a88e7ff40f18a Mon Sep 17 00:00:00 2001 From: Hierophect Date: Wed, 2 Oct 2019 16:03:22 -0400 Subject: [PATCH 2/7] Add some definitions for when F412 can be implemented --- .../stm32f412zg_discovery/mpconfigboard.h | 3 ++- .../stm32f412zg_discovery/mpconfigboard.mk | 5 +++++ supervisor/shared/external_flash/devices.h | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ports/stm32f4/boards/stm32f412zg_discovery/mpconfigboard.h b/ports/stm32f4/boards/stm32f412zg_discovery/mpconfigboard.h index 0a89d96465..ebee98f89f 100644 --- a/ports/stm32f4/boards/stm32f412zg_discovery/mpconfigboard.h +++ b/ports/stm32f4/boards/stm32f412zg_discovery/mpconfigboard.h @@ -34,4 +34,5 @@ #define FLASH_PAGE_SIZE (0x4000) #define AUTORESET_DELAY_MS 500 -#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000) \ No newline at end of file +#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000) + diff --git a/ports/stm32f4/boards/stm32f412zg_discovery/mpconfigboard.mk b/ports/stm32f4/boards/stm32f412zg_discovery/mpconfigboard.mk index 21af63735f..509a244106 100644 --- a/ports/stm32f4/boards/stm32f412zg_discovery/mpconfigboard.mk +++ b/ports/stm32f4/boards/stm32f412zg_discovery/mpconfigboard.mk @@ -6,6 +6,11 @@ USB_MANUFACTURER = "STMicroelectronics" INTERNAL_FLASH_FILESYSTEM = 1 LONGINT_IMPL = NONE +# QSPI_FLASH_FILESYSTEM = 1 +# EXTERNAL_FLASH_DEVICE_COUNT = 1 +# EXTERNAL_FLASH_DEVICES = N25Q128A +# LONGINT_IMPL = MPZ + MCU_SERIES = m4 MCU_VARIANT = stm32f4 MCU_SUB_VARIANT = stm32f412zx diff --git a/supervisor/shared/external_flash/devices.h b/supervisor/shared/external_flash/devices.h index e787ba739a..001e8efef6 100644 --- a/supervisor/shared/external_flash/devices.h +++ b/supervisor/shared/external_flash/devices.h @@ -442,4 +442,23 @@ typedef struct { .write_status_register_split = false, \ .single_status_byte = false, \ } + +// Settings for the Micron N25Q128A 16MiB SPI flash. +// Datasheet: https://www.micron.com/-/media/client/global/documents/products/data-sheet/nor-flash/serial-nor/n25q/n25q_128mb_3v_65nm.pdf +#define N25Q128A {\ + .total_size = (1 << 24), /* 16 MiB */ \ + .start_up_time_us = 5000, \ + .manufacturer_id = 0x20, \ + .memory_type = 0xBA, \ + .capacity = 0x18, \ + .max_clock_speed_mhz = 108, \ + .quad_enable_bit_mask = 0x00, \ + .has_sector_protection = false, \ /*maybe?*/ + .supports_fast_read = true, \ + .supports_qspi = true, \ + .supports_qspi_writes = true, \ + .write_status_register_split = false, \ + .single_status_byte = false, \ +} + #endif // MICROPY_INCLUDED_ATMEL_SAMD_EXTERNAL_FLASH_DEVICES_H From 86305bfdfece1d2fc89a7f65385133c84b8f500c Mon Sep 17 00:00:00 2001 From: Hierophect Date: Wed, 2 Oct 2019 17:32:54 -0400 Subject: [PATCH 3/7] Make all errors value errors --- ports/stm32f4/common-hal/busio/SPI.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/stm32f4/common-hal/busio/SPI.c b/ports/stm32f4/common-hal/busio/SPI.c index ec2bf7ef1b..5196835558 100644 --- a/ports/stm32f4/common-hal/busio/SPI.c +++ b/ports/stm32f4/common-hal/busio/SPI.c @@ -123,9 +123,9 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, SPIx = mcu_spi_banks[self->sck->spi_index-1]; } else { if (spi_taken) { - mp_raise_RuntimeError(translate("Hardware busy, try alternative pins")); + mp_raise_ValueError(translate("Hardware busy, try alternative pins")); } else { - mp_raise_RuntimeError(translate("Invalid SPI pin selection")); + mp_raise_ValueError(translate("Invalid SPI pin selection")); } } @@ -203,7 +203,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, self->handle.Init.CRCPolynomial = 10; if (HAL_SPI_Init(&self->handle) != HAL_OK) { - mp_raise_RuntimeError(translate("SPI Init Error")); + mp_raise_ValueError(translate("SPI Init Error")); } self->baudrate = (HAL_RCC_GetPCLK2Freq()/16); self->prescaler = 16; @@ -344,7 +344,7 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self, if (HAL_SPI_Init(&self->handle) != HAL_OK) { - mp_raise_RuntimeError(translate("SPI Re-initialization error")); + mp_raise_ValueError(translate("SPI Re-initialization error")); } self->baudrate = baudrate; From 5a6194839bebb356074fcc9f03a1107a6abdaa22 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Wed, 2 Oct 2019 17:59:42 -0400 Subject: [PATCH 4/7] Timeout fix for I2C device testing --- ports/stm32f4/common-hal/busio/I2C.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32f4/common-hal/busio/I2C.c b/ports/stm32f4/common-hal/busio/I2C.c index 8af670394a..c2f5dbe0a2 100644 --- a/ports/stm32f4/common-hal/busio/I2C.c +++ b/ports/stm32f4/common-hal/busio/I2C.c @@ -221,11 +221,11 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) { uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, const uint8_t *data, size_t len, bool transmit_stop_bit) { - HAL_StatusTypeDef result = HAL_I2C_Master_Transmit(&(self->handle), (uint16_t)(addr<<1), (uint8_t *)data, (uint16_t)len, 2); + HAL_StatusTypeDef result = HAL_I2C_Master_Transmit(&(self->handle), (uint16_t)(addr<<1), (uint8_t *)data, (uint16_t)len, 500); return result == HAL_OK ? 0 : MP_EIO; } uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t *data, size_t len) { - return HAL_I2C_Master_Receive(&(self->handle), (uint16_t)(addr<<1), data, (uint16_t)len, 2) == HAL_OK ? 0 : MP_EIO; + return HAL_I2C_Master_Receive(&(self->handle), (uint16_t)(addr<<1), data, (uint16_t)len, 500) == HAL_OK ? 0 : MP_EIO; } From 8a4bbae0772c7d5c536beef43a2c3af586404ad1 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Wed, 2 Oct 2019 18:18:52 -0400 Subject: [PATCH 5/7] Fix typo causing parsing error --- supervisor/shared/external_flash/devices.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervisor/shared/external_flash/devices.h b/supervisor/shared/external_flash/devices.h index 001e8efef6..4e037574b8 100644 --- a/supervisor/shared/external_flash/devices.h +++ b/supervisor/shared/external_flash/devices.h @@ -453,7 +453,7 @@ typedef struct { .capacity = 0x18, \ .max_clock_speed_mhz = 108, \ .quad_enable_bit_mask = 0x00, \ - .has_sector_protection = false, \ /*maybe?*/ + .has_sector_protection = false, \ .supports_fast_read = true, \ .supports_qspi = true, \ .supports_qspi_writes = true, \ From 83c49a5c8016790872d904689735cdbb66f7ab4c Mon Sep 17 00:00:00 2001 From: Hierophect Date: Thu, 3 Oct 2019 09:14:54 -0400 Subject: [PATCH 6/7] Increase SPI timeout duration --- ports/stm32f4/common-hal/busio/SPI.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/stm32f4/common-hal/busio/SPI.c b/ports/stm32f4/common-hal/busio/SPI.c index 5196835558..70d4adec44 100644 --- a/ports/stm32f4/common-hal/busio/SPI.c +++ b/ports/stm32f4/common-hal/busio/SPI.c @@ -383,20 +383,20 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) { bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size_t len) { - HAL_StatusTypeDef result = HAL_SPI_Transmit (&self->handle, (uint8_t *)data, (uint16_t)len, 5); + HAL_StatusTypeDef result = HAL_SPI_Transmit (&self->handle, (uint8_t *)data, (uint16_t)len, 500); return result == HAL_OK ? 1 : 0; } bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value) { - HAL_StatusTypeDef result = HAL_SPI_Receive (&self->handle, data, (uint16_t)len, 5); + HAL_StatusTypeDef result = HAL_SPI_Receive (&self->handle, data, (uint16_t)len, 500); return result == HAL_OK ? 1 : 0; } bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, uint8_t *data_out, uint8_t *data_in, size_t len) { HAL_StatusTypeDef result = HAL_SPI_TransmitReceive (&self->handle, - data_out, data_in, (uint16_t)len,5); + data_out, data_in, (uint16_t)len,500); return result == HAL_OK ? 1 : 0; } From eacdb1da6e91f9f02b552e51e3809001408cfc95 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Thu, 3 Oct 2019 14:43:25 -0400 Subject: [PATCH 7/7] Disable timeout, remove redundancy --- ports/stm32f4/common-hal/busio/SPI.c | 12 ++++++------ supervisor/shared/external_flash/devices.h | 18 ------------------ 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/ports/stm32f4/common-hal/busio/SPI.c b/ports/stm32f4/common-hal/busio/SPI.c index 70d4adec44..e20bd7a947 100644 --- a/ports/stm32f4/common-hal/busio/SPI.c +++ b/ports/stm32f4/common-hal/busio/SPI.c @@ -383,21 +383,21 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) { bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size_t len) { - HAL_StatusTypeDef result = HAL_SPI_Transmit (&self->handle, (uint8_t *)data, (uint16_t)len, 500); - return result == HAL_OK ? 1 : 0; + HAL_StatusTypeDef result = HAL_SPI_Transmit (&self->handle, (uint8_t *)data, (uint16_t)len, HAL_MAX_DELAY); + return result == HAL_OK; } bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value) { - HAL_StatusTypeDef result = HAL_SPI_Receive (&self->handle, data, (uint16_t)len, 500); - return result == HAL_OK ? 1 : 0; + HAL_StatusTypeDef result = HAL_SPI_Receive (&self->handle, data, (uint16_t)len, HAL_MAX_DELAY); + return result == HAL_OK; } bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, uint8_t *data_out, uint8_t *data_in, size_t len) { HAL_StatusTypeDef result = HAL_SPI_TransmitReceive (&self->handle, - data_out, data_in, (uint16_t)len,500); - return result == HAL_OK ? 1 : 0; + data_out, data_in, (uint16_t)len,HAL_MAX_DELAY); + return result == HAL_OK; } uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t* self) { diff --git a/supervisor/shared/external_flash/devices.h b/supervisor/shared/external_flash/devices.h index 4e037574b8..4de22719a7 100644 --- a/supervisor/shared/external_flash/devices.h +++ b/supervisor/shared/external_flash/devices.h @@ -443,22 +443,4 @@ typedef struct { .single_status_byte = false, \ } -// Settings for the Micron N25Q128A 16MiB SPI flash. -// Datasheet: https://www.micron.com/-/media/client/global/documents/products/data-sheet/nor-flash/serial-nor/n25q/n25q_128mb_3v_65nm.pdf -#define N25Q128A {\ - .total_size = (1 << 24), /* 16 MiB */ \ - .start_up_time_us = 5000, \ - .manufacturer_id = 0x20, \ - .memory_type = 0xBA, \ - .capacity = 0x18, \ - .max_clock_speed_mhz = 108, \ - .quad_enable_bit_mask = 0x00, \ - .has_sector_protection = false, \ - .supports_fast_read = true, \ - .supports_qspi = true, \ - .supports_qspi_writes = true, \ - .write_status_register_split = false, \ - .single_status_byte = false, \ -} - #endif // MICROPY_INCLUDED_ATMEL_SAMD_EXTERNAL_FLASH_DEVICES_H