From 908a27b869407d4ec8fa816952c76e1a628901cf Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Sun, 8 Jun 2014 23:59:34 +0200 Subject: [PATCH] Adding gpio config function, which in turn calls config_pio() and set pin direction and initial state. --- drivers/gpio.c | 22 ++++++++++++++++++++++ include/core/lpc_regs_12xx.h | 4 ++++ include/drivers/gpio.h | 11 ++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/drivers/gpio.c b/drivers/gpio.c index 8b6a01f..8fa1d21 100644 --- a/drivers/gpio.c +++ b/drivers/gpio.c @@ -65,6 +65,28 @@ void gpio_off(void) subsystem_power(LPC_SYS_ABH_CLK_CTRL_GPIO2, 0); } +/* + * This function calls the config_pio() function for the gpio with the given + * mode, configures the direction of the pin and sets the initial state. + */ +void config_gpio(struct pio* gpio, uint32_t mode, uint8_t dir, uint8_t ini_val) +{ + struct lpc_gpio* gpio_port = LPC_GPIO_REGS(gpio->port); + + /* Configure as GPIO */ + config_pio(gpio, mode); + + if (dir == GPIO_DIR_IN) { + gpio_port->data_dir &= ~(1 << gpio->pin); + } else { + gpio_port->data_dir |= (1 << gpio->pin); + } + if (ini_val == 0) { + gpio_port->clear = (1 << gpio->pin); + } else { + gpio_port->set = (1 << gpio->pin); + } +} /***************************************************************************** */ /* GPIO Interrupts Callbacks */ diff --git a/include/core/lpc_regs_12xx.h b/include/core/lpc_regs_12xx.h index f0dfd0d..9defa5f 100644 --- a/include/core/lpc_regs_12xx.h +++ b/include/core/lpc_regs_12xx.h @@ -465,6 +465,10 @@ struct lpc_gpio #define LPC_GPIO_REGS(x) ((struct lpc_gpio *) (LPC_AHB_BASE + (0x10000 * (x)))) +#define GPIO_DIR_IN 0 +#define GPIO_DIR_OUT 1 + + /***************************************************************************** */ /* Universal Asynchronous Receiver Transmitter */ diff --git a/include/drivers/gpio.h b/include/drivers/gpio.h index a0a3793..a7fae5a 100644 --- a/include/drivers/gpio.h +++ b/include/drivers/gpio.h @@ -38,12 +38,21 @@ enum gpio_interrupt_senses { LEVEL_LOW, }; - +/* GPIO Interrupts */ int set_gpio_callback(void (*callback) (uint32_t), struct pio* gpio, uint8_t sense); void remove_gpio_callback(struct pio* gpio); + +/* GPIO Activation */ void gpio_on(void); void gpio_off(void); +/* GPIO Configuration + * This function calls the config_pio() function for the gpio with the given + * mode, configures the direction of the pin and sets the initial state. + */ +void config_gpio(struct pio* gpio, uint32_t mode, uint8_t dir, uint8_t ini_val); + + #endif /* DRIVERS_GPIO_H */ -- 2.43.0