diff options
Diffstat (limited to 'firmware/drivers/adc-as3514.c')
-rw-r--r-- | firmware/drivers/adc-as3514.c | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/firmware/drivers/adc-as3514.c b/firmware/drivers/adc-as3514.c index 3b411a379d..9a81a52cc7 100644 --- a/firmware/drivers/adc-as3514.c +++ b/firmware/drivers/adc-as3514.c | |||
@@ -26,18 +26,35 @@ | |||
26 | /* Read 10-bit channel data */ | 26 | /* Read 10-bit channel data */ |
27 | unsigned short adc_read(int channel) | 27 | unsigned short adc_read(int channel) |
28 | { | 28 | { |
29 | unsigned char buf[2]; | 29 | unsigned short data = 0; |
30 | |||
31 | if ((unsigned)channel >= NUM_ADC_CHANNELS) | ||
32 | return 0; | ||
30 | 33 | ||
31 | ascodec_lock(); | 34 | ascodec_lock(); |
32 | 35 | ||
33 | /* Select channel */ | 36 | /* Select channel */ |
34 | ascodec_write(AS3514_ADC_0, (channel << 4)); | 37 | ascodec_write(AS3514_ADC_0, (channel << 4)); |
38 | /* | ||
39 | * The AS3514 ADC will trigger an interrupt when the conversion | ||
40 | * is finished, if the corresponding enable bit in IRQ_ENRD2 | ||
41 | * is set. | ||
42 | * Previously the code did not wait and this apparently did | ||
43 | * not pose any problems, but this should be more correct. | ||
44 | * Without the wait the data read back may be completely or | ||
45 | * partially (first one of the two bytes) stale. | ||
46 | */ | ||
47 | ascodec_wait_adc_finished(); | ||
48 | |||
35 | 49 | ||
50 | /* Read data */ | ||
51 | unsigned char buf[2] = { 0, 0 }; | ||
36 | ascodec_readbytes(AS3514_ADC_0, 2, buf); | 52 | ascodec_readbytes(AS3514_ADC_0, 2, buf); |
53 | data = (((buf[0] & 0x3) << 8) | buf[1]); | ||
37 | 54 | ||
38 | ascodec_unlock(); | 55 | ascodec_unlock(); |
39 | 56 | ||
40 | return (((buf[0] & 0x3) << 8) | buf[1]); | 57 | return data; |
41 | } | 58 | } |
42 | 59 | ||
43 | void adc_init(void) | 60 | void adc_init(void) |