From: Nathael Pajani Date: Tue, 22 Apr 2014 21:38:27 +0000 (+0200) Subject: Improved and easier GPIO ports access, based on the port defined in the pio struct... X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=26741900eb739000f793de84bbca1d694e648270;p=soft%2Flpc122x%2Fcore Improved and easier GPIO ports access, based on the port defined in the pio struct used for gpio definition. --- diff --git a/drivers/eeprom.c b/drivers/eeprom.c index f6d786b..70b0c17 100644 --- a/drivers/eeprom.c +++ b/drivers/eeprom.c @@ -36,22 +36,20 @@ /* These are place-holders to set the SPI chip select low if the SPI driver is not used. * These dummy functions will be over-ridden by SPI ones if the SPI driver is used. */ -#define I2C_CS_GPIO LPC_GPIO_0_15 +static struct pio i2c_eeprom_cs = LPC_GPIO_0_15; int I2C_CS_Default_Set(void) { - struct lpc_gpio* gpio0 = LPC_GPIO_0; - struct pio i2c_eeprom_cs = I2C_CS_GPIO; + struct lpc_gpio* gpio_port_regs = LPC_GPIO_REGS(i2c_eeprom_cs.port); config_pio(&i2c_eeprom_cs, (LPC_IO_MODE_PULL_UP | LPC_IO_DIGITAL)); /* Configure SPI_CS as output and set it low. */ - gpio0->data_dir |= (1 << i2c_eeprom_cs.pin); - gpio0->clear = (1 << i2c_eeprom_cs.pin); + gpio_port_regs->data_dir |= (1 << i2c_eeprom_cs.pin); + gpio_port_regs->clear = (1 << i2c_eeprom_cs.pin); return 0; } void I2C_CS_Default_Release(void) { - struct lpc_gpio* gpio0 = LPC_GPIO_0; - struct pio i2c_eeprom_cs = I2C_CS_GPIO; - gpio0->set = (1 << i2c_eeprom_cs.pin); + struct lpc_gpio* gpio_port_regs = LPC_GPIO_REGS(i2c_eeprom_cs.port); + gpio_port_regs->set = (1 << i2c_eeprom_cs.pin); } int spi_device_cs_pull_low(void) __attribute__ ((weak, alias ("I2C_CS_Default_Set"))); diff --git a/drivers/gpio.c b/drivers/gpio.c index 2c6bfc0..718eb75 100644 --- a/drivers/gpio.c +++ b/drivers/gpio.c @@ -32,10 +32,10 @@ #include "drivers/gpio.h" + /***************************************************************************** */ /* GPIO setup */ - /* Set all GPIO used in a default state */ extern struct pio gpio_pins[]; @@ -73,7 +73,7 @@ static void (*gpio_calbacks_port2[PORT2_NB_PINS]) (uint32_t); int set_gpio_callback(void (*callback) (uint32_t), struct pio* gpio, uint8_t sense) { - struct lpc_gpio* gpio_port = NULL; + struct lpc_gpio* gpio_port = LPC_GPIO_REGS(gpio->port); uint32_t irq = 0; /* Register the callback */ @@ -83,21 +83,18 @@ int set_gpio_callback(void (*callback) (uint32_t), struct pio* gpio, uint8_t sen if (gpio->pin >= PORT0_NB_PINS) return -EINVAL; gpio_calbacks_port0[gpio->pin] = callback; - gpio_port = LPC_GPIO_0; irq = PIO_0_IRQ; break; case 1: if (gpio->pin >= PORT1_NB_PINS) return -EINVAL; gpio_calbacks_port1[gpio->pin] = callback; - gpio_port = LPC_GPIO_1; irq = PIO_1_IRQ; break; case 2: if (gpio->pin >= PORT2_NB_PINS) return -EINVAL; gpio_calbacks_port2[gpio->pin] = callback; - gpio_port = LPC_GPIO_2; irq = PIO_2_IRQ; break; default: @@ -141,7 +138,7 @@ int set_gpio_callback(void (*callback) (uint32_t), struct pio* gpio, uint8_t sen } void remove_gpio_callback(struct pio* gpio) { - struct lpc_gpio* gpio_port = NULL; + struct lpc_gpio* gpio_port = LPC_GPIO_REGS(gpio->port); uint32_t irq = 0; /* Remove the handler */ @@ -150,21 +147,18 @@ void remove_gpio_callback(struct pio* gpio) if (gpio->pin >= PORT0_NB_PINS) return; gpio_calbacks_port0[gpio->pin] = NULL; - gpio_port = LPC_GPIO_0; irq = PIO_0_IRQ; break; case 1: if (gpio->pin >= PORT1_NB_PINS) return; gpio_calbacks_port1[gpio->pin] = NULL; - gpio_port = LPC_GPIO_1; irq = PIO_1_IRQ; break; case 2: if (gpio->pin >= PORT2_NB_PINS) return; gpio_calbacks_port2[gpio->pin] = NULL; - gpio_port = LPC_GPIO_2; irq = PIO_2_IRQ; break; default: diff --git a/drivers/status_led.c b/drivers/status_led.c index 728991b..e55efd4 100644 --- a/drivers/status_led.c +++ b/drivers/status_led.c @@ -39,7 +39,7 @@ void status_led_config(void) { - struct lpc_gpio* gpio1 = LPC_GPIO_1; + struct lpc_gpio* gpio1 = LPC_GPIO_REGS(1); uint32_t mode = (LPC_IO_MODE_PULL_UP | LPC_IO_DIGITAL | LPC_IO_DRIVE_HIGHCURENT); struct pio red_led = LPC_GPIO_1_5; struct pio green_led = LPC_GPIO_1_4; @@ -55,7 +55,7 @@ void status_led_config(void) void status_led(uint32_t val) { - struct lpc_gpio* gpio1 = LPC_GPIO_1; + struct lpc_gpio* gpio1 = LPC_GPIO_REGS(1); switch (val) { case red_only: diff --git a/include/core/lpc_regs_12xx.h b/include/core/lpc_regs_12xx.h index dfddff8..c476d40 100644 --- a/include/core/lpc_regs_12xx.h +++ b/include/core/lpc_regs_12xx.h @@ -463,6 +463,7 @@ struct lpc_gpio #define LPC_GPIO_1 ((struct lpc_gpio *) LPC_GPIO_1_BASE) #define LPC_GPIO_2 ((struct lpc_gpio *) LPC_GPIO_2_BASE) +#define LPC_GPIO_REGS(x) ((struct lpc_gpio *) (LPC_AHB_BASE + (0x10000 * (x)))) /***************************************************************************** */