summaryrefslogtreecommitdiffstats
path: root/bsps/arm/lpc176x/gpio/lpc-gpio.c
blob: e7f07fe65726bd91de19327d1d1cdd43e65e7df3 (plain) (blame)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
 * @file lpc-gpio.c
 *
 * @ingroup lpc176x
 *
 * @brief GPIO library for the lpc176x bsp.
 */

/*
 * Copyright (c) 2014 Taller Technologies.
 *
 * @author  Boretto Martin    (martin.boretto@tallertechnologies.com)
 * @author  Diaz Marcos (marcos.diaz@tallertechnologies.com)
 * @author  Lenarduzzi Federico  (federico.lenarduzzi@tallertechnologies.com)
 * @author  Daniel Chicco  (daniel.chicco@tallertechnologies.com)
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#include <assert.h>
#include <bsp/irq.h>
#include <bsp/io.h>
#include <bsp/lpc-gpio.h>
#include <rtems/status-checks.h>

static uint32_t                              function_vector_size = 0u;
static lpc176x_registered_interrupt_function function_vector[
  LPC176X_RESERVED_ISR_FUNCT_SIZE ];
static bool isr_installed = false;

rtems_status_code lpc176x_gpio_config(
  const lpc176x_pin_number     pin,
  const lpc176x_gpio_direction dir
)
{
  rtems_status_code status_code = RTEMS_INVALID_NUMBER;

  if ( ( pin < LPC176X_MAX_PORT_NUMBER ) &&
       ( dir < LPC176X_GPIO_FUNCTION_COUNT ) ) {
    const lpc176x_gpio_ports port = LPC176X_IO_PORT( pin );
    const uint32_t           pin_of_port = LPC176X_IO_PORT_BIT( pin );

    lpc176x_pin_select( pin, LPC176X_PIN_FUNCTION_00 );

    LPC176X_SET_BIT( LPC176X_FIO[ port ].dir, pin_of_port, dir );

    status_code = RTEMS_SUCCESSFUL;
  }

  /* else implies that the pin or the egde are out of range. Also,
     an invalid number is returned. */

  return status_code;
}

/**
 * @brief Check for a rising edge and call the interrupt function.
 *
 * @param statR Rising edge interrupt.
 * @param pin The pin to check.
 * @param registered_isr_function Interrupt to check.
 * @return TRUE if is a rising edge. FALSE otherwise.
 */
static bool lpc176x_check_rising_edge_and_call(
  const uint32_t                              statR,
  const lpc176x_registered_interrupt_function registered_isr_function,
  const uint32_t                              pin
)
{
  bool is_rising = false;

  if ( statR & LPC176X_PIN_BIT( pin ) ) {
    registered_isr_function.function( registered_isr_function.pin,
      LPC176X_GPIO_INTERRUPT_RISING );
    is_rising = true;
  }

  /* else implies that the current interrupt is not STATR. Also,
     there is nothing to do. */

  return is_rising;
}

/**
 * @brief Check for a falling edge and call the interrupt function.
 *
 * @param statR Falling edge interrupt.
 * @param pin The pin to check.
 * @param registered_isr_function Interrupt to check.
 * @return TRUE if is a falling edge. FALSE otherwise.
 */
static bool lpc176x_check_falling_edge_and_call(
  const uint32_t                              statF,
  const lpc176x_registered_interrupt_function registered_isr_function,
  const uint32_t                              pin
)
{
  bool is_falling = false;

  if ( statF & LPC176X_PIN_BIT( pin ) ) {
    registered_isr_function.function( registered_isr_function.pin,
      LPC176X_GPIO_INTERRUPT_FALLING );
    is_falling = true;
  }

  /* else implies that the current interrupt is not STATF. Also,
     there is nothing to do. */

  return is_falling;
}

/**
 * @brief Returns the interrupts base address according to the current port.
 *
 * @param  port Input/Output port.
 * @return Interrupt base address.
 */
static lpc176x_interrupt_control*lpc176x_get_interrupt_address(
  const lpc176x_gpio_ports port )
{
  lpc176x_interrupt_control *interrupt;

  switch ( port ) {
    case ( LPC176X_GPIO_PORT_0 ):
      interrupt = (lpc176x_interrupt_control *) LPC176X_IO0_INT_BASE_ADDRESS;
      break;
    case ( LPC176X_GPIO_PORT_2 ):
      interrupt = (lpc176x_interrupt_control *) LPC176X_IO2_INT_BASE_ADDRESS;
      break;
    case ( LPC176X_GPIO_PORT_1 ):
    case ( LPC176X_GPIO_PORT_3 ):
    case ( LPC176X_GPIO_PORT_4 ):
    default:
      interrupt = NULL;
  }

  return interrupt;
}

