Hello,
I'm trying to debug an issue where a function I wrote that should only be called under a specific condition is calling in an infinite loop whenever the program runs for the first time after building/resetting. Here is my main code:
int main(void)
{
bool erase_bonds;
ret_code_t rc;
// Initialize.
twi_init();
uart_init();
log_init();
timers_init();
buttons_leds_init(&erase_bonds);
power_management_init();
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
//Comment in setClock() function to set a hardcoded time. See setClock for details
//in current form need to run once with commented in, then comment out and run again, otherwise
//the time will reset after each sleep cycle. Is there a way to keep variables between sleep cycles
//without writing to flash? Could also just write to flash the flag that says if written and pull it > check it
//between runs. this will be the same logic to control more efficient powering of read/write
//setClock();
/* Register first to receive an event when initialization is complete. */
(void) fds_register(fds_evt_handler);
rc = fds_init();
APP_ERROR_CHECK(rc);
/* Wait for fds to initialize. */
wait_for_fds_ready();
// Start execution.
printf("\r\nUART started.\r\n");
//turn on LED3 while on and not in sleep mode. added to make sure device isn't running unnecessarily
nrf_gpio_cfg_output(LED2);
nrf_gpio_pin_clear(LED2);
NRF_LOG_INFO("Debug logging for UART over RTT started.");
//flow of events: init everyting > begin advertising > write the start time to memory > wait for button release event > on button release event record the end time
// >send the entire log of start and end times over ble notification > go to sleep 5 seconds later
//button press waits for a button press event or times out and goes to sleep mode if no button press is received in 15 seconds
buttonPress();
nrf_delay_ms(100);
countEntries();
//save power by only advertising after the data has been written to memory and is ready to send
advertising_start();
//advertise for 15 seconds then try to send the data
nrf_delay_ms(5000);
//SendTime sends the entire memory, should add a condition to make sure that it's connected to BLE, potentially only send new entries etc.
SendTime();
//wait 5 seconds after sending the data before entering sleep mode, just here to give time to read in devtools for debugging
nrf_delay_ms(5000);
sleep_mode_enter();
// Enter main loop.
for (;;)
{
idle_state_handle();
}
}
When I place a debug breakpoint at the buttonPress function (line 52 above) then step over the function it causes the infinite loop with deleteAll, but when I step into buttonPress and step through each line sequentially the infinite loop with deleteALL doesn't happen. I'm not sure how to approach fixing this or what could be going on, any help would be appreciated! I only want deleteAll to run if the loop for the buttonPress function loops 10 times (buttonPress function posted below).
void buttonPress()
{
uint8_t resetCount = 0;
const bool startTime = 0;
const bool endTime = 1;
nrf_gpio_cfg_output(LED); //configures the led pin as an output pin
nrf_gpio_cfg_input(Button, NRF_GPIO_PIN_PULLUP); //configures the button pin as an input pin. nrf_gpio_cfg_input takes argument for a pin and an enumerator.
APP_TIMER_DEF(mSleepTimer);
app_timer_create(&mSleepTimer, APP_TIMER_MODE_SINGLE_SHOT, mSleepTimer_timeout_handler); //mSleepTimer handler can be passed by ref like this since the third parameter is already the typedef of an app_timer_timeout_handler_t function
APP_TIMER_DEF(mConnectTimer);
app_timer_create(&mConnectTimer, APP_TIMER_MODE_SINGLE_SHOT, mConnectTimer_timeout_handler);
nrf_gpio_pin_set(LED); //turns off the led by setting = to 1, this is now default state
while (true) //enter this loop which will act as an event handler for a button press if a button press is received, or timeout and go into sleep mode if DoingNothingTooLong (15 seconds) time is reached
{
app_timer_start(mSleepTimer,DoingNothingTooLong, NULL); //timer to put into sleep mode for case of device woken up but no button press is received
while(nrf_gpio_pin_read(Button) == 0 && validConnectionFlag == false) //while the button is being pressed loop. stay here until button is released
{
nrf_gpio_pin_clear(LED); //led on while in loop couting to see if valid connection. this light will turn off if start time written successfully
app_timer_stop(mSleepTimer); //stop the doing nothing timer
app_timer_start(mConnectTimer, ValidConnectionTime, NULL); //if button held for 10 seconds then timeout and go to mConnect handler otherwise leave loop which starts process over
};
nrf_gpio_pin_set(LED);
app_timer_stop(mConnectTimer); //if the hold was less than 10 seconds leave the loop which stops the connect timer, then returns to the doing nothing too long timer
//if the times have been written leave this loop and go back to main
if(validConnectionFlag == true)
{
writeTime(endTime); //writing this in the event handler doesn't work for some reason, but it does here
nrf_delay_ms(150);
break;
}
//reset memory function for demo. 10 presses of button will clear all records written in flash memory
resetCount++;
if(resetCount >= 10)
{
deleteAll();
resetCount = 0;
}
}//end while loop
}