Freertos software timer, RTT logs -> Hard Fault

Hello. 

I am trying to use FreeRTOS and have run into a problem. If I call logging in the FreeRTOS software timer handler, the program stops. 

Here is a simple example:

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "nrf_drv_common.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#include "FreeRTOS.h"
#include "timers.h"
#include "task.h"

volatile TimerHandle_t xOneSec_Timer;

void blink_task(void *p){
    
    while(1){
        NRF_LOG_INFO("Blink");
        vTaskDelay(1000);
    }
};

void vOneSecTimer( TimerHandle_t xTimer ){
	configASSERT( xTimer );
	
    NRF_LOG_INFO("Timer");
}

int main(void)
{
    ret_code_t err_code;
    
    // Initialize.
    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("Start");

    for(uint32_t i=0; i<20; i++){
        NRF_LOG_INFO("Count %d",i);
        for(uint32_t k=0; k<0x100000; k++){__NOP();};
    }
    
    xOneSec_Timer = xTimerCreate( "1STimer", 1000 , pdTRUE, NULL, vOneSecTimer);
    xTimerStart(xOneSec_Timer,0);

    xTaskCreate(blink_task, "Blink", 256, NULL, 2, NULL);
    vTaskStartScheduler();

    while(1);
}

The output to the Ozone terminal looks like this: I see a "Start" message, messages with a counter, but as soon as the timer handler is called, the program stops. If in the timer handler you do not call a log entry, but for example blink the LED, then everything works. I see messages from the blink task and blinking LED.

What am I doing wrong?

Parents
  • You need to initialize your clocks before you use them. 

    Use this code snippet instead and test it again on your end.

    #include <stdbool.h>
    #include <stdint.h>
    #include <string.h>
    #include "nrf_drv_common.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #include "FreeRTOS.h"
    #include "timers.h"
    #include "task.h"
    
    volatile TimerHandle_t xOneSec_Timer;
    
    void blink_task(void *p){
        
        while(1){
            NRF_LOG_INFO("Blink");
            vTaskDelay(1000);
        }
    };
    
    void vOneSecTimer( TimerHandle_t xTimer ){
    	configASSERT( xTimer );
    	
        NRF_LOG_INFO("Timer");
    }
    
    static void clock_init(void)
    {
        ret_code_t err_code = nrf_drv_clock_init();
        ASSERT((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_MODULE_ALREADY_INITIALIZED));
    
        nrf_drv_clock_hfclk_request(NULL);
        while (!nrf_drv_clock_hfclk_is_running())
        {
            // spin lock
        }
    
        nrf_drv_clock_lfclk_request(NULL);
        while (!nrf_drv_clock_lfclk_is_running())
        {
            // spin lock
        }
    }
    
    int main(void)
    {
        ret_code_t err_code;
        
        // Initialize.
        err_code = NRF_LOG_INIT(NULL);
        APP_ERROR_CHECK(err_code);
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        NRF_LOG_INFO("Start");
    
        clock_init();
    
        for(uint32_t i=0; i<20; i++){
            NRF_LOG_INFO("Count %d",i);
            for(uint32_t k=0; k<0x100000; k++){__NOP();};
        }
        
        xOneSec_Timer = xTimerCreate( "1STimer", 1000 , pdTRUE, NULL, vOneSecTimer);
        xTimerStart(xOneSec_Timer,0);
    
        xTaskCreate(blink_task, "Blink", 256, NULL, 2, NULL);
        vTaskStartScheduler();
    
        while(1);
    }

  • This is a software timer, it works without clocking. If I remove the output to RTT in the timer handler, and make the LED blink, then everything works.


    I tried adding clocking on as you suggested, but the problem persists. If there is an output to RTT, the program stops at HF.

  • Then you and me are seeing two different things. The code I suggested worked fine on an nRF52832 DK with nRF5SDKv17.1.0 on my end.

    What is your setup? is it the same as mine?

  • Yes, it is the same. NRF52832 DK, SDK v17.1, softdevice s132_nrf52_7.2.0

    I pushed the entire project on github. github.com/.../nrf52_hf_example

    For build, you may need to fix the SDK_ROOT variable in the Makefile.

  • Can you please fix the access for this? My username is AryanNordic


Reply Children
Related