STM32L432 ADC problem
Posted: Fri Aug 17, 2018 3:16 pm
I'm having a problem with some ADC code "hanging up" in the adcConvert() routine -- I'm not sure why. Sometimes it works. Actually, what I really want is something much simpler -- no DMA, just a couple of direct conversions, but that's a different matter.
#define ADC_GRP1_NUM_CHANNELS 2
#define ADC_GRP1_BUF_DEPTH 1
static adcsample_t adc_samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH]
__attribute__ ((section(".ram0")));
/*
* ADC Conversion group
* Mode: Linear buffer, 1 sample of 3 channels, sw triggered
* Channels: 0 (vref), 17 (temp sensor)
*/
static const ADCConversionGroup adcgrpcfg1 = {
FALSE,
ADC_GRP1_NUM_CHANNELS,
NULL,
NULL,
0, //ADC_CFGR_CONT, /* CFGR */
ADC_TR(0, 4095), /* TR1 */
{ /* SMPR[2] */
ADC_SMPR1_SMP_AN0(ADC_SMPR_SMP_47P5) ,
ADC_SMPR2_SMP_AN17(ADC_SMPR_SMP_47P5) ,
},
{ /* SQR[4] */
ADC_SQR1_SQ1_N(ADC_CHANNEL_IN0) |
ADC_SQR1_SQ2_N(ADC_CHANNEL_IN17),
0,
0,
0
}
};
void adcVDD(uint16_t *vdd100, int16_t *temp10) {
uint32_t raw;
int32_t tmp;
uint32_t vdd;
uint16_t TS_CAL1 = *((uint16_t *) 0x1FFF75A8);
uint16_t TS_CAL2 = *((uint16_t *) 0x1FFF75CA);
uint16_t VREF_CAL = *((uint16_t *) 0x1FFF75AA);
adcAcquireBus(&ADCD1);
adcStart(&ADCD1,NULL);
adcSTM32EnableVREF(&ADCD1); // enable VREF sensor
adcSTM32EnableTS(&ADCD1); // enable temperature sensor
adcConvert(&ADCD1, &adcgrpcfg1, adc_samples, ADC_GRP1_BUF_DEPTH);
adcSTM32DisableTS(&ADCD1); // disable temperature sensor
adcSTM32DisableVREF(&ADCD1); // disable VREF sensor
adcStop(&ADCD1);
adcReleaseBus(&ADCD1);
// compute vdd*100
raw = adc_samples[0];
*vdd100 = 0;
if (raw > 0) {
vdd = (300*VREF_CAL)/raw;
*vdd100 = vdd;
}
// compute temperature *10
raw = adc_samples[1];
tmp = (vdd*raw)/300;
*temp10 = (800 *(tmp - TS_CAL1))/(TS_CAL2-TS_CAL1) + 300;
}
#define ADC_GRP1_NUM_CHANNELS 2
#define ADC_GRP1_BUF_DEPTH 1
static adcsample_t adc_samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH]
__attribute__ ((section(".ram0")));
/*
* ADC Conversion group
* Mode: Linear buffer, 1 sample of 3 channels, sw triggered
* Channels: 0 (vref), 17 (temp sensor)
*/
static const ADCConversionGroup adcgrpcfg1 = {
FALSE,
ADC_GRP1_NUM_CHANNELS,
NULL,
NULL,
0, //ADC_CFGR_CONT, /* CFGR */
ADC_TR(0, 4095), /* TR1 */
{ /* SMPR[2] */
ADC_SMPR1_SMP_AN0(ADC_SMPR_SMP_47P5) ,
ADC_SMPR2_SMP_AN17(ADC_SMPR_SMP_47P5) ,
},
{ /* SQR[4] */
ADC_SQR1_SQ1_N(ADC_CHANNEL_IN0) |
ADC_SQR1_SQ2_N(ADC_CHANNEL_IN17),
0,
0,
0
}
};
void adcVDD(uint16_t *vdd100, int16_t *temp10) {
uint32_t raw;
int32_t tmp;
uint32_t vdd;
uint16_t TS_CAL1 = *((uint16_t *) 0x1FFF75A8);
uint16_t TS_CAL2 = *((uint16_t *) 0x1FFF75CA);
uint16_t VREF_CAL = *((uint16_t *) 0x1FFF75AA);
adcAcquireBus(&ADCD1);
adcStart(&ADCD1,NULL);
adcSTM32EnableVREF(&ADCD1); // enable VREF sensor
adcSTM32EnableTS(&ADCD1); // enable temperature sensor
adcConvert(&ADCD1, &adcgrpcfg1, adc_samples, ADC_GRP1_BUF_DEPTH);
adcSTM32DisableTS(&ADCD1); // disable temperature sensor
adcSTM32DisableVREF(&ADCD1); // disable VREF sensor
adcStop(&ADCD1);
adcReleaseBus(&ADCD1);
// compute vdd*100
raw = adc_samples[0];
*vdd100 = 0;
if (raw > 0) {
vdd = (300*VREF_CAL)/raw;
*vdd100 = vdd;
}
// compute temperature *10
raw = adc_samples[1];
tmp = (vdd*raw)/300;
*temp10 = (800 *(tmp - TS_CAL1))/(TS_CAL2-TS_CAL1) + 300;
}