]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/mout.c
mout: Fix missing unlock + unify the code
[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         RPP_MUTEX_UNLOCK(mutex_mout);
73         if (ret != SUCCESS)
74                 return -3;
75
76         // FIXME Wait some time for synchronization
77         // Don't use vTaskDelay() here because the minimum time to wait is one tick,
78         // and depending of the configuration of the user model one tick can overrun
79         // the program.
80         int wait;
81         for (wait = 0; wait < 10; wait++) {
82                 asm (" nop");
83         }
84
85         RPP_MUTEX_LOCK(mutex_mout);
86         // Get value back and compare
87         ret = drv_mout_diag(idx);
88         RPP_MUTEX_UNLOCK(mutex_mout);
89         if (ret < 0) {
90                 return -4;
91         }
92
93 #else
94         RPP_MUTEX_LOCK(mutex_mout);
95         cache[idx] = val;
96         RPP_MUTEX_UNLOCK(mutex_mout);
97 #endif
98
99         return SUCCESS;
100 }
101
102
103 int8_t rpp_mout_get(uint8_t pin)
104 {
105         // Check range
106         if ((pin < 1) || (pin > 6))
107                 return -1;
108
109         RPP_MUTEX_LOCK(mutex_mout);
110         int8_t ret_val = cache[pin - 1];
111         RPP_MUTEX_UNLOCK(mutex_mout);
112
113         return ret_val;
114 }
115
116
117 int8_t rpp_mout_diag(uint8_t pin)
118 {
119         int ret;
120         // Check range
121         if ((pin < 1) || (pin > 6))
122                 return -RPP_EINVAL;
123
124 #ifndef FREERTOS_POSIX
125         RPP_MUTEX_LOCK(mutex_mout);
126         ret = drv_mout_diag(pin - 1);
127         RPP_MUTEX_UNLOCK(mutex_mout);
128 #endif
129         return ret;
130 }