]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp/lib/rpp/src/rpp/mout.c
5f79514fd75a3aad3504377263d2a1a5761c8fd7
[pes-rpp/rpp-test-sw.git] / rpp / lib / 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     uint8_t idx = pin - 1;
67
68     #if rppCONFIG_DRV == 1
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) == FAILURE) {
86         return -4;
87     }
88     #else
89     cache[idx] = val;
90     #endif
91
92     return SUCCESS;
93 }
94
95
96 int8_t rpp_mout_get(uint8_t pin)
97 {
98     // Check range
99     if((pin < 1) || (pin > 6)) {
100         return -1;
101     }
102
103     return cache[pin - 1];
104 }
105
106
107 int8_t rpp_mout_diag(uint8_t pin)
108 {
109     // Check range
110     if((pin < 1) || (pin > 6)) {
111         return -1;
112     }
113
114     #if rppCONFIG_DRV == 1
115     if(drv_mout_diag(pin - 1) == 0) {
116         return FAILURE;
117     }
118     #endif
119     return SUCCESS;
120 }
121
122
123 #endif /* rppCONFIG_INCLUDE_MOUT */
124