@@ -26,29 +26,33 @@ void pbdrv_block_device_init(void) {
2626
2727extern 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
4646typedef 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
0 commit comments