1 /* Copyright (C) 2013, 2015 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
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.
13 * Digital Input RPP API implementation file.
17 * RPP API documentation.
22 #include "rpp/mutex.h"
24 #ifndef FREERTOS_POSIX
26 #include "drv/spi_def.h"
29 RPP_MUTEX_DEFINE(mutex_din);
31 static boolean_t initialized = FALSE;
37 if (!RPP_MUTEX_INIT(mutex_din))
40 #ifndef FREERTOS_POSIX
50 int8_t rpp_din_ref(uint16_t ref_a, uint16_t ref_b)
52 if ((ref_a > 4095) || (ref_b > 4095))
55 #ifndef FREERTOS_POSIX
56 RPP_MUTEX_LOCK(mutex_din);
57 drv_din_ref(ref_a, ref_b);
58 RPP_MUTEX_UNLOCK(mutex_din);
64 // Check for configuration changes to avoid SPI overhead
65 static boolean_t config_changed = FALSE;
67 // All cached values are 16 bits in the form [SG7,...,SG0][SP7,...,SP0]
68 static uint16_t pull_cache = 0x0; /* 0 - pull-down, 1 - pull-up */
69 static uint16_t active_cache = 0x0; /* 0 - tri-state, 1 - active */
70 static uint16_t can_wake_cache = 0x0;
72 static boolean_t check_pin_busy(uint8_t pin)
74 if (rpp_irc_status(RPP_IRC_1) == 1 && (pin == 10 || pin == 11))
76 if (rpp_irc_status(RPP_IRC_2) == 1 && (pin == 14 || pin == 15))
81 int8_t rpp_din_setup(uint8_t pin, boolean_t pull_up,
82 boolean_t active, boolean_t can_wake)
88 // Check programmable feature
89 if (!pull_up && (pin > 7))
92 RPP_MUTEX_LOCK(mutex_din);
93 // Check blockade of specific pins
94 if (check_pin_busy(pin)) {
95 RPP_MUTEX_UNLOCK(mutex_din);
101 bit_set(pull_cache, pin);
103 bit_clear(pull_cache, pin);
106 bit_set(active_cache, pin);
108 bit_clear(active_cache, pin);
111 bit_set(can_wake_cache, pin);
113 bit_clear(can_wake_cache, pin);
115 config_changed = TRUE;
116 RPP_MUTEX_UNLOCK(mutex_din);
121 static uint16_t in_cache = 0x0;
123 int8_t rpp_din_get(uint8_t pin)
129 // Check blockade of specific pins
130 if (check_pin_busy(pin))
133 return is_bit_set(in_cache, pin) ? RPP_CLOSED : RPP_OPEN;
136 int8_t rpp_din_get_tr(uint8_t pin)
139 if (pin < 8 || pin > 15)
142 // Check blockade of specific pins
143 if (check_pin_busy(pin))
146 #ifndef FREERTOS_POSIX
147 if (drv_din_get_varthr(pin) == 1)
155 static uint16_t diag_cache = 0x0;
157 int8_t rpp_din_diag(uint8_t pin)
163 // Check blockade of specific pins
164 if (check_pin_busy(pin))
167 return is_bit_set(diag_cache, pin) ? HIGH : LOW;
171 * pouzivat din_mod s pouzivanim enumu
173 int8_t rpp_din_update()
175 RPP_MUTEX_LOCK(mutex_din);
176 #ifndef FREERTOS_POSIX
178 if (config_changed) {
183 din_set_reg(DIN_RESET_CMD, 0, 0);
184 //rpp_sci_printf("din_reset()\r\n");
187 // In DRV logic is inverted:
188 // DRV: 1 - set pin as switch-to-battery. RPP: 0 - pull-down.
189 // DRV: 0 - set pin as switch-to-ground. RPP: 1 - pull-up.
190 sp = (~pull_cache) & 0xFF;
191 din_set_reg(DIN_SETTINGS_CMD, 0xffff, sp);
192 //rpp_sci_printf("din_set_pr(%X)\r\n", sp);
194 // Set state type, active or tri-stated.
195 // In DRV logic is inverted:
196 // DRV: 1 - tri-state. RPP: 0 - tri-state.
197 // DRV: 0 - active. RPP: 1 - active.
198 sp = ((~active_cache) ) & 0xFF;
199 sg = ((~active_cache) >> 8) & 0xFF;
200 din_set_reg(DIN_TRI_STATE_CMD_YES, 0xffff, sp);
201 din_set_reg(DIN_TRI_STATE_CMD_NO, 0xffff, sg);
202 //rpp_sci_printf("din_set_stat(%X, %X)\r\n", sp, sg);
204 // Set wake / interrupt.
205 // IN DRV logic is not inverted.
206 // DRV: 1 - can wake. RPP: 1 - can wake.
207 // DRV: 0 - interrupt disabled. RPP: 0 - interrupt disabled.
208 sp = (can_wake_cache ) & 0xFF;
209 sg = (can_wake_cache >> 8) & 0xFF;
211 din_set_reg(DIN_WAKE_UP_CMD_ENB, 0xffff, sp);
212 din_set_reg(DIN_WAKE_UP_CMD_DIS, 0xffff, sg);
213 //rpp_sci_printf("din_set_int(%X, %X)\r\n", sp, sg);
215 // Mark configuration as commited
216 config_changed = FALSE;
219 // Update cached values
220 din_set_reg(DIN_SWITCH_STATUS_CMD, 0, 0);
221 in_cache = din_get_val_word();
223 // FIXME: Implement. Dummy assign for now.
224 diag_cache = in_cache;
226 if (diag_cache != in_cache) {
227 RPP_MUTEX_UNLOCK(mutex_din);
231 #else /* ifndef FREERTOS_POSIX */
232 UNUSED(config_changed);
233 #endif /* ifndef FREERTOS_POSIX */
235 RPP_MUTEX_UNLOCK(mutex_din);