Skip to content

Commit af1291e

Browse files
committed
dynamically enable or disable QSPI by default
1 parent d825738 commit af1291e

File tree

3 files changed

+18
-29
lines changed

3 files changed

+18
-29
lines changed

ports/nrf/boards/makerdiary_m60_keyboard/mpconfigboard.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#define MICROPY_HW_BOARD_NAME "Makerdiary M60 Keyboard"
3131
#define MICROPY_HW_MCU_NAME "nRF52840"
3232

33-
// not use the RGB LEDs to save energy
33+
// RGB LEDs use PWM peripheral, avoid using them to save energy
3434
// #define CP_RGB_STATUS_R (&pin_P0_30)
3535
// #define CP_RGB_STATUS_G (&pin_P0_29)
3636
// #define CP_RGB_STATUS_B (&pin_P0_31)
@@ -41,7 +41,6 @@
4141
#define MICROPY_QSPI_DATA3 NRF_GPIO_PIN_MAP(1, 12)
4242
#define MICROPY_QSPI_SCK NRF_GPIO_PIN_MAP(1, 11)
4343
#define MICROPY_QSPI_CS NRF_GPIO_PIN_MAP(1, 13)
44-
#define MICROPY_QSPI_OFF_WHEN_SLEEP
4544

4645
#define BOARD_HAS_CRYSTAL 1
4746

ports/nrf/supervisor/port.c

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

68-
#if defined(MICROPY_QSPI_CS) && defined(MICROPY_QSPI_OFF_WHEN_SLEEP)
68+
#if defined(MICROPY_QSPI_CS)
6969
extern void qspi_disable(void);
7070
#endif
7171

@@ -299,7 +299,7 @@ void port_interrupt_after_ticks(uint32_t ticks) {
299299
}
300300

301301
void port_sleep_until_interrupt(void) {
302-
#if defined(MICROPY_QSPI_CS) && defined(MICROPY_QSPI_OFF_WHEN_SLEEP)
302+
#if defined(MICROPY_QSPI_CS)
303303
qspi_disable();
304304
#endif
305305

ports/nrf/supervisor/qspi_flash.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,21 @@
3939
#include "supervisor/shared/external_flash/qspi_flash.h"
4040

4141
// When USB is disconnected, disable QSPI in sleep mode to save energy
42-
#if defined(MICROPY_QSPI_OFF_WHEN_SLEEP)
42+
void qspi_disable(void)
43+
{
44+
// If VBUS is detected, no need to disable QSPI
45+
if (NRF_QSPI->ENABLE && !(NRF_POWER->USBREGSTATUS & POWER_USBREGSTATUS_VBUSDETECT_Msk)) {
46+
// Keep CS high when QSPI is diabled
47+
nrf_gpio_cfg_output(MICROPY_QSPI_CS);
48+
nrf_gpio_pin_write(MICROPY_QSPI_CS, 1);
49+
50+
// Workaround to disable QSPI according to nRF52840 Revision 1 Errata V1.4 - 3.8
51+
NRF_QSPI->TASKS_DEACTIVATE = 1;
52+
*(volatile uint32_t *)0x40029054 = 1;
53+
NRF_QSPI->ENABLE = 0;
54+
}
55+
}
56+
4357
void qspi_enable(void)
4458
{
4559
if (NRF_QSPI->ENABLE) {
@@ -60,30 +74,6 @@ void qspi_enable(void)
6074
} while (--remaining_attempts);
6175
}
6276

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-
}
77-
#else
78-
void qspi_enable(void)
79-
{
80-
}
81-
82-
void qspi_disable(void)
83-
{
84-
}
85-
#endif
86-
8777
bool spi_flash_command(uint8_t command) {
8878
qspi_enable();
8979
nrf_qspi_cinstr_conf_t cinstr_cfg = {

0 commit comments

Comments
 (0)