Skip to content

Commit 6c4decd

Browse files
committed
decoupling chip specific functions from EXTERNAL_FLASH_QSPI_SINGLE
1 parent baad9c1 commit 6c4decd

8 files changed

Lines changed: 106 additions & 78 deletions

File tree

ports/atmel-samd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ include $(TOP)/supervisor/supervisor.mk
5353
# Include make rules and variables common across CircuitPython builds.
5454
include $(TOP)/py/circuitpy_defns.mk
5555

56-
CROSS_COMPILE = arm-none-eabi-
56+
CROSS_COMPILE = ~/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-
5757

5858
HAL_DIR=hal/$(MCU_SERIES)
5959

ports/atmel-samd/boards/pycubed/mpconfigboard.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
LD_FILE = boards/samd51x19-bootloader-external-flash.ld
21
USB_VID = 0x04D8
32
USB_PID = 0xEC44
43
USB_PRODUCT = "PyCubed"
@@ -12,6 +11,8 @@ EXTERNAL_FLASH_DEVICE_COUNT = 1
1211
EXTERNAL_FLASH_DEVICES = W25Q80DV
1312
LONGINT_IMPL = MPZ
1413

14+
CIRCUITPY_DRIVE_LABEL = "PYCUBED"
15+
1516
# Not needed.
1617
CIRCUITPY_AUDIOBUSIO = 0
1718
CIRCUITPY_DISPLAYIO = 0

ports/atmel-samd/boards/pycubed_mram/mpconfigboard.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#define MICROPY_HW_BOARD_NAME "PyCubedv04-MRAM"
32
#define MICROPY_HW_MCU_NAME "samd51j19"
43
#define CIRCUITPY_MCU_FAMILY samd51
@@ -11,8 +10,12 @@
1110
#define MICROPY_PORT_C (0)
1211
#define MICROPY_PORT_D (0)
1312

13+
#define SPI_FLASH_WP_PIN &pin_PA10
14+
#define SPI_FLASH_HOLD_PIN &pin_PA11
15+
1416
// External flash MR2xH40 MRAM
1517
#define EXTERNAL_FLASH_QSPI_SINGLE
18+
#define EXTERNAL_FLASH_NO_JEDEC
1619

1720
#define AUTORESET_DELAY_MS 500
1821

@@ -34,4 +37,3 @@
3437

3538
#define IGNORE_PIN_PA24 1
3639
#define IGNORE_PIN_PA25 1
37-

ports/atmel-samd/boards/pycubed_mram/mpconfigboard.mk

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
LD_FILE = boards/samd51x19-bootloader-external-flash.ld
21
USB_VID = 0x04D8
32
USB_PID = 0xEC44
43
USB_PRODUCT = "PyCubed"
@@ -12,13 +11,17 @@ EXTERNAL_FLASH_DEVICE_COUNT = 1
1211
EXTERNAL_FLASH_DEVICES = MR2xH40
1312
LONGINT_IMPL = MPZ
1413

14+
CIRCUITPY_DRIVE_LABEL = "PYCUBED"
15+
1516
# Not needed.
1617
CIRCUITPY_AUDIOBUSIO = 0
1718
CIRCUITPY_DISPLAYIO = 0
19+
CIRCUITPY_FRAMEBUFFERIO = 0
1820
CIRCUITPY_GAMEPAD = 0
21+
CIRCUITPY_RGBMATRIX = 0
1922
CIRCUITPY_PS2IO = 0
2023

2124
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
2225
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
2326
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Register
24-
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD
27+
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD

ports/atmel-samd/boards/pycubed_mram/pins.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
4040
{ MP_ROM_QSTR(MP_QSTR_EN_GPS), MP_ROM_PTR(&pin_PB01) },
4141
{ MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_PB02) },
4242
{ MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_PB03) },
43-
43+
4444
{ MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_PB12) },
4545
{ MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_PB13) },
4646

4747
{ MP_ROM_QSTR(MP_QSTR_WDT_WDI), MP_ROM_PTR(&pin_PA23) },
4848
{ MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PA21) },
49-
49+
5050
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) },
5151
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) },
5252
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) },
53-
53+
5454
};
55-
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);
55+
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

