]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/adc.c
Change the license from GPL to proprietary
[pes-rpp/rpp-lib.git] / rpp / src / rpp / 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 #if rppCONFIG_INCLUDE_ADC == 1
24
25 #if rppCONFIG_DRV == 1
26 #include "drv/drv.h"
27
28 // RPP has 12 ADC channels
29 // See s_adcSelect and s_adcFiFoSize in ti_drv_adc.c for hardware configuration.
30 #define ADC_CHANNELS 12
31 static adcData_t in_cache[ADC_CHANNELS];
32 #endif
33
34 static boolean_t initialized = FALSE;
35
36 int8_t rpp_adc_init()
37 {
38     if(initialized) {
39         return FAILURE;
40     }
41     initialized = TRUE;
42
43     #if rppCONFIG_DRV == 1
44     drv_adc_init();
45     #endif
46
47     return SUCCESS;
48 }
49
50
51 int16_t rpp_adc_get(uint8_t pin)
52 {
53     if((pin < 1) || (pin > 12)) {
54         return -1;
55     }
56
57     int16_t result = 0;
58
59     #if rppCONFIG_DRV == 1
60     // This conversion uint16_t -> int16_t is safe because we know values
61     // are 12bits. Here we are masking those 12bits just in case.
62     result = (int16_t) (in_cache[pin - 1].value & 0xFFF);
63     #endif
64
65     return result;
66 }
67
68
69 int8_t rpp_adc_update()
70 {
71     #if rppCONFIG_DRV == 1
72     // in_cache is thread safe because it's only write operation is mutexed
73     drv_adc_read_adc((adcData_t*)&in_cache);
74     #endif
75
76     return SUCCESS;
77 }
78
79
80 #endif /* rppCONFIG_INCLUDE_ADC */
81