]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/mout.c
Implemented RPP API for MOUT.
[pes-rpp/rpp-lib.git] / rpp / src / rpp / mout.c
1 /* Copyright (C) 2013 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * File : mout.c
20  * Abstract:
21  *     Power Output (12V, 2A, Push/Pull) RPP API implementation file.
22  *
23  * References:
24  *     mout.h
25  *     RPP API documentation.
26  */
27
28
29 #include "rpp/rpp.h"
30
31 #if rppCONFIG_INCLUDE_MOUT == 1
32
33 #if rppCONFIG_DRV == 1
34 #include "drv/mout.h"
35 #endif
36
37 static boolean_t initialized = FALSE;
38
39 int8_t rpp_mout_init()
40 {
41     if(initialized) {
42         return FAILURE;
43     }
44     initialized = TRUE;
45
46     return SUCCESS;
47 }
48
49
50 static uint8_t cache[] = {
51     LOW, LOW, LOW, LOW, LOW, LOW
52 };
53
54 int8_t rpp_mout_set(uint8_t pin, uint8_t val)
55 {
56     // Check range
57     if((pin < 1) || (pin > 6)) {
58         return -1;
59     }
60
61     // Check val
62     if((val != HIGH) && (val != LOW)) {
63         return -2;
64     }
65
66     #if rppCONFIG_DRV == 1
67     uint8_t idx = pin - 1;
68
69     // Set and store value
70     if(drv_mout_set(idx, val) != SUCCESS) {
71         return -3;
72     }
73     cache[idx] = val;
74
75     // FIXME Wait some time for synchronization
76     // Don't use vTaskDelay() here because the minimum time to wait is one tick,
77     // and depending of the configuration of the user model one tick can overrun
78     // the program.
79     int wait;
80     for(wait = 0; wait < 10; wait++) {
81         asm("nop");
82     }
83
84     // Get value back and compare
85     if(drv_mout_diag(idx) != val) {
86         return -4;
87     }
88     #endif
89
90     return SUCCESS;
91 }
92
93
94 int8_t rpp_mout_get(uint8_t pin)
95 {
96     // Check range
97     if((pin < 1) || (pin > 6)) {
98         return -1;
99     }
100
101     return cache[pin - 1];
102 }
103
104
105 int8_t rpp_mout_diag(uint8_t pin)
106 {
107     // Check range
108     if((pin < 1) || (pin > 6)) {
109         return -1;
110     }
111
112     #if rppCONFIG_DRV == 1
113     return drv_mout_diag(pin - 1);
114     #else
115     return LOW;
116     #endif
117 }
118
119
120 #endif /* rppCONFIG_INCLUDE_MOUT */
121