Skip to content

Commit 7e2a3bf

Browse files
authored
Merge pull request adafruit#1338 from tannewt/fix_nrf_internal_flash
Fix nrf internal flash
2 parents 9be61fe + b67c53e commit 7e2a3bf

File tree

6 files changed

+181
-238
lines changed

6 files changed

+181
-238
lines changed

ports/atmel-samd/supervisor/internal_flash.c

Lines changed: 34 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ uint32_t supervisor_flash_get_block_size(void) {
7070
}
7171

7272
uint32_t supervisor_flash_get_block_count(void) {
73-
return INTERNAL_FLASH_PART1_START_BLOCK + INTERNAL_FLASH_PART1_NUM_BLOCKS;
73+
return INTERNAL_FLASH_PART1_NUM_BLOCKS;
7474
}
7575

7676
void supervisor_flash_flush(void) {
@@ -80,116 +80,54 @@ void flash_flush(void) {
8080
supervisor_flash_flush();
8181
}
8282

83-
static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_block, uint32_t num_blocks) {
84-
buf[0] = boot;
85-
86-
if (num_blocks == 0) {
87-
buf[1] = 0;
88-
buf[2] = 0;
89-
buf[3] = 0;
90-
} else {
91-
buf[1] = 0xff;
92-
buf[2] = 0xff;
93-
buf[3] = 0xff;
94-
}
95-
96-
buf[4] = type;
97-
98-
if (num_blocks == 0) {
99-
buf[5] = 0;
100-
buf[6] = 0;
101-
buf[7] = 0;
102-
} else {
103-
buf[5] = 0xff;
104-
buf[6] = 0xff;
105-
buf[7] = 0xff;
106-
}
107-
108-
buf[8] = start_block;
109-
buf[9] = start_block >> 8;
110-
buf[10] = start_block >> 16;
111-
buf[11] = start_block >> 24;
112-
113-
buf[12] = num_blocks;
114-
buf[13] = num_blocks >> 8;
115-
buf[14] = num_blocks >> 16;
116-
buf[15] = num_blocks >> 24;
117-
}
118-
11983
static int32_t convert_block_to_flash_addr(uint32_t block) {
120-
if (INTERNAL_FLASH_PART1_START_BLOCK <= block && block < INTERNAL_FLASH_PART1_START_BLOCK + INTERNAL_FLASH_PART1_NUM_BLOCKS) {
84+
if (0 <= block && block < INTERNAL_FLASH_PART1_NUM_BLOCKS) {
12185
// a block in partition 1
122-
block -= INTERNAL_FLASH_PART1_START_BLOCK;
12386
return INTERNAL_FLASH_MEM_SEG1_START_ADDR + block * FILESYSTEM_BLOCK_SIZE;
12487
}
12588
// bad block
12689
return -1;
12790
}
12891

12992
bool supervisor_flash_read_block(uint8_t *dest, uint32_t block) {
130-
if (block == 0) {
131-
// fake the MBR so we can decide on our own partition table
132-
133-
for (int i = 0; i < 446; i++) {
134-
dest[i] = 0;
135-
}
136-
137-
build_partition(dest + 446, 0, 0x01 /* FAT12 */, INTERNAL_FLASH_PART1_START_BLOCK, INTERNAL_FLASH_PART1_NUM_BLOCKS);
138-
build_partition(dest + 462, 0, 0, 0, 0);
139-
build_partition(dest + 478, 0, 0, 0, 0);
140-
build_partition(dest + 494, 0, 0, 0, 0);
141-
142-
dest[510] = 0x55;
143-
dest[511] = 0xaa;
144-
145-
return true;
146-
147-
} else {
148-
// non-MBR block, get data from flash memory
149-
int32_t src = convert_block_to_flash_addr(block);
150-
if (src == -1) {
151-
// bad block number
152-
return false;
153-
}
154-
int32_t error_code = flash_read(&supervisor_flash_desc, src, dest, FILESYSTEM_BLOCK_SIZE);
155-
return error_code == ERR_NONE;
93+
// non-MBR block, get data from flash memory
94+
int32_t src = convert_block_to_flash_addr(block);
95+
if (src == -1) {
96+
// bad block number
97+
return false;
15698
}
99+
int32_t error_code = flash_read(&supervisor_flash_desc, src, dest, FILESYSTEM_BLOCK_SIZE);
100+
return error_code == ERR_NONE;
157101
}
158102

159103
bool supervisor_flash_write_block(const uint8_t *src, uint32_t block) {
160-
if (block == 0) {
161-
// can't write MBR, but pretend we did
162-
return true;
163-
164-
} else {
165-
#ifdef MICROPY_HW_LED_MSC
166-
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
167-
#endif
168-
temp_status_color(ACTIVE_WRITE);
169-
// non-MBR block, copy to cache
170-
int32_t dest = convert_block_to_flash_addr(block);
171-
if (dest == -1) {
172-
// bad block number
173-
return false;
174-
}
175-
int32_t error_code;
176-
error_code = flash_erase(&supervisor_flash_desc,
177-
dest,
178-
FILESYSTEM_BLOCK_SIZE / flash_get_page_size(&supervisor_flash_desc));
179-
if (error_code != ERR_NONE) {
180-
return false;
181-
}
104+
#ifdef MICROPY_HW_LED_MSC
105+
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
106+
#endif
107+
temp_status_color(ACTIVE_WRITE);
108+
// non-MBR block, copy to cache
109+
int32_t dest = convert_block_to_flash_addr(block);
110+
if (dest == -1) {
111+
// bad block number
112+
return false;
113+
}
114+
int32_t error_code;
115+
error_code = flash_erase(&supervisor_flash_desc,
116+
dest,
117+
FILESYSTEM_BLOCK_SIZE / flash_get_page_size(&supervisor_flash_desc));
118+
if (error_code != ERR_NONE) {
119+
return false;
120+
}
182121

183-
error_code = flash_append(&supervisor_flash_desc, dest, src, FILESYSTEM_BLOCK_SIZE);
184-
if (error_code != ERR_NONE) {
185-
return false;
186-
}
187-
clear_temp_status();
188-
#ifdef MICROPY_HW_LED_MSC
189-
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
190-
#endif
191-
return true;
122+
error_code = flash_append(&supervisor_flash_desc, dest, src, FILESYSTEM_BLOCK_SIZE);
123+
if (error_code != ERR_NONE) {
124+
return false;
192125
}
126+
clear_temp_status();
127+
#ifdef MICROPY_HW_LED_MSC
128+
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
129+
#endif
130+
return true;
193131
}
194132

195133
mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {

ports/atmel-samd/supervisor/internal_flash.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#endif
4242

4343
#define INTERNAL_FLASH_MEM_SEG1_START_ADDR (FLASH_SIZE - TOTAL_INTERNAL_FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE)
44-
#define INTERNAL_FLASH_PART1_START_BLOCK (0x1)
4544
#define INTERNAL_FLASH_PART1_NUM_BLOCKS (TOTAL_INTERNAL_FLASH_SIZE / FILESYSTEM_BLOCK_SIZE)
4645

4746
#define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms

ports/nrf/supervisor/internal_flash.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
#include "py/mpconfig.h"
3333

34-
#define FLASH_ROOT_POINTERS
35-
3634
#define FLASH_PAGE_SIZE 0x1000
3735
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
3836

0 commit comments

Comments
 (0)