Page 1 of 1

Code Freezing on accessing ADC BUffer

Posted: Fri Jan 29, 2016 12:12 pm
by kdsouza
Hey Everyone,

So i am using an STM32F103RG, ADC1 IN0 & IN4.

Here's my ADC setup

Code: Select all

#define ADC_BUF_DEPTH 8 // depth of buffer
#define ADC_CH_NUM 1    // number of used ADC channels
static adcsample_t samples_buf[ADC_BUF_DEPTH * ADC_CH_NUM]; // results array
// Fill ADCConversionGroup structure fields
  static ADCConversionGroup adccg = {
    circular : TRUE,
    num_channels: 2,
    end_cb      : NULL,
    error_cb    : NULL,
    cr1         : 0,
    cr2         : 0,
    smpr1       : 0,
    smpr2       : 0,
    sqr1        : ADC_SQR1_NUM_CH(2),
    sqr2        : 0,
    sqr3        : ADC_SQR3_SQ1_N(4) | ADC_SQR3_SQ1_N(0)
};





And here's my thread.

Code: Select all

//Battery Monitor
static WORKING_AREA(waThread4, 1024);
static msg_t Thread4(void) {
  chRegSetThreadName("Battery");

  int adc_value_a,adc_value_b;
  int ia,ib;

  while(1){
    chThdSleepMilliseconds(5000);
    ia = samples_buf[0];
    ib = samples_buf[1];

    adc_value_a = (((3.3)/4096) * ia) * 1000 ;
    adc_value_b = (((3.3)/4096) * ib) * 1000 ;

    sprintf(battery_global_a,"%d",(adc_value_a / 110));
    sprintf(battery_global_b,"%d",(adc_value_b / 110));
}
return 0;
}


I have another 2 Threads, 1 for a GPS Modem, ( decoding NMEA Packets, and another one taking care of a Cellular Modem.

If i run the battery thread alone, it runs fine. THe moment i run all 3 threads, it lands into an unhandled exception. So i went on commenting lines from below and found out that

Code: Select all

  ia = samples_buf[0];
    ib = samples_buf[1];


is causing the freeze.

Can anyone point out what's going wrong here?

Thanks,

Re: Code Freezing on accessing ADC BUffer

Posted: Fri Jan 29, 2016 4:19 pm
by Giovanni
Define "freeze", are debug options enabled?

This could help: http://chibios.org/dokuwiki/doku.php?id ... ebug_guide

Giovanni

Re: Code Freezing on accessing ADC BUffer

Posted: Sun Jan 31, 2016 9:47 am
by kdsouza
I rewrote all the UART ROutines again and changed the channels to 1. I guess it was my stupidity .

Its working fine now.

Code: Select all

/*ADC STUFF*/
#define ADC_BUF_DEPTH 8 // depth of buffer
#define ADC_CH_NUM 1    // number of used ADC channels
static adcsample_t samples_buf[ADC_BUF_DEPTH * ADC_CH_NUM]; // results array
// Fill ADCConversionGroup structure fields
  static ADCConversionGroup adccg = {
    circular : TRUE,
    num_channels: 1,
    end_cb      : NULL,
    error_cb    : NULL,
    cr1         : 0,
    cr2         : 0,
    smpr1       : 0,
    smpr2       : 0,
    sqr1        : ADC_SQR1_NUM_CH(2),
    sqr2        : 0,
    sqr3        : ADC_SQR3_SQ1_N(4) | ADC_SQR3_SQ1_N(0)
};

Re: Code Freezing on accessing ADC BUffer

Posted: Sun Jan 31, 2016 11:51 am
by kdsouza
Strange that config gives readings on 2 ADCs.

Well Here's the new config for 3 ADCs on th F103

Code: Select all

#define ADC_BUF_DEPTH 1 // depth of buffer
#define ADC_CH_NUM 3   // number of used ADC channels
static adcsample_t samples_buf[ADC_BUF_DEPTH * ADC_CH_NUM]; // results array
// Fill ADCConversionGroup structure fields
  static ADCConversionGroup adccg = {
    circular : TRUE,
    num_channels: ADC_CH_NUM,
    end_cb      : NULL,
    error_cb    : NULL,
    cr1         : 0,
    cr2         : 0,
    smpr1       : 0,
    smpr2       : 0,
    sqr1        : ADC_SQR1_NUM_CH(3),
    sqr2        : 0,
    sqr3        : ADC_SQR3_SQ1_N(0) | ADC_SQR3_SQ2_N(4) |  ADC_SQR3_SQ3_N(5)
};