1 /* Copyright (C) 2013 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Digital Input RPP API implementation file.
25 * RPP API documentation.
31 #if rppCONFIG_INCLUDE_DIN == 1
33 #if rppCONFIG_DRV == 1
37 static boolean_t initialized = FALSE;
49 int8_t rpp_din_ref(uint16_t ref_a, uint16_t ref_b)
51 if((ref_a > 4095) || (ref_b > 4095)) {
55 #if rppCONFIG_DRV == 1
56 drv_din_ref(ref_a, ref_b);
62 // Check for configuration changes to avoid SPI overhead
63 static boolean_t config_changed = FALSE;
65 // All cached values are 16 bits in the form [SG7,...,SG0][SP7,...,SP0]
66 static uint16_t pull_cache = 0x0;
67 static uint16_t active_cache = 0x0;
68 static uint16_t can_wake_cache = 0x0;
70 int8_t rpp_din_setup(uint8_t pin, boolean_t pull_type,
71 boolean_t active, boolean_t can_wake)
78 // Check programmable feature
79 if(pull_type && (pin > 7)) {
86 bit_set(pull_cache, index);
88 bit_clear(pull_cache, index);
92 bit_set(active_cache, index);
94 bit_clear(active_cache, index);
98 bit_set(can_wake_cache, index);
100 bit_clear(can_wake_cache, index);
103 config_changed = TRUE;
108 static uint16_t in_cache = 0x0;
110 int8_t rpp_din_get(uint8_t pin, boolean_t var_thr)
118 if(var_thr && (pin < 8)) {
122 // Use of variable threshold was requested
124 #if rppCONFIG_DRV == 1
125 if(drv_din_get_varthr(pin) == 1) {
132 if(is_bit_set(in_cache, pin)) {
139 static uint16_t diag_cache = 0x0;
141 int8_t rpp_din_diag(uint8_t pin)
148 if(is_bit_set(diag_cache, pin)) {
155 int8_t rpp_din_update()
157 #if rppCONFIG_DRV == 1
166 //rpp_sci_printf("din_reset()\r\n");
169 // In DRV logic is inverted:
170 // DRV: 1 - set pin as switch-to-battery. RPP: 0 - pull-down.
171 // DRV: 0 - set pin as switch-to-ground. RPP: 1 - pull-up.
172 sp = (~pull_cache) & 0xFF;
173 din_set_pr((uint8_t)sp);
175 //rpp_sci_printf("din_set_pr(%X)\r\n", sp);
177 // Set state type, active or tri-stated.
178 // In DRV logic is inverted:
179 // DRV: 1 - tri-state. RPP: 0 - tri-state.
180 // DRV: 0 - active. RPP: 1 - active.
181 sp = ((~active_cache) ) & 0xFF;
182 sg = ((~active_cache) >> 8) & 0xFF;
183 din_set_stat(sp, sg);
185 //rpp_sci_printf("din_set_stat(%X, %X)\r\n", sp, sg);
187 // Set wake / interrupt.
188 // IN DRV logic is not inverted.
189 // DRV: 1 - can wake. RPP: 1 - can wake.
190 // DRV: 0 - interrupt disabled. RPP: 0 - interrupt disabled.
191 sp = (can_wake_cache ) & 0xFF;
192 sg = (can_wake_cache >> 8) & 0xFF;
195 //rpp_sci_printf("din_set_int(%X, %X)\r\n", sp, sg);
197 // Mark configuration as commited
198 config_changed = FALSE;
201 // Update cached values
204 in_cache = din_get_val_word();
206 // FIXME: Implement. Dummy assign for now.
207 diag_cache = in_cache;
209 if(diag_cache != in_cache) {
213 UNUSED(config_changed);
220 #endif /* rppCONFIG_INCLUDE_DIN */