1 /* Copyright (C) 2012-2013 Czech Technical University in Prague
5 * - Carlos Jenkins <carlos@jenkins.co.cr>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * RPP driver implementation for ADC.
31 // Binary semaphore for ADC1 GRP1 conversion (AIN)
32 xSemaphoreHandle adcSemaphore_ADC1GRP1;
33 // Binary semaphore for ADC2 GRP1 conversion (HOUT-IFBK)
34 xSemaphoreHandle adcSemaphore_ADC2GRP1;
36 // Mutex for AIN read control
37 xSemaphoreHandle adcMutex_AIN;
38 // Mutex for HOUTIFBK control
39 xSemaphoreHandle adcMutex_HOUTIFBK;
44 vSemaphoreCreateBinary(adcSemaphore_ADC1GRP1);
45 xSemaphoreTake(adcSemaphore_ADC1GRP1, 0);
46 vSemaphoreCreateBinary(adcSemaphore_ADC2GRP1);
47 xSemaphoreTake(adcSemaphore_ADC2GRP1, 0);
49 adcMutex_AIN = xSemaphoreCreateMutex();
50 adcMutex_HOUTIFBK = xSemaphoreCreateMutex();
58 * ADC notification called by ADC conversion finished ISR.
60 * This procedure will just give semaphore.
62 * @param[in] adc Pointer to ADC module:
63 * - adcREG1: ADC1 module pointer
64 * - adcREG2: ADC2 module pointer
65 * @param[in] group Hardware group of ADC module:
66 * - adcGROUP0: ADC event group
67 * - adcGROUP1: ADC group 1
68 * - adcGROUP2: ADC group 2
70 void adcNotification(adcBASE_t *adc, uint32_t group)
72 if(adcIsConversionComplete(adc, group) == ADC_CONVERSION_IS_FINISHED) {
78 // Group0 is unused on RPP
81 // According to FreeRTOS documentation second parameter is
82 // optional (and can be set to NULL) from FreeRTOS
83 // V7.3.0. We are using 7.0.2. I confirmed this in the
84 // source code: src/os/queue.c line 821. - Carlos
86 signed portBASE_TYPE dummy;
87 xSemaphoreGiveFromISR(adcSemaphore_ADC1GRP1, &dummy);
91 // Group2 is unused on RPP
98 // Group0 is unused on RPP
102 signed portBASE_TYPE dummy;
103 xSemaphoreGiveFromISR(adcSemaphore_ADC2GRP1, &dummy);
107 // Group2 is unused on RPP
115 uint32_t drv_adc_generic_read(adcBASE_t *adc, uint32_t group,
116 xSemaphoreHandle adcSemaphore, adcData_t* data)
120 adcMidPointCalibration(adc);
123 adcEnableNotification(adc, group);
124 adcStartConversion(adc, group);
126 // Wait for conversion to complete
127 xSemaphoreTake(adcSemaphore, portMAX_DELAY);
128 adcDisableNotification(adc, group);
131 uint32_t channels = adcGetData(adc, group, data);
133 // Check if memory overrun
134 if(adcIsFifoFull(adc, group) == ADC_FIFO_OVERFLOW) {
135 // FIXME Should report somehow.
136 adcResetFiFo(adc, group);
143 uint32_t drv_adc_read_ain(adcData_t* data)
145 xSemaphoreTake(adcMutex_AIN, portMAX_DELAY);
146 uint32_t result = drv_adc_generic_read(
149 adcSemaphore_ADC1GRP1,
152 xSemaphoreGive(adcMutex_AIN);
156 uint32_t drv_adc_read_houtifbk(adcData_t* data)
158 xSemaphoreTake(adcMutex_HOUTIFBK, portMAX_DELAY);
159 uint32_t result = drv_adc_generic_read(
162 adcSemaphore_ADC2GRP1,
165 xSemaphoreGive(adcMutex_HOUTIFBK);
169 // FIXME make it work again
170 uint32_t adc_get_port_val(uint32_t* config, uint32_t num_channels,
174 adcBASE_t* adcReg = (adcBASE_t *)config[0];
175 uint32_t adcGroup = config[1];
177 adcData.adc_data = adc_data_origin;
178 adcData.ch_count = num_channels;
179 if (read_adc(adcReg, adcGroup) == 1)
182 for (i = 0; i < num_channels; i++) {
183 values[i] = adcData.adc_data[i].value;
184 values[i+num_channels] = adcData.adc_data[i].id;
186 adcData.adc_data = NULL;