]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/mout.c
mout: Remove stupid waiting in rpp_mout_set()
[pes-rpp/rpp-lib.git] / rpp / src / rpp / mout.c
1 /* Copyright (C) 2013, 2016 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         int ret;
56         // Check range
57         if ((pin < 1) || (pin > 6))
58                 return -1;
59
60         // Check val
61         if ((val != HIGH) && (val != LOW))
62                 return -2;
63
64         uint8_t idx = pin - 1;
65
66 #ifndef FREERTOS_POSIX
67         // Set and store value
68         RPP_MUTEX_LOCK(mutex_mout);
69         ret = drv_mout_set(idx, val);
70         if (ret == SUCCESS)
71                 cache[idx] = val;
72         else {
73                 RPP_MUTEX_UNLOCK(mutex_mout);
74                 return -3;
75         }
76
77         ret = drv_mout_diag(idx);
78         RPP_MUTEX_UNLOCK(mutex_mout);
79         if (ret == 0)
80                 return -4;
81 #else
82         RPP_MUTEX_LOCK(mutex_mout);
83         cache[idx] = val;
84         RPP_MUTEX_UNLOCK(mutex_mout);
85 #endif
86
87         return SUCCESS;
88 }
89
90
91 int8_t rpp_mout_get(uint8_t pin)
92 {
93         // Check range
94         if ((pin < 1) || (pin > 6))
95                 return -1;
96
97         RPP_MUTEX_LOCK(mutex_mout);
98         int8_t ret_val = cache[pin - 1];
99         RPP_MUTEX_UNLOCK(mutex_mout);
100
101         return ret_val;
102 }
103
104
105 int8_t rpp_mout_diag(uint8_t pin)
106 {
107         int ret;
108         // Check range
109         if ((pin < 1) || (pin > 6))
110                 return -RPP_EINVAL;
111
112 #ifndef FREERTOS_POSIX
113         RPP_MUTEX_LOCK(mutex_mout);
114         ret = drv_mout_diag(pin - 1);
115         RPP_MUTEX_UNLOCK(mutex_mout);
116 #endif
117         return ret;
118 }