summaryrefslogtreecommitdiffstats
path: root/libopencm3/lib/lm4f/gpio.c
blob: 22399bd8cb2a653fd97d73368400ae3826e10c83 (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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
 * This file is part of the libopencm3 project.
 *
 * Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
 * Copyright (C) 2013 Alexandru Gagniuc <mr.nuke.me@gmail.com>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

/** @defgroup gpio_file GPIO
 *
 *
 * @ingroup LM4Fxx
 *
 * @version 1.0.0
 *
 * @author @htmlonly &copy; @endhtmlonly 2011
 * Gareth McMullin <gareth@blacksphere.co.nz>
 * @author @htmlonly &copy; @endhtmlonly 2013
 * Alexandru Gagniuc <mr.nuke.me@gmail.com>
 *
 * @date 16 March 2013
 *
 * LGPL License Terms @ref lgpl_license
 *
 * @brief <b>libopencm3 LM4F General Purpose I/O</b>
 *
 * The LM4F GPIO API provides functionality for accessing the GPIO pins of the
 * LM4F.
 *
 * @attention @code  An important aspect to consider is that libopencm3 uses the
 * AHB aperture for accessing the GPIO registers on the LM4F. The AHB must be
 * explicitly enabled with a call to gpio_enable_ahb_aperture() before accessing
 * any GPIO functionality.
 * @endcode
 *
 * Please see the individual GPIO modules for more details. To use the GPIO, the
 * gpio.h header needs to be included:
 * @code{.c}
 *	#include <libopencm3/lm4f/gpio.h>
 * @endcode
 */

/**@{*/

#include <libopencm3/lm4f/gpio.h>
#include <libopencm3/lm4f/systemcontrol.h>

/* Value we need to write to unlock the GPIO commit register */
#define GPIO_LOCK_UNLOCK_CODE		0x4C4F434B


/** @defgroup gpio_config GPIO pin configuration
 * @ingroup gpio_file
 *
 * \brief <b>Enabling and configuring GPIO pins</b>
 *
 * @section gpio_api_enable Enabling GPIO ports
 * @attention
 * Before accessing GPIO functionality through this API, the AHB aperture for
 * GPIO ports must be enabled via a call to @ref gpio_enable_ahb_aperture().
 * Failing to do so will cause a hard fault.
 *
 * @note
 * Once the AHB aperture is enabled, GPIO registers can no longer be accessed
 * via the APB aperture. The two apertures are mutually exclusive.
 *
 * Enabling the AHB aperture only needs to be done once. However, in order to
 * access a certain GPIO port, its clock must also be enabled. Enabling the
 * GPIO clock needs to be done for every port that will be used.
 *
 * For example, to enable GPIOA and GPIOD:
 * @code{.c}
 *	// Make sure we can access the GPIO via the AHB aperture
 *	gpio_enable_ahb_aperture();
 *	...
 *	// Enable GPIO ports A and D
 *	periph_clock_enable(RCC_GPIOA);
 *	periph_clock_enable(RCC_GPIOD);
 * @endcode
 *
 * On reset all ports are configured as digital floating inputs (no pull-up or
 * pull-down), except for special function pins.
 *
 *
 * @section gpio_api_in Configuring pins as inputs
 *
 * Configuring GPIO pins as inputs is done with @ref gpio_mode_setup(), with
 * @ref GPIO_MODE_INPUT for the mode parameter. The direction of the pull-up
 * must be specified with the same call
 *
 * For example, PA2, PA3, and PA4 as inputs, with pull-up on PA4:
 * @code{.c}
 *	gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO2 | GPIO3);
 *	gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO4);
 * @endcode
 *
 *
 * @section gpio_api_out Configuring pins as outputs
 *
 * Output pins have more configuration options than input pins. LM4F pins can be
 * configured as either push-pull, or open drain. The drive strength of each pin
 * can be adjusted between 2mA, 4mA, or 8mA. Slew-rate control is available when
 * the pins are configured to drive 8mA. These extra options can be specified
 * with @ref gpio_set_output_config().
 * The default is push-pull configuration with 2mA drive capability.
 *
 * @note
 * @ref gpio_set_output_config() controls different capabilities than the
 * similar sounding gpio_set_output_options() from the STM GPIO API. They are
 * intentionally named differently to prevent confusion between the two. They
 * are API incompatible.
 *
 * For example, to set PA2 to output push-pull with a drive strength of 8mA:
 * @code{.c}
 *	gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO2);
 *	gpio_set_output_config(GPIOA, GPIO_OTYPE_PP, GPIO_DRIVE_8MA, GPIO2);
 * @endcode
 *
 *
 * @section gpio_api_analog Configuring pins as analog function
 *
 * Configuring GPIO pins to their analog function is done with
 * @ref gpio_mode_setup(), with @ref GPIO_MODE_ANALOG for the mode parameter.
 *
 * Suppose PD4 and PD5 are the USB pins. To enable their analog functionality
 * (USB D+ and D- in this case), use:
 * @code
 *	// Mux USB pins to their analog function
 *	gpio_mode_setup(GPIOD, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO4 | GPIO5);
 * @endcode
 *
 * @section gpio_api_alf_func Configuring pins as alternate functions
 *
 * Most pins have alternate functions associated with them. When a pin is set to
 * an alternate function, it is multiplexed to one of the dedicated hardware
 * peripheral in the chip. The alternate function mapping can be found in the
 * part's datasheet, and usually varies between arts of the same family.
 *
 * Multiplexing a pin, or group of pins to an alternate function is done with
 * @ref gpio_set_af(). Because AF0 is not used on the LM4F, passing 0 as the
 * alt_func_num parameter will disable the alternate function of the given pins.
 *
 * @code
 *	// Mux PB0 and PB1 to AF1 (UART1 TX/RX in this case)
 *	gpio_set_af(GPIOB, 1, GPIO0 | GPIO1);
 * @endcode
 *
 * @section gpio_api_sfpins Changing configuration of special function pins
 *
 * On the LM4F, the NMI and JTAG/SWD default to their alternate function. These
 * pins cannot normally be committed to GPIO usage. To enable these special
 * function pins to be used as GPIO, they must be unlocked. This may be achieved
 * via @ref gpio_unlock_commit. Once a special function pin is unlocked, its
 * settings may be altered in the usual way.
 *
 * For example, to unlock the PF0 pin (NMI on the LM4F120):
 * @code
 *	// PF0 is an NMI pin, and needs to be unlocked
 *	gpio_unlock_commit(GPIOF, GPIO0);
 *	// Now the pin can be configured
 *	gpio_mode_setup(RGB_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, btnpins);
 * @endcode
 */
/**@{*/

/**
 * \brief Enable access to GPIO registers via the AHB aperture
 *
 * All GPIO registers are accessed in libopencm3 via the AHB aperture. It
 * provides faster control over the older APB aperture. This aperture must be
 * enabled before calling any other gpio_*() function.
 *
 */
void gpio_enable_ahb_aperture(void)
{
	SYSCTL_GPIOHBCTL = 0xffffffff;
}

/**
 * \brief Configure a group of pins
 *
 * Sets the Pin direction, analog/digital mode, and pull-up configuration of
 * or a set of GPIO pins on a given GPIO port.
 *
 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
 * @param[in] mode Pin mode (@ref gpio_mode) \n
 *		   - GPIO_MODE_OUTPUT -- Configure pin as output \n
 *		   - GPIO_MODE_INPUT -- Configure pin as input \n
 *		   - GPIO_MODE_ANALOG -- Configure pin as analog function
 * @param[in] pullup Pin pullup/pulldown configuration (@ref gpio_pullup) \n
 *		     - GPIO_PUPD_NONE -- Do not pull the pin high or low \n
 *		     - GPIO_PUPD_PULLUP -- Pull the pin high \n
 *		     - GPIO_PUPD_PULLDOWN -- Pull the pin low
 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified
 *		    by OR'ing then together
 */
void gpio_mode_setup(uint32_t gpioport, enum gpio_mode mode,
		     enum gpio_pullup pullup, uint8_t gpios)
{
	switch (mode) {
	case GPIO_MODE_OUTPUT:
		GPIO_DIR(gpioport) |= gpios;
		GPIO_DEN(gpioport) |= gpios;
		GPIO_AMSEL(gpioport) &= ~gpios;
		break;
	case GPIO_MODE_INPUT:
		GPIO_DIR(gpioport) &= ~gpios;
		GPIO_DEN(gpioport) |= gpios;
		GPIO_AMSEL(gpioport) &= ~gpios;
		break;
	case GPIO_MODE_ANALOG:
		GPIO_DEN(gpioport) &= ~gpios;
		GPIO_AMSEL(gpioport) |= gpios;
		break;
	default:
		/* Don't do anything */
		break;
	}

	/*
	 * Setting a bit in the GPIO_PDR register clears the corresponding bit
	 * in the GPIO_PUR register, and vice-versa.
	 */
	switch (pullup) {
	case GPIO_PUPD_PULLUP:
		GPIO_PUR(gpioport) |= gpios;
		break;
	case GPIO_PUPD_PULLDOWN:
		GPIO_PDR(gpioport) |= gpios;
		break;
	case GPIO_PUPD_NONE:	/* Fall through */
	default:
		GPIO_PUR(gpioport) &= ~gpios;
		GPIO_PDR(gpioport) &= ~gpios;
		break;
	}
}

/**
 * \brief Configure output parameters of a group of pins
 *
 * Sets the output configuration and drive strength, of or a set of GPIO pins
 * for a set of GPIO pins in output mode.
 *
 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
 * @param[in] otype Output driver configuration (@ref gpio_output_type) \n
 *		    - GPIO_OTYPE_PP -- Configure pin driver as push-pull \n
 *		    - GPIO_OTYPE_OD -- Configure pin driver as open drain
 * @param[in] drive Pin drive strength (@ref gpio_drive_strength) \n
 *		    - GPIO_DRIVE_2MA -- 2mA drive \n
 *		    - GPIO_DRIVE_4MA -- 4mA drive \n
 *		    - GPIO_DRIVE_8MA -- 8mA drive \n
 *		    - GPIO_DRIVE_8MA_SLEW_CTL -- 8mA drive with slew rate
 *						 control
 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified
 *		    by OR'ing then together
 */
void gpio_set_output_config(uint32_t gpioport, enum gpio_output_type otype,
			     enum gpio_drive_strength drive, uint8_t gpios)
{
	if (otype == GPIO_OTYPE_OD) {
		GPIO_ODR(gpioport) |= gpios;
	} else {
		GPIO_ODR(gpioport) &= ~gpios;
	}

	/*
	 * Setting a bit in the GPIO_DRxR register clears the corresponding bit
	 * in the other GPIO_DRyR registers, and vice-versa.
	 */
	switch (drive) {
	case GPIO_DRIVE_8MA_SLEW_CTL:
		GPIO_DR8R(gpioport) |= gpios;
		GPIO_SLR(gpioport) |= gpios;
		break;
	case GPIO_DRIVE_8MA:
		GPIO_DR8R(gpioport) |= gpios;
		GPIO_SLR(gpioport) &= ~gpios;
		break;
	case GPIO_DRIVE_4MA:
		GPIO_DR4R(gpioport) |= gpios;
		break;
	case GPIO_DRIVE_2MA:	/* Fall through */
	default:
		GPIO_DR2R(gpioport) |= gpios;
		break;
	}
}

#define PCTL_AF(pin, af)		(af << (pin << 2))
#define PCTL_MASK(pin)			PCTL_AF(pin, 0xf)
/**
 * \brief Multiplex group of pins to the given alternate function
 *
 * Mux the pin or group of pins to the given alternate function. Note that a
 * number of pins may be set but only with a single AF number. This is useful
 * when one or more of a peripheral's pins are assigned to the same alternate
 * function.
 *
 * Because AF0 is not used on the LM4F, passing 0 as the alt_func_num parameter
 * will disable the alternate function of the given pins.
 *
 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
 * @param[in] alt_func_num Pin alternate function number or 0 to disable the
 *			   alternate function multiplexing.
 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified
 *		    by OR'ing then together
 */
void gpio_set_af(uint32_t gpioport, uint8_t alt_func_num, uint8_t gpios)
{
	uint32_t pctl32;
	uint8_t pin_mask;
	int i;

	/* Did we mean to disable the alternate function? */
	if (alt_func_num == 0) {
		GPIO_AFSEL(gpioport) &= ~gpios;
		return;
	}

	/* Enable the alternate function */
	GPIO_AFSEL(gpioport) |= gpios;
	/* Alternate functions are digital */
	GPIO_DEN(gpioport) |= gpios;

	/* Now take care of the actual multiplexing */
	pctl32 = GPIO_PCTL(gpioport);
	for (i = 0; i < 8; i++) {
		pin_mask = (1 << i);

		if (!(gpios & pin_mask)) {
			continue;
		}

		pctl32 &= ~PCTL_MASK(i);
		pctl32 |= PCTL_AF(i, (alt_func_num & 0xf));
	}

	GPIO_PCTL(gpioport) = pctl32;
}

/**
 * \brief Unlock the commit control of a special function pin
 *
 * Unlocks the commit control of the given pin or group of pins. If a pin is a
 * JTAG/SWD or NMI, the pin may then be reconfigured as a GPIO pin. If the pin
 * is not locked by default, this has no effect.
 *
 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified
 *		    by OR'ing then together.
 */
void gpio_unlock_commit(uint32_t gpioport, uint8_t gpios)
{
	/* Unlock the GPIO_CR register */
	GPIO_LOCK(gpioport) = GPIO_LOCK_UNLOCK_CODE;
	/* Enable committing changes */
	GPIO_CR(gpioport) |= gpios;
	/* Lock the GPIO_CR register */
	GPIO_LOCK(gpioport) = ~GPIO_LOCK_UNLOCK_CODE;
}
/**@}*/

/** @defgroup gpio_control GPIO pin control
 * @ingroup gpio_file
 *
 * \brief <b>Controlling GPIO pins</b>
 *
 * Each I/O port has 8 individually configurable bits. When reading and writing
 * data to the GPIO ports, address bits [9:2] mask the pins to be read or
 * written. This mechanism makes all GPIO port reads and writes on the LM4F
 * atomic operations. The GPIO API takes full advantage of this fact to preserve
 * the atomicity of these operations.
 *
 * Setting or clearing a group of bits can be accomplished with @ref gpio_set()
 * and @ref gpio_clear() respectively. These operation use the masking mechanism
 * described above to only affect the specified pins.
 *
 * Sometimes it is more appropriate to read or set the level of a group of pins
 * on a port, in one atomic operation. Reading the status can be accomplished
 * with @ref gpio_read(). The result is equivalent to reading all the pins, then
 * masking only the desired pins; however, the masking is done in hardware, and
 * does not require an extra hardware operation.
 *
 * Writing a group of pins can be accomplished with @ref gpio_write(). The mask
 * ('gpios' parameter) is applied in hardware, and the masked pins are not
 * affected, regardless of the value of the respective bits written to the GPIO
 * port.
 *
 * Two extra functions are provided, @ref gpio_port_read() and
 * @ref gpio_port_write(). They are functionally identical to
 * @ref gpio_read (port, GPIO_ALL) and @ref gpio_write (port, GPIO_ALL, val)
 * respectively. Hence, they are also atomic.
 *
 * GPIO pins may be toggled with @ref gpio_toggle(). This function does not
 * translate to an atomic operation.
 *
 * @note
 * The @ref gpio_toggle() operation is the only GPIO port operation which is not
 * atomic. It involves a read-modify-write cycle.
 *
 * Suppose PA0, PA1, PA2, and PA3 are to be modified without affecting the other
 * pins on port A. This is common when controlling, for example, a 4-bit bus:
 * @code{.c}
 *	// Pins 4,5,6, and 7 are unaffected, regardless of the bits in val
 *	gpio_write(GPIOA, GPIO0 | GPIO1 | GPIO2 | GPIO3, val);
 *	// Wait a bit then send the other 4 bits
 *	wait_a_bit();
 *	gpio_write(GPIOA, GPIO0 | GPIO1 | GPIO2 | GPIO3, val >> 4);
 * @endcode
 *
 * Suppose a LED is connected to PD4, and we want to flash the LED for a brief
 * period of time:
 * @code
 *	gpio_set(GPIOD, GPIO4);
 *	wait_a_bit();
 *	gpio_set(GPIOD, GPIO4);
 * @endcode
 */
/**@{*/
/**
 * \brief Toggle a Group of Pins
 *
 * Toggle one or more pins of the given GPIO port.
 *
 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
 * @param[in] gpios Pin identifiers. @ref gpio_pin_id
 */
void gpio_toggle(uint32_t gpioport, uint8_t gpios)
{
	/* The mask makes sure we only toggle the GPIOs we want to */
	GPIO_DATA(gpioport)[gpios] ^= GPIO_ALL;
}
/**@}*/


/** @defgroup gpio_irq GPIO Interrupt control
 * @ingroup gpio_file
 *
 * \brief <b>Configuring interrupts from GPIO pins</b>
 *
 * GPIO pins can trigger interrupts on either edges or levels. The type of
 * trigger can be configured with @ref gpio_configure_int_trigger(). To have an
 * event on the given pin generate an interrupt, its interrupt source must be
 * unmasked. This can be achieved with @ref gpio_enable_interrupts(). Interrupts
 * which are no longer needed can be disabled through
 * @ref gpio_disable_interrupts().
 *
 * In order for the interrupt to generate an IRQ and a call to the interrupt
 * service routine, the interrupt for the GPIO port must be routed through the
 * NVIC with @ref nvic_enable_irq(). For this last step, the nvic.h header is
 * needed:
 * @code{.c}
 *	#include <libopencm3/lm4f/nvic.h>
 * @endcode
 *
 * Enabling an interrupt is as simple as configuring the desired trigger,
 * unmasking the desired interrupt, and routing the desired GPIO port's
 * interrupt through the NVIC.
 * @code{.c}
 *	// Trigger interrupt on each rising edge
 *	gpio_configure_trigger(GPIOF, GPIO_TRIG_EDGE_RISE, GPIO0 | GPIO4);
 *	// Unmask the interrupt on those pins
 *	gpio_enable_interrupts(GPIOF, GPIO0 | GPIO4);
 *	// Enable the interrupt in the NVIC as well
 *	nvic_enable_irq(NVIC_GPIOF_IRQ);
 * @endcode
 *
 * After interrupts are properly enabled and routed through the NVIC, when an
 * event occurs, the appropriate IRQ flag is set by hardware, and execution
 * jumps to the GPIO ISR. The ISR should query the IRQ flags to determine which
 * event caused the interrupt. For this, use @ref gpio_is_interrupt_source(),
 * with the desired GPIO flag. After one or more interrupt sources are
 * serviced, the IRQ flags must be cleared by the ISR. This can be done with
 * @ref gpio_clear_interrupt_flag().
 *
 * A typical GPIO ISR may look like the following:
 * @code{.c}
 * void gpiof_isr(void)
 * {
 *	uint8_t serviced_irqs = 0;
 *
 *	// Process individual IRQs
 *	if (gpio_is_interrupt_source(GPIOF, GPIO0)) {
 *		process_gpio0_event();
 *		serviced_irq |= GPIO0;
 *	}
 *	if (gpio_is_interrupt_source(GPIOF, GPIO4)) {
 *		process_gpio4_event();
 *		serviced_irq |= GPIO4;
 *	}
 *
 *	// Clear the interupt flag for the processed IRQs
 *	gpio_clear_interrupt_flag(GPIOF, serviced_irqs);
 * }
 * @endcode
 */
/**@{*/
/**
 * \brief Configure the interrupt trigger on the given GPIO pins
 *
 * Sets the Pin direction, analog/digital mode, and pull-up configuration of
 * or a set of GPIO pins on a given GPIO port.
 *
 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
 * @param[in] trigger Trigger configuration (@ref gpio_trigger) \n
 *		      - GPIO_TRIG_LVL_LOW -- Trigger on low level \n
 *		      - GPIO_TRIG_LVL_HIGH -- Trigger on high level \n
 *		      - GPIO_TRIG_EDGE_FALL -- Trigger on falling edges \n
 *		      - GPIO_TRIG_EDGE_RISE -- Trigger on rising edges \n
 *		      - GPIO_TRIG_EDGE_BOTH -- Trigger on all edges
 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified
 *		    by OR'ing then together
 */
void gpio_configure_trigger(uint32_t gpioport, enum gpio_trigger trigger,
			    uint8_t gpios)
{
	switch (trigger) {
	case GPIO_TRIG_LVL_LOW:
		GPIO_IS(gpioport) |= gpios;
		GPIO_IEV(gpioport) &= ~gpios;
		break;
	case GPIO_TRIG_LVL_HIGH:
		GPIO_IS(gpioport) |= gpios;
		GPIO_IEV(gpioport) |= gpios;
		break;
	case GPIO_TRIG_EDGE_FALL:
		GPIO_IS(gpioport) &= ~gpios;
		GPIO_IBE(gpioport) &= ~gpios;
		GPIO_IEV(gpioport) &= ~gpios;
		break;
	case GPIO_TRIG_EDGE_RISE:
		GPIO_IS(gpioport) &= ~gpios;
		GPIO_IBE(gpioport) &= ~gpios;
		GPIO_IEV(gpioport) |= gpios;
		break;
	case GPIO_TRIG_EDGE_BOTH:
		GPIO_IS(gpioport) &= ~gpios;
		GPIO_IBE(gpioport) |= gpios;
		break;
	default:
		/* Don't do anything */
		break;
	}
}

/**
 * \brief Enable interrupts on specified GPIO pins
 *
 * Enable interrupts on the specified GPIO pins
 *
 * Note that the NVIC must be enabled and properly configured for the interrupt
 * to be routed to the CPU.
 *
 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
 * @param[in] gpios @ref gpio_pin_id. Pins whose interrupts to enable. Any
 *		    combination of pins may be specified by OR'ing them
 *		    together.
 */
void gpio_enable_interrupts(uint32_t gpioport, uint8_t gpios)
{
	GPIO_IM(gpioport) |= gpios;
}

/**
 * \brief Disable interrupts on specified GPIO pins
 *
 * Disable interrupts on the specified GPIO pins
 *
 * Note that the NVIC must be enabled and properly configured for the interrupt
 * to be routed to the CPU.
 *
 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
 * @param[in] gpios @ref gpio_pin_id. Pins whose interrupts to disable. Any
 *		    combination of pins may be specified by OR'ing them
 *		    together.
 */
void gpio_disable_interrupts(uint32_t gpioport, uint8_t gpios)
{
	GPIO_IM(gpioport) |= gpios;
}

/**@}*/

/**@}*/