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

SAADC using local timer

Hello,

based on the saadc example of SDK 11 I am trying to change the code to use the SAADC's local timer to sample the input given number of times (10 times for the below example), at a rate of 8000 Hz. Based on the documentation, it seems to be feasible by setting the NRF_SAADC->SAMPLERATE register's CC bitfield to 2000, plus, setting the MODE bit of the same register to 1.

The below code compiles and runs, but no values get written into sampleBuffer. Here is the code:

int main(void)
{
    uart_config();
    printf("SAADC test.\r\n");

    int32_t err_code;
    uint32_t const numberOfSamples= 10;
    int16_t sampleBuffer[numberOfSamples]= {0,1,2,3,4,5,6,7,8,9};

    nrf_saadc_channel_config_t channel_config=
    {
      .resistor_p = NRF_SAADC_RESISTOR_DISABLED,
      .resistor_n = NRF_SAADC_RESISTOR_DISABLED,
      .gain       = NRF_SAADC_GAIN1_6,
      .reference  = NRF_SAADC_REFERENCE_INTERNAL,
      .acq_time   = NRF_SAADC_ACQTIME_40US,
      .mode       = NRF_SAADC_MODE_SINGLE_ENDED,
      .pin_p      = NRF_SAADC_INPUT_AIN0,
      .pin_n      = NRF_SAADC_INPUT_DISABLED  
    };

    nrf_drv_saadc_config_t saadc_config=
    {
      .resolution= NRF_SAADC_RESOLUTION_10BIT,
      .oversample= NRF_SAADC_OVERSAMPLE_DISABLED,
      .interrupt_priority= APP_IRQ_PRIORITY_LOW
    };

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

    err_code = nrf_drv_saadc_channel_init(0, &channel_config);
    APP_ERROR_CHECK(err_code);    

    uint32_t samplingRate= 8000;
    NRF_SAADC->SAMPLERATE = (16000000/samplingRate);      // [80..2047] Capture and compare value. Sample rate is 16 MHz/CC
    NRF_SAADC->SAMPLERATE|= SAADC_SAMPLERATE_MODE_Timers  // Rate is controlled from local timer (use CC to control the rate)
                        << SAADC_SAMPLERATE_MODE_Pos;

    NRF_SAADC->RESULT.PTR = (uint32_t)sampleBuffer;
    NRF_SAADC->RESULT.MAXCNT = numberOfSamples;    

    printf("Sampling ... ");
    nrf_saadc_task_trigger(NRF_SAADC_TASK_START);

    // while (nrf_saadc_event_check(NRF_SAADC_EVENT_END) != 0);
    nrf_delay_ms(100);

    printf("ended.\r\n");

    for (uint32_t i=0; i<numberOfSamples; ++i) { printf("%d,", sampleBuffer[i]); }

    while(1) { __WFE(); }
}

And here is the output on the UART terminal:

SAADC test.
Sampling ... ended.
0,1,2,3,4,5,6,7,8,9,

So it is easy to see that sampleBuffer still contains the numbers stored in it at the startup. Any help on how to make the code work would be appreciated.

Thanks! Tamas

Related