1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/**************************************************************************
* arch/avr/src/at91uc3/at91uc3_gpio.c
*
* Copyright (C) 2010 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************/
/**************************************************************************
* Included Files
**************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <assert.h>
#include "at91uc3_config.h"
#include "up_internal.h"
#include "at91uc3_internal.h"
#include "up_arch.h"
#include "chip.h"
#include "at91uc3_gpio.h"
/**************************************************************************
* Private Definitions
**************************************************************************/
/* How many GPIO ports are supported? There are 32-pins per port and we
* know he number of GPIO pins supported by the architecture:
*/
#define AVR32_NGPIO_PORTS ((AVR32_NGPIO+31) >> 5)
/**************************************************************************
* Private Types
**************************************************************************/
/**************************************************************************
* Private Function Prototypes
**************************************************************************/
/**************************************************************************
* Global Variables
**************************************************************************/
/**************************************************************************
* Private Variables
**************************************************************************/
static uint32_t g_portmap[AVR32_NGPIO_PORTS] =
{
#if AVR32_NGPIO > 0
AVR32_GPIO0_BASE
#endif
#if AVR32_NGPIO > 32
, AVR32_GPIO1_BASE,
#endif
#if AVR32_NGPIO > 64
, AVR32_GPIO2_BASE,
#endif
#if AVR32_NGPIO > 96
, AVR32_GPIO3_BASE,
#endif
#if AVR32_NGPIO > 128
, AVR32_GPIO4_BASE,
#endif
};
/**************************************************************************
* Private Functions
**************************************************************************/
/**************************************************************************
* Public Functions
**************************************************************************/
/************************************************************************************
* Name: at91uc3_configgpio
*
* Description:
* Configure a GPIO pin based on bit-encoded description of the pin.
*
************************************************************************************/
int at91uc3_configgpio(uint16_t cfgset)
{
unsigned int port;
unsigned int pin;
uint32_t pinmask;
uint32_t base;
/* Extract then port number and the pin number from the configuration */
port = ((unsigned int)cfgset & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
pin = ((unsigned int)cfgset & GPIO_PIN_MASK) >> GPIO_PIN_SHIFT;
DEBUGASSERT(port < AVR32_NGPIO_PORTS);
/* Get pin mask and the GPIO base address */
pinmask = (1 << pin);
base = g_portmap[port];
/* First, just to be safe, disable the output driver, give GPIO control of
* the pin, rese the peripheral mux, set the output low, remove the pull-up,
* disable GPIO interrupts, reset the interrupt mode, and disable glitch
* filtering, while we reconfigure the pin.
*/
putreg32(pinmask, base + AVR32_GPIO_ODERC_OFFSET);
putreg32(pinmask, base + AVR32_GPIO_GPERS_OFFSET);
putreg32(pinmask, base + AVR32_GPIO_PMR0C_OFFSET);
putreg32(pinmask, base + AVR32_GPIO_PMR1C_OFFSET);
putreg32(pinmask, base + AVR32_GPIO_OVRC_OFFSET);
putreg32(pinmask, base + AVR32_GPIO_PUERC_OFFSET);
putreg32(pinmask, base + AVR32_GPIO_IERC_OFFSET);
putreg32(pinmask, base + AVR32_GPIO_IMR0C_OFFSET);
putreg32(pinmask, base + AVR32_GPIO_IMR0C_OFFSET);
putreg32(pinmask, base + AVR32_GPIO_GFERC_OFFSET);
/* Is this a GPIO? Or a peripheral */
if ((cfgset & GPIO_ENABLE) != 0)
{
/* Its a GPIO. Input or output? */
if ((cfgset & GPIO_OUTPUT) != 0)
{
/* Its a GPIO output. Set up the initial output value and enable
* the output driver.
*/
if ((cfgset & GPIO_VALUE) != 0)
{
putreg32(pinmask, base + AVR32_GPIO_OVRS_OFFSET);
}
putreg32(pinmask, base + AVR32_GPIO_ODERS_OFFSET);
}
else
{
/* Its a GPIO input. There is nothing more to do here. */
}
}
else
{
/* Its a peripheral. Set the peripheral mux */
if ((cfgset & GPIO_PMR0) != 0)
{
putreg32(pinmask, base + AVR32_GPIO_PMR0S_OFFSET);
}
if ((cfgset & GPIO_PMR1) != 0)
{
putreg32(pinmask, base + AVR32_GPIO_PMR1S_OFFSET);
}
/* And enable peripheral control of the pin */
putreg32(pinmask, base + AVR32_GPIO_GPERC_OFFSET);
}
/* Then the "ornaments" tha do not depend on gpio/peripheral mode:
* Pull-ups and glitch filering.
*/
if ((cfgset & GPIO_PULLUP) != 0)
{
putreg32(pinmask, base + AVR32_GPIO_PUERS_OFFSET);
}
if ((cfgset & GPIO_PULLUP) != 0)
{
putreg32(pinmask, base + AVR32_GPIO_GFERS_OFFSET);
}
/* Check for GPIO interrupt */
if ((cfgset & GPIO_INTR) != 0)
{
/* Set up the interrupt mode */
if ((cfgset & GPIO_IMR0) != 0)
{
putreg32(pinmask, base + AVR32_GPIO_IMR0S_OFFSET);
}
if ((cfgset & GPIO_IMR1) != 0)
{
putreg32(pinmask, base + AVR32_GPIO_IMR1S_OFFSET);
}
/* Then enable the GPIO interrupt */
putreg32(pinmask, base + AVR32_GPIO_IERS_OFFSET);
}
return OK;
}
/************************************************************************************
* Name: at91uc3_gpiowrite
*
* Description:
* Write one or zero to the selected GPIO pin
*
************************************************************************************/
void at91uc3_gpiowrite(uint16_t pinset, bool value)
{
unsigned int port;
unsigned int pin;
uint32_t pinmask;
uint32_t base;
/* Extract then port number and the pin number from the configuration */
port = ((unsigned int)pinset & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
pin = ((unsigned int)pinset & GPIO_PIN_MASK) >> GPIO_PIN_SHIFT;
DEBUGASSERT(port < AVR32_NGPIO_PORTS);
/* Get pin mask and the GPIO base address */
pinmask = (1 << pin);
base = g_portmap[port];
/* Now, set or clear the pin ouput value */
if (value)
{
putreg32(pinmask, base + AVR32_GPIO_OVRS_OFFSET);
}
else
{
putreg32(pinmask, base + AVR32_GPIO_OVRC_OFFSET);
}
}
/************************************************************************************
* Name: at91uc3_gpioread
*
* Description:
* Read one or zero from the selected GPIO pin
*
************************************************************************************/
bool at91uc3_gpioread(uint16_t pinset)
{
unsigned int port;
unsigned int pin;
uint32_t pinmask;
uint32_t base;
/* Extract then port number and the pin number from the configuration */
port = ((unsigned int)pinset & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
pin = ((unsigned int)pinset & GPIO_PIN_MASK) >> GPIO_PIN_SHIFT;
DEBUGASSERT(port < AVR32_NGPIO_PORTS);
/* Get pin mask and the GPIO base address */
pinmask = (1 << pin);
base = g_portmap[port];
/* Now, return the current pin value */
return (getreg32(base + AVR32_GPIO_PVR_OFFSET) & pinmask) != 0;
}
|