+++ /dev/null
-/* Copyright (C) 2012-2013 Czech Technical University in Prague
- *
- * Authors:
- * - Michal Horn
- * - Carlos Jenkins <carlos@jenkins.co.cr>
- *
- * This document contains proprietary information belonging to Czech
- * Technical University in Prague. Passing on and copying of this
- * document, and communication of its contents is not permitted
- * without prior written authorization.
- *
- * File : adc.c
- * Abstract:
- * RPP driver implementation for ADC.
- *
- * References:
- * ti_drv_adc.c
- */
-
-
-#include "drv/drv.h"
-
-// Binary semaphore for ADC1 GRP1 conversion (AIN)
-xSemaphoreHandle adcSemaphore_ADC1GRP1;
-// Binary semaphore for ADC2 GRP1 conversion (HOUT-IFBK)
-xSemaphoreHandle adcSemaphore_ADC2GRP1;
-
-// Mutex for AIN read control
-xSemaphoreHandle adcMutex_ADC;
-
-void drv_adc_init()
-{
- // Create semaphores
- vSemaphoreCreateBinary(adcSemaphore_ADC1GRP1);
- xSemaphoreTake(adcSemaphore_ADC1GRP1, 0);
- vSemaphoreCreateBinary(adcSemaphore_ADC2GRP1);
- xSemaphoreTake(adcSemaphore_ADC2GRP1, 0);
-
- adcMutex_ADC = xSemaphoreCreateMutex();
-
- // Low level init
- adcInit();
-}
-
-
-/**
- * ADC notification called by ADC conversion finished ISR.
- *
- * This procedure will just give semaphore.
- *
- * @param[in] adc Pointer to ADC module:
- * - adcREG1: ADC1 module pointer
- * - adcREG2: ADC2 module pointer
- * @param[in] group Hardware group of ADC module:
- * - adcGROUP0: ADC event group
- * - adcGROUP1: ADC group 1
- * - adcGROUP2: ADC group 2
- */
-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;
- }
- }
- }
-}
-
-
-uint32_t drv_adc_generic_read(adcBASE_t *adc, uint32_t group,
- xSemaphoreHandle adcSemaphore, adcData_t *data)
-{
- // Calibrate
- adcCalibration(adc);
- adcMidPointCalibration(adc);
-
- // Start conversion
- adcEnableNotification(adc, group);
- adcStartConversion(adc, group);
-
- // Wait for conversion to complete
- xSemaphoreTake(adcSemaphore, portMAX_DELAY);
- adcDisableNotification(adc, group);
-
- // Get data
- uint32_t channels = adcGetData(adc, group, data);
-
- // Check if memory overrun
- if (adcIsFifoFull(adc, group) == ADC_FIFO_OVERFLOW)
- // 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;
-}
-
-uint32_t adc_get_port_val(uint32_t *config, uint32_t num_channels,
- uint32_t *values)
-{
- uint32_t i;
- adcBASE_t *adcReg = (adcBASE_t *)config[0];
- uint32_t adcGroup = config[1];
- uint32_t adcSemaphore = config[2];
- adcData_t data[ADC_MAX_CHANNELS];
-
- drv_adc_generic_read(
- adcReg,
- adcGroup,
- (adcSemaphore == 1) ? adcSemaphore_ADC1GRP1 : adcSemaphore_ADC2GRP1,
- data
- );
-
- for (i = 0; i < num_channels; i++) {
- values[i] = data[i].value;
- values[i+num_channels] = data[i].id;
- }
-
- return 0;
-}