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