Skip to content

Commit 048d943

Browse files
committed
samd/boards/SAMD21_XPLAINED_PRO: Use the SPI flash for the file system.
The initial settings did not support it. The change required to add a dedicated handling of the Adesto 1MByte flash of the XPLAINED PRO board, which does not support the sfdp feature. Fixes the ID check of the Adesto/Renesas 1MByte flash. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent 695a0b9 commit 048d943

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
#define MICROPY_HW_MCU_NAME "SAMD21J18A"
33

44
#define MICROPY_HW_XOSC32K (1)
5+
6+
#define MICROPY_HW_SPIFLASH (1)
7+
#define MICROPY_HW_SPIFLASH_ID (5)
8+
#define MICROPY_HW_SPIFLASH_BAUDRATE (12000000)

ports/samd/boards/SAMD21_XPLAINED_PRO/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ MCU_SERIES = SAMD21
22
CMSIS_MCU = SAMD21J18A
33
LD_FILES = boards/samd21x18a.ld sections.ld
44
TEXT0 = 0x2000
5+
6+
MICROPY_HW_CODESIZE ?= 248K

ports/samd/boards/SAMD21_XPLAINED_PRO/pins.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ USB_DP,PA25
5353
SWCLK,PA30
5454
SWDIO,PA31
5555

56+
FLASH_MOSI,PB22
57+
FLASH_MISO,PB16
58+
FLASH_SCK,PB23
59+
FLASH_CS,PA13

ports/samd/samd_spiflash.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const uint8_t _COMMANDS_32BIT[] = {0x13, 0x12, 0x21}; // READ, PROGRAM_PAGE, ER
4545

4646
#define COMMAND_JEDEC_ID (0x9F)
4747
#define COMMAND_READ_STATUS (0x05)
48+
#define COMMAND_WRITE_SR1 (0x01)
4849
#define COMMAND_WRITE_ENABLE (0x06)
4950
#define COMMAND_READ_SFDP (0x5A)
5051
#define PAGE_SIZE (256)
@@ -88,7 +89,7 @@ static void wait(spiflash_obj_t *self) {
8889
mp_hal_pin_write(self->cs, 0);
8990
spi_transfer((mp_obj_base_t *)self->spi, 2, msg, msg);
9091
mp_hal_pin_write(self->cs, 1);
91-
} while (msg[1] != 0 && timeout-- > 0);
92+
} while ((msg[1] & 1) != 0 && timeout-- > 0);
9293
}
9394

9495
static void get_id(spiflash_obj_t *self, uint8_t id[3]) {
@@ -123,6 +124,17 @@ static void write_enable(spiflash_obj_t *self) {
123124
mp_hal_pin_write(self->cs, 1);
124125
}
125126

127+
// Write status register 1
128+
static void write_sr1(spiflash_obj_t *self, uint8_t value) {
129+
uint8_t msg[2];
130+
msg[0] = COMMAND_WRITE_SR1;
131+
msg[1] = value;
132+
133+
mp_hal_pin_write(self->cs, 0);
134+
spi_transfer(self->spi, 2, msg, NULL);
135+
mp_hal_pin_write(self->cs, 1);
136+
}
137+
126138
static void get_sfdp(spiflash_obj_t *self, uint32_t addr, uint8_t *buffer, int size) {
127139
uint8_t dummy[1];
128140
write_addr(self, COMMAND_READ_SFDP, addr);
@@ -155,27 +167,36 @@ static mp_obj_t spiflash_make_new(const mp_obj_type_t *type, size_t n_args, size
155167
mp_hal_pin_write(self->cs, 1);
156168

157169
wait(self);
158-
159170
// Get the flash size from the device ID (default)
160171
uint8_t id[3];
161172
get_id(self, id);
173+
bool read_sfdp = true;
174+
162175
if (id[1] == 0x84 && id[2] == 1) { // Adesto
163176
self->size = 512 * 1024;
164-
} else if (id[1] == 0x1f && id[2] == 1) { // Atmel / Renesas
177+
} else if (id[0] == 0x1f && id[1] == 0x45 && id[2] == 1) { // Adesto/Renesas 8 MBit
165178
self->size = 1024 * 1024;
179+
read_sfdp = false;
180+
self->sectorsize = 4096;
181+
self->addr_is_32bit = false;
182+
// Globally unlock the sectors, which are locked after power on.
183+
write_enable(self);
184+
write_sr1(self, 0);
166185
} else {
167186
self->size = 1 << id[2];
168187
}
169188

170189
// Get the addr_is_32bit flag and the sector size
171-
uint8_t buffer[128];
172-
get_sfdp(self, 0, buffer, 16); // get the header
173-
int len = MIN(buffer[11] * 4, sizeof(buffer));
174-
if (len >= 29) {
175-
int addr = buffer[12] + (buffer[13] << 8) + (buffer[14] << 16);
176-
get_sfdp(self, addr, buffer, len); // Get the JEDEC mandatory table
177-
self->sectorsize = 1 << buffer[28];
178-
self->addr_is_32bit = ((buffer[2] >> 1) & 0x03) != 0;
190+
if (read_sfdp) {
191+
uint8_t buffer[128];
192+
get_sfdp(self, 0, buffer, 16); // get the header
193+
int len = MIN(buffer[11] * 4, sizeof(buffer));
194+
if (len >= 29) {
195+
int addr = buffer[12] + (buffer[13] << 8) + (buffer[14] << 16);
196+
get_sfdp(self, addr, buffer, len); // Get the JEDEC mandatory table
197+
self->sectorsize = 1 << buffer[28];
198+
self->addr_is_32bit = ((buffer[2] >> 1) & 0x03) != 0;
199+
}
179200
}
180201
self->commands = self->addr_is_32bit ? _COMMANDS_32BIT : _COMMANDS_24BIT;
181202

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy