Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

hard fault when taking semaphore in FreeRTOS

Hi,

I'm trying to use a GPIO interrupt and a semaphore to trigger a free rtos taskm but whenever I run xSemaphoreTake i get a hard fault error.

// Semaphore to signal interrupt pin change
SemaphoreHandle_t m_ppg_semaphore;

static void ppg_test_task(void *pvParameter){
    UNUSED_PARAMETER(pvParameter);

    m_ppg_semaphore = xSemaphoreCreateBinary();
    ASSERT(NULL != m_ppg_semaphore);

    gpio_init();

    //volatile enum ppgInterupt ppgState = notReady;
    // Read the interrupt registers to clear them
    //uint8_t buffer;
    //bool stay = false;
    while (true) {
        bool temp = xSemaphoreTake(m_ppg_semaphore, portMAX_DELAY);

        // Turn an LED on to signify the start of data reading
        bsp_board_led_invert(BSP_BOARD_LED_3);
        }
}

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
    BaseType_t yield_req = pdFALSE;

    /* The returned value may be safely ignored, if error is returned it only means that
    * the semaphore is already given (raised). */
    UNUSED_VARIABLE(xSemaphoreGiveFromISR(m_ppg_semaphore, &yield_req));
    portYIELD_FROM_ISR(yield_req);
}
/**
 * @brief Function for configuring: PIN_IN pin for input, PIN_OUT pin for output,
 * and configures GPIOTE to give an interrupt on pin change.
 */
static void gpio_init(void) {
    ret_code_t err_code;

    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
    in_config.pull = NRF_GPIO_PIN_PULLUP;

    err_code = nrf_drv_gpiote_in_init(INT_IN, &in_config, in_pin_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_event_enable(INT_IN, true);

    NRF_LOG_INFO("GPIO Configured");
}

Parents
  • Hi George

    I had a quick chat with our FreeRTOS expert, and can confirm that your code looks clean. What's most likely to have happened if you have added to one of our existing examples, is that you have run out of stack memory. Please try increasing the stack memory by editing configTOTAL_HEAP_SIZE in FreeRTOSConfig.h.

    Also make sure you increase the stack size of your ppg_test_task.

    Best regards,

    Simon

Reply
  • Hi George

    I had a quick chat with our FreeRTOS expert, and can confirm that your code looks clean. What's most likely to have happened if you have added to one of our existing examples, is that you have run out of stack memory. Please try increasing the stack memory by editing configTOTAL_HEAP_SIZE in FreeRTOSConfig.h.

    Also make sure you increase the stack size of your ppg_test_task.

    Best regards,

    Simon

Children
Related