]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/mout.c
c5a93d61b7e8641c037ca1a02ead733274f5244f
[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 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 : mout.c
12  * Abstract:
13  *     Power Output (12V, 2A, Push/Pull) RPP API implementation file.
14  *
15  * References:
16  *     mout.h
17  *     RPP API documentation.
18  */
19
20
21 #include "rpp/rpp.h"
22 #include "rpp/mutex.h"
23
24 RPP_MUTEX_DEFINE(mutex_mout);
25
26 #ifndef FREERTOS_POSIX
27 #include "drv/mout.h"
28 #endif
29
30 static boolean_t initialized = FALSE;
31
32 int8_t rpp_mout_init()
33 {
34         if (initialized)
35                 return FAILURE;
36         if (!RPP_MUTEX_INIT(mutex_mout))
37                 return FAILURE;
38         initialized = TRUE;
39 #ifndef FREERTOS_POSIX
40         dmmInit();
41         gioInit();
42         hetInit();
43 #endif
44
45         return SUCCESS;
46 }
47
48
49 static uint8_t cache[] = {
50         LOW, LOW, LOW, LOW, LOW, LOW
51 };
52
53 int8_t rpp_mout_set(uint8_t pin, uint8_t val)
54 {
55         // Check range
56         if ((pin < 1) || (pin > 6))
57                 return -1;
58
59         // Check val
60         if ((val != HIGH) && (val != LOW))
61                 return -2;
62
63         uint8_t idx = pin - 1;
64
65 #ifndef FREERTOS_POSIX
66         // Set and store value
67         RPP_MUTEX_LOCK(mutex_mout);
68         if (drv_mout_set(idx, val) != SUCCESS)
69                 return -3;
70         cache[idx] = val;
71         RPP_MUTEX_UNLOCK(mutex_mout);
72
73         // FIXME Wait some time for synchronization
74         // Don't use vTaskDelay() here because the minimum time to wait is one tick,
75         // and depending of the configuration of the user model one tick can overrun
76         // the program.
77         int wait;
78         for (wait = 0; wait < 10; wait++) {
79                 asm (" nop");
80         }
81
82         RPP_MUTEX_LOCK(mutex_mout);
83         // Get value back and compare
84         if (drv_mout_diag(idx) == FAILURE) {
85                 RPP_MUTEX_UNLOCK(mutex_mout);
86                 return -4;
87         }
88         RPP_MUTEX_UNLOCK(mutex_mout);
89
90 #else
91         RPP_MUTEX_LOCK(mutex_mout);
92         cache[idx] = val;
93         RPP_MUTEX_UNLOCK(mutex_mout);
94 #endif
95
96         return SUCCESS;
97 }
98
99
100 int8_t rpp_mout_get(uint8_t pin)
101 {
102         // Check range
103         if ((pin < 1) || (pin > 6))
104                 return -1;
105
106         RPP_MUTEX_LOCK(mutex_mout);
107         int8_t ret_val = cache[pin - 1];
108         RPP_MUTEX_UNLOCK(mutex_mout);
109
110         return ret_val;
111 }
112
113
114 int8_t rpp_mout_diag(uint8_t pin)
115 {
116         // Check range
117         if ((pin < 1) || (pin > 6))
118                 return -1;
119
120 #ifndef FREERTOS_POSIX
121         RPP_MUTEX_LOCK(mutex_mout);
122         if (drv_mout_diag(pin - 1) == 0) {
123                 RPP_MUTEX_UNLOCK(mutex_mout);
124                 return FAILURE;
125         }
126 #endif
127         return SUCCESS;
128 }