Skip to content

Commit

Permalink
drv/block_device_w25qxx_stm32: move size out of linker script
Browse files Browse the repository at this point in the history
This driver is for external flash, so it doesn't make sense to put
some of it's config in the linker script (which only deals with
internal flash). This moves the config from the linker script to the
platform.c file where the rest of the config already is.

Also changed a couple while loops to for loops to make the code more
idiomatic while we are touching this.
  • Loading branch information
dlech committed Oct 10, 2022
1 parent 2e97a74 commit 3d86e39
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 32 deletions.
1 change: 0 additions & 1 deletion bricks/debug/debug.ld
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ MEMORY
}

_stack_size = 8K;
_pbdrv_block_device_storage_size = 64K;
7 changes: 0 additions & 7 deletions bricks/essentialhub/essential_hub.ld
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,3 @@ MEMORY

"FW_INFO_OFFSET" = 0x200;
_stack_size = 16K;

/**
* External flash storage size for 1 application program. The full user program
* is loaded into RAM to run it, so the storage must not be bigger than RAM.
* We also need to allow some RAM for bss and heap, so set storage a bit lower.
*/
_pbdrv_block_device_storage_size = 256K;
7 changes: 0 additions & 7 deletions bricks/primehub/prime_hub.ld
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,3 @@ MEMORY
}

_stack_size = 16K;

/**
* External flash storage size for 1 application program. The full user program
* is loaded into RAM to run it, so the storage must not be bigger than RAM.
* We also need to allow some RAM for bss and heap, so set storage a bit lower.
*/
_pbdrv_block_device_storage_size = 256K;
20 changes: 6 additions & 14 deletions lib/pbio/drv/block_device/block_device_w25qxx_stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,11 @@ PT_THREAD(pbdrv_block_device_read(struct pt *pt, uint32_t offset, uint8_t *buffe
bdev.process = PROCESS_CURRENT();

// Split up reads to maximum chunk size.
size_done = 0;
while (size_done < size) {
for (size_done = 0; size_done < size; size_done += size_now) {
size_now = pbio_math_min(size - size_done, FLASH_SIZE_READ);

// Set address for this read request and send it.
set_address_be(&cmd_request_read.buffer[1], bdev.pdata->first_safe_write_address + offset + size_done);
set_address_be(&cmd_request_read.buffer[1], bdev.pdata->start_address + offset + size_done);
PT_SPAWN(pt, &child, spi_command_thread(&child, &cmd_request_read, err));
if (*err != PBIO_SUCCESS) {
goto out;
Expand All @@ -391,8 +390,6 @@ PT_THREAD(pbdrv_block_device_read(struct pt *pt, uint32_t offset, uint8_t *buffe
if (*err != PBIO_SUCCESS) {
goto out;
}

size_done += size_now;
}

out:
Expand Down Expand Up @@ -486,22 +483,20 @@ PT_THREAD(pbdrv_block_device_store(struct pt *pt, uint8_t *buffer, uint32_t size
for (offset = 0; offset < size; offset += FLASH_SIZE_ERASE) {
// Writing size 0 means erase.
PT_SPAWN(pt, &child, flash_erase_or_write(&child,
bdev.pdata->first_safe_write_address + offset, NULL, 0, err));
bdev.pdata->start_address + offset, NULL, 0, err));
if (*err != PBIO_SUCCESS) {
goto out;
}
}

// Write page by page.
size_done = 0;
while (size_done < size) {
for (size_done = 0; size_done < size; size_done += size_now) {
size_now = pbio_math_min(size - size_done, FLASH_SIZE_WRITE);
PT_SPAWN(pt, &child, flash_erase_or_write(&child,
bdev.pdata->first_safe_write_address + size_done, buffer + size_done, size_now, err));
bdev.pdata->start_address + size_done, buffer + size_done, size_now, err));
if (*err != PBIO_SUCCESS) {
goto out;
}
size_done += size_now;
}

out:
Expand All @@ -511,10 +506,7 @@ PT_THREAD(pbdrv_block_device_store(struct pt *pt, uint8_t *buffer, uint32_t size
}

uint32_t pbdrv_block_device_get_size(void) {
// Defined in linker script. To get the numeric value assigned there, we
// need to read the address and cast to uint32_t.
extern uint32_t _pbdrv_block_device_storage_size;
return (uint32_t)(&_pbdrv_block_device_storage_size);
return bdev.pdata->end_address - bdev.pdata->start_address;
}

PROCESS(pbdrv_block_device_w25qxx_stm32_init_process, "w25qxx");
Expand Down
4 changes: 3 additions & 1 deletion lib/pbio/drv/block_device/block_device_w25qxx_stm32.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ typedef struct {
* this chip address. This can be used to protect the first region of flash
* if it is used to store things like bootloader information.
*/
uint32_t first_safe_write_address;
uint32_t start_address;
/** Ending address (exclusive) complementary to the start_address. */
uint32_t end_address;
} pbdrv_block_device_w25qxx_stm32_platform_data_t;

/**
Expand Down
7 changes: 6 additions & 1 deletion lib/pbio/platform/essential_hub/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,12 @@ const pbdrv_block_device_w25qxx_stm32_platform_data_t pbdrv_block_device_w25qxx_
.bank = GPIOB,
.pin = 12,
},
.first_safe_write_address = 512 * 1024,
// Carve out 256K from the reserved 1M area at the start of the flash.
// This avoids touching the file system, the area read by the LEGO
// bootloader and the area used by upstream MicroPython. Currently, this
// just needs to be big enough to back up the user program on shutdown.
.start_address = 512 * 1024,
.end_address = 768 * 1024,
};

// USB
Expand Down
7 changes: 6 additions & 1 deletion lib/pbio/platform/prime_hub/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,12 @@ const pbdrv_block_device_w25qxx_stm32_platform_data_t pbdrv_block_device_w25qxx_
.bank = GPIOB,
.pin = 12,
},
.first_safe_write_address = 512 * 1024,
// Carve out 256K from the reserved 1M area at the start of the flash.
// This avoids touching the file system, the area read by the LEGO
// bootloader and the area used by upstream MicroPython. Currently, this
// just needs to be big enough to back up the user program on shutdown.
.start_address = 512 * 1024,
.end_address = 768 * 1024,
};

// USB
Expand Down

0 comments on commit 3d86e39

Please sign in to comment.