Kenji Arai / Mbed 2 deprecated BLE_nRF51822_Monitor

Dependencies:   BLE_API mbed nRF51822 nRF51_Vdd

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers debugging_nRF51.h Source File

debugging_nRF51.h

00001 /*
00002  * mbed Application program
00003  *
00004  *  Copyright (c) 2016 Kenji Arai / JH1PJL
00005  *  http://www.page.sannet.ne.jp/kenjia/index.html
00006  *  http://mbed.org/users/kenjiArai/
00007  *      Created:  Feburary   1st, 2016
00008  *      Revised:  Feburary  18th, 2016
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
00011  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
00012  * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00013  * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00014  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00015  */
00016 
00017     //---------------------------------------------------------------------------------
00018     //  Something new and try new functions
00019     //---------------------------------------------------------------------------------
00020 #define NRF_RTC2_BASE                   0x40024000UL
00021 #define NRF_RTC2                        ((NRF_RTC_Type            *) NRF_RTC2_BASE)
00022 #define MAX_RTC_TASKS_DELAY     47  /**< Maximum delay until an RTC task is executed. */
00023  
00024 case '1' :
00025     step_by_step('1');
00026     sd_power_system_off();
00027     step_by_step('2');
00028     NRF_RTC2->PRESCALER = 0; /* for no pre-scaling. */
00029     NRF_RTC2->TASKS_START = 1;
00030     n = NRF_RTC2->COUNTER;
00031     wait(0.1);
00032     if ( n == NRF_RTC2->COUNTER){
00033         PRINTF("RTC2 is not running\r\n");
00034     } else {
00035         PRINTF("RTC2 is running & counter = %d\r\n", n);
00036     }
00037                 step_by_step('3');
00038     n = NRF_RTC2->COUNTER;
00039     wait(0.1);
00040     if ( n == NRF_RTC2->COUNTER){
00041         PRINTF("RTC2 is not running\r\n");
00042     } else {
00043         PRINTF("RTC2 is running & counter = %d\r\n", n);
00044     }
00045                 step_by_step('4');
00046     n = NRF_RTC0->COUNTER;
00047     wait(0.1);
00048     if ( n == NRF_RTC0->COUNTER){
00049         PRINTF("RTC0 is not running\r\n");
00050     } else {
00051         PRINTF("RTC0 is running & counter = %d\r\n", n);
00052     }
00053     n = NRF_RTC1->COUNTER;
00054     wait(0.1);
00055     if ( n == NRF_RTC1->COUNTER){
00056         PRINTF("RTC1 is not running\r\n");
00057         NRF_RTC1->PRESCALER = 0; /* for no pre-scaling. */
00058         NRF_RTC1->TASKS_START = 1;
00059         wait(0.5);
00060         n = NRF_RTC1->COUNTER;
00061         wait(0.1);
00062         if ( n == NRF_RTC1->COUNTER){
00063             PRINTF("RTC1 is not running\r\n");
00064         } else {
00065             PRINTF("RTC1 is running & counter = %d\r\n", n);
00066         }
00067     } else {
00068         PRINTF("RTC1 is running & counter = %d\r\n", n);
00069     }                
00070     step_by_step('2');                
00071     NRF_POWER->SYSTEMOFF = 1;         
00072     #if 0
00073     NRF_CLOCK->TASKS_HFCLKSTOP = 0xffffffff;
00074     NRF_CLOCK->TASKS_LFCLKSTOP = 0xffffffff;
00075     step_by_step('1');
00076     sd_power_system_off();
00077     //*(uint32_t *)0x40004ffc = 0;
00078     //shutdown();
00079     //softdevice_handler_sd_disable();
00080     PRINTF("POWER=0x%x",NRF_RADIO->POWER);
00081     put_rn();
00082     if (NRF_RADIO->POWER){
00083         PRINTF("Radio power is still enabled");
00084     } else {
00085         PRINTF("Radio power is disabled");
00086     }
00087     put_rn();
00088     step_by_step('2');
00089     NRF_CLOCK->TASKS_HFCLKSTOP = 1;
00090     step_by_step('1');
00091     NRF_RADIO->POWER = 0;
00092     //NRF_POWER->SYSTEMOFF = 1;
00093     //sd_power_system_off();
00094     NRF_SPI0->POWER =0;
00095     NRF_SPI1->POWER =0;
00096     NRF_TWI0->POWER =0;
00097     NRF_TWI1->POWER =0;
00098     step_by_step('4');
00099     //step_by_step('x');
00100     //NRF_UART0->ENABLE = 0;  // Disable UART
00101     step_by_step('2');
00102     NRF_SPI0->ENABLE =0;
00103     NRF_SPI1->ENABLE =0;
00104     NRF_TWI0->ENABLE =0;
00105     NRF_TWI1->ENABLE =0;
00106     step_by_step('3');
00107     NRF_POWER->RAMON = 0;
00108     NRF_POWER->RAMONB = 0;
00109     step_by_step('5');
00110     sd_power_system_off();
00111     //step_by_step('6');
00112     NRF_POWER->SYSTEMOFF = 1;
00113     NRF_POWER->INTENCLR = 0;
00114     #endif
00115     while(true){
00116        sleep();
00117     }
00118     //break;
00119 case '2' :
00120 #if 1
00121     step_by_step('a');              
00122     // Stop Radio
00123     NRF_RADIO->SHORTS          = 0;
00124     NRF_RADIO->EVENTS_DISABLED = 0;
00125     NRF_RADIO->TEST            = 0;
00126     NRF_RADIO->TASKS_DISABLE   = 1;
00127     while (NRF_RADIO->EVENTS_DISABLED == 0){
00128         ;// Do nothing.
00129     }
00130     NRF_RADIO->EVENTS_DISABLED = 0;
00131     // Set RTC1 for wake-up
00132     NVIC_ClearPendingIRQ(RTC1_IRQn);
00133     NVIC_DisableIRQ(RTC1_IRQn);
00134     NRF_RTC1->TASKS_STOP = 1;
00135     nrf_delay_us(MAX_RTC_TASKS_DELAY);
00136     NRF_RTC1->TASKS_CLEAR = 1;
00137     nrf_delay_us(MAX_RTC_TASKS_DELAY);
00138     NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
00139     NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
00140     NRF_RTC1->INTENCLR = RTC_INTENSET_OVRFLW_Msk;
00141     NRF_RTC1->EVTENCLR = RTC_EVTEN_OVRFLW_Msk;
00142     NRF_RTC1->EVENTS_COMPARE[0] = 0;
00143     NRF_RTC1->EVTENCLR = 0x000f0003; // all clear
00144     NRF_RTC1->EVTENSET = RTC_EVTEN_COMPARE0_Msk;
00145 #if 0
00146     do {
00147         NRF_RTC1->PRESCALER = 4095;
00148     } while (NRF_RTC1->PRESCALER != 4095);
00149 #else
00150     NRF_RTC1->PRESCALER = 4095;
00151 #endif
00152     // Set wake-up time
00153     NRF_RTC1->CC[0] = 50;
00154     // GPIOE
00155 #if 0
00156     NRF_GPIO->PIN_CNF[21] =   (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
00157                             | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
00158                             | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
00159                             | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
00160                             | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
00161 #endif
00162     NRF_GPIOTE->CONFIG[0] =   (GPIOTE_CONFIG_POLARITY_LoToHi << GPIOTE_CONFIG_POLARITY_Pos)
00163                             | (21 << GPIOTE_CONFIG_PSEL_Pos) 
00164                             | (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos);   
00165     // Set PPI
00166     NRF_PPI->CH[0].EEP = (uint32_t)&NRF_RTC1->EVENTS_COMPARE[0];
00167     NRF_PPI->CH[0].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[0];
00168     // Enable only PPI channels 0
00169     NRF_PPI->CHEN = PPI_CHEN_CH0_Enabled << PPI_CHEN_CH0_Pos;
00170     // Restart RTC1
00171     NRF_RTC1->TASKS_START = 1;
00172     nrf_delay_us(MAX_RTC_TASKS_DELAY);
00173     // DEBUG
00174     PRINTF("NRF_RTC1->PRESCALER=0x%x\r\n", NRF_RTC1->PRESCALER);
00175     PRINTF("NRF_RTC1->EVTEN=0x%x\r\n", NRF_RTC1->EVTEN);
00176     PRINTF("NRF_RTC1->COUNTER=0x%x\r\n", NRF_RTC1->COUNTER);
00177     PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=0x%x\r\n", NRF_RTC1->EVENTS_COMPARE[0]);
00178     PRINTF("NRF_RTC1->CC[0]=0x%x\r\n", NRF_RTC1->CC[0]);
00179     do {
00180         PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=");
00181         PRINTF("%d", NRF_RTC1->EVENTS_COMPARE[0]);
00182         PRINTF(" %d", NRF_RTC1->CC[0]);
00183         PRINTF(" %d\r\n", NRF_RTC1->COUNTER);
00184         nrf_delay_us(125000);
00185     } while(NRF_RTC1->EVENTS_COMPARE[0] == 0);
00186     PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=");
00187     PRINTF("%d", NRF_RTC1->EVENTS_COMPARE[0]);
00188     PRINTF(" %d", NRF_RTC1->CC[0]);
00189     PRINTF(" %d\r\n", NRF_RTC1->COUNTER);
00190     step_by_step('b');
00191     SCB->AIRCR = 0x05fa0004;    // System RESET!!
00192     // Not come here (Just in case)
00193     deepsleep();
00194 #else
00195     step_by_step('a');              
00196     // Internal 32kHz RC
00197     //NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos;           
00198     // Start the 32 kHz clock, and wait for the start up to complete
00199     //NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
00200     //NRF_CLOCK->TASKS_LFCLKSTART = 1;
00201     //while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
00202     // Configure the RTC to run at 2 second intervals, and make sure COMPARE0 generates an interrupt (this will be the wakeup source)
00203     NVIC_DisableIRQ(RTC0_IRQn);
00204     NRF_RTC1->TASKS_STOP = 1;
00205     // GPIOE
00206 #if 0
00207     NRF_GPIO->PIN_CNF[21] =   (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
00208                             | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
00209                             | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
00210                             | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
00211                             | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
00212 #endif    
00213     NRF_GPIOTE->CONFIG[0] =   (GPIOTE_CONFIG_POLARITY_LoToHi << GPIOTE_CONFIG_POLARITY_Pos)
00214                             | (21 << GPIOTE_CONFIG_PSEL_Pos) 
00215                             | (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos);
00216     // RTC1
00217     NRF_RTC1->TASKS_STOP = 1;
00218     NRF_RTC1->EVENTS_COMPARE[0] = 0;
00219     NRF_RTC1->EVTENCLR = 0x000f0003; // all clear
00220     NRF_RTC1->EVTENSET = RTC_EVTEN_COMPARE0_Msk;
00221     NRF_RTC1->TASKS_CLEAR = 1;    
00222     NRF_RTC1->CC[0] = 100;
00223     NRF_RTC1->PRESCALER = 4095;
00224     do {
00225         NRF_RTC1->TASKS_STOP = 1; 
00226         NRF_RTC1->PRESCALER = 4095;
00227         PRINTF("Retry PRESCALER setting\r\n");
00228     } while (NRF_RTC1->PRESCALER != 4095);    
00229     PRINTF("NRF_RTC1->PRESCALER=0x%x\r\n", NRF_RTC1->PRESCALER);
00230     PRINTF("NRF_RTC1->EVTEN=0x%x\r\n", NRF_RTC1->EVTEN);
00231     PRINTF("NRF_RTC1->COUNTER=0x%x\r\n", NRF_RTC1->COUNTER);
00232     PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=0x%x\r\n", NRF_RTC1->EVENTS_COMPARE[0]);
00233     PRINTF("NRF_RTC1->CC[0]=0x%x\r\n", NRF_RTC1->CC[0]);
00234     // PPI
00235     NRF_PPI->CH[0].EEP = (uint32_t)&NRF_RTC1->EVENTS_COMPARE[0];
00236     NRF_PPI->CH[0].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[0];
00237     // Enable only PPI channels 0
00238     NRF_PPI->CHEN = PPI_CHEN_CH0_Enabled << PPI_CHEN_CH0_Pos;
00239     NRF_RTC1->TASKS_START = 1;
00240     do {
00241         PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=");
00242         PRINTF("%d", NRF_RTC1->EVENTS_COMPARE[0]);
00243         PRINTF(" %d", NRF_RTC1->CC[0]);
00244         PRINTF(" %d\r\n", NRF_RTC1->COUNTER);
00245     } while(NRF_RTC1->EVENTS_COMPARE[0] == 0);
00246     PRINTF("NRF_RTC1->EVENTS_COMPARE[0]=");
00247     PRINTF("%d", NRF_RTC1->EVENTS_COMPARE[0]);
00248     PRINTF(" %d", NRF_RTC1->CC[0]);
00249     PRINTF(" %d\r\n", NRF_RTC1->COUNTER);
00250     step_by_step('b');
00251     SCB->AIRCR = 0x05fa0004;    // System RESET!!
00252     // Not come here (Just in case)
00253     deepsleep();
00254 #endif
00255 #if 0       
00256     // Configure the RAM retention parameters
00257     NRF_POWER->RAMON = POWER_RAMON_ONRAM0_RAM0On   << POWER_RAMON_ONRAM0_Pos
00258                      | POWER_RAMON_ONRAM1_RAM1Off  << POWER_RAMON_ONRAM1_Pos
00259                      | POWER_RAMON_OFFRAM0_RAM0Off << POWER_RAMON_OFFRAM0_Pos
00260                      | POWER_RAMON_OFFRAM1_RAM1Off << POWER_RAMON_OFFRAM1_Pos;
00261     while(1) {
00262         __WFI();
00263         //NRF_RTC1->TASKS_STOP = 1;
00264     }
00265     break;
00266 #endif
00267 #if 0
00268 case '3' :
00269     step_by_step('x');
00270     p_out = 1;
00271     step_by_step('y');
00272     p_out = 0;
00273     step_by_step('z');
00274     for (uint8_t n =100; n > 0; n--){
00275         p_out = 1;
00276         wait(0.1);
00277         p_out = 0;
00278         wait(0.1);
00279     }
00280     break;
00281 #endif