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

nrf523840 with softdevice and app_timer and hfclk timer -> fault

hello Nordic

i am working with the nrf52840 , sdk 16.0, s140

i am using the peripheral ble_app_blinky example as a base

i added saadc and twi, (spi and uart will be added later)

i palne to sample the saadc at high frequency (32kHz) so i need to be using the nrf _drv_timer, i have some other counters in the system which i will use the app_timer for them because the lfclk is fine for them

the flow is do the sampling first and then do the connection of the ble, they will not work simultaneously. 

i get a fault in the  "nrf_drv_timer_init() function.. specificaly it falls in the following line in the "nrfx_timer.c" file 

for (i = 0; i < p_instance->cc_channel_count; ++i)
    {
        nrf_timer_event_clear(p_instance->p_reg,
                              nrf_timer_compare_event_get(i));
    }

i am not sure why ???

i added my saadc init as well if it may help (all the initiations of the ble is untouched and remain as they are in the sample (just moved it to a different module to clear my main.c a bit)

{
    ret_code_t err_code;
    nrf_drv_saadc_config_t saadc_config;
    nrf_saadc_channel_config_t channel_config_0;

    nrf_gpio_cfg_output(ARDUINO_13_PIN); // FOR DEBUG

    //Configure SAADC
    saadc_config.resolution = NRF_SAADC_RESOLUTION_14BIT;       //Set SAADC resolution to 12-bit. This will make the SAADC output values from 0 (when input voltage is 0V) to 2^12=2048 (when input voltage is 3.6V for channel gain setting of 1/6).
    saadc_config.oversample = NRF_SAADC_OVERSAMPLE_DISABLED;    //DIABLED - //Set oversample to 4x. This will make the SAADC output a single averaged value when the SAMPLE task is triggered 4 times.
    saadc_config.interrupt_priority = APP_IRQ_PRIORITY_LOW;     //Set SAADC interrupt to low priority.
    saadc_config.low_power_mode = NRFX_SAADC_CONFIG_LP_MODE;
	
    //Configure SAADC channels:
    channel_config_0.reference = NRF_SAADC_REFERENCE_VDD4;          //Set internal reference of fixed 0.6 volts
    channel_config_0.gain = NRF_SAADC_GAIN1_4;                      //Set input gain to 1/6. The maximum SAADC input voltage is then 0.6V/(1/6)=3.6V. The single ended input range is then 0V-3.6V
    channel_config_0.acq_time = NRF_SAADC_ACQTIME_10US;             //Set acquisition time. Set low acquisition time to enable maximum sampling frequency of 200kHz. Set high acquisition time to allow maximum source resistance up to 800 kohm, see the SAADC electrical specification in the PS. 
    channel_config_0.mode = NRF_SAADC_MODE_SINGLE_ENDED;            //Set SAADC as single ended. This means it will only have the positive pin as input, and the negative pin is shorted to ground (0V) internally.
    channel_config_0.resistor_p = NRF_SAADC_RESISTOR_DISABLED;      //Disable pullup resistor on the input pin
    channel_config_0.resistor_n = NRF_SAADC_RESISTOR_DISABLED;        
    channel_config_0.burst = NRF_SAADC_BURST_ENABLED;   

    err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback);
    APP_ERROR_CHECK(err_code);

    channel_config_0.pin_p = AUDIO_PIEZO_ADC_INPUT_PIN; 
    err_code = nrf_drv_saadc_channel_init(0, &channel_config_0);
    APP_ERROR_CHECK(err_code);

    channel_config_0.pin_p = AUDIO_MIC_ADC_INPUT_PIN;
    err_code = nrf_drv_saadc_channel_init(1, &channel_config_0);
    APP_ERROR_CHECK(err_code);

    channel_config_0.pin_p = AUDIO_PIEZO_ADC_INPUT_PREAMPLYFIER_INPUT_PIN;
    err_code = nrf_drv_saadc_channel_init(2, &channel_config_0);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], CONVERTED_ADC_SAMPELS_BUFF_SIZE);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], CONVERTED_ADC_SAMPELS_BUFF_SIZE);
    APP_ERROR_CHECK(err_code);


