Hardfault when using both UART and GPIOTE, how can I solve this problem?

jordi836 gravatar image

asked 2017-04-21 09:14:13 +0200

updated 2017-04-25 12:29:00 +0200


I feel like this question has an answer similar to this question, but I can't seem to get it working https://devzone.nordicsemi.com/questi...

I am using the nrf51422 chip on a board I designed myself and I'm using a UART and GPIO interrupt. The UART interrupt works fine on it's own, but as soon as I generate an interrupt on the GPIO, the software goes into hardfault and thus everything stops working. Without UART the GPIO interrupt was working fine.

I'm using the nrf51 DK with the PCA10028 chip on it to program my PCB.

I'm initializing the UART like this:

void init_uart()
    uint32_t err_code;

    const app_uart_comm_params_t comm_params =
        0x004EBF00 //just about 19230 baud



and the GPIO interrupt is intitialized like this:

void gpio_init(void)
    ret_code_t err_code;

    err_code = nrf_drv_gpiote_init();

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
    nrf_drv_gpiote_in_config_t in_config2 = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
    in_config.pull = NRF_GPIO_PIN_NOPULL;
    in_config2.pull = NRF_GPIO_PIN_NOPULL;

    err_code = nrf_drv_gpiote_in_init(encoder_a, &in_config, encoder_a_handler);

    err_code = nrf_drv_gpiote_in_init(encoder_b, &in_config2, encoder_b_handler);

    nrf_drv_gpiote_in_event_enable(encoder_a, true);
    nrf_drv_gpiote_in_event_enable(encoder_b, true);

An interrupt on the UART module is generated like this:

void uart_error_handle(app_uart_evt_t * p_event)
        if(p_event->evt_type == APP_UART_DATA_READY)
                                //function to call when data arrives on Rx

And the interrupt handlers for the gpio look like this (the other handler is similair):

void encoder_b_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t NRF_GPIOTE_POLARITY_TOGGLE)
    if(bToggle == false){
        bToggle = true;
        bToggle = false;

    if(aToggle == true)
        transfer_completed = true;
        aToggle = false;
        bToggle = false;

As I've said: the answer to this question is probably similair to the one linked above. However, I have checked the interrupt priority in the nrf_drv_gpiote.h, but it was already on a low priority and does not seem to do much when I change it from low to high.

I'm really stumped as to what might cause this problem. Could it the speed at which the UART interrupt is caused? I receive about 33 bytes of data in 17 ms after which I do not receive data for about 58 ms.

edit retag flag offensive close delete report spam

1 answer

Sort by ยป oldest newest most voted
jordi836 gravatar image

answered 2017-04-21 10:49:12 +0200

It seems to be that the problem was in my main.c.

I called the gpio_init(); function first and the init_uart(); function second. By switching this around the problem seems to be solved.

I don't know exactly why this is. Does anyone know why this happens? I call both these functions only once, so how does this impact my code?

edit flag offensive delete publish link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

User menu

    or sign up

Recent questions

Question Tools

1 follower


Asked: 2017-04-21 09:14:13 +0200

Seen: 110 times

Last updated: Apr 25