ports/atmel-samd/supervisor/qspi_flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ bool spi_flash_read_data(uint32_t address, uint8_t* data, uint32_t length) {
169169
#ifdef EXTERNAL_FLASH_QSPI_SINGLE
170170
QSPI->INSTRCTRL.bit.INSTR = CMD_READ_DATA;
171171
uint32_t mode = QSPI_INSTRFRAME_WIDTH_SINGLE_BIT_SPI;
172-
#elif defined EXTERNAL_FLASH_QSPI_DUAL
172+
#elif defined(EXTERNAL_FLASH_QSPI_DUAL)
173173
QSPI->INSTRCTRL.bit.INSTR = CMD_DUAL_READ;
174174
uint32_t mode = QSPI_INSTRFRAME_WIDTH_DUAL_OUTPUT;
175175
#else

supervisor/shared/external_flash/devices.h

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ typedef struct {
6464
// True when the status register is a single byte. This implies the Quad Enable bit is in the
6565
// first byte and the Read Status Register 2 command (0x35) is unsupported.
6666
bool single_status_byte: 1;
67+
68+
// Does not support using a ready bit within the status register
69+
bool no_ready_bit: 1;
70+
71+
// Does not support the erase command (0x20)
72+
bool no_erase_cmd: 1;
73+
74+
// Device does not have a reset command
75+
bool no_reset_cmd: 1;
6776
} external_flash_device;
6877

6978
// Settings for the Adesto Tech AT25DF081A 1MiB SPI flash. It's on the SAMD21
@@ -426,21 +435,25 @@ typedef struct {
426435
.single_status_byte = false, \
427436
}
428437

429-
// Everspin MRAM
438+
// Settings for the Everspin MR20H40 / MR25H40 magnetic non-volatile RAM
439+
// Datasheet: https://www.everspin.com/supportdocs/MR25H40CDFR
430440
#define MR2xH40 {\
431441
.total_size = (1 << 22), /* 4 MiB */ \
432442
.start_up_time_us = 10000, \
433443
.manufacturer_id = 0xef, /*no JDEC*/ \
434444
.memory_type = 0x40, /*no JDEC*/ \
435445
.capacity = 0x14, /*no JDEC*/ \
436-
.max_clock_speed_mhz = 40, \
446+
.max_clock_speed_mhz = 10, \
437447
.quad_enable_bit_mask = 0x00, \
438448
.has_sector_protection = false, \
439449
.supports_fast_read = false, \
440450
.supports_qspi = false, \
441451
.supports_qspi_writes = false, \
442452
.write_status_register_split = false, \
443453
.single_status_byte = true, \
454+
.no_ready_bit = true, \
455+
.no_erase_cmd = true, \
456+
.no_reset_cmd = true, \
444457
}
445458

446459
// Settings for the Macronix MX25L1606 2MiB SPI flash.
@@ -498,25 +511,6 @@ typedef struct {
498511
.single_status_byte = true, \
499512
}
500513

501-
// Settings for the Macronix MX25R1635F 8MiB SPI flash.
502-
// Datasheet: https://www.macronix.com/Lists/Datasheet/Attachments/7595/MX25R1635F,%20Wide%20Range,%2016Mb,%20v1.6.pdf
503-
// In low power mode, quad operations can only run at 8 MHz.
504-
#define MX25R1635F {\
505-
.total_size = (1 << 21), /* 2 MiB */ \
506-
.start_up_time_us = 800, \
507-
.manufacturer_id = 0xc2, \
508-
.memory_type = 0x28, \
509-
.capacity = 0x18, \
510-
.max_clock_speed_mhz = 33, /* 8 mhz for dual/quad */ \
511-
.quad_enable_bit_mask = 0x80, \
512-
.has_sector_protection = false, \
513-
.supports_fast_read = true, \
514-
.supports_qspi = true, \
515-
.supports_qspi_writes = true, \
516-
.write_status_register_split = false, \
517-
.single_status_byte = true, \
518-
}
519-
520514
// Settings for the Macronix MX25L51245G 64MiB SPI flash.
521515
// Datasheet: https://www.macronix.com/Lists/Datasheet/Attachments/7437/MX25L51245G,%203V,%20512Mb,%20v1.6.pdf
522516
#define MX25L51245G {\

supervisor/shared/external_flash/external_flash.c

