Skip to content

Fix nrf internal flash #1338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 34 additions & 96 deletions ports/atmel-samd/supervisor/internal_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ uint32_t supervisor_flash_get_block_size(void) {
}

uint32_t supervisor_flash_get_block_count(void) {
return INTERNAL_FLASH_PART1_START_BLOCK + INTERNAL_FLASH_PART1_NUM_BLOCKS;
return INTERNAL_FLASH_PART1_NUM_BLOCKS;
}

void supervisor_flash_flush(void) {
Expand All @@ -80,116 +80,54 @@ void flash_flush(void) {
supervisor_flash_flush();
}

static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_block, uint32_t num_blocks) {
buf[0] = boot;

if (num_blocks == 0) {
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
} else {
buf[1] = 0xff;
buf[2] = 0xff;
buf[3] = 0xff;
}

buf[4] = type;

if (num_blocks == 0) {
buf[5] = 0;
buf[6] = 0;
buf[7] = 0;
} else {
buf[5] = 0xff;
buf[6] = 0xff;
buf[7] = 0xff;
}

buf[8] = start_block;
buf[9] = start_block >> 8;
buf[10] = start_block >> 16;
buf[11] = start_block >> 24;

buf[12] = num_blocks;
buf[13] = num_blocks >> 8;
buf[14] = num_blocks >> 16;
buf[15] = num_blocks >> 24;
}

static int32_t convert_block_to_flash_addr(uint32_t block) {
if (INTERNAL_FLASH_PART1_START_BLOCK <= block && block < INTERNAL_FLASH_PART1_START_BLOCK + INTERNAL_FLASH_PART1_NUM_BLOCKS) {
if (0 <= block && block < INTERNAL_FLASH_PART1_NUM_BLOCKS) {
// a block in partition 1
block -= INTERNAL_FLASH_PART1_START_BLOCK;
return INTERNAL_FLASH_MEM_SEG1_START_ADDR + block * FILESYSTEM_BLOCK_SIZE;
}
// bad block
return -1;
}

bool supervisor_flash_read_block(uint8_t *dest, uint32_t block) {
if (block == 0) {
// fake the MBR so we can decide on our own partition table

for (int i = 0; i < 446; i++) {
dest[i] = 0;
}

build_partition(dest + 446, 0, 0x01 /* FAT12 */, INTERNAL_FLASH_PART1_START_BLOCK, INTERNAL_FLASH_PART1_NUM_BLOCKS);
build_partition(dest + 462, 0, 0, 0, 0);
build_partition(dest + 478, 0, 0, 0, 0);
build_partition(dest + 494, 0, 0, 0, 0);

dest[510] = 0x55;
dest[511] = 0xaa;

return true;

} else {
// non-MBR block, get data from flash memory
int32_t src = convert_block_to_flash_addr(block);
if (src == -1) {
// bad block number
return false;
}
int32_t error_code = flash_read(&supervisor_flash_desc, src, dest, FILESYSTEM_BLOCK_SIZE);
return error_code == ERR_NONE;
// non-MBR block, get data from flash memory
int32_t src = convert_block_to_flash_addr(block);
if (src == -1) {
// bad block number
return false;
}
int32_t error_code = flash_read(&supervisor_flash_desc, src, dest, FILESYSTEM_BLOCK_SIZE);
return error_code == ERR_NONE;
}

bool supervisor_flash_write_block(const uint8_t *src, uint32_t block) {
if (block == 0) {
// can't write MBR, but pretend we did
return true;

} else {
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
#endif
temp_status_color(ACTIVE_WRITE);
// non-MBR block, copy to cache
int32_t dest = convert_block_to_flash_addr(block);
if (dest == -1) {
// bad block number
return false;
}
int32_t error_code;
error_code = flash_erase(&supervisor_flash_desc,
dest,
FILESYSTEM_BLOCK_SIZE / flash_get_page_size(&supervisor_flash_desc));
if (error_code != ERR_NONE) {
return false;
}
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
#endif
temp_status_color(ACTIVE_WRITE);
// non-MBR block, copy to cache
int32_t dest = convert_block_to_flash_addr(block);
if (dest == -1) {
// bad block number
return false;
}
int32_t error_code;
error_code = flash_erase(&supervisor_flash_desc,
dest,
FILESYSTEM_BLOCK_SIZE / flash_get_page_size(&supervisor_flash_desc));
if (error_code != ERR_NONE) {
return false;
}

error_code = flash_append(&supervisor_flash_desc, dest, src, FILESYSTEM_BLOCK_SIZE);
if (error_code != ERR_NONE) {
return false;
}
clear_temp_status();
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
#endif
return true;
error_code = flash_append(&supervisor_flash_desc, dest, src, FILESYSTEM_BLOCK_SIZE);
if (error_code != ERR_NONE) {
return false;
}
clear_temp_status();
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
#endif
return true;
}

mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
Expand Down
1 change: 0 additions & 1 deletion ports/atmel-samd/supervisor/internal_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#endif

#define INTERNAL_FLASH_MEM_SEG1_START_ADDR (FLASH_SIZE - TOTAL_INTERNAL_FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE)
#define INTERNAL_FLASH_PART1_START_BLOCK (0x1)
#define INTERNAL_FLASH_PART1_NUM_BLOCKS (TOTAL_INTERNAL_FLASH_SIZE / FILESYSTEM_BLOCK_SIZE)

#define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms
Expand Down
2 changes: 0 additions & 2 deletions ports/nrf/supervisor/internal_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@

#include "py/mpconfig.h"

#define FLASH_ROOT_POINTERS

#define FLASH_PAGE_SIZE 0x1000
#define CIRCUITPY_INTERNAL_NVM_SIZE 0

Expand Down
Loading