1 /* Copyright (C) 2013, 2015 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
29 * Digital Input RPP API implementation file.
33 * RPP API documentation.
38 #include "rpp/mutex.h"
40 #ifndef FREERTOS_POSIX
42 #include "drv/spi_def.h"
45 RPP_MUTEX_DEFINE(mutex_din);
47 static boolean_t initialized = FALSE;
53 if (!RPP_MUTEX_INIT(mutex_din))
56 #ifndef FREERTOS_POSIX
66 int8_t rpp_din_ref(uint16_t ref_a, uint16_t ref_b)
68 if ((ref_a > 4095) || (ref_b > 4095))
71 #ifndef FREERTOS_POSIX
72 RPP_MUTEX_LOCK(mutex_din);
73 drv_din_ref(ref_a, ref_b);
74 RPP_MUTEX_UNLOCK(mutex_din);
80 // Check for configuration changes to avoid SPI overhead
81 static boolean_t config_changed = FALSE;
83 // All cached values are 16 bits in the form [SG7,...,SG0][SP7,...,SP0]
84 static uint16_t pull_cache = 0x0; /* 0 - pull-down, 1 - pull-up */
85 static uint16_t active_cache = 0x0; /* 0 - tri-state, 1 - active */
86 static uint16_t can_wake_cache = 0x0;
88 static boolean_t check_pin_busy(uint8_t pin)
90 if (rpp_irc_status(RPP_IRC_1) == 1 && (pin == 10 || pin == 11))
92 if (rpp_irc_status(RPP_IRC_2) == 1 && (pin == 14 || pin == 15))
97 int8_t rpp_din_setup(uint8_t pin, boolean_t pull_up,
98 boolean_t active, boolean_t can_wake)
104 // Check programmable feature
105 if (!pull_up && (pin > 7))
108 RPP_MUTEX_LOCK(mutex_din);
109 // Check blockade of specific pins
110 if (check_pin_busy(pin)) {
111 RPP_MUTEX_UNLOCK(mutex_din);
117 bit_set(pull_cache, pin);
119 bit_clear(pull_cache, pin);
122 bit_set(active_cache, pin);
124 bit_clear(active_cache, pin);
127 bit_set(can_wake_cache, pin);
129 bit_clear(can_wake_cache, pin);
131 config_changed = TRUE;
132 RPP_MUTEX_UNLOCK(mutex_din);
137 static uint16_t in_cache = 0x0;
139 int8_t rpp_din_get(uint8_t pin)
145 // Check blockade of specific pins
146 if (check_pin_busy(pin))
149 return is_bit_set(in_cache, pin) ? RPP_CLOSED : RPP_OPEN;
152 int8_t rpp_din_get_tr(uint8_t pin)
155 if (pin < 8 || pin > 15)
158 // Check blockade of specific pins
159 if (check_pin_busy(pin))
162 #ifndef FREERTOS_POSIX
163 if (drv_din_get_varthr(pin) == 1)
171 static uint16_t diag_cache = 0x0;
173 int8_t rpp_din_diag(uint8_t pin)
179 // Check blockade of specific pins
180 if (check_pin_busy(pin))
183 return is_bit_set(diag_cache, pin) ? HIGH : LOW;
187 * pouzivat din_mod s pouzivanim enumu
189 int8_t rpp_din_update()
191 RPP_MUTEX_LOCK(mutex_din);
192 #ifndef FREERTOS_POSIX
194 if (config_changed) {
199 din_set_reg(DIN_RESET_CMD, 0, 0);
200 //rpp_sci_printf("din_reset()\r\n");
203 // In DRV logic is inverted:
204 // DRV: 1 - set pin as switch-to-battery. RPP: 0 - pull-down.
205 // DRV: 0 - set pin as switch-to-ground. RPP: 1 - pull-up.
206 sp = (~pull_cache) & 0xFF;
207 din_set_reg(DIN_SETTINGS_CMD, 0xffff, sp);
208 //rpp_sci_printf("din_set_pr(%X)\r\n", sp);
210 // Set state type, active or tri-stated.
211 // In DRV logic is inverted:
212 // DRV: 1 - tri-state. RPP: 0 - tri-state.
213 // DRV: 0 - active. RPP: 1 - active.
214 sp = ((~active_cache) ) & 0xFF;
215 sg = ((~active_cache) >> 8) & 0xFF;
216 din_set_reg(DIN_TRI_STATE_CMD_YES, 0xffff, sp);
217 din_set_reg(DIN_TRI_STATE_CMD_NO, 0xffff, sg);
218 //rpp_sci_printf("din_set_stat(%X, %X)\r\n", sp, sg);
220 // Set wake / interrupt.
221 // IN DRV logic is not inverted.
222 // DRV: 1 - can wake. RPP: 1 - can wake.
223 // DRV: 0 - interrupt disabled. RPP: 0 - interrupt disabled.
224 sp = (can_wake_cache ) & 0xFF;
225 sg = (can_wake_cache >> 8) & 0xFF;
227 din_set_reg(DIN_WAKE_UP_CMD_ENB, 0xffff, sp);
228 din_set_reg(DIN_WAKE_UP_CMD_DIS, 0xffff, sg);
229 //rpp_sci_printf("din_set_int(%X, %X)\r\n", sp, sg);
231 // Mark configuration as commited
232 config_changed = FALSE;
235 // Update cached values
236 din_set_reg(DIN_SWITCH_STATUS_CMD, 0, 0);
237 in_cache = din_get_val_word();
239 // FIXME: Implement. Dummy assign for now.
240 diag_cache = in_cache;
242 if (diag_cache != in_cache) {
243 RPP_MUTEX_UNLOCK(mutex_din);
247 #else /* ifndef FREERTOS_POSIX */
248 UNUSED(config_changed);
249 #endif /* ifndef FREERTOS_POSIX */
251 RPP_MUTEX_UNLOCK(mutex_din);