Lines changed: 74 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ static supervisor_allocation* supervisor_cache = NULL;
5959
static bool wait_for_flash_ready(void) {
6060
bool ok = true;
6161
// Both the write enable and write in progress bits should be low.
62-
#ifdef EXTERNAL_FLASH_QSPI_SINGLE
62+
if (flash_device->no_ready_bit){
6363
// For NVM without a ready bit in status register
6464
return ok;
65-
#else
65+
} else {
6666
uint8_t read_status_response[1] = {0x00};
6767
do {
6868
ok = spi_flash_read_command(CMD_READ_STATUS, read_status_response, 1);
6969
} while (ok && (read_status_response[0] & 0x3) != 0);
7070
return ok;
71-
#endif
71+
}
7272
}
7373

7474
// Turn on the write enable bit so we can program and erase the flash.
@@ -96,7 +96,8 @@ static bool write_flash(uint32_t address, const uint8_t* data, uint32_t data_len
9696
}
9797
// Don't bother writing if the data is all 1s. Thats equivalent to the flash
9898
// state after an erase.
99-
#ifndef EXTERNAL_FLASH_QSPI_SINGLE
99+
if (!flash_device->no_erase_cmd){
100+
// Only do this if the device has an erase command
100101
bool all_ones = true;
101102
for (uint16_t i = 0; i < data_length; i++) {
102103
if (data[i] != 0xff) {
@@ -107,7 +108,7 @@ static bool write_flash(uint32_t address, const uint8_t* data, uint32_t data_len
107108
if (all_ones) {
108109
return true;
109110
}
110-
#endif
111+
}
111112

112113
for (uint32_t bytes_written = 0;
113114
bytes_written < data_length;
@@ -127,42 +128,55 @@ static bool write_flash(uint32_t address, const uint8_t* data, uint32_t data_len
127128
static bool page_erased(uint32_t sector_address) {
128129
// Check the first few bytes to catch the common case where there is data
129130
// without using a bunch of memory.
130-
uint8_t short_buffer[4];
131-
if (read_flash(sector_address, short_buffer, 4)) {
132-
for (uint16_t i = 0; i < 4; i++) {
133-
if (short_buffer[i] != 0xff) {
134-
return false;
131+
if (flash_device->no_erase_cmd){
132+
// skip this if device doesn't have an erase command.
133+
return true;
134+
} else {
135+
uint8_t short_buffer[4];
136+
if (read_flash(sector_address, short_buffer, 4)) {
137+
for (uint16_t i = 0; i < 4; i++) {
138+
if (short_buffer[i] != 0xff) {
139+
return false;
140+
}
135141
}
142+
} else {
143+
return false;
136144
}
137-
} else {
138-
return false;
139-
}
140145

141-
// Now check the full length.
142-
uint8_t full_buffer[FILESYSTEM_BLOCK_SIZE];
143-
if (read_flash(sector_address, full_buffer, FILESYSTEM_BLOCK_SIZE)) {
144-
for (uint16_t i = 0; i < FILESYSTEM_BLOCK_SIZE; i++) {
145-
if (short_buffer[i] != 0xff) {
146-
return false;
146+
// Now check the full length.
147+
uint8_t full_buffer[FILESYSTEM_BLOCK_SIZE];
148+
if (read_flash(sector_address, full_buffer, FILESYSTEM_BLOCK_SIZE)) {
149+
for (uint16_t i = 0; i < FILESYSTEM_BLOCK_SIZE; i++) {
150+
if (short_buffer[i] != 0xff) {
151+
return false;
152+
}
147153
}
154+
} else {
155+
return false;
148156
}
149-
} else {
150-
return false;
157+
return true;
151158
}
152-
return true;
153159
}
154160

155161
// Erases the given sector. Make sure you copied all of the data out of it you
156162
// need! Also note, sector_address is really 24 bits.
157163
static bool erase_sector(uint32_t sector_address) {
158164
// Before we erase the sector we need to wait for any writes to finish and
159165
// and then enable the write again.
160-
if (!wait_for_flash_ready() || !write_enable()) {
161-
return false;
166+
if (flash_device->no_erase_cmd){
167+
// skip this if device doesn't have an erase command.
168+
return true;
169+
} else {
170+
if (!wait_for_flash_ready() || !write_enable()) {
171+
return false;
172+
}
173+
if (flash_device->no_erase_cmd) {
174+
return true;
175+
} else {
176+
spi_flash_sector_command(CMD_SECTOR_ERASE, sector_address);
177+
return true;
178+
}
162179
}
163-
164-
spi_flash_sector_command(CMD_SECTOR_ERASE, sector_address);
165-
return true;
166180
}
167181

168182
// Sector is really 24 bits.
@@ -198,20 +212,31 @@ void supervisor_flash_init(void) {
198212

199213
spi_flash_init();
200214

201-
#ifdef EXTERNAL_FLASH_QSPI_SINGLE
202-
// For NVM that don't have JEDEC response
203-
spi_flash_command(CMD_WAKE);
204-
for (uint8_t i = 0; i < EXTERNAL_FLASH_DEVICE_COUNT; i++) {
205-
const external_flash_device* possible_device = &possible_devices[i];
215+
#ifdef EXTERNAL_FLASH_NO_JEDEC
216+
// For NVM that don't have JEDEC response
217+
spi_flash_command(CMD_WAKE);
218+
for (uint8_t i = 0; i < EXTERNAL_FLASH_DEVICE_COUNT; i++) {
219+
const external_flash_device* possible_device = &possible_devices[i];
220+
flash_device = possible_device;
221+
break;
222+
}
223+
#else
224+
// The response will be 0xff if the flash needs more time to start up.
225+
uint8_t jedec_id_response[3] = {0xff, 0xff, 0xff};
226+
while (jedec_id_response[0] == 0xff) {
227+
spi_flash_read_command(CMD_READ_JEDEC_ID, jedec_id_response, 3);
228+
}
229+
230+
for (uint8_t i = 0; i < EXTERNAL_FLASH_DEVICE_COUNT; i++) {
231+
const external_flash_device* possible_device = &possible_devices[i];
232+
if (jedec_id_response[0] == possible_device->manufacturer_id &&
233+
jedec_id_response[1] == possible_device->memory_type &&
234+
jedec_id_response[2] == possible_device->capacity) {
206235
flash_device = possible_device;
207236
break;
237+
}
208238
}
209-
#else
210-
// The response will be 0xff if the flash needs more time to start up.
211-
uint8_t jedec_id_response[3] = {0xff, 0xff, 0xff};
212-
while (jedec_id_response[0] == 0xff) {
213-
spi_flash_read_command(CMD_READ_JEDEC_ID, jedec_id_response, 3);
214-
}
239+
#endif
215240

216241
for (uint8_t i = 0; i < EXTERNAL_FLASH_DEVICE_COUNT; i++) {
217242
const external_flash_device* possible_device = &possible_devices[i];
@@ -227,20 +252,23 @@ void supervisor_flash_init(void) {
227252
return;
228253
}
229254

230-
// We don't know what state the flash is in so wait for any remaining writes and then reset.
231-
uint8_t read_status_response[1] = {0x00};
232-
// The write in progress bit should be low.
233-
do {
234-
spi_flash_read_command(CMD_READ_STATUS, read_status_response, 1);
235-
} while ((read_status_response[0] & 0x1) != 0);
236-
#ifndef EXTERNAL_FLASH_QSPI_SINGLE
255+
// We don't know what state the flash is in so wait for any remaining writes and then reset.
256+
uint8_t read_status_response[1] = {0x00};
257+
// The write in progress bit should be low.
258+
do {
259+
spi_flash_read_command(CMD_READ_STATUS, read_status_response, 1);
260+
} while ((read_status_response[0] & 0x1) != 0);
261+
262+
if (!(flash_device->no_reset_cmd)){
237263
// The suspended write/erase bit should be low.
238264
do {
239265
spi_flash_read_command(CMD_READ_STATUS2, read_status_response, 1);
240266
} while ((read_status_response[0] & 0x80) != 0);
267+
} else {
241268
spi_flash_command(CMD_ENABLE_RESET);
242269
spi_flash_command(CMD_RESET);
243-
#endif
270+
}
271+
244272
// Wait 30us for the reset
245273
common_hal_mcu_delay_us(30);
246274

0 commit comments

Comments
 (0)