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

hardfault about the saadc channel init

Hi,

I meet a hard fault handler issue when initialize the saadc channel. The code is as followed.

 

#define SAADC_SAMPLES_IN_BUFFER 2
int16_t touchs[2];
nrf_saadc_channel_config_t channel[2]; //saadc channel
nrf_drv_saadc_config_t saadc_config = NRFX_SAADC_DEFAULT_CONFIG;  //saadc
static nrf_saadc_value_t buffer[2][SAADC_SAMPLES_IN_BUFFER];  //buffer for saadc
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
        nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAADC_SAMPLES_IN_BUFFER);
        for (int i = 0; i < SAADC_SAMPLES_IN_BUFFER; i++)
        {
            touchs[i] = p_event->data.done.p_buffer[i];
        }
    }
}

void saadc_init()
{
    nrf_drv_saadc_init(&saadc_config, saadc_callback); //initial the saadc with the callback function
    saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT; //res = 12?
    channel[0].pin_p = yp;  //channel 0 yp
    nrf_drv_saadc_channel_init(0, &channel[0]);
    channel[1].pin_p = xp;   //channel 1 xp
    nrf_drv_saadc_channel_init(1, &channel[1]);
    nrf_drv_saadc_buffer_convert(buffer[0], SAADC_SAMPLES_IN_BUFFER); //read sample from saadc to buffer?
    nrf_drv_saadc_buffer_convert(buffer[1], SAADC_SAMPLES_IN_BUFFER);
}

When I set the breakpoint at line 22, the debugging process can work well, but when I move the breakpoint to line 24, the debugging process will jump to hard fault handler. Do you think my configuration of the saadc channel is wrong?

Thanks in advance for your help,

Leo

Parents Reply Children
  • Thanks for your response. I try to change yp to 6 which is AIN2, but it still jump to the hardfault handler. after initialize the channel Also, in another project, I am using pin 28, 30 and 31 as the positive input and you can see from the picture, the register can set properly and I can get the sample value. 

    By the way, could you please tell me what will cause the hard fault issue?

    Thanks very much for your help:)

  • Have you read the definition of nrf_drv_saadc_channel_init/nrfx_saadc_channel_init?

    Leogean said:
    the register can set properly and I can get the sample value. 

    Are you sure? Can you share that code?

  • Thanks for your reply. I extract the saadc part from my project. The terminal output and the register setting are the same as the pictures before. It should print three sample values in the terminal. Please have a try. 

    Thanks a lot.

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    #include <stdbool.h>
    #include <stdbool.h>
    #include <stdint.h>
    #include <ble_gap.h>
    #include "nrf.h"
    #include "app_util_platform.h"
    #include "nrf_delay.h"
    #include "app_timer.h"
    #include "app_error.h"
    #include "nrf_delay.h"
    #include "nrf_log.h"
    #include "boards.h"
    #include "pca10056.h"
    #include "nrf_gpio.h"
    #include "nrf_drv_timer.h"
    #include "nrfx_gpiote.h"
    #include "nrf_drv_gpiote.h"
    #include "nrf_drv_ppi.h"
    #include "nrf_drv_rtc.h"
    #include "nrf_drv_clock.h"
    #include "nrf_drv_lpcomp.h"
    #include "nrf_drv_saadc.h"
    #include "nrf_drv_twi.h"
    #include "nrf_libuarte_async.h"
    #include "app_pwm.h"
    
    #define SAADC_SAMPLES_IN_BUFFER 3
    
    nrf_saadc_channel_config_t channel[3]; //saadc channel
    nrf_drv_saadc_config_t saadc_config;  //saadc
    static nrf_saadc_value_t buffer[2][SAADC_SAMPLES_IN_BUFFER];  //buffer for saadc
    int adcData[3];
    
    /*saadc data read*/
    void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
    {
        if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
        {
            ret_code_t err_code;
    
            err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAADC_SAMPLES_IN_BUFFER);
    
            for (int i = 0; i < SAADC_SAMPLES_IN_BUFFER; i++)
            {
                adcData[i] = p_event->data.done.p_buffer[i];
            }
        }
    }
    
    void saadc_init()
    {
        nrf_drv_saadc_init(&saadc_config, saadc_callback); //initial the saadc with the callback function
        saadc_config.resolution = 2; //res = 12?
    
        channel[0].reference = NRF_SAADC_REFERENCE_VDD4; //reference vvd
        channel[0].gain = NRF_SAADC_GAIN1_4; //gain 1/4
        channel[0].acq_time = NRF_SAADC_ACQTIME_3US; //tacq=3
        channel[0].pin_p = 31; //D31? positive pin?
        nrf_drv_saadc_channel_init(0, &channel[0]);
    
        channel[1].reference = NRF_SAADC_REFERENCE_INTERNAL; //refvvd false
        channel[1].gain = NRF_SAADC_GAIN1_6;
        channel[1].acq_time = NRF_SAADC_ACQTIME_3US;
        channel[1].pin_p = 28;
        nrf_drv_saadc_channel_init(1, &channel[1]);
    
        channel[2].reference = NRF_SAADC_REFERENCE_VDD4;
        channel[2].gain = NRF_SAADC_GAIN1_6;
        channel[2].acq_time = NRF_SAADC_ACQTIME_3US;
        channel[2].pin_p = 30;
        nrf_drv_saadc_channel_init(2, &channel[2]);
    
        nrf_drv_saadc_buffer_convert(buffer[0], SAADC_SAMPLES_IN_BUFFER); //read sample from saadc to buffer?
        nrf_drv_saadc_buffer_convert(buffer[1], SAADC_SAMPLES_IN_BUFFER);
    }
    
    void updateADC()
    {
            for(int i=0; i<3; i++)
            {
                nrf_drv_saadc_sample();
                printf("%d \n", i);
            }
    }
    
    int main(void)
    {
        printf("hello\n");
        saadc_init();
        updateADC();
        printf("%d, %d, %d \n", adcData[0], adcData[1], adcData[2]);
    
    }

Related