]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/adc.c
911b6e1244dbf962fcd7af3042d67d5db3f0cbb2
[pes-rpp/rpp-lib.git] / rpp / src / rpp / adc.c
1 /* Copyright (C) 2013, 2015 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 #include "drv/port.h"
26
27 static uint16_t in_cache[PORT_ADC_CHANNELS];
28 #endif
29
30 static boolean_t initialized = FALSE;
31
32 int8_t rpp_adc_init()
33 {
34         if (initialized)
35                 return FAILURE;
36
37         initialized = TRUE;
38
39 #ifndef FREERTOS_POSIX
40         drv_adc_init();
41 #endif
42
43         return SUCCESS;
44 }
45
46
47 int16_t rpp_adc_get(uint8_t pin)
48 {
49         if ((pin < 1) || (pin > PORT_ADC_CHANNELS))
50                 return -1;
51
52         int16_t result = 0;
53
54 #ifndef FREERTOS_POSIX
55         // This conversion uint16_t -> int16_t is safe because we know values
56         // are 12bits. Here we are masking those 12bits just in case.
57         result = in_cache[pin - 1] & 0xFFF;
58 #endif
59
60         return result;
61 }
62
63
64 int8_t rpp_adc_update()
65 {
66 #ifndef FREERTOS_POSIX
67         const struct port_desc *port = &port_desc[PORT_ID_ADC];
68
69         /* Thread safety is ensured in port_adc_get() resp.
70          * drv_adc_generic_read() */
71         port->get(port, in_cache, sizeof(in_cache));
72 #endif
73
74         return SUCCESS;
75 }