sam3x: Add gpio_init convenience function.

This commit is contained in:
Gareth McMullin
2013-05-01 13:17:21 -07:00
committed by Piotr Esden-Tempski
parent f311966f09
commit 614e26a3b1
2 changed files with 47 additions and 0 deletions

View File

@@ -19,6 +19,40 @@
#include <libopencm3/sam3x/gpio.h>
void gpio_init(u32 port, u32 pins, enum gpio_flags flags)
{
switch (flags & 3) {
case GPIO_FLAG_GPINPUT:
/* input mode doesn't really exist, so we make a high
* output in open-drain mode
*/
PIO_SODR(port) = pins;
flags |= GPIO_FLAG_OPEN_DRAIN;
/* fall through */
case GPIO_FLAG_GPOUTPUT:
PIO_OER(port) = pins;
PIO_PER(port) = pins;
break;
case GPIO_FLAG_PERIPHA:
PIO_ABSR(port) &= ~pins;
PIO_PDR(port) = pins;
break;
case GPIO_FLAG_PERIPHB:
PIO_ABSR(port) |= pins;
PIO_PDR(port) = pins;
}
if (flags & GPIO_FLAG_OPEN_DRAIN)
PIO_MDER(port) = pins;
else
PIO_MDDR(port) = pins;
if (flags & GPIO_FLAG_PULL_UP)
PIO_PUER(port) = pins;
else
PIO_PUDR(port) = pins;
}
void gpio_toggle(u32 gpioport, u32 gpios)
{
u32 odsr = PIO_ODSR(gpioport);