Skip to content

Commit d825738

Browse files
committed
add qspi_disable()
1 parent 7854625 commit d825738

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

ports/nrf/supervisor/port.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
#include "common-hal/audiopwmio/PWMAudioOut.h"
6666
#endif
6767

68+
#if defined(MICROPY_QSPI_CS) && defined(MICROPY_QSPI_OFF_WHEN_SLEEP)
69+
extern void qspi_disable(void);
70+
#endif
71+
6872
static void power_warning_handler(void) {
6973
reset_into_safe_mode(BROWNOUT);
7074
}
@@ -296,17 +300,7 @@ void port_interrupt_after_ticks(uint32_t ticks) {
296300

297301
void port_sleep_until_interrupt(void) {
298302
#if defined(MICROPY_QSPI_CS) && defined(MICROPY_QSPI_OFF_WHEN_SLEEP)
299-
// Turn off QSPI when USB is disconnected
300-
if (NRF_QSPI->ENABLE && !(NRF_POWER->USBREGSTATUS & POWER_USBREGSTATUS_VBUSDETECT_Msk)) {
301-
// Keep CS high when QSPI is diabled
302-
nrf_gpio_cfg_output(MICROPY_QSPI_CS);
303-
nrf_gpio_pin_write(MICROPY_QSPI_CS, 1);
304-
305-
// Workaround to disable QSPI according to nRF52840 Revision 1 Errata V1.4 - 3.8
306-
NRF_QSPI->TASKS_DEACTIVATE = 1;
307-
*(volatile uint32_t *)0x40029054 = 1;
308-
NRF_QSPI->ENABLE = 0;
309-
}
303+
qspi_disable();
310304
#endif
311305

312306
// Clear the FPU interrupt because it can prevent us from sleeping.

ports/nrf/supervisor/qspi_flash.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@
3838
#include "supervisor/shared/external_flash/common_commands.h"
3939
#include "supervisor/shared/external_flash/qspi_flash.h"
4040

41+
// When USB is disconnected, disable QSPI in sleep mode to save energy
4142
#if defined(MICROPY_QSPI_OFF_WHEN_SLEEP)
42-
#define QSPI_ENABLE() qspi_enable()
43-
44-
static void qspi_enable(void)
43+
void qspi_enable(void)
4544
{
4645
if (NRF_QSPI->ENABLE) {
4746
return;
@@ -61,12 +60,32 @@ static void qspi_enable(void)
6160
} while (--remaining_attempts);
6261
}
6362

63+
void qspi_disable(void)
64+
{
65+
// Turn off QSPI when USB is disconnected
66+
if (NRF_QSPI->ENABLE && !(NRF_POWER->USBREGSTATUS & POWER_USBREGSTATUS_VBUSDETECT_Msk)) {
67+
// Keep CS high when QSPI is diabled
68+
nrf_gpio_cfg_output(MICROPY_QSPI_CS);
69+
nrf_gpio_pin_write(MICROPY_QSPI_CS, 1);
70+
71+
// Workaround to disable QSPI according to nRF52840 Revision 1 Errata V1.4 - 3.8
72+
NRF_QSPI->TASKS_DEACTIVATE = 1;
73+
*(volatile uint32_t *)0x40029054 = 1;
74+
NRF_QSPI->ENABLE = 0;
75+
}
76+
}
6477
#else
65-
#define QSPI_ENABLE() ((void)0)
78+
void qspi_enable(void)
79+
{
80+
}
81+
82+
void qspi_disable(void)
83+
{
84+
}
6685
#endif
6786

6887
bool spi_flash_command(uint8_t command) {
69-
QSPI_ENABLE();
88+
qspi_enable();
7089
nrf_qspi_cinstr_conf_t cinstr_cfg = {
7190
.opcode = command,
7291
.length = 1,
@@ -79,7 +98,7 @@ bool spi_flash_command(uint8_t command) {
7998
}
8099

81100
bool spi_flash_read_command(uint8_t command, uint8_t* response, uint32_t length) {
82-
QSPI_ENABLE();
101+
qspi_enable();
83102
nrf_qspi_cinstr_conf_t cinstr_cfg = {
84103
.opcode = command,
85104
.length = length + 1,
@@ -93,7 +112,7 @@ bool spi_flash_read_command(uint8_t command, uint8_t* response, uint32_t length)
93112
}
94113

95114
bool spi_flash_write_command(uint8_t command, uint8_t* data, uint32_t length) {
96-
QSPI_ENABLE();
115+
qspi_enable();
97116
nrf_qspi_cinstr_conf_t cinstr_cfg = {
98117
.opcode = command,
99118
.length = length + 1,
@@ -106,23 +125,23 @@ bool spi_flash_write_command(uint8_t command, uint8_t* data, uint32_t length) {
106125
}
107126

108127
bool spi_flash_sector_command(uint8_t command, uint32_t address) {
109-
QSPI_ENABLE();
128+
qspi_enable();
110129
if (command != CMD_SECTOR_ERASE) {
111130
return false;
112131
}
113132
return nrfx_qspi_erase(NRF_QSPI_ERASE_LEN_4KB, address) == NRFX_SUCCESS;
114133
}
115134

116135
bool spi_flash_write_data(uint32_t address, uint8_t* data, uint32_t length) {
117-
QSPI_ENABLE();
136+
qspi_enable();
118137
// TODO: In theory, this also needs to handle unaligned data and
119138
// non-multiple-of-4 length. (in practice, I don't think the fat layer
120139
// generates such writes)
121140
return nrfx_qspi_write(data, length, address) == NRFX_SUCCESS;
122141
}
123142

124143
bool spi_flash_read_data(uint32_t address, uint8_t* data, uint32_t length) {
125-
QSPI_ENABLE();
144+
qspi_enable();
126145
int misaligned = ((intptr_t)data) & 3;
127146
// If the data is misaligned, we need to read 4 bytes
128147
// into an aligned buffer, and then copy 1, 2, or 3 bytes from the aligned

0 commit comments

Comments
 (0)