38
38
#include "supervisor/shared/external_flash/common_commands.h"
39
39
#include "supervisor/shared/external_flash/qspi_flash.h"
40
40
41
+ #if defined(MICROPY_QSPI_OFF_WHEN_SLEEP )
42
+ #define QSPI_ENABLE qspi_enable
43
+
44
+ static void qspi_enable (void )
45
+ {
46
+ if (NRF_QSPI -> ENABLE ) {
47
+ return ;
48
+ }
49
+
50
+ nrf_qspi_enable (NRF_QSPI );
51
+
52
+ nrf_qspi_event_clear (NRF_QSPI , NRF_QSPI_EVENT_READY );
53
+ nrf_qspi_task_trigger (NRF_QSPI , NRF_QSPI_TASK_ACTIVATE );
54
+
55
+ uint32_t remaining_attempts = 100 ;
56
+ do {
57
+ if (nrf_qspi_event_check (NRF_QSPI , NRF_QSPI_EVENT_READY )) {
58
+ break ;
59
+ }
60
+ NRFX_DELAY_US (10 );
61
+ } while (-- remaining_attempts );
62
+ }
63
+
64
+ #else
65
+ #define QSPI_ENABLE ()
66
+ #endif
67
+
41
68
bool spi_flash_command (uint8_t command ) {
69
+ QSPI_ENABLE ();
42
70
nrf_qspi_cinstr_conf_t cinstr_cfg = {
43
71
.opcode = command ,
44
72
.length = 1 ,
@@ -51,6 +79,7 @@ bool spi_flash_command(uint8_t command) {
51
79
}
52
80
53
81
bool spi_flash_read_command (uint8_t command , uint8_t * response , uint32_t length ) {
82
+ QSPI_ENABLE ();
54
83
nrf_qspi_cinstr_conf_t cinstr_cfg = {
55
84
.opcode = command ,
56
85
.length = length + 1 ,
@@ -64,6 +93,7 @@ bool spi_flash_read_command(uint8_t command, uint8_t* response, uint32_t length)
64
93
}
65
94
66
95
bool spi_flash_write_command (uint8_t command , uint8_t * data , uint32_t length ) {
96
+ QSPI_ENABLE ();
67
97
nrf_qspi_cinstr_conf_t cinstr_cfg = {
68
98
.opcode = command ,
69
99
.length = length + 1 ,
@@ -76,20 +106,23 @@ bool spi_flash_write_command(uint8_t command, uint8_t* data, uint32_t length) {
76
106
}
77
107
78
108
bool spi_flash_sector_command (uint8_t command , uint32_t address ) {
109
+ QSPI_ENABLE ();
79
110
if (command != CMD_SECTOR_ERASE ) {
80
111
return false;
81
112
}
82
113
return nrfx_qspi_erase (NRF_QSPI_ERASE_LEN_4KB , address ) == NRFX_SUCCESS ;
83
114
}
84
115
85
116
bool spi_flash_write_data (uint32_t address , uint8_t * data , uint32_t length ) {
117
+ QSPI_ENABLE ();
86
118
// TODO: In theory, this also needs to handle unaligned data and
87
119
// non-multiple-of-4 length. (in practice, I don't think the fat layer
88
120
// generates such writes)
89
121
return nrfx_qspi_write (data , length , address ) == NRFX_SUCCESS ;
90
122
}
91
123
92
124
bool spi_flash_read_data (uint32_t address , uint8_t * data , uint32_t length ) {
125
+ QSPI_ENABLE ();
93
126
int misaligned = ((intptr_t )data ) & 3 ;
94
127
// If the data is misaligned, we need to read 4 bytes
95
128
// into an aligned buffer, and then copy 1, 2, or 3 bytes from the aligned
0 commit comments