/**
 * @brief Checks the type of the current interrupt.
 *
 * @param registered_isr_function Interrupt to check.
 */
static void check_for_interrupt(
  const lpc176x_registered_interrupt_function registered_isr_function )
{
  assert( registered_isr_function.pin < LPC176X_MAX_PORT_NUMBER );

  const lpc176x_gpio_ports port = LPC176X_IO_PORT(
    registered_isr_function.pin );
  const uint32_t pin = LPC176X_IO_PORT_BIT( registered_isr_function.pin );

  lpc176x_interrupt_control *interrupt = lpc176x_get_interrupt_address( port );
  assert( interrupt != NULL );

  bool is_rising_edge = lpc176x_check_rising_edge_and_call( interrupt->StatR,
    registered_isr_function,
    pin );

  bool is_falling_edge = lpc176x_check_falling_edge_and_call( interrupt->StatF,
    registered_isr_function,
    pin );

  if ( is_rising_edge || is_falling_edge ) {
    interrupt->Clr = LPC176X_PIN_BIT( pin );
  }

  /* else implies that the current interrupt is not CLR. Also,
     there is nothing to do. */
}

/**
 * @brief Checks all interrupts types.
 *
 * @param arg Interrupt to check.
 */
static inline void lpc176x_gpio_isr( void *arg )
{
  unsigned int i;

  for ( i = 0; i < function_vector_size; ++i ) {
    check_for_interrupt( function_vector[ i ] );
  }
}

/**
 * @brief Depending of the current edge sets rising/falling interrupt.
 *
 * @param edge Current edge.
 * @param pin_of_port Pin of the port to set the interrupt.
 * @param interrupt To enable the falling o rising edge.
 */
static void lpc176x_set_falling_or_rising_interrupt(
  const lpc176x_gpio_interrupt edge,
  const uint32_t               pin_of_port,
  lpc176x_interrupt_control   *interrupt
)
{
  if ( edge & LPC176X_GPIO_INTERRUPT_RISING ) {
    LPC176X_SET_BIT( interrupt->EnR, pin_of_port, LPC176X_INT_ENABLE );
  }

  /* else implies that it should not install the interrupt for a RISING edge.
      Also, there is nothing to do. */

  if ( edge & LPC176X_GPIO_INTERRUPT_FALLING ) {
    LPC176X_SET_BIT( interrupt->EnF, pin_of_port, LPC176X_INT_ENABLE );
  }

  /* else implies that it should not install the interrupt for a FALLING edge.
      Also, there is nothing to do. */
}

/**
 * @brief Registers the pin and the callbacks functions.
 *
 * @param edge Current edge.
 * @param pin The pin to configure.
 * @param isr_funct Callback function to set.
 */
static void lpc176x_register_pin_and_callback(
  const lpc176x_gpio_interrupt          edge,
  const lpc176x_pin_number              pin,
  const lpc176x_gpio_interrupt_function isr_funct
)
{
  if ( edge ) {
    assert( function_vector_size < LPC176X_RESERVED_ISR_FUNCT_SIZE );
    function_vector[ function_vector_size ].function = isr_funct;
    function_vector[ function_vector_size ].pin = pin;
    ++function_vector_size;
  }

  /* else implies that the current interrupt is DISABLED or BOTH. Also,
     there is nothing to do. */
}

/**
 * @brief Installs the interrupt handler.
 *
 * @param edge Which edge enable.
 * @return  RTEMS_SUCCESSFUL if the installation was success.
 */
static rtems_status_code lpc176x_install_interrupt_handler(
  const lpc176x_gpio_interrupt edge )
{
  rtems_status_code status_code = RTEMS_SUCCESSFUL;

  if ( !isr_installed && edge ) {
    status_code = rtems_interrupt_handler_install( LPC176X_IRQ_EINT_3,
      "gpio_interrupt",
      RTEMS_INTERRUPT_UNIQUE,
      lpc176x_gpio_isr,
      NULL );
    isr_installed = true;
  }

  /* else implies that the interrupts have been previously installed. Also,
     there is nothing to do. */

  return status_code;
}

