Skip to content

Commit 600e42a

Browse files
committed
pbio/drv/block_device: Use new os format.
The pbsys storage is split into an init thread and a deinit thread instead of having it wait the entire uptime. Makes it easier to follow and achieves smaller code size. Saves 500 bytes on Prime Hub, 150 on the hubs with the builtin flash storage (since fewer async functions needed changing). Also includes cleanups for block_device_w25qxx_stm32, which had a duplicate unused spi_status flag.
1 parent addd23f commit 600e42a

File tree

5 files changed

+188
-215
lines changed

5 files changed

+188
-215
lines changed

lib/pbio/drv/block_device/block_device_flash_stm32.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,33 @@ void pbdrv_block_device_init(void) {
2626

2727
extern uint8_t _pbdrv_block_device_storage_start[];
2828

29-
PT_THREAD(pbdrv_block_device_read(struct pt *pt, uint32_t offset, uint8_t *buffer, uint32_t size, pbio_error_t *err)) {
29+
pbio_error_t pbdrv_block_device_read(pbio_os_state_t *state, uint32_t offset, uint8_t *buffer, uint32_t size) {
3030

31-
PT_BEGIN(pt);
31+
// NB: This function is called as an awaitable for compatibility with other
32+
// external storage mediums. It blocks during the memcpy, but we only use
33+
// this at boot.
3234

3335
// Exit on invalid size.
3436
if (size == 0 || offset + size > PBDRV_CONFIG_BLOCK_DEVICE_FLASH_STM32_SIZE) {
35-
*err = PBIO_ERROR_INVALID_ARG;
36-
PT_EXIT(pt);
37+
return PBIO_ERROR_INVALID_ARG;
3738
}
3839

3940
// Copy requested data to RAM.
4041
memcpy(buffer, _pbdrv_block_device_storage_start + offset, size);
41-
*err = PBIO_SUCCESS;
4242

43-
PT_END(pt);
43+
return PBIO_SUCCESS;
4444
}
4545

4646
typedef union _double_word_t {
4747
uint8_t data[8];
4848
uint64_t dword;
4949
} double_word_t;
5050

51-
static pbio_error_t block_device_erase_and_write(uint8_t *buffer, uint32_t size) {
51+
pbio_error_t pbdrv_block_device_store(pbio_os_state_t *state, uint8_t *buffer, uint32_t size) {
52+
53+
// NB: This function is called as an awaitable for compatibility with other
54+
// external storage mediums. This implementation is blocking, but we only
55+
// use it during shutdown so this is acceptable.
5256

5357
static const uint32_t base_address = (uint32_t)(&_pbdrv_block_device_storage_start[0]);
5458

@@ -78,13 +82,13 @@ static pbio_error_t block_device_erase_and_write(uint8_t *buffer, uint32_t size)
7882
};
7983

8084
// Disable interrupts to avoid crash if reading while writing/erasing.
81-
uint32_t state = __get_PRIMASK();
85+
uint32_t irq = __get_PRIMASK();
8286
__disable_irq();
8387

8488
// Erase and re-enable interrupts.
8589
uint32_t page_error;
8690
hal_err = HAL_FLASHEx_Erase(&erase_init, &page_error);
87-
__set_PRIMASK(state);
91+
__set_PRIMASK(irq);
8892
if (hal_err != HAL_OK || page_error != 0xFFFFFFFFU) {
8993
HAL_FLASH_Lock();
9094
return PBIO_ERROR_IO;
@@ -95,12 +99,12 @@ static pbio_error_t block_device_erase_and_write(uint8_t *buffer, uint32_t size)
9599
while (done < size) {
96100

97101
// Disable interrupts while writing as above.
98-
state = __get_PRIMASK();
102+
irq = __get_PRIMASK();
99103
__disable_irq();
100104

101105
// Write the data and re-enable interrupts.
102106
hal_err = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, base_address + done, *(uint64_t *)(buffer + done));
103-
__set_PRIMASK(state);
107+
__set_PRIMASK(irq);
104108
if (hal_err != HAL_OK) {
105109
HAL_FLASH_Lock();
106110
return PBIO_ERROR_IO;
@@ -116,10 +120,4 @@ static pbio_error_t block_device_erase_and_write(uint8_t *buffer, uint32_t size)
116120
return PBIO_SUCCESS;
117121
}
118122

119-
PT_THREAD(pbdrv_block_device_store(struct pt *pt, uint8_t *buffer, uint32_t size, pbio_error_t *err)) {
120-
PT_BEGIN(pt);
121-
*err = block_device_erase_and_write(buffer, size);
122-
PT_END(pt);
123-
}
124-
125123
#endif // PBDRV_CONFIG_BLOCK_DEVICE_FLASH_STM32

lib/pbio/drv/block_device/block_device_test.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,21 @@ void pbdrv_block_device_init(void) {
8181
memcpy(&blockdev.program_data[0], _program_data, sizeof(_program_data));
8282
}
8383

84-
PT_THREAD(pbdrv_block_device_read(struct pt *pt, uint32_t offset, uint8_t *buffer, uint32_t size, pbio_error_t *err)) {
85-
86-
PT_BEGIN(pt);
84+
pbio_error_t pbdrv_block_device_read(pbio_os_state_t *state, uint32_t offset, uint8_t *buffer, uint32_t size) {
8785

8886
// Exit on invalid size.
8987
if (size == 0 || offset + size > PBDRV_CONFIG_BLOCK_DEVICE_TEST_SIZE) {
90-
*err = PBIO_ERROR_INVALID_ARG;
91-
PT_EXIT(pt);
88+
return PBIO_ERROR_INVALID_ARG;
9289
}
9390

9491
// Copy requested data to RAM.
9592
memcpy(buffer, (uint8_t *)&blockdev + offset, size);
96-
*err = PBIO_SUCCESS;
97-
98-
PT_END(pt);
93+
return PBIO_SUCCESS;
9994
}
10095

10196
// Don't store any data in this implementation.
102-
PT_THREAD(pbdrv_block_device_store(struct pt *pt, uint8_t *buffer, uint32_t size, pbio_error_t *err)) {
103-
PT_BEGIN(pt);
104-
*err = PBIO_SUCCESS;
105-
PT_END(pt);
97+
pbio_error_t pbdrv_block_device_store(pbio_os_state_t *state, uint8_t *buffer, uint32_t size) {
98+
return PBIO_ERROR_NOT_IMPLEMENTED;
10699
}
107100

108-
#endif // PBDRV_CONFIG_BLOCK_DEVICE_FLASH_STM32
101+
#endif // PBDRV_CONFIG_BLOCK_DEVICE_TEST

0 commit comments

Comments
 (0)