#include "drv/drv.h"
#include "drv/port.h"
-// Binary semaphore for ADC1 GRP1 conversion (AIN)
-xSemaphoreHandle adcSemaphore_ADC1GRP1;
-// Binary semaphore for ADC2 GRP1 conversion (HOUT-IFBK)
-xSemaphoreHandle adcSemaphore_ADC2GRP1;
+// Binary semaphores for finish notifications
+static xSemaphoreHandle sem[2][2];
-// Mutex for AIN read control
-xSemaphoreHandle adcMutex_ADC;
-// Mutex for HOUTIFBK control
-xSemaphoreHandle adcMutex_HOUTIFBK;
+// Mutex for two ADCs
+xSemaphoreHandle mutex[2];
void drv_adc_init()
{
- // Create semaphores
- vSemaphoreCreateBinary(adcSemaphore_ADC1GRP1);
- xSemaphoreTake(adcSemaphore_ADC1GRP1, 0);
- vSemaphoreCreateBinary(adcSemaphore_ADC2GRP1);
- xSemaphoreTake(adcSemaphore_ADC2GRP1, 0);
-
- adcMutex_ADC = xSemaphoreCreateMutex();
- adcMutex_HOUTIFBK = xSemaphoreCreateMutex();
+ int adc, grp;
+ for (adc = 0; adc < 2; adc++) {
+ for (grp = 0; grp < 2; grp++) {
+ vSemaphoreCreateBinary(sem[adc][grp]);
+ xSemaphoreTake(sem[adc][grp], 0);
+ }
+ mutex[adc] = xSemaphoreCreateMutex();
+ }
// Low level init
adcInit();
}
void adcNotification(adcBASE_t *adc, uint32_t group)
{
if (adcIsConversionComplete(adc, group) == ADC_CONVERSION_IS_FINISHED) {
-
- // ADC1
- if (adc == adcREG1) {
- switch (group) {
- case adcGROUP0:
- // Group0 is unused on RPP
- break;
- case adcGROUP1:
- // According to FreeRTOS documentation second parameter is
- // optional (and can be set to NULL) from FreeRTOS
- // V7.3.0. We are using 7.0.2. I confirmed this in the
- // source code: src/os/queue.c line 821. - Carlos
- {
- signed portBASE_TYPE dummy;
- xSemaphoreGiveFromISR(adcSemaphore_ADC1GRP1, &dummy);
- }
- break;
- default:
- // Group2 is unused on RPP
- break;
- }
- // ADC2
- }
- else {
- switch (group) {
- case adcGROUP0:
- // Group0 is unused on RPP
- break;
- case adcGROUP1:
- {
- signed portBASE_TYPE dummy;
- xSemaphoreGiveFromISR(adcSemaphore_ADC2GRP1, &dummy);
- }
- break;
- default:
- // Group2 is unused on RPP
- break;
- }
- }
+ signed portBASE_TYPE dummy;
+ int adc_idx = (adc == adcREG1) ? 0 : 1;
+ int grp_idx = (group == adcGROUP1) ? 0 : 1;
+ xSemaphoreGiveFromISR(sem[adc_idx][grp_idx], &dummy);
}
}
uint32_t drv_adc_generic_read(adcBASE_t *adc, uint32_t group,
- xSemaphoreHandle adcSemaphore, adcData_t *data)
+ adcData_t *data)
{
+ int adc_idx = (adc == adcREG1) ? 0 : 1;
+ int grp_idx = (group == adcGROUP1) ? 0 : 1;
+
+ xSemaphoreTake(mutex[adc_idx], portMAX_DELAY);
+
// Calibrate
adcCalibration(adc);
adcMidPointCalibration(adc);
adcStartConversion(adc, group);
// Wait for conversion to complete
- xSemaphoreTake(adcSemaphore, portMAX_DELAY);
+ xSemaphoreTake(sem[adc_idx][grp_idx], portMAX_DELAY);
adcDisableNotification(adc, group);
// Get data
// FIXME Should report somehow.
adcResetFiFo(adc, group);
- return channels;
-}
-
-
-uint32_t drv_adc_read_adc(adcData_t *data)
-{
- xSemaphoreTake(adcMutex_ADC, portMAX_DELAY);
- uint32_t result = drv_adc_generic_read(
- adcREG1,
- adcGROUP1,
- adcSemaphore_ADC1GRP1,
- data
- );
- xSemaphoreGive(adcMutex_ADC);
- return result;
-}
+ xSemaphoreGive(mutex[adc_idx]);
-uint32_t drv_adc_read_houtifbk(adcData_t *data)
-{
- xSemaphoreTake(adcMutex_HOUTIFBK, portMAX_DELAY);
- uint32_t result = drv_adc_generic_read(
- adcREG2,
- adcGROUP1,
- adcSemaphore_ADC2GRP1,
- data
- );
- xSemaphoreGive(adcMutex_HOUTIFBK);
- return result;
+ return channels;
}
#define MIN(a,b) ((a) < (b) ? (a) : (b))
drv_adc_generic_read(
port->cfg.adc.reg,
port->cfg.adc.group,
- (port->cfg.adc.sem == 1) ? adcSemaphore_ADC1GRP1 : adcSemaphore_ADC2GRP1,
data
);