1 /* Copyright (C) 2013-2015 Czech Technical University in Prague
3 * - Michal Horn <hornmich@fel.cvut.cz>
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.
13 #include "drv/digital_io.h"
15 static uint32_t ports_initialized = 0;
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.
24 int8_t rpp_gio_init(uint32_t init_ports)
28 gioREG->GCR0 = 1; // Bring GIO out of reset
29 dio_pin_map_element_t* pin_map = dio_gpio_get_pin_map();
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);
39 ports_initialized |= init_ports;
44 int8_t rpp_gio_set_val(const char* pin_name, uint8_t value) {
45 if (ports_initialized == 0) {
48 uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
49 if (pin_dsc == NULL) {
53 dio_gpio_pin_set_value(*pin_dsc, value);
57 int8_t rpp_gio_get_val(const char* pin_name) {
58 if (ports_initialized == 0) {
62 uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
63 if (pin_dsc == NULL) {
67 return dio_gpio_pin_get_value(*pin_dsc);
70 int8_t rpp_gio_set_output(const char* pin_name, uint8_t value) {
71 if (ports_initialized == 0) {
75 uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
76 if (pin_dsc == NULL) {
80 dio_gpio_pin_set_dir_out(*pin_dsc, value);
85 int8_t rpp_gio_set_input(const char* pin_name) {
86 if (ports_initialized == 0) {
90 uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
91 if (pin_dsc == NULL) {
95 dio_gpio_pin_set_dir_in(*pin_dsc);
100 int8_t rpp_gio_set_push_pull(const char* pin_name, boolean_t pull_up) {
101 if (ports_initialized == 0) {
105 uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
106 if (pin_dsc == NULL) {
110 dio_gpio_pin_set_od(*pin_dsc, DIO_PORT_CONF_OD_OFF);
112 dio_gpio_pin_set_mode(*pin_dsc, DIO_PORT_CONF_MODE_PU|DIO_PORT_CONF_MODE_PEN);
115 dio_gpio_pin_set_mode(*pin_dsc, DIO_PORT_CONF_MODE_PD|DIO_PORT_CONF_MODE_PEN);
118 dio_gpio_pin_set_dir_in(*pin_dsc);
123 int8_t rpp_gio_set_open_drain(const char* pin_name) {
124 if (ports_initialized == 0) {
128 uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
129 if (pin_dsc == NULL) {
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);
136 dio_gpio_pin_set_dir_in(*pin_dsc);
141 int8_t rpp_gio_set_pull_up(const char* pin_name) {
142 if (ports_initialized == 0) {
146 uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
147 if (pin_dsc == NULL) {
151 dio_gpio_pin_set_mode(*pin_dsc, DIO_PORT_CONF_MODE_PU|DIO_PORT_CONF_MODE_PEN);
156 int8_t rpp_gio_set_pull_down(const char* pin_name) {
157 if (ports_initialized == 0) {
161 uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
162 if (pin_dsc == NULL) {
166 dio_gpio_pin_set_mode(*pin_dsc, DIO_PORT_CONF_MODE_PD|DIO_PORT_CONF_MODE_PEN);
171 boolean_t rpp_gio_is_dir_output(const char* pin_name) {
172 if (ports_initialized == 0) {
176 uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
177 if (pin_dsc == NULL) {
181 int dir = dio_gpio_pin_get_dir(*pin_dsc);
182 return (dir == 1) ? TRUE : FALSE;
185 boolean_t rpp_gio_is_push_pull(const char* pin_name) {
186 if (ports_initialized == 0) {
190 uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
191 if (pin_dsc == NULL) {
194 return !(dio_gpio_pin_get_port_base(*pin_dsc)->PULDIS >> ((*pin_dsc & 0x1f) & 1));
197 boolean_t rpp_gio_is_pull_up(const char* pin_name) {
198 if (ports_initialized == 0) {
202 uint32_t* pin_dsc = dio_gpio_pin_get_dsc(pin_name, -1);
203 if (pin_dsc == NULL) {
206 return dio_gpio_pin_get_port_base(*pin_dsc)->PSL >> ((*pin_dsc & 0x1f) & 1);
209 uint32_t rpp_gio_get_pin_cnt() {
210 return dio_gpio_get_pin_cnt();
213 int8_t rpp_gio_get_pin_names(const char** pin_names, uint8_t max_names_cnt) {
214 if (ports_initialized == 0) {
217 if (pin_names == NULL) {
221 dio_pin_map_element_t* pin_map = dio_gpio_get_pin_map();
222 uint32_t pin_cnt = dio_gpio_get_pin_cnt();
225 for (i = 0; i < pin_cnt && i < max_names_cnt; i++) {
226 pin_names[i] = pin_map[i].pin_name;
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) {
236 if (port_name == NULL || values == NULL) {
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) {
244 dio_gpio_port_set_val(port->config, port_num_vals, values);
248 int8_t rpp_port_get_val(const char* port_name, uint8_t port_num_vals, uint32_t* values) {
249 if (ports_initialized == 0) {
252 if (port_name == NULL || values == NULL) {
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) {
260 dio_gpio_port_get_val(port->config, port_num_vals, values);
264 int32_t rpp_port_get_pin_cnt(const char* port_name) {
265 if (ports_initialized == 0) {
268 if (port_name == NULL) {
271 return dio_port_get_val_cnt(dio_port_get_dsc(port_name, -1));
274 int32_t rpp_port_get_port_cnt() {
278 int8_t rpp_port_get_port_names(const char** port_names, uint8_t max_names_cnt) {
279 if (ports_initialized == 0) {
282 if (port_names == NULL) {
286 const dio_port_def_t* port_map = dio_port_get_map();
288 uint32_t port_cnt = DIO_PORT_CNT;
290 for (i = 0; i < port_cnt && i < max_names_cnt; i++) {
291 port_names[i] = port_map[i].name;
297 int8_t rpp_port_get_interface_type(const char* port_name) {
298 if (ports_initialized == 0) {
301 if (port_name == NULL) {
305 dio_port_desc_t* port = dio_port_get_dsc(port_name, -1);
310 return port->interfaceType;