diff options
Diffstat (limited to 'firmware/target/arm/gigabeat/meg-fx/adc-meg-fx.c')
-rw-r--r-- | firmware/target/arm/gigabeat/meg-fx/adc-meg-fx.c | 122 |
1 files changed, 94 insertions, 28 deletions
diff --git a/firmware/target/arm/gigabeat/meg-fx/adc-meg-fx.c b/firmware/target/arm/gigabeat/meg-fx/adc-meg-fx.c index 78b9dea5b2..4c448c2e41 100644 --- a/firmware/target/arm/gigabeat/meg-fx/adc-meg-fx.c +++ b/firmware/target/arm/gigabeat/meg-fx/adc-meg-fx.c | |||
@@ -18,24 +18,67 @@ | |||
18 | ****************************************************************************/ | 18 | ****************************************************************************/ |
19 | #include "cpu.h" | 19 | #include "cpu.h" |
20 | #include "adc-target.h" | 20 | #include "adc-target.h" |
21 | #include "kernel.h" | ||
21 | 22 | ||
22 | 23 | ||
23 | void adc_init(void) { | ||
24 | /* Turn on the ADC PCLK */ | ||
25 | CLKCON |= (1<<15); | ||
26 | 24 | ||
27 | /* Set channel 0, normal mode, disable "start by read" */ | 25 | static unsigned short adc_readings[NUM_ADC_CHANNELS]; |
28 | ADCCON &= ~(0x3F); | ||
29 | 26 | ||
30 | /* No start delay. Use nromal conversion mode. */ | 27 | /* prototypes */ |
31 | ADCDLY |= 0x1; | 28 | static unsigned short __adc_read(int channel); |
29 | static void adc_tick(void); | ||
32 | 30 | ||
33 | /* Set and enable the prescaler */ | 31 | |
34 | ADCCON = (ADCCON & ~(0xff<<6)) | (0x19<<6); | 32 | |
35 | ADCCON |= (1<<14); | 33 | void adc_init(void) |
34 | { | ||
35 | int i; | ||
36 | |||
37 | /* Turn on the ADC PCLK */ | ||
38 | CLKCON |= (1<<15); | ||
39 | |||
40 | /* Set channel 0, normal mode, disable "start by read" */ | ||
41 | ADCCON &= ~(0x3F); | ||
42 | |||
43 | /* No start delay. Use normal conversion mode. */ | ||
44 | ADCDLY = 0x1; | ||
45 | |||
46 | /* Set and enable the prescaler */ | ||
47 | ADCCON = (ADCCON & ~(0xff<<6)) | (0x19<<6); | ||
48 | ADCCON |= (1<<14); | ||
49 | |||
50 | /* prefill the adc channels */ | ||
51 | for (i = 0; i < NUM_ADC_CHANNELS; i++) | ||
52 | { | ||
53 | adc_readings[i] = __adc_read(i); | ||
54 | } | ||
55 | |||
56 | /* start at zero so when the tick starts it is at zero */ | ||
57 | adc_readings[0] = __adc_read(0); | ||
58 | |||
59 | /* attach the adc reading to the tick */ | ||
60 | tick_add_task(adc_tick); | ||
61 | |||
62 | |||
63 | } | ||
64 | |||
65 | |||
66 | |||
67 | /* Called to get the recent ADC reading */ | ||
68 | inline unsigned short adc_read(int channel) | ||
69 | { | ||
70 | return adc_readings[channel]; | ||
36 | } | 71 | } |
37 | 72 | ||
38 | unsigned short adc_read(int channel) { | 73 | |
74 | |||
75 | /** | ||
76 | * Read the ADC by polling | ||
77 | * @param channel The ADC channel to read | ||
78 | * @return 10bit reading from ADC channel or ADC_READ_ERROR if timeout | ||
79 | */ | ||
80 | static unsigned short __adc_read(int channel) | ||
81 | { | ||
39 | int i; | 82 | int i; |
40 | 83 | ||
41 | /* Set the channel */ | 84 | /* Set the channel */ |
@@ -45,34 +88,57 @@ unsigned short adc_read(int channel) { | |||
45 | ADCCON |= 0x1; | 88 | ADCCON |= 0x1; |
46 | 89 | ||
47 | /* Wait for a low Enable_start */ | 90 | /* Wait for a low Enable_start */ |
48 | i = 20000; | 91 | for (i = 20000;;) { |
49 | while(i > 0) { | 92 | if(0 == (ADCCON & 0x1)) { |
50 | if(ADCCON & 0x1) { | 93 | break; |
51 | i--; | ||
52 | } | 94 | } |
53 | else { | 95 | else { |
54 | break; | 96 | i--; |
97 | if (0 == i) { | ||
98 | /* Ran out of time */ | ||
99 | return ADC_READ_ERROR; | ||
100 | } | ||
55 | } | 101 | } |
56 | } | 102 | } |
57 | if(i == 0) { | ||
58 | /* Ran out of time */ | ||
59 | return(0); | ||
60 | } | ||
61 | 103 | ||
62 | /* Wait for high End_of_Conversion */ | 104 | /* Wait for high End_of_Conversion */ |
63 | i = 20000; | 105 | for(i = 20000;;) { |
64 | while(i > 0) { | 106 | if(ADCCON & (1<<15)) { |
65 | if(ADCCON & (1<<15)) { | ||
66 | break; | 107 | break; |
67 | } | 108 | } |
68 | else { | 109 | else { |
69 | i--; | 110 | i--; |
111 | if(0 == i) { | ||
112 | /* Ran out of time */ | ||
113 | return ADC_READ_ERROR; | ||
114 | } | ||
70 | } | 115 | } |
71 | } | 116 | } |
72 | if(i == 0) { | ||
73 | /* Ran out of time */ | ||
74 | return(0); | ||
75 | } | ||
76 | 117 | ||
77 | return(ADCDAT0 & 0x3ff); | 118 | return (ADCDAT0 & 0x3ff); |
119 | } | ||
120 | |||
121 | |||
122 | |||
123 | /* add this to the tick so that the ADC converts are done in the background */ | ||
124 | static void adc_tick(void) | ||
125 | { | ||
126 | static unsigned channel; | ||
127 | |||
128 | /* Check if the End Of Conversion is set */ | ||
129 | if (ADCCON & (1<<15)) | ||
130 | { | ||
131 | adc_readings[channel] = (ADCDAT0 & 0x3FF); | ||
132 | if (++channel >= NUM_ADC_CHANNELS) | ||
133 | { | ||
134 | channel = 0; | ||
135 | } | ||
136 | |||
137 | /* setup the next conversion and start it*/ | ||
138 | ADCCON = (ADCCON & ~(0x7<<3)) | (channel<<3) | 0x01; | ||
139 | } | ||
78 | } | 140 | } |
141 | |||
142 | |||
143 | |||
144 | |||