]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/gio.c
2d2b47323d81565eb50d3a6b2e78521db60c976b
[pes-rpp/rpp-lib.git] / rpp / src / rpp / gio.c
1 /* Copyright (C) 2013-2015 Czech Technical University in Prague
2  * Authors:
3  *     - Michal Horn <hornmich@fel.cvut.cz>
4  *
5  * This document contains proprietary information belonging to Czech
6  * Technical University in Prague. Passing on and copying of this
7  * document, and communication of its contents is not permitted
8  * without prior written authorization.
9  *
10  */
11
12 #include "rpp/gio.h"
13 #include "drv/digital_io.h"
14
15 static uint32_t ports_initialized = 0;
16
17 /* Configuration consistency check */
18 #if RPP_GIO_PORT_GIOA != (1 << DIO_PORT_ID_GIOA) || \
19         RPP_GIO_PORT_GIOB != (1 << DIO_PORT_ID_GIOB) || \
20         RPP_GIO_PORT_NHET1 != (1 << DIO_PORT_ID_HET1)
21 #error Port configuration is not consistent.
22 #endif
23
24 int8_t rpp_gio_init(uint32_t init_ports)
25 {
26         unsigned pin;
27
28         gioREG->GCR0 = 1;   // Bring GIO out of reset
29         dio_pin_map_element_t* pin_map = dio_gpio_get_pin_map();
30
31
32         for (pin = 0; pin < dio_gpio_get_pin_cnt(); pin++) {
33                 int port_num = dio_gpio_pin_get_port_num(pin_map[pin].pin_desc);
34                 if ((init_ports & (1 << port_num)) &&
35                         !(ports_initialized & (1 << port_num)))
36                         dio_gpio_pin_configure(pin_map[pin].pin_desc);
37         }
38
39         ports_initialized |= init_ports;
40
41         return SUCCESS;
42 }
43
44 int8_t rpp_gio_set_val(const char* pin_name, uint8_t value) {
45         if (ports_initialized == 0) {
46                 return FAILURE;
47         }
48         uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
49         if (pin_dsc == NULL) {
50                 return FAILURE;
51         }
52
53         dio_gpio_pin_set_value(*pin_dsc, value);
54         return SUCCESS;
55 }
56
57 int8_t rpp_gio_get_val(const char* pin_name) {
58         if (ports_initialized == 0) {
59                 return FAILURE;
60         }
61
62         uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
63         if (pin_dsc == NULL) {
64                 return FAILURE;
65         }
66
67         return dio_gpio_pin_get_value(*pin_dsc);
68 }
69
70 int8_t rpp_gio_set_output(const char* pin_name, uint8_t value) {
71         if (ports_initialized == 0) {
72                 return FAILURE;
73         }
74
75         uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
76         if (pin_dsc == NULL) {
77                 return FAILURE;
78         }
79
80         dio_gpio_pin_set_dir_out(*pin_dsc, value);
81
82         return SUCCESS;
83 }
84
85 int8_t rpp_gio_set_input(const char* pin_name) {
86         if (ports_initialized == 0) {
87                 return FAILURE;
88         }
89
90         uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
91         if (pin_dsc == NULL) {
92                 return FAILURE;
93         }
94
95         dio_gpio_pin_set_dir_in(*pin_dsc);
96
97         return SUCCESS;
98 }
99
100 int8_t rpp_gio_set_push_pull(const char* pin_name, boolean_t pull_up) {
101         if (ports_initialized == 0) {
102                 return FAILURE;
103         }
104
105         uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
106         if (pin_dsc == NULL) {
107                 return FAILURE;
108         }
109
110         dio_gpio_pin_set_od(*pin_dsc, DIO_PORT_CONF_OD_OFF);
111         if (pull_up) {
112                 dio_gpio_pin_set_mode(*pin_dsc, DIO_PORT_CONF_MODE_PU|DIO_PORT_CONF_MODE_PEN);
113         }
114         else {
115                 dio_gpio_pin_set_mode(*pin_dsc, DIO_PORT_CONF_MODE_PD|DIO_PORT_CONF_MODE_PEN);
116         }
117
118         dio_gpio_pin_set_dir_in(*pin_dsc);
119
120         return SUCCESS;
121 }
122
123 int8_t rpp_gio_set_open_drain(const char* pin_name) {
124         if (ports_initialized == 0) {
125                 return FAILURE;
126         }
127
128         uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
129         if (pin_dsc == NULL) {
130                 return FAILURE;
131         }
132
133         dio_gpio_pin_set_od(*pin_dsc, DIO_PORT_CONF_OD_ON);
134         dio_gpio_pin_set_mode(*pin_dsc, DIO_PORT_CONF_MODE_PU|DIO_PORT_CONF_MODE_PDIS);
135
136         dio_gpio_pin_set_dir_in(*pin_dsc);
137
138         return SUCCESS;
139 }
140
141 int8_t rpp_gio_set_pull_up(const char* pin_name) {
142         if (ports_initialized == 0) {
143                 return FAILURE;
144         }
145
146         uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
147         if (pin_dsc == NULL) {
148                 return FAILURE;
149         }
150
151         dio_gpio_pin_set_mode(*pin_dsc, DIO_PORT_CONF_MODE_PU|DIO_PORT_CONF_MODE_PEN);
152
153         return SUCCESS;
154 }
155
156 int8_t rpp_gio_set_pull_down(const char* pin_name) {
157         if (ports_initialized == 0) {
158                 return FAILURE;
159         }
160
161         uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
162         if (pin_dsc == NULL) {
163                 return FAILURE;
164         }
165
166         dio_gpio_pin_set_mode(*pin_dsc, DIO_PORT_CONF_MODE_PD|DIO_PORT_CONF_MODE_PEN);
167
168         return SUCCESS;
169 }
170
171 boolean_t rpp_gio_is_dir_output(const char* pin_name) {
172         if (ports_initialized == 0) {
173                 return FAILURE;
174         }
175
176         uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
177         if (pin_dsc == NULL) {
178                 return FAILURE;
179         }
180
181         int dir = dio_gpio_pin_get_dir(*pin_dsc);
182         return (dir == 1) ? TRUE : FALSE;
183 }
184
185 boolean_t rpp_gio_is_push_pull(const char* pin_name) {
186         if (ports_initialized == 0) {
187                 return FAILURE;
188         }
189
190         uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
191         if (pin_dsc == NULL) {
192                 return FAILURE;
193         }
194         return !(dio_gpio_pin_get_port_base(*pin_dsc)->PULDIS >> ((*pin_dsc & 0x1f) & 1));
195 }
196
197 boolean_t rpp_gio_is_pull_up(const char* pin_name) {
198         if (ports_initialized == 0) {
199                 return FAILURE;
200         }
201
202         uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
203         if (pin_dsc == NULL) {
204                 return FAILURE;
205         }
206         return dio_gpio_pin_get_port_base(*pin_dsc)->PSL >> ((*pin_dsc & 0x1f) & 1);
207 }
208
209 uint32_t rpp_gio_get_pin_cnt() {
210         return dio_gpio_get_pin_cnt();
211 }
212
213 int8_t rpp_gio_get_pin_names(const char** pin_names, uint8_t max_names_cnt) {
214         if (ports_initialized == 0) {
215                 return FAILURE;
216         }
217         if (pin_names == NULL) {
218                 return FAILURE;
219         }
220
221         dio_pin_map_element_t* pin_map = dio_gpio_get_pin_map();
222         uint32_t pin_cnt = dio_gpio_get_pin_cnt();
223
224         uint32_t i;
225         for (i = 0; i < pin_cnt && i < max_names_cnt; i++) {
226                 pin_names[i] = pin_map[i].pin_name;
227         }
228
229         return i;
230 }
231
232 int8_t rpp_port_set_val(const char* port_name, uint8_t port_num_vals, const uint32_t* values) {
233         if (ports_initialized == 0) {
234                 return FAILURE;
235         }
236         if (port_name == NULL || values == NULL) {
237                 return FAILURE;
238         }
239         const dio_port_desc_t* port = dio_port_get_dsc(port_name, -1);
240         if (port != NULL && dio_port_get_val_cnt(port) != port_num_vals) {
241                 return FAILURE;
242         }
243
244         dio_gpio_port_set_val(port->config, port_num_vals, values);
245         return SUCCESS;
246 }
247
248 int8_t rpp_port_get_val(const char* port_name, uint8_t port_num_vals, uint32_t* values) {
249         if (ports_initialized == 0) {
250                 return FAILURE;
251         }
252         if (port_name == NULL || values == NULL) {
253                 return FAILURE;
254         }
255         dio_port_desc_t* port = dio_port_get_dsc(port_name, -1);
256         if (port != NULL && dio_port_get_val_cnt(port) != port_num_vals) {
257                 return FAILURE;
258         }
259
260         dio_gpio_port_get_val(port->config, port_num_vals, values);
261         return SUCCESS;
262 }
263
264 int32_t rpp_port_get_pin_cnt(const char* port_name) {
265         if (ports_initialized == 0) {
266                 return FAILURE;
267         }
268         if (port_name == NULL) {
269                 return FAILURE;
270         }
271         return dio_port_get_val_cnt(dio_port_get_dsc(port_name, -1));
272 }
273
274 int32_t rpp_port_get_port_cnt() {
275         return DIO_PORT_CNT;
276 }
277
278 int8_t rpp_port_get_port_names(const char** port_names, uint8_t max_names_cnt) {
279         if (ports_initialized == 0) {
280                 return FAILURE;
281         }
282         if (port_names == NULL) {
283                 return FAILURE;
284         }
285
286         const dio_port_def_t* port_map = dio_port_get_map();
287
288         uint32_t port_cnt = DIO_PORT_CNT;
289         uint32_t i;
290         for (i = 0; i < port_cnt && i < max_names_cnt; i++) {
291                 port_names[i] = port_map[i].name;
292         }
293
294         return i;
295 }
296
297 int8_t rpp_port_get_interface_type(const char* port_name) {
298         if (ports_initialized == 0) {
299                 return FAILURE;
300         }
301         if (port_name == NULL) {
302                 return FAILURE;
303         }
304
305         dio_port_desc_t* port = dio_port_get_dsc(port_name, -1);
306         if (port == NULL) {
307                 return FAILURE;
308         }
309
310         return port->interfaceType;
311 }
312