]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/gio.c
Update rpp/gio.* to the new drv/gio interface and remove crap from it
[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  *     - Michal Sojka <sojkam1@fel.cvut.cz>
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  */
12
13 #include "base.h"
14 #include "rpp/gio.h"
15 #include "drv/gio_tab.h"
16
17 static uint32_t ports_initialized = 0;
18
19 /* Configuration consistency check */
20 STATIC_ASSERT(RPP_GIO_PORT_GIOA == (1 << GIO_PORT_GIOA) &&
21                           RPP_GIO_PORT_GIOB == (1 << GIO_PORT_GIOB) &&
22                           RPP_GIO_PORT_NHET1 == (1 << GIO_PORT_HET1),
23                           Port_configuration_is_not_consistent);
24
25 int8_t rpp_gio_init(uint32_t init_ports)
26 {
27         enum pin_name pin;
28
29         gioREG->GCR0 = 1;   // Bring GIO out of reset
30
31         for (pin = (enum pin_name)0; pin < _PIN_COUNT; pin++) {
32                 int port_num = gio_port(gio_table[pin].pin_dsc);
33                 if ((init_ports & (1 << port_num)) &&
34                         !(ports_initialized & (1 << port_num)))
35                         gio_setup(gio_table[pin].pin_dsc);
36         }
37
38         ports_initialized |= init_ports;
39
40         return SUCCESS;
41 }
42
43 int8_t rpp_gio_set(enum pin_name pin, boolean_t value)
44 {
45         if (ports_initialized == 0 || pin >= _PIN_COUNT || pin < 0)
46                 return FAILURE;
47
48         gio_tab_set(pin, value);
49         return SUCCESS;
50 }
51
52 int8_t rpp_gio_get(enum pin_name pin)
53 {
54         if (ports_initialized == 0 || pin >= _PIN_COUNT || pin < 0)
55                 return FAILURE;
56
57         return gio_tab_get(pin) ? 1 : 0;
58 }
59
60 int8_t rpp_gio_setup(enum pin_name pin, enum rpp_gio_io io, enum rpp_gio_in_mode in_mode,
61                                          boolean_t open_drain)
62 {
63         if (ports_initialized == 0 || pin >= _PIN_COUNT || pin < 0)
64                 return FAILURE;
65
66         uint32_t mode_flags[] = {
67                 [RPP_GIO_MODE_PULLDIS]  = GIO_PIN_CONF_MODE_PDIS,
68                 [RPP_GIO_MODE_PULLUP]   = GIO_PIN_CONF_MODE_PU | GIO_PIN_CONF_MODE_PEN,
69                 [RPP_GIO_MODE_PULLDOWN] = GIO_PIN_CONF_MODE_PD | GIO_PIN_CONF_MODE_PEN,
70         };
71         uint32_t dsc = gio_table[pin].pin_dsc & ~GIO_PIN_CONF_MASK;
72         dsc |= (io == RPP_GIO_OUT) ? GIO_PIN_CONF_DIR_OUT : GIO_PIN_CONF_DIR_IN;
73         dsc |= mode_flags[in_mode];
74         dsc |= open_drain ? GIO_PIN_CONF_OD_ON : GIO_PIN_CONF_OD_OFF;
75
76         gio_setup(dsc);
77         return SUCCESS;
78 }