Device Stuck in Sleep Mode (System Off) - nRF52840

Hardware: nRF52840 (Custom Hardware)

Software: nRF Connect SDK v2.6.1


I am using the nRF52840 with custom hardware, where the device operates in two modes: Sleep Mode (System Off) and Connect Mode. After programming, the device starts in Sleep Mode by default. When the button is pressed (long push), the device transitions to Connect Mode.

The problem arises when transitioning the device from Connect Mode back to Sleep Mode. Occasionally, when attempting to wake the device from Sleep Mode, it becomes unresponsive (stuck), and I am unable to wake it up. This issue occurs sporadically and is difficult to reproduce consistently.

The device does reset itself after the watchdog timer(10-min) triggers, allowing me to wake it up and return it to Connect Mode. Interestingly, the issue does not occur when the debugger is attached.

Here i attached my main code:

#include "nrf_log.h"
LOG_MODULE_REGISTER(MN, LOG_LEVEL_INF);

/************************************************************************************/
#define SW0_NODE   DT_ALIAS(sw0)
static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(SW0_NODE, gpios);

#define LED0_NODE1 	DT_ALIAS(led0)
static const struct gpio_dt_spec led_1 = GPIO_DT_SPEC_GET(LED0_NODE1, gpios);

#define LED0_NODE2 	DT_ALIAS(led1)
static const struct gpio_dt_spec led_2 = GPIO_DT_SPEC_GET(LED0_NODE2, gpios);


static bool _serviceMode = false;
static uint32_t _resetReason;

/***************************************************************************/
static void MNi_AppProtect(void)
{
#ifdef ENABLE_APPROTECT
	if ((NRF_UICR->APPROTECT & UICR_APPROTECT_PALL_Msk) !=
		(UICR_APPROTECT_PALL_Enabled << UICR_APPROTECT_PALL_Pos)) {

        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}

        NRF_UICR->APPROTECT = ((NRF_UICR->APPROTECT & ~((uint32_t)UICR_APPROTECT_PALL_Msk)) |
		    (UICR_APPROTECT_PALL_Enabled << UICR_APPROTECT_PALL_Pos));

        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}

		NVIC_SystemReset();
   	}
#else
	if ((NRF_UICR->APPROTECT & UICR_APPROTECT_PALL_Msk) !=
		(UICR_APPROTECT_PALL_HwDisabled << UICR_APPROTECT_PALL_Pos)) {

        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}

        NRF_UICR->APPROTECT = ((NRF_UICR->APPROTECT & ~((uint32_t)UICR_APPROTECT_PALL_Msk)) |
		    (UICR_APPROTECT_PALL_HwDisabled << UICR_APPROTECT_PALL_Pos));

        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
		
		NVIC_SystemReset();
	}
#endif
}

/***************************************************************************/
static void MNi_EnableDCDCPower(void)
{
    NRF_POWER->DCDCEN0 = true;
    NRF_POWER->DCDCEN = true;
}

/***************************************************************************/
void MNi_GetResetReason(void)
{
	_resetReason = NRF_POWER->RESETREAS;
	NRF_POWER->RESETREAS = 0xFFFFFFFF;
}

/***************************************************************************/
static int MNi_ServiceModeEntry(void)
{
	ret_code_t errCode = 0;

#if (CONFIG_BOARD_MINEW_MBM01_NRF52840 || CONFIG_BOARD_RAYTAC_MDBT50Q_NRF52840 )
    
    if (!device_is_ready(button0.port)) 
	{ 
		LOG_ERR("button0 port is not ready");
		return -EBUSY;
	}
    errCode = gpio_pin_configure_dt(&button0, GPIO_INPUT);
    if (errCode < 0) { return errCode;}	

	MNi_GetResetReason();
	LOG_INF("_resetReason:%d",_resetReason);

	if (_resetReason == 0)
	{
		if(gpio_pin_get_dt(&button0))
		{
			_serviceMode = true;
		}
	}
#endif
	return errCode;
}

