@@ -42,10 +42,12 @@ const external_flash_device *flash_device;
42
42
uint32_t spi_flash_baudrate ;
43
43
44
44
// Enable the flash over SPI.
45
- static void flash_enable (void ) {
46
- while (!common_hal_busio_spi_try_lock (& supervisor_flash_spi_bus )) {
45
+ static bool flash_enable (void ) {
46
+ if (common_hal_busio_spi_try_lock (& supervisor_flash_spi_bus )) {
47
+ common_hal_digitalio_digitalinout_set_value (& cs_pin , false);
48
+ return true;
47
49
}
48
- common_hal_digitalio_digitalinout_set_value ( & cs_pin , false) ;
50
+ return false;
49
51
}
50
52
51
53
// Disable the flash over SPI.
@@ -54,8 +56,10 @@ static void flash_disable(void) {
54
56
common_hal_busio_spi_unlock (& supervisor_flash_spi_bus );
55
57
}
56
58
57
- static bool transfer (uint8_t * command , uint32_t command_length , uint8_t * data_in , uint8_t * data_out , uint32_t data_length ) {
58
- flash_enable ();
59
+ static bool transfer (uint8_t * command , uint32_t command_length , uint8_t * data_in , uint8_t * data_out , uint32_t data_length ) {
60
+ if (!flash_enable ()) {
61
+ return false;
62
+ }
59
63
bool status = common_hal_busio_spi_write (& supervisor_flash_spi_bus , command , command_length );
60
64
if (status ) {
61
65
if (data_in != NULL && data_out != NULL ) {
@@ -103,7 +107,9 @@ bool spi_flash_write_data(uint32_t address, uint8_t *data, uint32_t data_length)
103
107
uint8_t request [4 ] = {CMD_PAGE_PROGRAM , 0x00 , 0x00 , 0x00 };
104
108
// Write the SPI flash write address into the bytes following the command byte.
105
109
address_to_bytes (address , request + 1 );
106
- flash_enable ();
110
+ if (!flash_enable ()) {
111
+ return false;
112
+ }
107
113
common_hal_busio_spi_configure (& supervisor_flash_spi_bus , spi_flash_baudrate , 0 , 0 , 8 );
108
114
bool status = common_hal_busio_spi_write (& supervisor_flash_spi_bus , request , 4 );
109
115
if (status ) {
@@ -120,9 +126,11 @@ bool spi_flash_read_data(uint32_t address, uint8_t *data, uint32_t data_length)
120
126
request [0 ] = CMD_FAST_READ_DATA ;
121
127
command_length = 5 ;
122
128
}
123
- // Write the SPI flash write address into the bytes following the command byte.
129
+ // Write the SPI flash read address into the bytes following the command byte.
124
130
address_to_bytes (address , request + 1 );
125
- flash_enable ();
131
+ if (!flash_enable ()) {
132
+ return false;
133
+ }
126
134
common_hal_busio_spi_configure (& supervisor_flash_spi_bus , spi_flash_baudrate , 0 , 0 , 8 );
127
135
bool status = common_hal_busio_spi_write (& supervisor_flash_spi_bus , request , command_length );
128
136
if (status ) {
0 commit comments