Initial commit, code based on LPC1224 support and LPC812-MAX CMSIS-DAP interface...
[lpc11u3x] / core / pio.c
1 /****************************************************************************
2  *  core/pio.c
3  *
4  * Copyright 2012 Nathael Pajani <nathael.pajani@ed3l.fr>
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *************************************************************************** */
21 /***************************************************************************** */
22 /*                GPIOs                                                        */
23 /***************************************************************************** */
25 /*   Public access to Pins setup   */
28 #include <stdint.h>
29 #include "core/lpc_regs_11u3x.h"
30 #include "core/lpc_core_cm0.h"
31 #include "core/system.h"
32 #include "core/pio.h"
36 /***************************************************************************** */
37 static volatile uint32_t* pio_regs_handles_port0[PORT0_NB_PINS] = {
38         &(LPC_IO_CONTROL->pio0_0),
39         &(LPC_IO_CONTROL->pio0_1),
40         &(LPC_IO_CONTROL->pio0_2),
41         &(LPC_IO_CONTROL->pio0_3),
42         &(LPC_IO_CONTROL->pio0_4),
43         &(LPC_IO_CONTROL->pio0_5),
44         &(LPC_IO_CONTROL->pio0_6),
45         &(LPC_IO_CONTROL->pio0_7),
46         &(LPC_IO_CONTROL->pio0_8),
47         &(LPC_IO_CONTROL->pio0_9),
48         &(LPC_IO_CONTROL->pio0_10),
49         &(LPC_IO_CONTROL->pio0_11),
50         &(LPC_IO_CONTROL->pio0_12),
51         &(LPC_IO_CONTROL->pio0_13),
52         &(LPC_IO_CONTROL->pio0_14),
53         &(LPC_IO_CONTROL->pio0_15),
54         &(LPC_IO_CONTROL->pio0_16),
55         &(LPC_IO_CONTROL->pio0_17),
56         &(LPC_IO_CONTROL->pio0_18),
57         &(LPC_IO_CONTROL->pio0_19),
58         &(LPC_IO_CONTROL->pio0_20),
59         &(LPC_IO_CONTROL->pio0_21),
60         &(LPC_IO_CONTROL->pio0_22),
61         &(LPC_IO_CONTROL->pio0_23),
62 };
63 static volatile uint32_t* pio_regs_handles_port1[PORT1_NB_PINS] = {
64         &(LPC_IO_CONTROL->pio1_0),
65         &(LPC_IO_CONTROL->pio1_1),
66         &(LPC_IO_CONTROL->pio1_2),
67         &(LPC_IO_CONTROL->pio1_3),
68         &(LPC_IO_CONTROL->pio1_4),
69         &(LPC_IO_CONTROL->pio1_5),
70         &(LPC_IO_CONTROL->pio1_6),
71         &(LPC_IO_CONTROL->pio1_7),
72         &(LPC_IO_CONTROL->pio1_8),
73         &(LPC_IO_CONTROL->pio1_9),
74         &(LPC_IO_CONTROL->pio1_10),
75         &(LPC_IO_CONTROL->pio1_11),
76         &(LPC_IO_CONTROL->pio1_12),
77         &(LPC_IO_CONTROL->pio1_13),
78         &(LPC_IO_CONTROL->pio1_14),
79         &(LPC_IO_CONTROL->pio1_15),
80         &(LPC_IO_CONTROL->pio1_16),
81         &(LPC_IO_CONTROL->pio1_17),
82         &(LPC_IO_CONTROL->pio1_18),
83         &(LPC_IO_CONTROL->pio1_19),
84         &(LPC_IO_CONTROL->pio1_20),
85         &(LPC_IO_CONTROL->pio1_21),
86         &(LPC_IO_CONTROL->pio1_22),
87         &(LPC_IO_CONTROL->pio1_23),
88         &(LPC_IO_CONTROL->pio1_24),
89         &(LPC_IO_CONTROL->pio1_25),
90         &(LPC_IO_CONTROL->pio1_26),
91         &(LPC_IO_CONTROL->pio1_27),
92         &(LPC_IO_CONTROL->pio1_28),
93         &(LPC_IO_CONTROL->pio1_29),
94         0,
95         &(LPC_IO_CONTROL->pio1_31),
96 };
98 /* Simple copy function. */
99 void pio_copy(struct pio* dst, const struct pio* src)
101         if ((dst == NULL) || (src == NULL)) {
102                 return;
103         }
104         dst->port = src->port;
105         dst->pin = src->pin;
106         dst->alt_setting = src->alt_setting;
109 /* Configure the pin in the requested function and mode. */
110 void config_pio(const struct pio* pp, uint32_t mode)
112         volatile uint32_t* handle = NULL;
114         if (pp == NULL) {
115                 return;
116         }
117         switch (pp->port) {
118                 case 0:
119                         if (pp->pin >= PORT0_NB_PINS)
120                                 return;
121                         handle = pio_regs_handles_port0[pp->pin];
122                         break;
123                 case 1:
124                         if (pp->pin >= PORT1_NB_PINS)
125                                 return;
126                         handle = pio_regs_handles_port1[pp->pin];
127                         break;
128                 default:
129                         return;
130         }
131         /* Make sure IO_Config is clocked */
132         io_config_clk_on();
134         *handle = (LPC_IO_FUNC_ALT(pp->alt_setting) | mode);
136         /* Config done, power off IO_CONFIG block */
137         io_config_clk_off();
141 void set_pins(const struct pio_config* pins)
143         int i = 0;
144         for (i = 0; pins[i].pio.port != 0xFF; i++) {
145                 config_pio(&(pins[i].pio), pins[i].mode);
146         }