Skip to content

Commit

Permalink
Update wake from deep sleep handling
Browse files Browse the repository at this point in the history
-Change wake up when the reset button is pressed instead of released
-Add hook in bootloader project to bypass reset button check to jump directly to the interface
-Reset the nRF whenever we wakeup from the reset button independently of the press duration
  • Loading branch information
gerargz committed Sep 4, 2020
1 parent 41a5bef commit e8480a6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
16 changes: 16 additions & 0 deletions source/board/kl27z_microbit_bl.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "compiler.h"
#include "target_board.h"
#include "target_family.h"
#include "fsl_device_registers.h"
#include "gpio.h"

// Warning - changing the interface start will break backwards compatibility
COMPILER_ASSERT(DAPLINK_ROM_IF_START == KB(32));
Expand Down Expand Up @@ -62,3 +64,17 @@ const board_info_t g_board_info = {
.daplink_target_url = "https://microbit.org/device/?id=@B&v=@V&bl=1",
.target_cfg = &target_device,
};

bool reset_button_pressed()
{
bool btn_pressed = false;
// Bypass button check if we are waking from Low Leakage Wakeup Reset
if (RCM->SRS0 & RCM_SRS0_WAKEUP_MASK) {
btn_pressed = false;
}
else {
btn_pressed = gpio_get_reset_btn();
}

return btn_pressed;
}
4 changes: 2 additions & 2 deletions source/board/microbitv2/IO_Config_Override.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ COMPILER_ASSERT(DAPLINK_HIC_ID == DAPLINK_HIC_ID_KL27Z);
#define SW_RESET_PRESSED (0)
#define SW_RESET_NOT_PRESSED (1)
#define PIN_SW_RESET_LLWU_PIN (14)
#define PIN_SW_RESET_LLWU_WAKEUP_TYPE kLLWU_ExternalPinRisingEdge
#define PIN_SW_RESET_PORT_WAKEUP_TYPE kPORT_InterruptRisingEdge
#define PIN_SW_RESET_LLWU_WAKEUP_TYPE kLLWU_ExternalPinFallingEdge
#define PIN_SW_RESET_PORT_WAKEUP_TYPE kPORT_InterruptFallingEdge

// WAKE_ON_EDGE PTC4
#define PIN_WAKE_ON_EDGE_PORT PORTC
Expand Down
5 changes: 4 additions & 1 deletion source/board/microbitv2/microbitv2.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const char * const board_id_mb_2_1 = "9904";
uint16_t board_id_hex_min = 0x9903;
uint16_t board_id_hex = 0;

volatile uint8_t wake_from_reset = 0;

typedef enum {
BOARD_VERSION_2_0 = 0,
BOARD_VERSION_2_1 = 1,
Expand Down Expand Up @@ -262,7 +264,7 @@ void handle_reset_button()
static uint8_t reset_pressed = 0;

// handle reset button without eventing
if (!reset_pressed && gpio_get_reset_btn_fwrd()) {
if (!reset_pressed && (gpio_get_reset_btn_fwrd() || wake_from_reset)) {
#ifdef DRAG_N_DROP_SUPPORT
if (!flash_intf_target->flash_busy()) //added checking if flashing on target is in progress
#endif
Expand All @@ -271,6 +273,7 @@ void handle_reset_button()
target_set_state(RESET_HOLD);
reset_pressed = 1;
gpio_reset_count = 0;
wake_from_reset = 0;
main_shutdown_state = MAIN_LED_FULL_BRIGHTNESS;
}
} else if (reset_pressed && !gpio_get_reset_btn_fwrd()) {
Expand Down
7 changes: 3 additions & 4 deletions source/board/microbitv2/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static void power_pre_switch_hook(smc_power_state_t originPowerState, app_power_
static void power_post_switch_hook(smc_power_state_t originPowerState, app_power_mode_t targetMode);
static void power_enter_mode(app_power_mode_t targetPowerMode);

extern volatile uint8_t wake_from_reset;

/*******************************************************************************
* Code
******************************************************************************/
Expand All @@ -31,14 +33,12 @@ void LLWU_IRQHandler(void)
/* If wakeup by external pin BTN_NOT_PRESSED. */
if (LLWU_GetExternalWakeupPinFlag(LLWU, PIN_SW_RESET_LLWU_PIN))
{
PORT_SetPinInterruptConfig(PIN_SW_RESET_PORT, PIN_SW_RESET_BIT, kPORT_InterruptOrDMADisabled);
PORT_ClearPinsInterruptFlags(PIN_SW_RESET_PORT, (1U << PIN_SW_RESET_BIT));
LLWU_ClearExternalWakeupPinFlag(LLWU, PIN_SW_RESET_LLWU_PIN);
wake_from_reset = 1;
}
/* If wakeup by external pin WAKE_ON_EDGE. */
if (LLWU_GetExternalWakeupPinFlag(LLWU, PIN_WAKE_ON_EDGE_LLWU_PIN))
{
PORT_ClearPinsInterruptFlags(PIN_WAKE_ON_EDGE_PORT, (1U << PIN_WAKE_ON_EDGE_BIT));
LLWU_ClearExternalWakeupPinFlag(LLWU, PIN_WAKE_ON_EDGE_LLWU_PIN);
}
}
Expand Down Expand Up @@ -71,7 +71,6 @@ void power_init(void)
if (kRCM_SourceWakeup & RCM_GetPreviousResetSources(RCM)) /* Wakeup from VLLS. */
{
PMC_ClearPeriphIOIsolationFlag(PMC);
NVIC_ClearPendingIRQ(LLWU_IRQn);
}

/* Enable rising edge interrupt on WAKE_ON_EDGE pin (VBUS falling edge) to detect USB detach */
Expand Down
7 changes: 6 additions & 1 deletion source/daplink/bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ static main_led_state_t msc_led_state = MAIN_LED_FLASH;
static main_usb_busy_t usb_busy;
static uint32_t usb_busy_count;

__WEAK bool reset_button_pressed()
{
return gpio_get_reset_btn();
}

// Timer task, set flags every 30mS and 90mS
void timer_task_30mS(void * arg)
{
Expand Down Expand Up @@ -236,7 +241,7 @@ int main(void)

// check for invalid app image or rst button press. Should be checksum or CRC but NVIC validation is better than nothing.
// If the interface has set the hold in bootloader setting don't jump to app
if (!gpio_get_reset_btn() && g_board_info.target_cfg && validate_bin_nvic((uint8_t *)g_board_info.target_cfg->flash_regions[0].start) && !config_ram_get_initial_hold_in_bl()) {
if (!reset_button_pressed() && g_board_info.target_cfg && validate_bin_nvic((uint8_t *)g_board_info.target_cfg->flash_regions[0].start) && !config_ram_get_initial_hold_in_bl()) {
// change to the new vector table
SCB->VTOR = g_board_info.target_cfg->flash_regions[0].start; //bootloaders should only have one flash region for interface
// modify stack pointer and start app
Expand Down

0 comments on commit e8480a6

Please sign in to comment.