// samling event configurations - set ppi:
    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);

// samling event configurations - set timer:
    sd_clock_hfclk_request();
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;               // ?? WHAT IS THIS WIDTH ??
    err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, NULL);  //timer_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, TICS_FOR_32KHZ, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);
    nrf_drv_timer_enable(&m_timer);

// samling event configurations - get handlers address: 
    uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer, NRF_TIMER_CC_CHANNEL0);
    uint32_t saadc_sample_task_addr   = nrf_drv_saadc_sample_task_get();

// samling event configurations - ppi timer event to start saadc sampling:
    err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_ppi_channel_assign(m_ppi_channel, timer_compare_event_addr, saadc_sample_task_addr);
    APP_ERROR_CHECK(err_code);

hope to read from you soon 

best regards

Ziv

Parents Reply Children
  • hi Einar

    well for some reason i thought that the softdevice uses the LFCLK and not the HFCLK (which right now seems less logic with BLE operations), but anyway, so i set the instance of the timer i create with 1 instead of 0 and then i get compilation error here (in file nrfx_timer.h) :

    /** @brief Macro for creating a timer driver instance. */
    #define NRFX_TIMER_INSTANCE(id)                                   \
    {                                                                 \
        .p_reg            = NRFX_CONCAT_2(NRF_TIMER, id),             \
        .instance_id      = NRFX_CONCAT_3(NRFX_TIMER, id, _INST_IDX), \
        .cc_channel_count = NRF_TIMER_CC_CHANNEL_COUNT(id),           \
    }

    saying that :

    'NRFX_TIMER1_INST_IDX' undeclared here (not in a function); did you mean 'NRFX_TIMER0_INST_IDX'?

    so what am i defining wrong in the sdk_config.h file or the notorious apply_old_config.h file (which i did not touch yet) Slight smile

    hope to read from you soon

    best regards

    Ziv

  • Hi Ziv,

    ziv123 said:
    well for some reason i thought that the softdevice uses the LFCLK and not the HFCLK (which right now seems less logic with BLE operations)

    The SoftDevice reserve both RTC0 and TIMER0. Neither can be used by the application.

    ziv123 said:
    so what am i defining wrong in the sdk_config.h file or the notorious apply_old_config.h file (which i did not touch yet)

    You do not need to (nor should you) touch apply_old_config.h, but you need to ensure that you enable TIMER1 properly in sdk_config.h. That would be by setting TIMER1_ENABLED to 1 (with that, apply_old_config.h would automatically set NRFX_TIMER1_ENABLED to 1).

    Einar

  • great, it seems to compile, soon i will run it and test how it works.. but before, just to be sure .. do i need to change the channel in the timer creation as well:

    so instead of using "NRF_TIMER_CC_CHANNEL0" i should be using "NRF_TIMER_CC_CHANNEL1" ?

    nrf_drv_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, TICS_FOR_32KHZ, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);
        nrf_drv_timer_enable(&m_timer);
    
    // samling event configurations - get handlers address: 
        uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer, NRF_TIMER_CC_CHANNEL0);
        uint32_t saadc_sample_task_addr   = nrf_drv_saadc_sample_task_get();

    also, if i understand correct the RTC0 is LFCLK and the TIMER0 is HFCLK ?

    best regards 

    Ziv

  • Hi Ziv,

    ziv123 said:
    so instead of using "NRF_TIMER_CC_CHANNEL0" i should be using "NRF_TIMER_CC_CHANNEL1" ?

    That should not be needed, though it also should not matter.bEach timer have a set of CC channels, so channel 0 on timer 1 has nothing to do with channel 0 on timer 0.

    ziv123 said:
    if i understand correct the RTC0 is LFCLK and the TIMER0 is HFCLK ?

    Yes. The RTCs are clocked by the LFCLK (32.768 kHz) and the timers are clocked by the HF clock (specifically a 16 MHz peripheral clock which is the HF clock divided by 4).

Related