/**
 * @brief Configures the pin as input, enables interrupt for an
 * edge/s and sets isrfunct as the function to call when that
 * interrupt occurs.
 *
 * @param pin The pin to configure.
 * @param edge Which edge or edges will activate the interrupt.
 * @param isrfunct The function that is called when the interrupt occurs.
 * @return RTEMS_SUCCESSFUL if the configuration was success.
 */
static rtems_status_code lpc176x_check_edge_and_set_gpio_interrupts(
  const lpc176x_pin_number              pin,
  const lpc176x_gpio_interrupt          edge,
  const lpc176x_gpio_interrupt_function isr_funct
)
{
  rtems_status_code status_code = RTEMS_SUCCESSFUL;

  const lpc176x_gpio_ports port = LPC176X_IO_PORT( pin );
  const uint32_t           pin_of_port = LPC176X_IO_PORT_BIT( pin );

  lpc176x_interrupt_control *interrupt = lpc176x_get_interrupt_address( port );

  assert( interrupt != NULL );

  lpc176x_gpio_config( pin, LPC176X_GPIO_FUNCTION_INPUT );

  lpc176x_set_falling_or_rising_interrupt( edge, pin_of_port, interrupt );

  lpc176x_register_pin_and_callback( edge, pin, isr_funct );

  status_code = lpc176x_install_interrupt_handler( edge );

  return status_code;
}

rtems_status_code lpc176x_gpio_config_input_with_interrupt(
  const lpc176x_pin_number              pin,
  const lpc176x_gpio_interrupt          edge,
  const lpc176x_gpio_interrupt_function isr_funct
)
{
  rtems_status_code status_code = RTEMS_INVALID_NUMBER;

  if ( ( pin < LPC176X_MAX_PORT_NUMBER )
       && ( edge < LPC176X_GPIO_INTERRUPT_COUNT ) ) {
    status_code = lpc176x_check_edge_and_set_gpio_interrupts( pin,
      edge,
      isr_funct );
  }

  /* else implies that the pin or the egde are out of range. Also,
     an invalid number is returned. */

  return status_code;
}

rtems_status_code lpc176x_gpio_set_pin( const lpc176x_pin_number pin )
{
  rtems_status_code status_code = RTEMS_INVALID_NUMBER;

  if ( pin < LPC176X_MAX_PORT_NUMBER ) {
    const lpc176x_gpio_ports port = LPC176X_IO_PORT( pin );
    const uint32_t           pin_of_port = LPC176X_IO_PORT_BIT( pin );

    LPC176X_FIO[ port ].set = LPC176X_PIN_BIT( pin_of_port );

    status_code = RTEMS_SUCCESSFUL;
  }

  /* else implies that the pin or the egde are out of range. Also,
     an invalid number is returned. */

  return status_code;
}

rtems_status_code lpc176x_gpio_clear_pin( const lpc176x_pin_number pin )
{
  rtems_status_code status_code = RTEMS_INVALID_NUMBER;

  if ( pin < LPC176X_MAX_PORT_NUMBER ) {
    const lpc176x_gpio_ports port = LPC176X_IO_PORT( pin );
    const uint32_t           pin_of_port = LPC176X_IO_PORT_BIT( pin );

    LPC176X_FIO[ port ].clr = LPC176X_PIN_BIT( pin_of_port );

    status_code = RTEMS_SUCCESSFUL;
  }

  /* else implies that the pin or the egde are out of range. Also,
     an invalid number is returned. */

  return status_code;
}

rtems_status_code lpc176x_gpio_write_pin(
  const lpc176x_pin_number pin,
  const bool               value
)
{
  rtems_status_code status_code;

  if ( value ) {
    status_code = lpc176x_gpio_set_pin( pin );
  } else {
    status_code = lpc176x_gpio_clear_pin( pin );
  }

  return status_code;
}

inline rtems_status_code lpc176x_gpio_get_pin_value(
  const lpc176x_pin_number pin,
  bool                    *pin_value
)
{
  assert( pin < LPC176X_MAX_PORT_NUMBER );

  rtems_status_code status_code = RTEMS_SUCCESSFUL;

  const lpc176x_gpio_ports port = LPC176X_IO_PORT( pin );
  const uint32_t           pin_of_port = LPC176X_IO_PORT_BIT( pin );
  *pin_value = ( LPC176X_FIO[ port ].pin & LPC176X_PIN_BIT( pin_of_port ) );

  return status_code;
}