platform: s3c2440 + u-boot 1.1.4 + linux 2.6.18
为了防止低电开机启动到一半的时候系统down掉,所以有必要在 u-boot 中加入电池电量的侦测。
侦测电池电量只需读 AN0 口上的 AD 值就可以了,当电量少于 576 的时候就不让系统开机。
在 lib_arm/board.c 中加入 battery_probe() 函数:
static int battery_probe(void)
{
S3C2410_ADC * const adc = S3C2410_GetBase_ADC();
S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
unsigned int val, res, ret, powerflag;
int i;
// use AN0;
val = (1<<14) | (9<<6) | (0<<3);
adc->ADCCON = val;
val = adc->ADCCON;
val |= (1<<0);
adc->ADCCON = val;
for(i=0;i<5;i++)
{
// start ADC
while(res&0x1)
{
res = adc->ADCCON;
}
res = 0;
// end ADC
while(!(res&0x8000))
{
res = adc->ADCCON;
}
// read data
ret = adc->ADCDAT0;
ret = ret & 0x3ff;
if(ret<576)
powerflag = 1;
else
powerflag = 0;
}
if(powerflag)
{
printf("The battery too low./n/n");
gpio->GPBDAT &= ~(1<<5); // power off.
}
return 0;
}
然后在 int print_cpuinfo() 中加入:
display_dram_config,
+battery_probe,
NULL,
------------------------------
这样就可以在u-boot 中实现电池电量的侦测了。