From 2ab923862b4adca6946cf89b4c08c4744635220a Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 28 Feb 2018 19:15:54 -0800 Subject: [PATCH 1/2] Structify flash device definitions. --- .../circuitplayground_express/mpconfigboard.h | 6 +- .../boards/metro_m4_express/mpconfigboard.h | 10 +- ports/atmel-samd/external_flash/devices.h | 180 ++++++++++++++++++ .../external_flash/devices/AT25DF081A.h | 52 ----- .../external_flash/devices/GD25Q16C.h | 56 ------ .../external_flash/devices/S25FL064L.h | 56 ------ .../external_flash/devices/S25FL116K.h | 55 ------ .../external_flash/devices/S25FL216K.h | 55 ------ .../external_flash/devices/W25Q16FW.h | 53 ------ .../external_flash/devices/W25Q32BV.h | 52 ----- .../external_flash/devices/W25Q80DV.h | 52 ----- .../external_flash/external_flash.c | 145 +++++++------- ports/atmel-samd/external_flash/qspi_flash.c | 4 +- 13 files changed, 264 insertions(+), 512 deletions(-) create mode 100644 ports/atmel-samd/external_flash/devices.h delete mode 100644 ports/atmel-samd/external_flash/devices/AT25DF081A.h delete mode 100644 ports/atmel-samd/external_flash/devices/GD25Q16C.h delete mode 100644 ports/atmel-samd/external_flash/devices/S25FL064L.h delete mode 100644 ports/atmel-samd/external_flash/devices/S25FL116K.h delete mode 100644 ports/atmel-samd/external_flash/devices/S25FL216K.h delete mode 100644 ports/atmel-samd/external_flash/devices/W25Q16FW.h delete mode 100644 ports/atmel-samd/external_flash/devices/W25Q32BV.h delete mode 100644 ports/atmel-samd/external_flash/devices/W25Q80DV.h diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index cc09d7f790..599dac56de 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -38,7 +38,7 @@ #define SPEAKER_ENABLE_PIN (&pin_PA30) -#include "external_flash/external_flash.h" +#include "external_flash/devices.h" // If you change this, then make sure to update the linker scripts as well to // make sure you don't overwrite code. @@ -47,8 +47,8 @@ #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) -#include "external_flash/devices/S25FL216K.h" -#include "external_flash/devices/GD25Q16C.h" +#define EXTERNAL_FLASH_DEVICES 25FL216K, \ + GD25Q16C #define CALIBRATE_CRYSTALLESS 1 diff --git a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h index 18d65df2b2..98f69ba804 100644 --- a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h @@ -20,8 +20,6 @@ #define AUTORESET_DELAY_MS 500 -#include "external_flash/external_flash.h" - // If you change this, then make sure to update the linker scripts as well to // make sure you don't overwrite code // #define CIRCUITPY_INTERNAL_NVM_SIZE 256 @@ -29,5 +27,9 @@ #define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE) -#include "external_flash/devices/S25FL116K.h" -#include "external_flash/devices/GD25Q16C.h" +#include "external_flash/devices.h" + +#define EXTERNAL_FLASH_DEVICE_COUNT 2 +#define EXTERNAL_FLASH_DEVICES S25FL116K, GD25Q16C + +#include "external_flash/external_flash.h" diff --git a/ports/atmel-samd/external_flash/devices.h b/ports/atmel-samd/external_flash/devices.h new file mode 100644 index 0000000000..a02d268287 --- /dev/null +++ b/ports/atmel-samd/external_flash/devices.h @@ -0,0 +1,180 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_EXTERNAL_FLASH_DEVICES_H +#define MICROPY_INCLUDED_ATMEL_SAMD_EXTERNAL_FLASH_DEVICES_H + +#include +#include + +typedef struct { + uint32_t total_size; + uint16_t erase_size; + uint16_t page_size; + uint16_t start_up_time_us; + uint8_t manufacturer_id; + uint8_t memory_type; + uint8_t capacity; + uint8_t max_clock_speed_mhz; + bool has_sector_protection; + bool supports_qspi; + bool supports_qspi_writes; +} external_flash_device; + +// Settings for the Adesto Tech AT25DF081A 1MiB SPI flash. Its on the SAMD21 +// Xplained board. +// Datasheet: https://www.adestotech.com/wp-content/uploads/doc8715.pdf +#define AT25DF081A {\ + .total_size = (1 << 20), /* 1 MiB */ \ + .erase_size = (1 << 12), /* 4 KiB */ \ + .page_size = 256, /*256 bytes */ \ + .start_up_time_us = 10000, \ + .manufacturer_id = 0x1f, \ + .memory_type = 0x45, \ + .capacity = 0x01, \ + .max_clock_speed_mhz = 104, \ + .has_sector_protection = true, \ + .supports_qspi = true, \ + .supports_qspi_writes = false, \ +} + +// Settings for the Gigadevice GD25Q16C 2MiB SPI flash. +// Datasheet: http://www.gigadevice.com/wp-content/uploads/2017/12/DS-00086-GD25Q16C-Rev2.6.pdf + +#define GD25Q16C {\ + .total_size = (1 << 21), /* 2 MiB */ \ + .erase_size = (1 << 12), /* 4 KiB */ \ + .page_size = 256, /*256 bytes */ \ + .start_up_time_us = 5000, \ + .manufacturer_id = 0xc8, \ + .memory_type = 0x40, \ + .capacity = 0x15, \ + .max_clock_speed_mhz = 104, \ + .has_sector_protection = true, \ + .supports_qspi = true, \ + .supports_qspi_writes = false, \ +} + +// Settings for the Cypress (was Spansion) S25FL064L 8MiB SPI flash. +// Datasheet: http://www.cypress.com/file/316661/download +#define S25FL064L {\ + .total_size = (1 << 23), /* 8 MiB */ \ + .erase_size = (1 << 12), /* 4 KiB */ \ + .page_size = 256, /*256 bytes */ \ + .start_up_time_us = 300, \ + .manufacturer_id = 0x01, \ + .memory_type = 0x60, \ + .capacity = 0x17, \ + .max_clock_speed_mhz = 104, \ + .has_sector_protection = true, \ + .supports_qspi = true, \ + .supports_qspi_writes = false, \ +} + +// Settings for the Cypress (was Spansion) S25FL116K 2MiB SPI flash. +// Datasheet: http://www.cypress.com/file/196886/download +#define S25FL116K {\ + .total_size = (1 << 21), /* 2 MiB */ \ + .erase_size = (1 << 12), /* 4 KiB */ \ + .page_size = 256, /*256 bytes */ \ + .start_up_time_us = 10000, \ + .manufacturer_id = 0x01, \ + .memory_type = 0x40, \ + .capacity = 0x15, \ + .max_clock_speed_mhz = 104, \ + .has_sector_protection = true, \ + .supports_qspi = true, \ + .supports_qspi_writes = false, \ +} + + +// Settings for the Cypress (was Spansion) S25FL216K 2MiB SPI flash. +// Datasheet: http://www.cypress.com/file/197346/download + +#define S25FL216K {\ + .total_size = (1 << 21), /* 2 MiB */ \ + .erase_size = (1 << 12), /* 4 KiB */ \ + .page_size = 256, /*256 bytes */ \ + .start_up_time_us = 10000, \ + .manufacturer_id = 0x01, \ + .memory_type = 0x40, \ + .capacity = 0x15, \ + .max_clock_speed_mhz = 104, \ + .has_sector_protection = false, \ + .supports_qspi = true, \ + .supports_qspi_writes = false, \ +} + +// Settings for the Winbond W25Q16FW 2MiB SPI flash. +// Datasheet: https://www.winbond.com/resource-files/w25q16fw%20revj%2005182017%20sfdp.pdf + +#define W25Q16FW {\ + .total_size = (1 << 21), /* 2 MiB */ \ + .erase_size = (1 << 12), /* 4 KiB */ \ + .page_size = 256, /*256 bytes */ \ + .start_up_time_us = 5000, \ + .manufacturer_id = 0xef, \ + .memory_type = 0x60, \ + .capacity = 0x15, \ + .max_clock_speed_mhz = 104, \ + .has_sector_protection = false, \ + .supports_qspi = true, \ + .supports_qspi_writes = false, \ +} + +// Settings for the Winbond W25Q32BV 2MiB SPI flash. +// Datasheet: https://www.winbond.com/resource-files/w25q32bv_revi_100413_wo_automotive.pdf + +#define W25Q32BV {\ + .total_size = (1 << 21), /* 2 MiB */ \ + .erase_size = (1 << 12), /* 4 KiB */ \ + .page_size = 256, /*256 bytes */ \ + .start_up_time_us = 5000, \ + .manufacturer_id = 0xef, \ + .memory_type = 0x60, \ + .capacity = 0x16, \ + .max_clock_speed_mhz = 104, \ + .has_sector_protection = false, \ + .supports_qspi = true, \ + .supports_qspi_writes = false, \ +} + +// Settings for the Winbond W25Q80DL 1MiB SPI flash. +// Datasheet: https://www.winbond.com/resource-files/w25q80dv%20dl_revh_10022015.pdf +#define W25Q80DL {\ + .total_size = (1 << 20), /* 1 MiB */ \ + .erase_size = (1 << 12), /* 4 KiB */ \ + .page_size = 256, /*256 bytes */ \ + .start_up_time_us = 5000, \ + .manufacturer_id = 0xef, \ + .memory_type = 0x60, \ + .capacity = 0x14, \ + .max_clock_speed_mhz = 104, \ + .has_sector_protection = false, \ + .supports_qspi = true, \ + .supports_qspi_writes = false, \ +} + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_EXTERNAL_FLASH_DEVICES_H diff --git a/ports/atmel-samd/external_flash/devices/AT25DF081A.h b/ports/atmel-samd/external_flash/devices/AT25DF081A.h deleted file mode 100644 index ba0fc6659b..0000000000 --- a/ports/atmel-samd/external_flash/devices/AT25DF081A.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_AT25DF081A_H -#define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_AT25DF081A_H - -// Settings for the Adesto Tech AT25DF081A 1MiB SPI flash. Its on the SAMD21 -// Xplained board. -// Datasheet: https://www.adestotech.com/wp-content/uploads/doc8715.pdf - -// The total flash size in bytes. -#define SPI_FLASH_TOTAL_SIZE (1 << 20) // 2 MiB - -// The size of the smallest erase unit thats erased with command 0x20. -#define SPI_FLASH_ERASE_SIZE (1 << 12) // 4 KiB - -// The size of a page that is programmed with page program command 0x02. -#define SPI_FLASH_PAGE_SIZE (256) // 256 bytes - -// These are the first three response bytes to the JEDEC ID command 0x9f that is -// used to confirm we're talking to the flash we expect. The data sheet calls -// them something else so we just match it byte for byte. -#define SPI_FLASH_JEDEC_MANUFACTURER 0x1F -#define SPI_FLASH_JEDEC_MEMORY_TYPE 0x45 -#define SPI_FLASH_JEDEC_CAPACITY 0x01 - -#define SPI_FLASH_SECTOR_PROTECTION - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_AT25DF081A_H diff --git a/ports/atmel-samd/external_flash/devices/GD25Q16C.h b/ports/atmel-samd/external_flash/devices/GD25Q16C.h deleted file mode 100644 index e123265e29..0000000000 --- a/ports/atmel-samd/external_flash/devices/GD25Q16C.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_GD25Q16C_H -#define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_GD25Q16C_H - -// Settings for the Gigadevice GD25Q16C 2MiB SPI flash. -// Datasheet: http://www.gigadevice.com/product/download/410.html?locale=en_US - -// The total flash size in bytes. -#define SPI_FLASH_TOTAL_SIZE (1 << 21) // 2 MiB - -// The size of the smallest erase unit thats erased with command 0x20. -#define SPI_FLASH_ERASE_SIZE (1 << 12) // 4 KiB - -// The size of a page that is programmed with page program command 0x02. -#define SPI_FLASH_PAGE_SIZE (256) // 256 bytes - -#ifndef SPI_FLASH_JEDEC_MANUFACTURER -// These are the first three response bytes to the JEDEC ID command 0x9f that is -// used to confirm we're talking to the flash we expect. -#define SPI_FLASH_JEDEC_MANUFACTURER 0xc8 -#define SPI_FLASH_SECTOR_PROTECTION true -#define SPI_FLASH_JEDEC_MEMORY_TYPE 0x40 -#else -#define SPI_FLASH_JEDEC_MANUFACTURER_2 0xc8 -#define SPI_FLASH_SECTOR_PROTECTION_2 true -#define SPI_FLASH_JEDEC_MEMORY_TYPE_2 0x40 -#endif -#define SPI_FLASH_JEDEC_CAPACITY 0x15 - - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_GD25Q16C_H diff --git a/ports/atmel-samd/external_flash/devices/S25FL064L.h b/ports/atmel-samd/external_flash/devices/S25FL064L.h deleted file mode 100644 index 5a38836874..0000000000 --- a/ports/atmel-samd/external_flash/devices/S25FL064L.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * Copyright (c) 2017 Dave Astels - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL064L_H -#define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL064L_H - -// Settings for the Cypress (was Spansion) S25FL064L 8MiB SPI flash. -// Datasheet: http://www.cypress.com/file/316661/download - -// The total flash size in bytes. -#define SPI_FLASH_TOTAL_SIZE (1 << 23) // 8 MiB - -// The size of the smallest erase unit thats erased with command 0x20. -#define SPI_FLASH_ERASE_SIZE (1 << 12) // 4 KiB - -// The size of a page that is programmed with page program command 0x02. -#define SPI_FLASH_PAGE_SIZE (256) // 256 bytes - -// These are the first three response bytes to the JEDEC ID command 0x9f that is -// used to confirm we're talking to the flash we expect. -#ifndef SPI_FLASH_JEDEC_MANUFACTURER -#define SPI_FLASH_JEDEC_MANUFACTURER 0x01 -#define SPI_FLASH_SECTOR_PROTECTION false -#define SPI_FLASH_JEDEC_MEMORY_TYPE 0x60 -#else -#define SPI_FLASH_JEDEC_MANUFACTURER_2 0x013 -#define SPI_FLASH_SECTOR_PROTECTION_2 false -#define SPI_FLASH_JEDEC_MEMORY_TYPE_2 0x60 -#endif -#define SPI_FLASH_JEDEC_CAPACITY 0x17 - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL216K_H diff --git a/ports/atmel-samd/external_flash/devices/S25FL116K.h b/ports/atmel-samd/external_flash/devices/S25FL116K.h deleted file mode 100644 index 0ccb58ed07..0000000000 --- a/ports/atmel-samd/external_flash/devices/S25FL116K.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL116K_H -#define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL116K_H - -// Settings for the Cypress (was Spansion) S25FL116K 2MiB SPI flash. -// Datasheet: http://www.cypress.com/file/196886/download - -// The total flash size in bytes. -#define SPI_FLASH_TOTAL_SIZE (1 << 21) // 2 MiB - -// The size of the smallest erase unit thats erased with command 0x20. -#define SPI_FLASH_ERASE_SIZE (1 << 12) // 4 KiB - -// The size of a page that is programmed with page program command 0x02. -#define SPI_FLASH_PAGE_SIZE (256) // 256 bytes - -// These are the first three response bytes to the JEDEC ID command 0x9f that is -// used to confirm we're talking to the flash we expect. -#ifndef SPI_FLASH_JEDEC_MANUFACTURER -#define SPI_FLASH_JEDEC_MANUFACTURER 0x01 -#define SPI_FLASH_SECTOR_PROTECTION false -#define SPI_FLASH_JEDEC_MEMORY_TYPE 0x40 -#else -#define SPI_FLASH_JEDEC_MANUFACTURER_2 0x01 -#define SPI_FLASH_SECTOR_PROTECTION_2 false -#define SPI_FLASH_JEDEC_MEMORY_TYPE_2 0x40 -#endif -#define SPI_FLASH_JEDEC_CAPACITY 0x15 - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL216K_H diff --git a/ports/atmel-samd/external_flash/devices/S25FL216K.h b/ports/atmel-samd/external_flash/devices/S25FL216K.h deleted file mode 100644 index a6a0a12fa2..0000000000 --- a/ports/atmel-samd/external_flash/devices/S25FL216K.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL216K_H -#define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL216K_H - -// Settings for the Cypress (was Spansion) S25FL216K 2MiB SPI flash. -// Datasheet: http://www.cypress.com/file/197346/download - -// The total flash size in bytes. -#define SPI_FLASH_TOTAL_SIZE (1 << 21) // 2 MiB - -// The size of the smallest erase unit thats erased with command 0x20. -#define SPI_FLASH_ERASE_SIZE (1 << 12) // 4 KiB - -// The size of a page that is programmed with page program command 0x02. -#define SPI_FLASH_PAGE_SIZE (256) // 256 bytes - -// These are the first three response bytes to the JEDEC ID command 0x9f that is -// used to confirm we're talking to the flash we expect. -#ifndef SPI_FLASH_JEDEC_MANUFACTURER -#define SPI_FLASH_JEDEC_MANUFACTURER 0x01 -#define SPI_FLASH_SECTOR_PROTECTION false -#define SPI_FLASH_JEDEC_MEMORY_TYPE 0x40 -#else -#define SPI_FLASH_JEDEC_MANUFACTURER_2 0x01 -#define SPI_FLASH_SECTOR_PROTECTION_2 false -#define SPI_FLASH_JEDEC_MEMORY_TYPE_2 0x40 -#endif -#define SPI_FLASH_JEDEC_CAPACITY 0x15 - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL216K_H diff --git a/ports/atmel-samd/external_flash/devices/W25Q16FW.h b/ports/atmel-samd/external_flash/devices/W25Q16FW.h deleted file mode 100644 index 219a7f058f..0000000000 --- a/ports/atmel-samd/external_flash/devices/W25Q16FW.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q16FW_H -#define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q16FW_H - -// The total flash size in bytes. -#define SPI_FLASH_TOTAL_SIZE (1 << 21) // 2 MiB - -// The size of the smallest erase unit thats erased with command 0x20. -#define SPI_FLASH_ERASE_SIZE (1 << 12) // 4 KiB - -// The size of a page that is programmed with page program command 0x02. -#define SPI_FLASH_PAGE_SIZE (256) // 256 bytes - -// These are the first three response bytes to the JEDEC ID command 0x9f that is -// used to confirm we're talking to the flash we expect. -#ifndef SPI_FLASH_JEDEC_MANUFACTURER - #define SPI_FLASH_JEDEC_MANUFACTURER 0xef - #define SPI_FLASH_SECTOR_PROTECTION false - #define SPI_FLASH_JEDEC_MEMORY_TYPE 0x60 -#else - #define SPI_FLASH_JEDEC_MANUFACTURER_2 0xef - #define SPI_FLASH_SECTOR_PROTECTION_2 false - #define SPI_FLASH_JEDEC_MEMORY_TYPE_2 0x60 -#endif - -#define SPI_FLASH_JEDEC_CAPACITY 0x15 - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q16BV_H diff --git a/ports/atmel-samd/external_flash/devices/W25Q32BV.h b/ports/atmel-samd/external_flash/devices/W25Q32BV.h deleted file mode 100644 index d71771babc..0000000000 --- a/ports/atmel-samd/external_flash/devices/W25Q32BV.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q32BV_H -#define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q32BV_H - -// The total flash size in bytes. -#define SPI_FLASH_TOTAL_SIZE (1 << 21) // 2 MiB - -// The size of the smallest erase unit thats erased with command 0x20. -#define SPI_FLASH_ERASE_SIZE (1 << 12) // 4 KiB - -// The size of a page that is programmed with page program command 0x02. -#define SPI_FLASH_PAGE_SIZE (256) // 256 bytes - -// These are the first three response bytes to the JEDEC ID command 0x9f that is -// used to confirm we're talking to the flash we expect. -#ifndef SPI_FLASH_JEDEC_MANUFACTURER -#define SPI_FLASH_JEDEC_MANUFACTURER 0xef -#define SPI_FLASH_SECTOR_PROTECTION false -#define SPI_FLASH_JEDEC_MEMORY_TYPE 0x40 -#else -#define SPI_FLASH_JEDEC_MANUFACTURER_2 0xef -#define SPI_FLASH_SECTOR_PROTECTION_2 false -#define SPI_FLASH_JEDEC_MEMORY_TYPE_2 0x40 -#endif -#define SPI_FLASH_JEDEC_CAPACITY 0x16 - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q32BV_H diff --git a/ports/atmel-samd/external_flash/devices/W25Q80DV.h b/ports/atmel-samd/external_flash/devices/W25Q80DV.h deleted file mode 100644 index de12e3fd44..0000000000 --- a/ports/atmel-samd/external_flash/devices/W25Q80DV.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q80DV_H -#define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q80DV_H - -// The total flash size in bytes. -#define SPI_FLASH_TOTAL_SIZE (1 << 20) // 1 MiB - -// The size of the smallest erase unit thats erased with command 0x20. -#define SPI_FLASH_ERASE_SIZE (1 << 12) // 4 KiB - -// The size of a page that is programmed with page program command 0x02. -#define SPI_FLASH_PAGE_SIZE (256) // 256 bytes - -// These are the first three response bytes to the JEDEC ID command 0x9f that is -// used to confirm we're talking to the flash we expect. -#ifndef SPI_FLASH_JEDEC_MANUFACTURER -#define SPI_FLASH_JEDEC_MANUFACTURER 0xef -#define SPI_FLASH_SECTOR_PROTECTION false -#define SPI_FLASH_JEDEC_MEMORY_TYPE 0x40 -#else -#define SPI_FLASH_JEDEC_MANUFACTURER_2 0xef -#define SPI_FLASH_SECTOR_PROTECTION_2 false -#define SPI_FLASH_JEDEC_MEMORY_TYPE_2 0x40 -#endif -#define SPI_FLASH_JEDEC_CAPACITY 0x14 - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q80DV_H diff --git a/ports/atmel-samd/external_flash/external_flash.c b/ports/atmel-samd/external_flash/external_flash.c index b432567ef2..26437e975c 100644 --- a/ports/atmel-samd/external_flash/external_flash.c +++ b/ports/atmel-samd/external_flash/external_flash.c @@ -37,6 +37,7 @@ #include "py/runtime.h" #include "lib/oofatfs/ff.h" #include "peripherals.h" +#include "shared-bindings/microcontroller/__init__.h" #include "supervisor/shared/rgb_led_status.h" //#include "shared_dma.h" @@ -48,20 +49,19 @@ #define NO_SECTOR_LOADED 0xFFFFFFFF -static bool spi_flash_is_initialised = false; - struct spi_m_sync_descriptor spi_flash_desc; // The currently cached sector in the cache, ram or flash based. static uint32_t current_sector; +external_flash_device possible_devices[EXTERNAL_FLASH_DEVICE_COUNT] = {EXTERNAL_FLASH_DEVICES}; + +static external_flash_device* flash_device = NULL; + // Track which blocks (up to 32) in the current sector currently live in the // cache. static uint32_t dirty_mask; -// Address of the scratch flash sector. -#define SCRATCH_SECTOR (SPI_FLASH_TOTAL_SIZE - SPI_FLASH_ERASE_SIZE) - // Wait until both the write enable and write in progress bits have cleared. static bool wait_for_flash_ready(void) { uint8_t read_status_response[1] = {0x00}; @@ -80,7 +80,7 @@ static bool write_enable(void) { // Read data_length's worth of bytes starting at address into data. static bool read_flash(uint32_t address, uint8_t* data, uint32_t data_length) { - if (!spi_flash_is_initialised) { + if (flash_device == NULL) { return false; } if (!wait_for_flash_ready()) { @@ -93,7 +93,7 @@ static bool read_flash(uint32_t address, uint8_t* data, uint32_t data_length) { // that the sector that address resides in has already been erased. So make sure // to run erase_sector. static bool write_flash(uint32_t address, const uint8_t* data, uint32_t data_length) { - if (!spi_flash_is_initialised) { + if (flash_device == NULL) { return false; } // Don't bother writing if the data is all 1s. Thats equivalent to the flash @@ -111,13 +111,13 @@ static bool write_flash(uint32_t address, const uint8_t* data, uint32_t data_len for (uint32_t bytes_written = 0; bytes_written < data_length; - bytes_written += SPI_FLASH_PAGE_SIZE) { + bytes_written += flash_device->page_size) { if (!wait_for_flash_ready() || !write_enable()) { return false; } if (!spi_flash_write_data(address + bytes_written, (uint8_t*) data + bytes_written, - SPI_FLASH_PAGE_SIZE)) { + flash_device->page_size)) { return false; } } @@ -168,12 +168,13 @@ static bool erase_sector(uint32_t sector_address) { // Sector is really 24 bits. static bool copy_block(uint32_t src_address, uint32_t dest_address) { // Copy page by page to minimize RAM buffer. - uint8_t buffer[SPI_FLASH_PAGE_SIZE]; - for (uint32_t i = 0; i < FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE; i++) { - if (!read_flash(src_address + i * SPI_FLASH_PAGE_SIZE, buffer, SPI_FLASH_PAGE_SIZE)) { + uint16_t page_size = flash_device->page_size; + uint8_t buffer[page_size]; + for (uint32_t i = 0; i < FILESYSTEM_BLOCK_SIZE / page_size; i++) { + if (!read_flash(src_address + i * page_size, buffer, page_size)) { return false; } - if (!write_flash(dest_address + i * SPI_FLASH_PAGE_SIZE, buffer, SPI_FLASH_PAGE_SIZE)) { + if (!write_flash(dest_address + i * page_size, buffer, page_size)) { return false; } } @@ -181,12 +182,41 @@ static bool copy_block(uint32_t src_address, uint32_t dest_address) { } void external_flash_init(void) { - if (spi_flash_is_initialised) { + if (flash_device != NULL) { return; } + uint8_t num_possible_devices = sizeof(*possible_devices) / sizeof(external_flash_device); + + // Delay to give the SPI Flash time to get going. + // TODO(tannewt): Only do this when we know power was applied vs a reset. + uint16_t max_start_up_delay_us = 0; + for (uint8_t i = 0; i < num_possible_devices; i++) { + if (possible_devices[i].start_up_time_us > max_start_up_delay_us) { + max_start_up_delay_us = possible_devices[i].start_up_time_us; + } + } + common_hal_mcu_delay_us(max_start_up_delay_us); spi_flash_init(); + + for (uint8_t i = 0; i < num_possible_devices; i++) { + external_flash_device* possible_device = &possible_devices[i]; + uint8_t jedec_id_response[3] = {0x00, 0x00, 0x00}; + spi_flash_read_command(CMD_READ_JEDEC_ID, jedec_id_response, 3); + if (jedec_id_response[0] == possible_device->manufacturer_id && + jedec_id_response[1] == possible_device->memory_type && + jedec_id_response[2] == possible_device->capacity) { + flash_device = possible_device; + break; + } + } + + if (flash_device == NULL) { + asm("bkpt"); + return; + } + // Activity LED for flash writes. #ifdef MICROPY_HW_LED_MSC gpio_set_pin_function(SPI_FLASH_CS_PIN, GPIO_PIN_FUNCTION_OFF); @@ -195,33 +225,7 @@ void external_flash_init(void) { gpio_set_pin_level(MICROPY_HW_LED_MSC, false); #endif - uint8_t jedec_id_response[3] = {0x00, 0x00, 0x00}; - spi_flash_read_command(CMD_READ_JEDEC_ID, jedec_id_response, 3); - - uint8_t manufacturer = jedec_id_response[0]; - if ((jedec_id_response[0] == SPI_FLASH_JEDEC_MANUFACTURER -#ifdef SPI_FLASH_JEDEC_MANUFACTURER_2 - || jedec_id_response[0] == SPI_FLASH_JEDEC_MANUFACTURER_2 -#endif - ) && - (jedec_id_response[1] == SPI_FLASH_JEDEC_MEMORY_TYPE -#ifdef SPI_FLASH_JEDEC_MANUFACTURER_2 - || jedec_id_response[1] == SPI_FLASH_JEDEC_MEMORY_TYPE_2 -#endif - ) && - jedec_id_response[2] == SPI_FLASH_JEDEC_CAPACITY) { - spi_flash_is_initialised = true; - } else { - // Unknown flash chip! - spi_flash_is_initialised = false; - return; - } - - if ((manufacturer == SPI_FLASH_JEDEC_MANUFACTURER && SPI_FLASH_SECTOR_PROTECTION) -#ifdef SPI_FLASH_JEDEC_MANUFACTURER_2 - || (manufacturer == SPI_FLASH_JEDEC_MANUFACTURER_2 && SPI_FLASH_SECTOR_PROTECTION_2) -#endif - ) { + if (flash_device->has_sector_protection) { write_enable(); // Turn off sector protection @@ -237,8 +241,6 @@ void external_flash_init(void) { current_sector = NO_SECTOR_LOADED; dirty_mask = 0; MP_STATE_VM(flash_ram_cache) = NULL; - - spi_flash_is_initialised = true; } // The size of each individual block. @@ -250,7 +252,7 @@ uint32_t external_flash_get_block_size(void) { uint32_t external_flash_get_block_count(void) { // We subtract one erase sector size because we may use it as a staging area // for writes. - return SPI_FLASH_PART1_START_BLOCK + (SPI_FLASH_TOTAL_SIZE - SPI_FLASH_ERASE_SIZE) / FILESYSTEM_BLOCK_SIZE; + return SPI_FLASH_PART1_START_BLOCK + (flash_device->total_size - flash_device->erase_size) / FILESYSTEM_BLOCK_SIZE; } // Flush the cache that was written to the scratch portion of flash. Only used @@ -259,11 +261,12 @@ static bool flush_scratch_flash(void) { // First, copy out any blocks that we haven't touched from the sector we've // cached. bool copy_to_scratch_ok = true; - for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) { + uint32_t scratch_sector = flash_device->total_size - flash_device->erase_size; + for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { if ((dirty_mask & (1 << i)) == 0) { copy_to_scratch_ok = copy_to_scratch_ok && copy_block(current_sector + i * FILESYSTEM_BLOCK_SIZE, - SCRATCH_SECTOR + i * FILESYSTEM_BLOCK_SIZE); + scratch_sector + i * FILESYSTEM_BLOCK_SIZE); } } if (!copy_to_scratch_ok) { @@ -274,8 +277,8 @@ static bool flush_scratch_flash(void) { // Second, erase the current sector. erase_sector(current_sector); // Finally, copy the new version into it. - for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) { - copy_block(SCRATCH_SECTOR + i * FILESYSTEM_BLOCK_SIZE, + for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { + copy_block(scratch_sector + i * FILESYSTEM_BLOCK_SIZE, current_sector + i * FILESYSTEM_BLOCK_SIZE); } return true; @@ -285,8 +288,8 @@ static bool flush_scratch_flash(void) { // ram. Each page is allocated separately so that the GC doesn't need to provide // one huge block. We can free it as we write if we want to also. static bool allocate_ram_cache(void) { - uint8_t blocks_per_sector = SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; - uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE; + uint8_t blocks_per_sector = flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; + uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; MP_STATE_VM(flash_ram_cache) = m_malloc_maybe(blocks_per_sector * pages_per_block * sizeof(uint32_t), false); if (MP_STATE_VM(flash_ram_cache) == NULL) { return false; @@ -298,7 +301,7 @@ static bool allocate_ram_cache(void) { bool success = true; for (i = 0; i < blocks_per_sector; i++) { for (j = 0; j < pages_per_block; j++) { - uint8_t *page_cache = m_malloc_maybe(SPI_FLASH_PAGE_SIZE, false); + uint8_t *page_cache = m_malloc_maybe(flash_device->page_size, false); if (page_cache == NULL) { success = false; break; @@ -333,14 +336,14 @@ static bool flush_ram_cache(bool keep_cache) { // we've cached. If we don't do this we'll erase the data during the sector // erase below. bool copy_to_ram_ok = true; - uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE; - for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) { + uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; + for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { if ((dirty_mask & (1 << i)) == 0) { for (uint8_t j = 0; j < pages_per_block; j++) { copy_to_ram_ok = read_flash( - current_sector + (i * pages_per_block + j) * SPI_FLASH_PAGE_SIZE, + current_sector + (i * pages_per_block + j) * flash_device->page_size, MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j], - SPI_FLASH_PAGE_SIZE); + flash_device->page_size); if (!copy_to_ram_ok) { break; } @@ -357,11 +360,11 @@ static bool flush_ram_cache(bool keep_cache) { // Second, erase the current sector. erase_sector(current_sector); // Lastly, write all the data in ram that we've cached. - for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) { + for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { for (uint8_t j = 0; j < pages_per_block; j++) { - write_flash(current_sector + (i * pages_per_block + j) * SPI_FLASH_PAGE_SIZE, + write_flash(current_sector + (i * pages_per_block + j) * flash_device->page_size, MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j], - SPI_FLASH_PAGE_SIZE); + flash_device->page_size); if (!keep_cache) { m_free(MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j]); } @@ -485,21 +488,21 @@ bool external_flash_read_block(uint8_t *dest, uint32_t block) { } // Mask out the lower bits that designate the address within the sector. - uint32_t this_sector = address & (~(SPI_FLASH_ERASE_SIZE - 1)); - uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE); + uint32_t this_sector = address & (~(flash_device->erase_size - 1)); + uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (flash_device->erase_size / FILESYSTEM_BLOCK_SIZE); uint8_t mask = 1 << (block_index); // We're reading from the currently cached sector. if (current_sector == this_sector && (mask & dirty_mask) > 0) { if (MP_STATE_VM(flash_ram_cache) != NULL) { - uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE; + uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; for (int i = 0; i < pages_per_block; i++) { - memcpy(dest + i * SPI_FLASH_PAGE_SIZE, + memcpy(dest + i * flash_device->page_size, MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i], - SPI_FLASH_PAGE_SIZE); + flash_device->page_size); } return true; } else { - uint32_t scratch_address = SCRATCH_SECTOR + block_index * FILESYSTEM_BLOCK_SIZE; + uint32_t scratch_address = flash_device->total_size - flash_device->erase_size + block_index * FILESYSTEM_BLOCK_SIZE; return read_flash(scratch_address, dest, FILESYSTEM_BLOCK_SIZE); } } @@ -521,8 +524,8 @@ bool external_flash_write_block(const uint8_t *data, uint32_t block) { // Wait for any previous writes to finish. wait_for_flash_ready(); // Mask out the lower bits that designate the address within the sector. - uint32_t this_sector = address & (~(SPI_FLASH_ERASE_SIZE - 1)); - uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE); + uint32_t this_sector = address & (~(flash_device->erase_size - 1)); + uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (flash_device->erase_size / FILESYSTEM_BLOCK_SIZE); uint8_t mask = 1 << (block_index); // Flush the cache if we're moving onto a sector or we're writing the // same block again. @@ -536,7 +539,7 @@ bool external_flash_write_block(const uint8_t *data, uint32_t block) { spi_flash_flush_keep_cache(true); } if (MP_STATE_VM(flash_ram_cache) == NULL && !allocate_ram_cache()) { - erase_sector(SCRATCH_SECTOR); + erase_sector(flash_device->total_size - flash_device->erase_size); wait_for_flash_ready(); } current_sector = this_sector; @@ -545,15 +548,15 @@ bool external_flash_write_block(const uint8_t *data, uint32_t block) { dirty_mask |= mask; // Copy the block to the appropriate cache. if (MP_STATE_VM(flash_ram_cache) != NULL) { - uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE; + uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; for (int i = 0; i < pages_per_block; i++) { memcpy(MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i], - data + i * SPI_FLASH_PAGE_SIZE, - SPI_FLASH_PAGE_SIZE); + data + i * flash_device->page_size, + flash_device->page_size); } return true; } else { - uint32_t scratch_address = SCRATCH_SECTOR + block_index * FILESYSTEM_BLOCK_SIZE; + uint32_t scratch_address = flash_device->total_size - flash_device->erase_size + block_index * FILESYSTEM_BLOCK_SIZE; return write_flash(scratch_address, data, FILESYSTEM_BLOCK_SIZE); } } diff --git a/ports/atmel-samd/external_flash/qspi_flash.c b/ports/atmel-samd/external_flash/qspi_flash.c index d9dc3bf01e..54f1af0dce 100644 --- a/ports/atmel-samd/external_flash/qspi_flash.c +++ b/ports/atmel-samd/external_flash/qspi_flash.c @@ -167,7 +167,7 @@ void spi_flash_init(void) { QSPI->CTRLA.reg = QSPI_CTRLA_SWRST; // We don't need to wait because we're running as fast as the CPU. - QSPI->BAUD.bit.BAUD = 10; + QSPI->BAUD.bit.BAUD = 1; QSPI->CTRLB.reg = QSPI_CTRLB_MODE_MEMORY | QSPI_CTRLB_DATALEN_8BITS | QSPI_CTRLB_CSMODE_LASTXFER; @@ -191,6 +191,4 @@ void spi_flash_init(void) { spi_flash_command(CMD_ENABLE_WRITE); spi_flash_write_command(0x01, full_status, 3); } - - asm("nop"); } From 01aceaae50e913840064691d3e7c6915e820939f Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 1 Mar 2018 12:45:12 -0800 Subject: [PATCH 2/2] Support all boards and remove erase and page sizes because they never change. --- .../circuitplayground_express/mpconfigboard.h | 7 +- .../boards/feather_m0_express/mpconfigboard.h | 9 +- .../feather_m0_supersized/mpconfigboard.h | 7 +- .../itsybitsy_m0_express/mpconfigboard.h | 11 +- .../boards/metro_m0_express/mpconfigboard.h | 11 +- .../boards/metro_m4_express/mpconfigboard.h | 4 +- .../metro_m4_express_revb/mpconfigboard.h | 9 +- .../trinket_m0_haxpress/mpconfigboard.h | 7 +- .../atmel-samd/boards/ugame10/mpconfigboard.h | 8 +- ports/atmel-samd/external_flash/devices.h | 108 +++++++++++------- .../external_flash/external_flash.c | 66 +++++------ .../external_flash/external_flash.h | 4 + 12 files changed, 157 insertions(+), 94 deletions(-) diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index 599dac56de..02802a6a90 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -47,9 +47,14 @@ #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) -#define EXTERNAL_FLASH_DEVICES 25FL216K, \ +#include "external_flash/devices.h" + +#define EXTERNAL_FLASH_DEVICE_COUNT 2 +#define EXTERNAL_FLASH_DEVICES S25FL216K, \ GD25Q16C +#include "external_flash/external_flash.h" + #define CALIBRATE_CRYSTALLESS 1 // Explanation of how a user got into safe mode. diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h index 378b0891ad..ef13bcafec 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h @@ -40,5 +40,10 @@ #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) -#include "external_flash/devices/S25FL216K.h" -#include "external_flash/devices/GD25Q16C.h" +#include "external_flash/devices.h" + +#define EXTERNAL_FLASH_DEVICE_COUNT 2 +#define EXTERNAL_FLASH_DEVICES S25FL216K, \ + GD25Q16C + +#include "external_flash/external_flash.h" diff --git a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h index b5ed682962..883d7a2e53 100644 --- a/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h @@ -42,4 +42,9 @@ #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) -#include "external_flash/devices/S25FL064L.h" +#include "external_flash/devices.h" + +#define EXTERNAL_FLASH_DEVICE_COUNT 1 +#define EXTERNAL_FLASH_DEVICES S25FL064L + +#include "external_flash/external_flash.h" diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h index 46eb8c8d9e..ca39792a92 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h @@ -33,13 +33,16 @@ #define MICROPY_PORT_B (PORT_PB22 | PORT_PB23 | PORT_PB03 ) #define MICROPY_PORT_C (0) -#include "external_flash/external_flash.h" - // If you change this, then make sure to update the linker scripts as well to // make sure you don't overwrite code. // #define CIRCUITPY_INTERNAL_NVM_SIZE 256 #define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) -#include "external_flash/devices/GD25Q16C.h" -#include "external_flash/devices/W25Q16FW.h" +#include "external_flash/devices.h" + +#define EXTERNAL_FLASH_DEVICE_COUNT 2 +#define EXTERNAL_FLASH_DEVICES W25Q16FW, \ + GD25Q16C + +#include "external_flash/external_flash.h" diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h index 70e830620d..c8ceaa386b 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h @@ -34,8 +34,6 @@ #define MICROPY_PORT_B (PORT_PB03 | PORT_PB22 | PORT_PB23) #define MICROPY_PORT_C (0) -#include "external_flash/external_flash.h" - // If you change this, then make sure to update the linker scripts as well to // make sure you don't overwrite code. // #define CIRCUITPY_INTERNAL_NVM_SIZE 256 @@ -43,5 +41,10 @@ #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) -#include "external_flash/devices/S25FL216K.h" -#include "external_flash/devices/GD25Q16C.h" +#include "external_flash/devices.h" + +#define EXTERNAL_FLASH_DEVICE_COUNT 2 +#define EXTERNAL_FLASH_DEVICES S25FL216K, \ + GD25Q16C + +#include "external_flash/external_flash.h" diff --git a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h index 98f69ba804..b541af80c9 100644 --- a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h @@ -29,7 +29,7 @@ #include "external_flash/devices.h" -#define EXTERNAL_FLASH_DEVICE_COUNT 2 -#define EXTERNAL_FLASH_DEVICES S25FL116K, GD25Q16C +#define EXTERNAL_FLASH_DEVICE_COUNT 3 +#define EXTERNAL_FLASH_DEVICES S25FL116K, S25FL216K, GD25Q16C #include "external_flash/external_flash.h" diff --git a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h index 7c51bae870..70c92ea444 100644 --- a/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h @@ -54,5 +54,10 @@ #define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE) -#include "external_flash/devices/S25FL216K.h" -#include "external_flash/devices/GD25Q16C.h" +#include "external_flash/devices.h" + +#define EXTERNAL_FLASH_DEVICE_COUNT 2 +#define EXTERNAL_FLASH_DEVICES S25FL216K, \ + GD25Q16C + +#include "external_flash/external_flash.h" diff --git a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h index 62bca9a413..340e25e59b 100644 --- a/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h +++ b/ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h @@ -48,4 +48,9 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) -#include "external_flash/devices/W25Q32BV.h" +#include "external_flash/devices.h" + +#define EXTERNAL_FLASH_DEVICE_COUNT 1 +#define EXTERNAL_FLASH_DEVICES W25Q32BV + +#include "external_flash/external_flash.h" diff --git a/ports/atmel-samd/boards/ugame10/mpconfigboard.h b/ports/atmel-samd/boards/ugame10/mpconfigboard.h index d0d9296a40..b9ae952a8c 100644 --- a/ports/atmel-samd/boards/ugame10/mpconfigboard.h +++ b/ports/atmel-samd/boards/ugame10/mpconfigboard.h @@ -41,5 +41,9 @@ #define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) -//#include "external_flash/devices/W25Q32BV.h" -#include "external_flash/devices/S25FL216K.h" +#include "external_flash/devices.h" + +#define EXTERNAL_FLASH_DEVICE_COUNT 1 +#define EXTERNAL_FLASH_DEVICES S25FL216K + +#include "external_flash/external_flash.h" diff --git a/ports/atmel-samd/external_flash/devices.h b/ports/atmel-samd/external_flash/devices.h index a02d268287..ce2c6c3dc0 100644 --- a/ports/atmel-samd/external_flash/devices.h +++ b/ports/atmel-samd/external_flash/devices.h @@ -31,16 +31,29 @@ typedef struct { uint32_t total_size; - uint16_t erase_size; - uint16_t page_size; uint16_t start_up_time_us; + + // Three response bytes to 0x9f JEDEC ID command. uint8_t manufacturer_id; uint8_t memory_type; uint8_t capacity; + + // Max clock speed for all operations and the fastest read mode. uint8_t max_clock_speed_mhz; - bool has_sector_protection; - bool supports_qspi; - bool supports_qspi_writes; + bool has_sector_protection : 1; + + // Supports the 0x0b fast read command with 8 dummy cycles. + bool supports_fast_read : 1; + + // Supports the fast read, quad output command 0x6b with 8 dummy cycles. + bool supports_qspi : 1; + + // Requires quad enable set in status bit 9. + bool has_quad_enable : 1; + + // Supports the quad input page program command 0x32. This is known as 1-1-4 because it only + // uses all four lines for data. + bool supports_qspi_writes: 1; } external_flash_device; // Settings for the Adesto Tech AT25DF081A 1MiB SPI flash. Its on the SAMD21 @@ -48,116 +61,127 @@ typedef struct { // Datasheet: https://www.adestotech.com/wp-content/uploads/doc8715.pdf #define AT25DF081A {\ .total_size = (1 << 20), /* 1 MiB */ \ - .erase_size = (1 << 12), /* 4 KiB */ \ - .page_size = 256, /*256 bytes */ \ .start_up_time_us = 10000, \ .manufacturer_id = 0x1f, \ .memory_type = 0x45, \ .capacity = 0x01, \ - .max_clock_speed_mhz = 104, \ + .max_clock_speed_mhz = 85, \ .has_sector_protection = true, \ - .supports_qspi = true, \ + .supports_fast_read = true, \ + .supports_qspi = false, \ + .has_quad_enable = false, \ .supports_qspi_writes = false, \ } // Settings for the Gigadevice GD25Q16C 2MiB SPI flash. // Datasheet: http://www.gigadevice.com/wp-content/uploads/2017/12/DS-00086-GD25Q16C-Rev2.6.pdf - #define GD25Q16C {\ .total_size = (1 << 21), /* 2 MiB */ \ - .erase_size = (1 << 12), /* 4 KiB */ \ - .page_size = 256, /*256 bytes */ \ .start_up_time_us = 5000, \ .manufacturer_id = 0xc8, \ .memory_type = 0x40, \ .capacity = 0x15, \ - .max_clock_speed_mhz = 104, \ - .has_sector_protection = true, \ + .max_clock_speed_mhz = 104, /* if we need 120 then we can turn on high performance mode */ \ + .has_sector_protection = false, \ + .supports_fast_read = true, \ .supports_qspi = true, \ - .supports_qspi_writes = false, \ + .has_quad_enable = true, \ + .supports_qspi_writes = true, \ } // Settings for the Cypress (was Spansion) S25FL064L 8MiB SPI flash. // Datasheet: http://www.cypress.com/file/316661/download #define S25FL064L {\ .total_size = (1 << 23), /* 8 MiB */ \ - .erase_size = (1 << 12), /* 4 KiB */ \ - .page_size = 256, /*256 bytes */ \ .start_up_time_us = 300, \ .manufacturer_id = 0x01, \ .memory_type = 0x60, \ .capacity = 0x17, \ - .max_clock_speed_mhz = 104, \ - .has_sector_protection = true, \ + .max_clock_speed_mhz = 108, \ + .has_sector_protection = false, \ + .supports_fast_read = true, \ .supports_qspi = true, \ - .supports_qspi_writes = false, \ + .has_quad_enable = true, \ + .supports_qspi_writes = true, \ } // Settings for the Cypress (was Spansion) S25FL116K 2MiB SPI flash. // Datasheet: http://www.cypress.com/file/196886/download #define S25FL116K {\ .total_size = (1 << 21), /* 2 MiB */ \ - .erase_size = (1 << 12), /* 4 KiB */ \ - .page_size = 256, /*256 bytes */ \ .start_up_time_us = 10000, \ .manufacturer_id = 0x01, \ .memory_type = 0x40, \ .capacity = 0x15, \ - .max_clock_speed_mhz = 104, \ - .has_sector_protection = true, \ + .max_clock_speed_mhz = 108, \ + .has_sector_protection = false, \ + .supports_fast_read = true, \ .supports_qspi = true, \ + .has_quad_enable = true, \ .supports_qspi_writes = false, \ } - // Settings for the Cypress (was Spansion) S25FL216K 2MiB SPI flash. // Datasheet: http://www.cypress.com/file/197346/download - #define S25FL216K {\ .total_size = (1 << 21), /* 2 MiB */ \ - .erase_size = (1 << 12), /* 4 KiB */ \ - .page_size = 256, /*256 bytes */ \ .start_up_time_us = 10000, \ .manufacturer_id = 0x01, \ .memory_type = 0x40, \ .capacity = 0x15, \ - .max_clock_speed_mhz = 104, \ + .max_clock_speed_mhz = 65, \ .has_sector_protection = false, \ - .supports_qspi = true, \ + .supports_fast_read = true, \ + .supports_qspi = false, \ + .has_quad_enable = false, \ .supports_qspi_writes = false, \ } // Settings for the Winbond W25Q16FW 2MiB SPI flash. // Datasheet: https://www.winbond.com/resource-files/w25q16fw%20revj%2005182017%20sfdp.pdf - #define W25Q16FW {\ .total_size = (1 << 21), /* 2 MiB */ \ - .erase_size = (1 << 12), /* 4 KiB */ \ - .page_size = 256, /*256 bytes */ \ .start_up_time_us = 5000, \ .manufacturer_id = 0xef, \ .memory_type = 0x60, \ .capacity = 0x15, \ - .max_clock_speed_mhz = 104, \ + .max_clock_speed_mhz = 133, \ .has_sector_protection = false, \ + .supports_fast_read = true, \ .supports_qspi = true, \ - .supports_qspi_writes = false, \ + .has_quad_enable = true, \ + .supports_qspi_writes = true, \ +} + +// Settings for the Winbond W25Q16JV 2MiB SPI flash. +// Datasheet: https://www.winbond.com/resource-files/w25q16jv%20spi%20revf%2005092017.pdf +#define W25Q16JV {\ + .total_size = (1 << 21), /* 2 MiB */ \ + .start_up_time_us = 5000, \ + .manufacturer_id = 0xef, \ + .memory_type = 0x40, \ + .capacity = 0x15, \ + .max_clock_speed_mhz = 133, \ + .has_sector_protection = false, \ + .supports_fast_read = true, \ + .supports_qspi = true, \ + .has_quad_enable = true, \ + .supports_qspi_writes = true, \ } // Settings for the Winbond W25Q32BV 2MiB SPI flash. // Datasheet: https://www.winbond.com/resource-files/w25q32bv_revi_100413_wo_automotive.pdf - #define W25Q32BV {\ - .total_size = (1 << 21), /* 2 MiB */ \ - .erase_size = (1 << 12), /* 4 KiB */ \ - .page_size = 256, /*256 bytes */ \ - .start_up_time_us = 5000, \ + .total_size = (1 << 22), /* 4 MiB */ \ + .start_up_time_us = 10000, \ .manufacturer_id = 0xef, \ .memory_type = 0x60, \ .capacity = 0x16, \ .max_clock_speed_mhz = 104, \ .has_sector_protection = false, \ + .supports_fast_read = true, \ .supports_qspi = true, \ + .has_quad_enable = true, \ .supports_qspi_writes = false, \ } @@ -165,15 +189,15 @@ typedef struct { // Datasheet: https://www.winbond.com/resource-files/w25q80dv%20dl_revh_10022015.pdf #define W25Q80DL {\ .total_size = (1 << 20), /* 1 MiB */ \ - .erase_size = (1 << 12), /* 4 KiB */ \ - .page_size = 256, /*256 bytes */ \ .start_up_time_us = 5000, \ .manufacturer_id = 0xef, \ .memory_type = 0x60, \ .capacity = 0x14, \ .max_clock_speed_mhz = 104, \ .has_sector_protection = false, \ + .supports_fast_read = true, \ .supports_qspi = true, \ + .has_quad_enable = true, \ .supports_qspi_writes = false, \ } diff --git a/ports/atmel-samd/external_flash/external_flash.c b/ports/atmel-samd/external_flash/external_flash.c index 26437e975c..ca8121bc93 100644 --- a/ports/atmel-samd/external_flash/external_flash.c +++ b/ports/atmel-samd/external_flash/external_flash.c @@ -54,9 +54,9 @@ struct spi_m_sync_descriptor spi_flash_desc; // The currently cached sector in the cache, ram or flash based. static uint32_t current_sector; -external_flash_device possible_devices[EXTERNAL_FLASH_DEVICE_COUNT] = {EXTERNAL_FLASH_DEVICES}; +const external_flash_device possible_devices[EXTERNAL_FLASH_DEVICE_COUNT] = {EXTERNAL_FLASH_DEVICES}; -static external_flash_device* flash_device = NULL; +static const external_flash_device* flash_device = NULL; // Track which blocks (up to 32) in the current sector currently live in the // cache. @@ -111,13 +111,13 @@ static bool write_flash(uint32_t address, const uint8_t* data, uint32_t data_len for (uint32_t bytes_written = 0; bytes_written < data_length; - bytes_written += flash_device->page_size) { + bytes_written += SPI_FLASH_PAGE_SIZE) { if (!wait_for_flash_ready() || !write_enable()) { return false; } if (!spi_flash_write_data(address + bytes_written, (uint8_t*) data + bytes_written, - flash_device->page_size)) { + SPI_FLASH_PAGE_SIZE)) { return false; } } @@ -168,7 +168,7 @@ static bool erase_sector(uint32_t sector_address) { // Sector is really 24 bits. static bool copy_block(uint32_t src_address, uint32_t dest_address) { // Copy page by page to minimize RAM buffer. - uint16_t page_size = flash_device->page_size; + uint16_t page_size = SPI_FLASH_PAGE_SIZE; uint8_t buffer[page_size]; for (uint32_t i = 0; i < FILESYSTEM_BLOCK_SIZE / page_size; i++) { if (!read_flash(src_address + i * page_size, buffer, page_size)) { @@ -201,7 +201,7 @@ void external_flash_init(void) { for (uint8_t i = 0; i < num_possible_devices; i++) { - external_flash_device* possible_device = &possible_devices[i]; + const external_flash_device* possible_device = &possible_devices[i]; uint8_t jedec_id_response[3] = {0x00, 0x00, 0x00}; spi_flash_read_command(CMD_READ_JEDEC_ID, jedec_id_response, 3); if (jedec_id_response[0] == possible_device->manufacturer_id && @@ -252,7 +252,7 @@ uint32_t external_flash_get_block_size(void) { uint32_t external_flash_get_block_count(void) { // We subtract one erase sector size because we may use it as a staging area // for writes. - return SPI_FLASH_PART1_START_BLOCK + (flash_device->total_size - flash_device->erase_size) / FILESYSTEM_BLOCK_SIZE; + return SPI_FLASH_PART1_START_BLOCK + (flash_device->total_size - SPI_FLASH_ERASE_SIZE) / FILESYSTEM_BLOCK_SIZE; } // Flush the cache that was written to the scratch portion of flash. Only used @@ -261,8 +261,8 @@ static bool flush_scratch_flash(void) { // First, copy out any blocks that we haven't touched from the sector we've // cached. bool copy_to_scratch_ok = true; - uint32_t scratch_sector = flash_device->total_size - flash_device->erase_size; - for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { + uint32_t scratch_sector = flash_device->total_size - SPI_FLASH_ERASE_SIZE; + for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) { if ((dirty_mask & (1 << i)) == 0) { copy_to_scratch_ok = copy_to_scratch_ok && copy_block(current_sector + i * FILESYSTEM_BLOCK_SIZE, @@ -277,7 +277,7 @@ static bool flush_scratch_flash(void) { // Second, erase the current sector. erase_sector(current_sector); // Finally, copy the new version into it. - for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { + for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) { copy_block(scratch_sector + i * FILESYSTEM_BLOCK_SIZE, current_sector + i * FILESYSTEM_BLOCK_SIZE); } @@ -288,8 +288,8 @@ static bool flush_scratch_flash(void) { // ram. Each page is allocated separately so that the GC doesn't need to provide // one huge block. We can free it as we write if we want to also. static bool allocate_ram_cache(void) { - uint8_t blocks_per_sector = flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; - uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; + uint8_t blocks_per_sector = SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; + uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE; MP_STATE_VM(flash_ram_cache) = m_malloc_maybe(blocks_per_sector * pages_per_block * sizeof(uint32_t), false); if (MP_STATE_VM(flash_ram_cache) == NULL) { return false; @@ -301,7 +301,7 @@ static bool allocate_ram_cache(void) { bool success = true; for (i = 0; i < blocks_per_sector; i++) { for (j = 0; j < pages_per_block; j++) { - uint8_t *page_cache = m_malloc_maybe(flash_device->page_size, false); + uint8_t *page_cache = m_malloc_maybe(SPI_FLASH_PAGE_SIZE, false); if (page_cache == NULL) { success = false; break; @@ -336,14 +336,14 @@ static bool flush_ram_cache(bool keep_cache) { // we've cached. If we don't do this we'll erase the data during the sector // erase below. bool copy_to_ram_ok = true; - uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; - for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { + uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE; + for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) { if ((dirty_mask & (1 << i)) == 0) { for (uint8_t j = 0; j < pages_per_block; j++) { copy_to_ram_ok = read_flash( - current_sector + (i * pages_per_block + j) * flash_device->page_size, + current_sector + (i * pages_per_block + j) * SPI_FLASH_PAGE_SIZE, MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j], - flash_device->page_size); + SPI_FLASH_PAGE_SIZE); if (!copy_to_ram_ok) { break; } @@ -360,11 +360,11 @@ static bool flush_ram_cache(bool keep_cache) { // Second, erase the current sector. erase_sector(current_sector); // Lastly, write all the data in ram that we've cached. - for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { + for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) { for (uint8_t j = 0; j < pages_per_block; j++) { - write_flash(current_sector + (i * pages_per_block + j) * flash_device->page_size, + write_flash(current_sector + (i * pages_per_block + j) * SPI_FLASH_PAGE_SIZE, MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j], - flash_device->page_size); + SPI_FLASH_PAGE_SIZE); if (!keep_cache) { m_free(MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j]); } @@ -488,21 +488,21 @@ bool external_flash_read_block(uint8_t *dest, uint32_t block) { } // Mask out the lower bits that designate the address within the sector. - uint32_t this_sector = address & (~(flash_device->erase_size - 1)); - uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (flash_device->erase_size / FILESYSTEM_BLOCK_SIZE); + uint32_t this_sector = address & (~(SPI_FLASH_ERASE_SIZE - 1)); + uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE); uint8_t mask = 1 << (block_index); // We're reading from the currently cached sector. if (current_sector == this_sector && (mask & dirty_mask) > 0) { if (MP_STATE_VM(flash_ram_cache) != NULL) { - uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; + uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE; for (int i = 0; i < pages_per_block; i++) { - memcpy(dest + i * flash_device->page_size, + memcpy(dest + i * SPI_FLASH_PAGE_SIZE, MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i], - flash_device->page_size); + SPI_FLASH_PAGE_SIZE); } return true; } else { - uint32_t scratch_address = flash_device->total_size - flash_device->erase_size + block_index * FILESYSTEM_BLOCK_SIZE; + uint32_t scratch_address = flash_device->total_size - SPI_FLASH_ERASE_SIZE + block_index * FILESYSTEM_BLOCK_SIZE; return read_flash(scratch_address, dest, FILESYSTEM_BLOCK_SIZE); } } @@ -524,8 +524,8 @@ bool external_flash_write_block(const uint8_t *data, uint32_t block) { // Wait for any previous writes to finish. wait_for_flash_ready(); // Mask out the lower bits that designate the address within the sector. - uint32_t this_sector = address & (~(flash_device->erase_size - 1)); - uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (flash_device->erase_size / FILESYSTEM_BLOCK_SIZE); + uint32_t this_sector = address & (~(SPI_FLASH_ERASE_SIZE - 1)); + uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE); uint8_t mask = 1 << (block_index); // Flush the cache if we're moving onto a sector or we're writing the // same block again. @@ -539,7 +539,7 @@ bool external_flash_write_block(const uint8_t *data, uint32_t block) { spi_flash_flush_keep_cache(true); } if (MP_STATE_VM(flash_ram_cache) == NULL && !allocate_ram_cache()) { - erase_sector(flash_device->total_size - flash_device->erase_size); + erase_sector(flash_device->total_size - SPI_FLASH_ERASE_SIZE); wait_for_flash_ready(); } current_sector = this_sector; @@ -548,15 +548,15 @@ bool external_flash_write_block(const uint8_t *data, uint32_t block) { dirty_mask |= mask; // Copy the block to the appropriate cache. if (MP_STATE_VM(flash_ram_cache) != NULL) { - uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; + uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE; for (int i = 0; i < pages_per_block; i++) { memcpy(MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i], - data + i * flash_device->page_size, - flash_device->page_size); + data + i * SPI_FLASH_PAGE_SIZE, + SPI_FLASH_PAGE_SIZE); } return true; } else { - uint32_t scratch_address = flash_device->total_size - flash_device->erase_size + block_index * FILESYSTEM_BLOCK_SIZE; + uint32_t scratch_address = flash_device->total_size - SPI_FLASH_ERASE_SIZE + block_index * FILESYSTEM_BLOCK_SIZE; return write_flash(scratch_address, data, FILESYSTEM_BLOCK_SIZE); } } diff --git a/ports/atmel-samd/external_flash/external_flash.h b/ports/atmel-samd/external_flash/external_flash.h index 5a43b10c84..f8ec5673ce 100644 --- a/ports/atmel-samd/external_flash/external_flash.h +++ b/ports/atmel-samd/external_flash/external_flash.h @@ -37,6 +37,10 @@ // Erase sector size. #define SPI_FLASH_SECTOR_SIZE (0x1000 - 100) +// These are common across all NOR Flash. +#define SPI_FLASH_ERASE_SIZE (1 << 12) +#define SPI_FLASH_PAGE_SIZE (256) + #define SPI_FLASH_SYSTICK_MASK (0x1ff) // 512ms #define SPI_FLASH_IDLE_TICK(tick) (((tick) & SPI_FLASH_SYSTICK_MASK) == 2)