]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/din.c
1892faa440cdf8703139185c6f53f73fbf6bb2e6
[pes-rpp/rpp-lib.git] / rpp / src / rpp / din.c
1 /* Copyright (C) 2013, 2015 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 : din.c
12  * Abstract:
13  *     Digital Input RPP API implementation file.
14  *
15  * References:
16  *     din.h
17  *     RPP API documentation.
18  */
19
20
21 #include "rpp/rpp.h"
22
23 #ifndef FREERTOS_POSIX
24 #include "drv/din.h"
25 #include "drv/spi_tms570.h"
26 #endif
27
28 static boolean_t initialized = FALSE;
29
30 int8_t rpp_din_init()
31 {
32         if (initialized)
33                 return FAILURE;
34         initialized = TRUE;
35 #ifndef FREERTOS_POSIX
36         dmmInit();
37         gioInit();
38         hetInit();
39         spi_tms570_init();
40 #endif
41
42         return SUCCESS;
43 }
44
45 int8_t rpp_din_ref(uint16_t ref_a, uint16_t ref_b)
46 {
47         if ((ref_a > 4095) || (ref_b > 4095))
48                 return -1;
49
50 #ifndef FREERTOS_POSIX
51         drv_din_ref(ref_a, ref_b);
52 #endif
53         return SUCCESS;
54 }
55
56
57 // Check for configuration changes to avoid SPI overhead
58 static boolean_t config_changed = FALSE;
59
60 // All cached values are 16 bits in the form [SG7,...,SG0][SP7,...,SP0]
61 static uint16_t pull_cache     = 0x0; /* 0 - pull-down, 1 - pull-up */
62 static uint16_t active_cache   = 0x0; /* 0 - tri-state, 1 - active */
63 static uint16_t can_wake_cache = 0x0;
64
65 static boolean_t check_pin_busy(uint8_t pin)
66 {
67         if (rpp_irc_status(RPP_IRC_1) == 1 && (pin == 10 || pin == 11))
68                 return TRUE;
69         if (rpp_irc_status(RPP_IRC_2) == 1 && (pin == 14 || pin == 15))
70                 return TRUE;
71         return FALSE;
72 }
73
74 int8_t rpp_din_setup(uint8_t pin, boolean_t pull_up,
75                                          boolean_t active, boolean_t can_wake)
76 {
77         // Check range
78         if (pin > 15)
79                 return -1;
80
81         // Check programmable feature
82         if (!pull_up && (pin > 7))
83                 return -2;
84
85         // Check blockade of specific pins
86         if (check_pin_busy(pin))
87                 return -RPP_EBUSY;
88
89         // Set bits
90         if (pull_up)
91                 bit_set(pull_cache, pin);
92         else
93                 bit_clear(pull_cache, pin);
94
95         if (active)
96                 bit_set(active_cache, pin);
97         else
98                 bit_clear(active_cache, pin);
99
100         if (can_wake)
101                 bit_set(can_wake_cache, pin);
102         else
103                 bit_clear(can_wake_cache, pin);
104
105         config_changed = TRUE;
106         return SUCCESS;
107 }
108
109
110 static uint16_t in_cache = 0x0;
111
112 int8_t rpp_din_get(uint8_t pin)
113 {
114         // Check range
115         if (pin > 15)
116                 return -1;
117
118         // Check blockade of specific pins
119         if (check_pin_busy(pin))
120                 return -RPP_EBUSY;
121
122
123         if (is_bit_set(in_cache, pin))
124                 return RPP_CLOSED;
125         return RPP_OPEN;
126 }
127
128 int8_t rpp_din_get_tr(uint8_t pin)
129 {
130         // Check range
131         if (pin < 8 || pin > 15)
132                 return -1;
133
134         // Check blockade of specific pins
135         if (check_pin_busy(pin))
136                 return -RPP_EBUSY;
137
138
139 #ifndef FREERTOS_POSIX
140         if (drv_din_get_varthr(pin) == 1)
141                 return HIGH;
142
143 #endif
144         return LOW;
145 }
146
147
148
149 static uint16_t diag_cache = 0x0;
150
151 int8_t rpp_din_diag(uint8_t pin)
152 {
153         // Check range
154         if (pin > 15)
155                 return -1;
156
157         // Check blockade of specific pins
158         if (check_pin_busy(pin))
159                 return -RPP_EBUSY;
160
161
162         if (is_bit_set(diag_cache, pin))
163                 return HIGH;
164         return LOW;
165 }
166
167 /*
168  * pouzivat din_mod s pouzivanim enumu
169  */
170 int8_t rpp_din_update()
171 {
172 #ifndef FREERTOS_POSIX
173         /// Setup pins
174         if (config_changed) {
175                 uint16_t sp = 0x0;
176                 uint16_t sg = 0x0;
177
178                 // Reset chip
179                 din_set_reg(DIN_RESET_CMD, 0, 0);
180                 //rpp_sci_printf("din_reset()\r\n");
181
182                 // Set pull-type.
183                 // In DRV logic is inverted:
184                 // DRV: 1 - set pin as switch-to-battery. RPP: 0 - pull-down.
185                 // DRV: 0 - set pin as switch-to-ground.  RPP: 1 - pull-up.
186                 sp = (~pull_cache) & 0xFF;
187                 din_set_reg(DIN_SETTINGS_CMD, 0xffff, sp);
188                 //rpp_sci_printf("din_set_pr(%X)\r\n", sp);
189
190                 // Set state type, active or tri-stated.
191                 // In DRV logic is inverted:
192                 // DRV: 1 - tri-state. RPP: 0 - tri-state.
193                 // DRV: 0 - active.    RPP: 1 - active.
194                 sp = ((~active_cache)     ) & 0xFF;
195                 sg = ((~active_cache) >> 8) & 0xFF;
196                 din_set_reg(DIN_TRI_STATE_CMD_YES, 0xffff, sp);
197                 din_set_reg(DIN_TRI_STATE_CMD_NO, 0xffff, sg);
198                 //rpp_sci_printf("din_set_stat(%X, %X)\r\n", sp, sg);
199
200                 // Set wake / interrupt.
201                 // IN DRV logic is not inverted.
202                 // DRV: 1 - can wake.           RPP: 1 - can wake.
203                 // DRV: 0 - interrupt disabled. RPP: 0 - interrupt disabled.
204                 sp = (can_wake_cache     ) & 0xFF;
205                 sg = (can_wake_cache >> 8) & 0xFF;
206
207                 din_set_reg(DIN_WAKE_UP_CMD_ENB, 0xffff, sp);
208                 din_set_reg(DIN_WAKE_UP_CMD_DIS, 0xffff, sg);
209                 //rpp_sci_printf("din_set_int(%X, %X)\r\n", sp, sg);
210
211                 // Mark configuration as commited
212                 config_changed = FALSE;
213         }
214
215         // Update cached values
216         din_set_reg(DIN_SWITCH_STATUS_CMD, 0, 0);
217         in_cache = din_get_val_word();
218
219         // FIXME: Implement. Dummy assign for now.
220         diag_cache = in_cache;
221
222         if (diag_cache != in_cache)
223                 return FAILURE;
224
225         #else /* ifndef FREERTOS_POSIX */
226         UNUSED(config_changed);
227         #endif /* ifndef FREERTOS_POSIX */
228
229         return SUCCESS;
230 }