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