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.
23 #ifndef FREERTOS_POSIX
25 #include "drv/spi_tms570.h"
28 static boolean_t initialized = FALSE;
35 #ifndef FREERTOS_POSIX
45 int8_t rpp_din_ref(uint16_t ref_a, uint16_t ref_b)
47 if ((ref_a > 4095) || (ref_b > 4095))
50 #ifndef FREERTOS_POSIX
51 drv_din_ref(ref_a, ref_b);
57 // Check for configuration changes to avoid SPI overhead
58 static boolean_t config_changed = FALSE;
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;
65 static boolean_t check_pin_busy(uint8_t pin)
67 if (rpp_irc_status(RPP_IRC_1) == 1 && (pin == 10 || pin == 11))
69 if (rpp_irc_status(RPP_IRC_2) == 1 && (pin == 14 || pin == 15))
74 int8_t rpp_din_setup(uint8_t pin, boolean_t pull_up,
75 boolean_t active, boolean_t can_wake)
81 // Check programmable feature
82 if (!pull_up && (pin > 7))
85 // Check blockade of specific pins
86 if (check_pin_busy(pin))
91 bit_set(pull_cache, pin);
93 bit_clear(pull_cache, pin);
96 bit_set(active_cache, pin);
98 bit_clear(active_cache, pin);
101 bit_set(can_wake_cache, pin);
103 bit_clear(can_wake_cache, pin);
105 config_changed = TRUE;
110 static uint16_t in_cache = 0x0;
112 int8_t rpp_din_get(uint8_t pin)
118 // Check blockade of specific pins
119 if (check_pin_busy(pin))
123 if (is_bit_set(in_cache, pin))
128 int8_t rpp_din_get_tr(uint8_t pin)
131 if (pin < 8 || pin > 15)
134 // Check blockade of specific pins
135 if (check_pin_busy(pin))
139 #ifndef FREERTOS_POSIX
140 if (drv_din_get_varthr(pin) == 1)
149 static uint16_t diag_cache = 0x0;
151 int8_t rpp_din_diag(uint8_t pin)
157 // Check blockade of specific pins
158 if (check_pin_busy(pin))
162 if (is_bit_set(diag_cache, pin))
168 * pouzivat din_mod s pouzivanim enumu
170 int8_t rpp_din_update()
172 #ifndef FREERTOS_POSIX
174 if (config_changed) {
179 din_set_reg(DIN_RESET_CMD, 0, 0);
180 //rpp_sci_printf("din_reset()\r\n");
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);
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);
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;
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);
211 // Mark configuration as commited
212 config_changed = FALSE;
215 // Update cached values
216 din_set_reg(DIN_SWITCH_STATUS_CMD, 0, 0);
217 in_cache = din_get_val_word();
219 // FIXME: Implement. Dummy assign for now.
220 diag_cache = in_cache;
222 if (diag_cache != in_cache)
225 #else /* ifndef FREERTOS_POSIX */
226 UNUSED(config_changed);
227 #endif /* ifndef FREERTOS_POSIX */