/***************************************************************************/
int main(void)
{
	if (!device_is_ready(led_1.port)) 
	{
		LOG_ERR("led_1 port is not ready");
	}

	if (!device_is_ready(led_2.port)) 
	{
		LOG_ERR("led_2 port is not ready");
	}


	if (_serviceMode)
    {
		/* put device in service mode blue led on */
		gpio_pin_configure_dt(&led_2, GPIO_OUTPUT_ACTIVE);
        SM_InitServiceMode();
    }
	else
	{
		/* code start red led on for 250ms */
		gpio_pin_configure_dt(&led_1, GPIO_OUTPUT_ACTIVE);
		k_sleep(K_MSEC(250));
		gpio_pin_configure_dt(&led_1, GPIO_OUTPUT_INACTIVE);

		NRF_LOG_INFO("MT Extender (0x%04X)", GSC_OWN_MODEL_NUMBER);
		NRF_LOG_INFO("Build v%d.%d.%d-beta.%d", 
					CD_FW_MAJOR_VERSION, CD_FW_MINOR_VERSION, CD_FW_PATCH_VERSION, CD_FW_BETA_VERSION);
		NRF_LOG_INFO("Build time: " __DATE__ " " __TIME__ " ");	

		// MNi_AppProtect();
		WD_InitWatchdog();
    	MNi_EnableDCDCPower();
		AI_InitRadioDisableEvent();
		BH_InitBle();
		DH_InitFlashStorageModule();
		AR_InitAdvReportHandler();	
		SC_InitScanHandler();
		CSH_InitCentralServices();
		BSP_InitButtonsLeds();
		SM_InitStorageModehandler();
		PSH_InitPeripheralServices();
					
		if(GMH_GetDeviceMode() == GDM_DEVICE_MODE_STORAGE)
		{
			if(BSP_GetButton0PushStatus() == 1)
			{
				NRF_LOG_INFO("Button 0 Push");
				/* If we reach this point, the application was woken up by long press button. */
				ASM_InitStorageAdv();
				SM_ExitSleepMode();
			}
			else
			{
				NRF_LOG_INFO("Entering in standby mode...");

				/* Go into System-Off mode. Button presses will wake the chip up.
				Configure to generate PORT event (wakeup) on button 1 press. */
				nrf_gpio_cfg_input(NRF_DT_GPIOS_TO_PSEL(DT_ALIAS(sw0), gpios),
											NRF_GPIO_PIN_PULLUP);
				nrf_gpio_cfg_sense_set(NRF_DT_GPIOS_TO_PSEL(DT_ALIAS(sw0), gpios),
											NRF_GPIO_PIN_SENSE_LOW);

				/* Above we disabled entry to deep sleep based on duration of
				* controlled delay.  Here we need to override that, then
				* force entry to deep sleep on any delay.
				*/
				sys_poweroff();

			}
		}
		else
		{
			/* If we reach this point, the device is in connect mode */
			ACM_InitAdvData();
			ACM_UpdateAdvManufData();
		}
	
		SC_StartScan();

		for (;;) 
		{
			k_sleep(K_MSEC(1000));
		}
	}
}

SYS_INIT(MNi_ServiceModeEntry, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);

Debugging Attempts:
To troubleshoot, I added a 250ms LED blink at the start of the main code. During testing, I observed that on one occasion, the LED remained continuously on and the device was stuck. When I attached the debugger during this state, the following log was printed (see attached). However, this behavior only happened once.

In other instances, the LED blinks as expected, but the device remains unresponsive when trying to wake it up from Sleep Mode. In some cases, the LED doesn’t blink at all.

Attached Log:

