]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/_rm48_hdk/adc.c
8f0ede023e7c5a8ed43784d1923e6c923131d71d
[pes-rpp/rpp-lib.git] / rpp / src / rpp / _rm48_hdk / adc.c
1 /* Copyright (C) 2013 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * This document contains proprietary information belonging to Czech
7  * Technical University in Prague. Passing on and copying of this
8  * document, and communication of its contents is not permitted
9  * without prior written authorization.
10  *
11  * File : adc.c
12  * Abstract:
13  *     Analog Input RPP API implementation file.
14  *
15  * References:
16  *     adc.h
17  *     RPP API documentation.
18  */
19
20
21 #include "rpp/rpp.h"
22
23 #ifndef FREERTOS_POSIX
24 #include "drv/drv.h"
25
26 // RPP has 12 ADC channels
27 // See s_adcSelect and s_adcFiFoSize in ti_drv_adc.c for hardware configuration.
28 #define ADC_CHANNELS 16
29 static adcData_t in_cache[ADC_CHANNELS];
30 #endif
31
32 static boolean_t initialized = FALSE;
33
34 int8_t rpp_adc_init()
35 {
36         if (initialized)
37                 return FAILURE;
38         initialized = TRUE;
39
40 #ifndef FREERTOS_POSIX
41         drv_adc_init();
42 #endif
43
44         return SUCCESS;
45 }
46
47
48 int16_t rpp_adc_get(uint8_t pin)
49 {
50         if ((pin < 1) || (pin > 16))
51                 return -1;
52
53         int16_t result = 0;
54
55 #ifndef FREERTOS_POSIX
56         // This conversion uint16_t -> int16_t is safe because we know values
57         // are 12bits. Here we are masking those 12bits just in case.
58         result = (int16_t)(in_cache[pin - 1].value & 0xFFF);
59 #endif
60
61         return result;
62 }
63
64
65 int8_t rpp_adc_update()
66 {
67 #ifndef FREERTOS_POSIX
68         // in_cache is thread safe because it's only write operation is mutexed
69         drv_adc_read_adc((adcData_t *)&in_cache);
70 #endif
71
72         return SUCCESS;
73 }