This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How long am I holding a button app_uart example

Hi,

I am trying to change the uart example using NRF 52 with SDK 12.3, so that I would hold a button pressed and it would send the time of button being pressed after release to my PC via UART connection. What I've got so far is making it say "hello" after pressing button 3, now I would like to get the timer on button 2. To do this, I was referring to an answer I've found here, but is not working. When I push the button 2, the board resets, sending "nUART START!" to PC, the same happens when I release the button. UART connection works fine.

Here's what I'm doing:

image description

I commented out the buttons_leds_init because I am not using them in the same way as in the UART example and I don't need the LED indicator of readiness for now.

In buttons_init() the button 2 is initiated. In button_handler I declare what is done when the button is pressed/released:

image description

Here are also the includes and button definitions, if needed:

image description

Can you tell me what I am doing wrong?

image description

EDIT STARTS HERE:

Alright, I think the problem is in timer. I set up another example, where I used the gpiote function from timer example from github to initiate my buttons. So now I'm using button 1 to start the timer and button 2 to stop it. Whenever I stop the timer, the board resets, leaving me with no useful results. So, here's some of my code:

int main(void) {

lfclk_request(); 
uint32_t err_code;
bool erase_bonds;

	    // Initialize.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
uart_init();
gpio_config();

ble_stack_init();
gap_params_init();
services_init();
advertising_init();
conn_params_init();

printf("\r\nUART Start!\r\n");
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);

	

// Enter main loop.
for (;;)
{
    power_manage();
}

}

static void gpio_config() {

 ret_code_t err_code;
 uint32_t e_code;
 create_timer();
// Initialze driver.
err_code = nrf_drv_gpiote_init();
APP_ERROR_CHECK(err_code);

// Configure 3 output pins for LED's.
nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
err_code = nrf_drv_gpiote_out_init(LED_1_PIN, &out_config);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_gpiote_out_init(LED_2_PIN, &out_config);
APP_ERROR_CHECK(err_code);

// Set output pins (this will turn off the LED's).
nrf_drv_gpiote_out_set(LED_1_PIN);
nrf_drv_gpiote_out_set(LED_2_PIN);

// Make a configuration for input pints. This is suitable for both pins in this example.
nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
in_config.pull = NRF_GPIO_PIN_PULLUP;

// Configure input pins for buttons, with separate event handlers for each button.
err_code = nrf_drv_gpiote_in_init(BUTTON_1_PIN, &in_config, gpiote_event_handler);
APP_ERROR_CHECK(err_code);
e_code = nrf_drv_gpiote_in_init(BUTTON_2_PIN, &in_config, gpiote_event_handler);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_gpiote_in_init(BUTTON_3_PIN, &in_config, gpiote_event_handler);
APP_ERROR_CHECK(err_code);
//err_code = nrf_drv_gpiote_in_init(BUTTON_4_PIN, &in_config, gpiote_event_handler);
//APP_ERROR_CHECK(err_code);

// Enable input pins for buttons.
nrf_drv_gpiote_in_event_enable(BUTTON_1_PIN, true);
nrf_drv_gpiote_in_event_enable(BUTTON_2_PIN, true);
nrf_drv_gpiote_in_event_enable(BUTTON_3_PIN, true);
//nrf_drv_gpiote_in_event_enable(BUTTON_4_PIN, true); 

}

void gpiote_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { uint32_t err_code; uint32_t counter;

	err_code = NRF_SUCCESS;
	APP_ERROR_CHECK(err_code);

switch (pin)
{
	case BUTTON_1_PIN:
			app_timer_start(m_button_tick_timer, 65535, NULL);
			counter = app_timer_cnt_get();
    APP_ERROR_CHECK(err_code);
			printf("timer start");
	break;
	case BUTTON_2_PIN:
			counter = app_timer_cnt_get();
    APP_ERROR_CHECK(counter);

    err_code = app_timer_stop(m_button_tick_timer);
    APP_ERROR_CHECK(err_code);

    err_code = app_timer_cnt_diff_compute(button_tick_release, button_tick, &total_ticks);
    APP_ERROR_CHECK(err_code);
    printf("total ticks was: %d\r\n", counter);
break;
case BUTTON_3_PIN:
  // err_code = app_timer_start(m_led_a_timer_id, APP_TIMER_TICKS(200, APP_TIMER_PRESCALER), NULL);
			
			nrf_drv_gpiote_out_toggle(LED_2_PIN);
			printf("hello\r\n");
	
  //  APP_ERROR_CHECK(err_code);
    break;
 default:
    break;
}

}

static void create_timer() {

 uint32_t err_code;

 err_code = app_timer_create(&m_button_tick_timer, 
															
 APP_TIMER_MODE_SINGLE_SHOT, 
															
 timer_handler);
 APP_ERROR_CHECK(err_code);

}

I believe this timer is somehow colliding with the one from UART example, but I am not sure. It runs, which I see if I leave it running - it soon runs out, sending msg "timer timed out", it just doesn't work if I stop it. Anyone knows how to solve this?

Related