Hello!
Please help. I've made an adc convertion in uboot but for some reason the read value is different from one that is done via iio in linux.
Linux raw value shows: 2564
My code in uboot shows: 3770
#define ADC1_BASE_ADDR 0x02198000
#define ADCx_HC0 0x00
#define ADCx_HS 0x08
#define ADCx_HS_C0 BIT(0)
#define ADCx_R0 0x0c
#define ADCx_CFG 0x14
#define ADCx_CFG_SWMODE 0x308
#define ADCx_GC 0x18
#define ADCx_GC_CAL BIT(7)
static int read_adc(u32 *val)
{
int ret;
void __iomem *b = map_physmem(ADC1_BASE_ADDR, 0x100, MAP_NOCACHE);
/* use software mode */
writel(ADCx_CFG_SWMODE, b + ADCx_CFG);
/* start auto calibration */
setbits_le32(b + ADCx_GC, ADCx_GC_CAL);
ret = wait_for_bit("Wait for calibration", b + ADCx_GC, ADCx_GC_CAL, ADCx_GC_CAL, 10, 0);
if (ret)
goto adc_exit;
/* start conversion */
writel(0, b + ADCx_HC0);
/* wait for conversion */
ret = wait_for_bit("Wait for conversion", b + ADCx_HS, ADCx_HS_C0, ADCx_HS_C0, 10, 0);
if (ret)
goto adc_exit;
/* read result */
*val = readl(b + ADCx_R0);
adc_exit:
if (ret)
printf("ADC failure (ret=%i)\n", ret);
unmap_physmem(b, MAP_NOCACHE);
return ret;
}
static int check_battery(void)
{
u32 val;
int ret;
ret = read_adc(&val);
if (ret)
return ret;
printf("Battery voltage: %d\n", val);
return ret;
}
Hi Sergey
one can printf adc registers in both cases and compare them, in particular
pay attention to pull resistor configuration on input, please check Chapter 4
External Signals and Pin Multiplexing, Chapter 30 IOMUX Controller (IOMUXC),
Chapter 13 Analog-to-Digital Converter (ADC) i.MX6UL Reference Manual
http://www.nxp.com/docs/en/reference-manual/IMX6ULRM.pdf
Best regards
igor
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
I've found a fix for the problem but its very strange. GC_CAL bit was cleared, but the result conversion was still wrong. I added 50ms delay after calibration before the read and that read value matched voltmeter.