This is a monitor program for BLE NRF51822 chip. You can check memory, register and system information.

Dependencies:   BLE_API mbed nRF51822 nRF51_Vdd

Please refer follows.
/users/kenjiArai/code/debug_tools/
/users/kenjiArai/notebook/ble--tytaiyo-yuden-module-for-mbed/

Revision:
5:8c37a47ac34c
Parent:
4:36ad7c7d0400
--- a/debug_tools/debugging_nRF51.h	Sun Feb 14 21:55:30 2016 +0000
+++ b/debug_tools/debugging_nRF51.h	Sun Jan 08 04:36:55 2017 +0000
@@ -5,7 +5,7 @@
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
  *      Created:  Feburary   1st, 2016
- *      Revised:  Feburary  15th, 2016
+ *      Revised:  Feburary  18th, 2016
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -19,7 +19,8 @@
     //---------------------------------------------------------------------------------
 #define NRF_RTC2_BASE                   0x40024000UL
 #define NRF_RTC2                        ((NRF_RTC_Type            *) NRF_RTC2_BASE)
-    
+#define MAX_RTC_TASKS_DELAY     47  /**< Maximum delay until an RTC task is executed. */
+ 
 case '1' :
     step_by_step('1');
     sd_power_system_off();
@@ -95,8 +96,6 @@
     NRF_TWI0->POWER =0;
     NRF_TWI1->POWER =0;
     step_by_step('4');
-    NRF_GPIO->DIR = 0xffff; // all ports set output mode
-    NRF_GPIO->OUTCLR = 0;   // all ports are low level
     //step_by_step('x');
     //NRF_UART0->ENABLE = 0;  // Disable UART
     step_by_step('2');
@@ -118,20 +117,142 @@
     }
     //break;
 case '2' :
