summaryrefslogtreecommitdiffstats
path: root/nuttx/arch/arm/src/lpc43xx/lpc43_gpio.c
blob: 2b2c88f311f487572b1fdfa22a41ebd688d0128b (plain)
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
/****************************************************************************
 *  arch/arm/src/lpc43/lpc43_gpio.c
 *
 *   Copyright (C) 2012 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * 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 <arch/board/board.h>
#include <nuttx/config.h>

#include <nuttx/arch.h>
#include <errno.h>
#include <debug.h>

#include "up_arch.h"
#include "lpc43_gpio.h"

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

/****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: lpc43_configinput
 *
 * Description:
 *   Configure a GPIO pin as an input (or pre-configured the pin for an
 *   interrupt).
 *
 * Returned Value:
 *   None
 *
 * Assumptions:
 *   Interrupts are disabled so that read-modify-write operations are safe.
 *
 ****************************************************************************/

static inline void lpc43_configinput(uint16_t gpiocfg,
                                     unsigned int port, unsigned int pin)
{
  uintptr_t regaddr;
  uint32_t regval;

  /* Then configure the pin as a normal input by clearing the corresponding
   * bit in the GPIO DIR register for the port.
   */

  regaddr = LPC43_GPIO_DIR(port);
  regval  = getreg32(regaddr);
  regval &= ~GPIO_DIR(pin);
  putreg32(regval, regaddr);
  
  /* To be able to read the signal on the GPIO input, the input
   * buffer must be enabled in the syscon block for the corresponding pin.
   * This should have been done when the pin was configured as a GPIO.
   */
}

/****************************************************************************
 * Name: lpc43_configoutput
 *
 * Description:
 *   Configure a GPIO pin as an output.
 *
 * Returned Value:
 *   None
 *
 * Assumptions:
 *   Interrupts are disabled so that read-modify-write operations are safe.
 *
 ****************************************************************************/

static inline void lpc43_configoutput(uint16_t gpiocfg,
                                      unsigned int port, unsigned int pin)
{
  uintptr_t regaddr;
  uint32_t regval;

  /* Then configure the pin as an output by setting the corresponding
   * bit in the GPIO DIR register for the port.
   */

  regaddr = LPC43_GPIO_DIR(port);
  regval  = getreg32(regaddr);
  regval |= GPIO_DIR(pin);
  putreg32(regval, regaddr);

  /* Set the initial value of the output */

  lpc43_gpio_write(gpiocfg, GPIO_IS_ONE(gpiocfg));

  /* To be able to read the signal on the GPIO input, the input
   * buffer must be enabled in the syscon block for the corresponding pin.
   * This should have been done when the pin was configured as a GPIO.
   */
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: lpc43_gpio_config
 *
 * Description:
 *   Configure a GPIO based on bit-encoded description of the pin.  NOTE:
 *   The pin *must* have first been configured for GPIO usage with a 
 *   corresponding call to lpc43_pin_config().
 *
 * Returned Value:
 *   OK on success; A negated errno value on failure.
 *
 ****************************************************************************/

int lpc43_gpio_config(uint16_t gpiocfg)
{
  unsigned int port  = ((gpiocfg & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT);
  unsigned int pin   = ((gpiocfg & GPIO_PIN_MASK) >> GPIO_PIN_SHIFT);
  irqstate_t   flags;
  int          ret   = OK;

  DEBUGASSERT(port < NUM_GPIO_PORTS && pin < NUM_GPIO_PINS);

  /* Handle the GPIO configuration by the basic mode of the pin */

  flags = irqsave();
  switch (gpiocfg & GPIO_MODE_MASK)
    {
      case GPIO_MODE_INPUT:     /* GPIO input pin */
        lpc43_configinput(gpiocfg, port, pin);
        break;

      case GPIO_MODE_OUTPUT:     /* GPIO output pin */
        lpc43_configoutput(gpiocfg, port, pin);
        break;

      case GPIO_MODE_PININTR:    /* GPIO pin interrupt */
        lpc43_configinput(gpiocfg, port, pin);
#ifdef CONFIG_GPIO_IRQ
        ret = lpc43_gpioint_pinconfig(gpiocfg);
#endif
        break;

      case GPIO_MODE_GRPINTR:    /* GPIO group interrupt */
        lpc43_configinput(gpiocfg, port, pin);
#ifdef CONFIG_GPIO_IRQ
        ret = lpc43_gpioint_grpconfig(gpiocfg);
#endif
        break;

      default :
        sdbg("ERROR: Unrecognized pin mode: %04x\n", gpiocfg);
        ret = -EINVAL;
        break;
    }

  irqrestore(flags);
  return ret;
}

/****************************************************************************
 * Name: lpc43_gpio_write
 *
 * Description:
 *   Write one or zero to the selected GPIO pin
 *
 * Returned Value:
 *   None
 *
 ****************************************************************************/

void lpc43_gpio_write(uint16_t gpiocfg, bool value)
{
  unsigned int port = ((gpiocfg & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT);
  unsigned int pin  = ((gpiocfg & GPIO_PIN_MASK) >> GPIO_PIN_SHIFT);

  DEBUGASSERT(port < NUM_GPIO_PORTS && pin < NUM_GPIO_PINS);

  /* Write the value (0 or 1).  To the pin byte register */

  putreg8((uint8_t)value, LPC43_GPIO_B(port, pin));
}

/****************************************************************************
 * Name: lpc43_gpio_read
 *
 * Description:
 *   Read one or zero from the selected GPIO pin
 *
 * Returned Value:
 *   The boolean state of the input pin
 *
 ****************************************************************************/

bool lpc43_gpio_read(uint16_t gpiocfg)
{
  unsigned int port = ((gpiocfg & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT);
  unsigned int pin  = ((gpiocfg & GPIO_PIN_MASK) >> GPIO_PIN_SHIFT);

  DEBUGASSERT(port < NUM_GPIO_PORTS && pin < NUM_GPIO_PINS);

  /* Get the value of the pin from the pin byte register */

  return (getreg8(LPC43_GPIO_B(port, pin)) & GPIO_B) != 0;
}