]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blobdiff - rpp/src/rpp/adc.c
Make the RPP layer thread safe
[pes-rpp/rpp-lib.git] / rpp / src / rpp / adc.c
index 94bc84caa88e2cf0dc0df8f402aca06a686fee09..911b6e1244dbf962fcd7af3042d67d5db3f0cbb2 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2013 Czech Technical University in Prague
+/* Copyright (C) 2013, 2015 Czech Technical University in Prague
  *
  * Authors:
  *     - Carlos Jenkins <carlos@jenkins.co.cr>
 
 #include "rpp/rpp.h"
 
-#if rppCONFIG_DRV == 1
+#ifndef FREERTOS_POSIX
 #include "drv/drv.h"
+#include "drv/port.h"
 
-// RPP has 12 ADC channels
-// See s_adcSelect and s_adcFiFoSize in ti_drv_adc.c for hardware configuration.
-#define ADC_CHANNELS 12
-static adcData_t in_cache[ADC_CHANNELS];
+static uint16_t in_cache[PORT_ADC_CHANNELS];
 #endif
 
 static boolean_t initialized = FALSE;
 
 int8_t rpp_adc_init()
 {
-    if(initialized) {
-        return FAILURE;
-    }
-    initialized = TRUE;
+       if (initialized)
+               return FAILURE;
 
-    #if rppCONFIG_DRV == 1
-    drv_adc_init();
-    #endif
+       initialized = TRUE;
 
-    return SUCCESS;
+#ifndef FREERTOS_POSIX
+       drv_adc_init();
+#endif
+
+       return SUCCESS;
 }
 
 
 int16_t rpp_adc_get(uint8_t pin)
 {
-    if((pin < 1) || (pin > 12)) {
-        return -1;
-    }
+       if ((pin < 1) || (pin > PORT_ADC_CHANNELS))
+               return -1;
 
-    int16_t result = 0;
+       int16_t result = 0;
 
-    #if rppCONFIG_DRV == 1
-    // This conversion uint16_t -> int16_t is safe because we know values
-    // are 12bits. Here we are masking those 12bits just in case.
-    result = (int16_t) (in_cache[pin - 1].value & 0xFFF);
-    #endif
+#ifndef FREERTOS_POSIX
+       // This conversion uint16_t -> int16_t is safe because we know values
+       // are 12bits. Here we are masking those 12bits just in case.
+       result = in_cache[pin - 1] & 0xFFF;
+#endif
 
-    return result;
+       return result;
 }
 
 
 int8_t rpp_adc_update()
 {
-    #if rppCONFIG_DRV == 1
-    // in_cache is thread safe because it's only write operation is mutexed
-    drv_adc_read_adc((adcData_t*)&in_cache);
-    #endif
+#ifndef FREERTOS_POSIX
+       const struct port_desc *port = &port_desc[PORT_ID_ADC];
+
+       /* Thread safety is ensured in port_adc_get() resp.
+        * drv_adc_generic_read() */
+       port->get(port, in_cache, sizeof(in_cache));
+#endif
 
-    return SUCCESS;
+       return SUCCESS;
 }