+#if 1
+    step_by_step('a');              
+    // Stop Radio
+    NRF_RADIO->SHORTS          = 0;
+    NRF_RADIO->EVENTS_DISABLED = 0;
+    NRF_RADIO->TEST            = 0;
+    NRF_RADIO->TASKS_DISABLE   = 1;
+    while (NRF_RADIO->EVENTS_DISABLED == 0){
+        ;// Do nothing.
+    }
+    NRF_RADIO->EVENTS_DISABLED = 0;
+    // Set RTC1 for wake-up
+    NVIC_ClearPendingIRQ(RTC1_IRQn);
+    NVIC_DisableIRQ(RTC1_IRQn);
+    NRF_RTC1->TASKS_STOP = 1;
+    nrf_delay_us(MAX_RTC_TASKS_DELAY);
+    NRF_RTC1->TASKS_CLEAR = 1;
+    nrf_delay_us(MAX_RTC_TASKS_DELAY);
+    NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
+    NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
+    NRF_RTC1->INTENCLR = RTC_INTENSET_OVRFLW_Msk;
+    NRF_RTC1->EVTENCLR = RTC_EVTEN_OVRFLW_Msk;
+    NRF_RTC1->EVENTS_COMPARE[0] = 0;
+    NRF_RTC1->EVTENCLR = 0x000f0003; // all clear
+    NRF_RTC1->EVTENSET = RTC_EVTEN_COMPARE0_Msk;
+#if 0
+    do {
+        NRF_RTC1->PRESCALER = 4095;
+    } while (NRF_RTC1->PRESCALER != 4095);
+#else
+    NRF_RTC1->PRESCALER = 4095;
+#endif
+    // Set wake-up time
+    NRF_RTC1->CC[0] = 50;
+    // GPIOE
+#if 0
+    NRF_GPIO->PIN_CNF[21] =   (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
+                            | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
+                            | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
+                            | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
+                            | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
+#endif
+    NRF_GPIOTE->CONFIG[0] =   (GPIOTE_CONFIG_POLARITY_LoToHi << GPIOTE_CONFIG_POLARITY_Pos)
+                            | (21 << GPIOTE_CONFIG_PSEL_Pos) 
+                            | (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos);   
+    // Set PPI
+    NRF_PPI->CH[0].EEP = (uint32_t)&NRF_RTC1->EVENTS_COMPARE[0];
+    NRF_PPI->CH[0].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[0];
+    // Enable only PPI channels 0
+    NRF_PPI->CHEN = PPI_CHEN_CH0_Enabled << PPI_CHEN_CH0_Pos;
+    // Restart RTC1
+    NRF_RTC1->TASKS_START = 1;
+    nrf_delay_us(MAX_RTC_TASKS_DELAY);
+    // DEBUG
+    PRINTF("NRF_RTC1->PRESCALER=0x%x\r\n", NRF_RTC1->PRESCALER);
+    PRINTF("NRF_RTC1->EVTEN=0x%x\r\n", NRF_RTC1->EVTEN);
+    PRINTF("NRF_RTC1->COUNTER=0x%x\r\n", NRF_RTC1->COUNTER);
+    PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=0x%x\r\n", NRF_RTC1->EVENTS_COMPARE[0]);
+    PRINTF("NRF_RTC1->CC[0]=0x%x\r\n", NRF_RTC1->CC[0]);
+    do {
+        PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=");
+        PRINTF("%d", NRF_RTC1->EVENTS_COMPARE[0]);
+        PRINTF(" %d", NRF_RTC1->CC[0]);
+        PRINTF(" %d\r\n", NRF_RTC1->COUNTER);
+        nrf_delay_us(125000);
+    } while(NRF_RTC1->EVENTS_COMPARE[0] == 0);
+    PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=");
+    PRINTF("%d", NRF_RTC1->EVENTS_COMPARE[0]);
+    PRINTF(" %d", NRF_RTC1->CC[0]);
+    PRINTF(" %d\r\n", NRF_RTC1->COUNTER);
+    step_by_step('b');
+    SCB->AIRCR = 0x05fa0004;    // System RESET!!
+    // Not come here (Just in case)
+    deepsleep();
+#else
     step_by_step('a');              
     // Internal 32kHz RC
-    NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos;           
+    //NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos;           
     // Start the 32 kHz clock, and wait for the start up to complete
-    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
-    NRF_CLOCK->TASKS_LFCLKSTART = 1;
-    while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
+    //NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
+    //NRF_CLOCK->TASKS_LFCLKSTART = 1;
+    //while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
     // Configure the RTC to run at 2 second intervals, and make sure COMPARE0 generates an interrupt (this will be the wakeup source)
-    NRF_RTC1->PRESCALER = 3277;
+    NVIC_DisableIRQ(RTC0_IRQn);
+    NRF_RTC1->TASKS_STOP = 1;
+    // GPIOE
+#if 0
+    NRF_GPIO->PIN_CNF[21] =   (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
+                            | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
+                            | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
+                            | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
+                            | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
+#endif    
+    NRF_GPIOTE->CONFIG[0] =   (GPIOTE_CONFIG_POLARITY_LoToHi << GPIOTE_CONFIG_POLARITY_Pos)
+                            | (21 << GPIOTE_CONFIG_PSEL_Pos) 
+                            | (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos);
+    // RTC1
+    NRF_RTC1->TASKS_STOP = 1;
+    NRF_RTC1->EVENTS_COMPARE[0] = 0;
+    NRF_RTC1->EVTENCLR = 0x000f0003; // all clear
     NRF_RTC1->EVTENSET = RTC_EVTEN_COMPARE0_Msk;
-    NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk;
-    NRF_RTC1->CC[0] = 2*10;
+    NRF_RTC1->TASKS_CLEAR = 1;    
+    NRF_RTC1->CC[0] = 100;
+    NRF_RTC1->PRESCALER = 4095;
+    do {
+        NRF_RTC1->TASKS_STOP = 1; 
+        NRF_RTC1->PRESCALER = 4095;
+        PRINTF("Retry PRESCALER setting\r\n");
+    } while (NRF_RTC1->PRESCALER != 4095);    
+    PRINTF("NRF_RTC1->PRESCALER=0x%x\r\n", NRF_RTC1->PRESCALER);
+    PRINTF("NRF_RTC1->EVTEN=0x%x\r\n", NRF_RTC1->EVTEN);
+    PRINTF("NRF_RTC1->COUNTER=0x%x\r\n", NRF_RTC1->COUNTER);
+    PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=0x%x\r\n", NRF_RTC1->EVENTS_COMPARE[0]);
+    PRINTF("NRF_RTC1->CC[0]=0x%x\r\n", NRF_RTC1->CC[0]);
+    // PPI
+    NRF_PPI->CH[0].EEP = (uint32_t)&NRF_RTC1->EVENTS_COMPARE[0];
+    NRF_PPI->CH[0].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[0];
+    // Enable only PPI channels 0
+    NRF_PPI->CHEN = PPI_CHEN_CH0_Enabled << PPI_CHEN_CH0_Pos;
     NRF_RTC1->TASKS_START = 1;
-    //NVIC_EnableIRQ(RTC1_IRQn);
+    do {
+        PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=");
+        PRINTF("%d", NRF_RTC1->EVENTS_COMPARE[0]);
+        PRINTF(" %d", NRF_RTC1->CC[0]);
+        PRINTF(" %d\r\n", NRF_RTC1->COUNTER);
+    } while(NRF_RTC1->EVENTS_COMPARE[0] == 0);
+    PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=");
+    PRINTF("%d", NRF_RTC1->EVENTS_COMPARE[0]);
+    PRINTF(" %d", NRF_RTC1->CC[0]);
+    PRINTF(" %d\r\n", NRF_RTC1->COUNTER);
+    step_by_step('b');
+    SCB->AIRCR = 0x05fa0004;    // System RESET!!
+    // Not come here (Just in case)
+    deepsleep();
+#endif
+#if 0       
     // Configure the RAM retention parameters
     NRF_POWER->RAMON = POWER_RAMON_ONRAM0_RAM0On   << POWER_RAMON_ONRAM0_Pos
                      | POWER_RAMON_ONRAM1_RAM1Off  << POWER_RAMON_ONRAM1_Pos
@@ -141,8 +262,9 @@
         __WFI();
         //NRF_RTC1->TASKS_STOP = 1;
     }
-    //break;
-    #if 0
+    break;
+#endif
+#if 0
 case '3' :
     step_by_step('x');
     p_out = 1;
@@ -156,4 +278,4 @@
         wait(0.1);
     }
     break;
-    #endif              
+#endif