00> *** Booting nRF Connect SDK v3.5.99-ncs1-1 ***
00> [00:00:00.501,647] <inf> MN: _resetReason:12
00> [00:00:01.002,197] <inf> MN: MT Extender (0x0043)
00> [00:00:01.002,563] <inf> MN: Build v1.3.0-beta.1
00> [00:00:01.002,960] <inf> MN: Build time: Aug 13 2024 14:03:20 
00> [00:00:01.011,016] <inf> fs_nvs: 2 Sectors of 4096 bytes
00> [00:00:01.011,413] <inf> fs_nvs: alloc wra: 0, fe8
00> [00:00:01.011,779] <inf> fs_nvs: data wra: 0, 0
00> [00:00:01.012,237] <inf> bt_sdc_hci_driver: SoftDevice Controller build revision: 
00>                                             36 f0 e5 0e 87 68 48 fb  02 fd 9f 82 cc 32 e5 7b |6....hH. .....2.{
00>                                             91 b1 5c ed                                      |..\.             
00> [00:00:01.016,143] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
00> [00:00:01.016,601] <inf> bt_hci_core: HW Variant: nRF52x (0x0002)
00> [00:00:01.016,998] <inf> bt_hci_core: Firmware: Standard Bluetooth controller (0x00) Version 54.58864 Build 1214809870
00> [00:00:01.018,280] <inf> bt_hci_core: No ID address. App must call settings_load()
00> [00:00:01.019,256] <inf> bt_hci_core: Identity: C0:EC:9F:6F:F9:48 (random)
00> [00:00:01.019,714] <inf> bt_hci_core: HCI: version 5.4 (0x0d) revision 0x118f, manufacturer 0x0059
00> [00:00:01.020,263] <inf> bt_hci_core: LMP: version 5.4 (0x0d) subver 0x118f
00> *** Booting nRF Connect SDK v3.5.99-ncs1-1 ***
00> [00:00:00.501,647] <inf> MN: _resetReason:4
00> [00:00:01.002,197] <inf> MN: MT Extender (0x0043)
00> [00:00:01.002,563] <inf> MN: Build v1.3.0-beta.1
00> [00:00:01.002,960] <inf> MN: Build time: Aug 13 2024 14:03:20 
00> [00:00:01.010,467] <inf> fs_nvs: 2 Sectors of 4096 bytes
00> [00:00:01.010,864] <inf> fs_nvs: alloc wra: 0, fd0
00> [00:00:01.011,230] <inf> fs_nvs: data wra: 0, 1c
00> [00:00:01.011,688] <inf> bt_sdc_hci_driver: SoftDevice Controller build revision: 
00>                                             36 f0 e5 0e 87 68 48 fb  02 fd 9f 82 cc 32 e5 7b |6....hH. .....2.{
00>                                             91 b1 5c ed                                      |..\.             
00> [00:00:01.015,625] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
00> [00:00:01.016,052] <inf> bt_hci_core: HW Variant: nRF52x (0x0002)
00> [00:00:01.016,479] <inf> bt_hci_core: Firmware: Standard Bluetooth controller (0x00) Version 54.58864 Build 1214809870
00> [00:00:01.017,761] <inf> bt_hci_core: No ID address. App must call settings_load()
00> [00:00:01.018,768] <inf> bt_hci_core: Identity: C0:EC:9F:6F:F9:48 (random)
00> [00:00:01.019,226] <inf> bt_hci_core: HCI: version 5.4 (0x0d) revision 0x118f, manufacturer 0x0059
00> [00:00:01.019,775] <inf> bt_hci_core: LMP: version 5.4 (0x0d) subver 0x118f
00> [00:00:01.060,485] <inf> fs_nvs: 3 Sectors of 4096 bytes
00> [00:00:01.060,882] <inf> fs_nvs: alloc wra: 0, fe8
00> [00:00:01.061,248] <inf> fs_nvs: data wra: 0, 0
00> [00:00:01.161,651] <inf> SC: SC_SetDefaultScanWindowPercentage
00> [00:00:01.162,811] <inf> SC: SC_SetDefaultScanWindowPercentage
00> [00:00:01.369,415] <inf> SC: SC_InitScanHandler
00> [00:00:01.371,520] <inf> BSP: BSP_GetButton0PushStatus: 0
00> [00:00:01.872,039] <inf> MN: Entering in standby mode...
00> *** Booting nRF Connect SDK v3.5.9ä´a: 0, 1c
00> [00:00:01.011,657] <inf> bt_sdc_hci_driver: SoftDevice Controller build revision: 
00>                                             36 f0 e5 0e 87 68 48 fb  02 fd 9f 82 cc 32 e5 7b |6....hH. .....2.{
00>                                             91 b1 5c ed                                      |..\.             
00> [00:00:01.015,594] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
00> [00:00:01.016,021] <inf> bt_hci_core: HW Variant: nRF52x (0x0002)
00> [00:00:01.016,448] <inf> bt_hci_core: Firmware: Standard Bluet[00:01:01.598,693] 1;313.0-beta.1
00> [00:00:01.002,929] <inf> MN: Build time: Aug 13 2024 14:03:20 
00> [00:00:01.010,437] <inf> fs_nvs: 2 Sectors of 4096 bytes
00> [00:00:01.010,833] <inf> fs_nvs: alloc wra: 0, fd0
00> [00:00:01.011,199] <inf> fs_nvs: data wra: 0, 1c
00> [00:00:01.011,657] <inf> bt_sdc_hci_driver: SoftDevice Controller build revision: 
00>                                             36 f0 e5 0e 87 68 48 fb  02 fd 9f 82 cc 32 e5 7b |6....hH. .....2.{
00>                                             91 b1 5c ed                                      |..\.             
00> [00:00:01.015,594] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
00> [00:00:01.016,021] <inf> bt_hci_core: HW Variant: nRF52x (0x0002)
00> [00:00:01.016,448] <inf> bt_hci_core: Firmware: Standard Bluet[00:01:01.598,693] <err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:02.038,513] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:02.448,364] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:02.857,910] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:03.267,852] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:03.677,429] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:04.086,914] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> 
00> [00:01:04.496,429] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> 
00> [00:01:04.905,822] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> 
00> [00:01:05.315,551] <err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:05.725,128] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> [0m
00> [00:01:06.306,732] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:07.266,510] <err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:08.913,238] <err> mpu: Failed to allocate new MPU region 248
00> [0m
00> [00:01:10.060,577] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> [0m
00> [00:01:10.470,153] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> [0m
00> [00:01:10.879,913] <err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:11.289,642] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> [0m
00> [00:01:11.699,157] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> [0m
00> [00:01:12.108,673] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> [0m
00> [00:01:12.518,249] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:12.928,009] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:13.337,371] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:13.747,100] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:14.156,890] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:14.566,406] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:14.976,074] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:15.385,742] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:15.795,471] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:16.205,017] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:16.614,868] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:17.024,353] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:17.434,051] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:17.843,566] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:18.252,929] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:18.662,719] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:19.072,052] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:19.481,567] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:19.891,052] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:20.300,720] <err> mpu: Failed to allocate new MPU region 248
00> 
00> 
00> [00:01:20.710,266] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> 
00> [00:01:21.119,842] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> [00:01:21.529,449] [1;31m<err> mpu: Failed to allocate new MPU region 248
00> 
00> 
00> [00:01:21.938,751] [1;31m<err
(Connection lost)
00> *** Booting nRF Connect SDK v3.5.99-ncs1-1 ***
00> [00:00:00.501,678] <inf> MN: _resetReason:0
00> [00:00:01.002,258] <inf> MN: MT Extender (0x0043)
00> [00:00:01.002,593] <inf> MN: Build v1.3.0-beta.1
00> [00:00:01.002,990] <inf> MN: Build time: Aug 13 2024 14:03:20 
00> [00:00:01.010,528] <inf> fs_nvs: 2 Sectors of 4096 bytes
00> [00:00:01.010,925] <inf> fs_nvs: alloc wra: 0, fd0
00> [00:00:01.011,291] <inf> fs_nvs: data wra: 0, 1c
00> [00:00:01.011,779] <inf> bt_sdc_hci_driver: SoftDevice Controller build revision: 
00>                                             36 f0 e5 0e 87 68 48 fb  02 fd 9f 82 cc 32 e5 7b |6....hH. .....2.{
00>                                             91 b1 5c ed                                      |..\.             
00> [00:00:01.015,716] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
00> [00:00:01.016,143] <inf> bt_hci_core: HW Variant: nRF52x (0x0002)
00> [00:00:01.016,571] <inf> bt_hci_core: Firmware: Standard Bluet[00:00:11.517,456] <inf> WD: WDT feed: Done
00> [00:00:11.517,822] <inf> SM: SMi_HandleSecondTimer:
00> [00:00:12.517,456] <inf> WD: WDT feed: Done
00> [00:00:12.517,791] <inf> SM: SMi_HandleSecondTimer:
00> [00:00:13.517,456] <inf> WD: WDT feed: Done
00> [00:00:13.517,791] <inf> SM: SMi_HandleSecondTimer:
00> [00:00:14.517,700] <inf> WD: WDT feed: Done
00> [00:00:14.518,035] <inf> SM: SMi_HandleSecondTimer:
(Connection lost)

Notes:
The issue is intermittent and difficult to reproduce consistently.
The issue cannot be reproduced when the debugger is connected.

Any insights or suggestions on how to resolve this issue would be greatly appreciated.

Thank you!

/Mehul

Related