From: Nathael Pajani Date: Mon, 22 Jul 2013 23:29:36 +0000 (+0200) Subject: implement subsystem_power() instead of a specific function in each subsystem X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=1504d070be9deebf6429dcaa4ac484f7256a46ae;p=soft%2Flpc122x%2Fcore implement subsystem_power() instead of a specific function in each subsystem --- diff --git a/core/system.c b/core/system.c index 7f26794..e0c588e 100644 --- a/core/system.c +++ b/core/system.c @@ -134,6 +134,17 @@ void enter_deep_sleep(void) /* FIXME */ } +/* Power on or off a subsystem */ +void subsystem_power(uint32_t power_bit, uint32_t on_off) +{ + struct lpc_sys_control* sys_ctrl = LPC_SYS_CONTROL; + if (on_off == 1) { + sys_ctrl->sys_AHB_clk_ctrl |= power_bit; + } else { + sys_ctrl->sys_AHB_clk_ctrl &= ~(power_bit); + } +} + /***************************************************************************** */ /* System Clock */ /***************************************************************************** */ diff --git a/drivers/gpio.c b/drivers/gpio.c index 973b81a..bf60cfc 100644 --- a/drivers/gpio.c +++ b/drivers/gpio.c @@ -36,39 +36,34 @@ void config_gpio(volatile uint32_t* handle, uint32_t mode) { - struct lpc_sys_control* sys_ctrl = LPC_SYS_CONTROL; - /* Make sure IO_Config is clocked */ - sys_ctrl->sys_AHB_clk_ctrl |= LPC_SYS_ABH_CLK_CTRL_IO_CONFIG; + subsystem_power(LPC_SYS_ABH_CLK_CTRL_IO_CONFIG, 1); *handle = mode; /* Config done, power off IO_CONFIG block */ - sys_ctrl->sys_AHB_clk_ctrl &= ~LPC_SYS_ABH_CLK_CTRL_IO_CONFIG; + subsystem_power(LPC_SYS_ABH_CLK_CTRL_IO_CONFIG, 0); } void gpio_on(void) { - struct lpc_sys_control* sys_ctrl = LPC_SYS_CONTROL; - /* Provide power to IO ports */ - sys_ctrl->sys_AHB_clk_ctrl |= LPC_SYS_ABH_CLK_CTRL_GPIO0; - sys_ctrl->sys_AHB_clk_ctrl |= LPC_SYS_ABH_CLK_CTRL_GPIO1; + /* Provide power to GPIO control blocks */ + subsystem_power(LPC_SYS_ABH_CLK_CTRL_GPIO0, 1); + subsystem_power(LPC_SYS_ABH_CLK_CTRL_GPIO1, 1); } void gpio_off(void) { - struct lpc_sys_control* sys_ctrl = LPC_SYS_CONTROL; - /* Provide power to IO ports */ - sys_ctrl->sys_AHB_clk_ctrl &= ~(LPC_SYS_ABH_CLK_CTRL_GPIO0); - sys_ctrl->sys_AHB_clk_ctrl &= ~(LPC_SYS_ABH_CLK_CTRL_GPIO1); + /* Remove power from GPIO control blocks */ + subsystem_power(LPC_SYS_ABH_CLK_CTRL_GPIO0, 0); + subsystem_power(LPC_SYS_ABH_CLK_CTRL_GPIO1, 0); } /* Set all GPIO used on the GPIO_Demo module in a default state */ void set_gpio_pins(void) { - struct lpc_sys_control* sys_ctrl = LPC_SYS_CONTROL; struct lpc_io_control* ioctrl = LPC_IO_CONTROL; /* Make sure IO_Config is clocked */ - sys_ctrl->sys_AHB_clk_ctrl |= LPC_SYS_ABH_CLK_CTRL_IO_CONFIG; + subsystem_power(LPC_SYS_ABH_CLK_CTRL_IO_CONFIG, 1); /* Configure GPIO pins */ ioctrl->pio0_0 = LPC_IO_FUNC_ALT(0) | LPC_IO_MODE_PULL_UP; @@ -96,7 +91,7 @@ void set_gpio_pins(void) ioctrl->pio1_3 = LPC_IO_FUNC_ALT(1) | LPC_IO_ANALOG; /* Config done, power off IO_CONFIG block */ - sys_ctrl->sys_AHB_clk_ctrl &= ~LPC_SYS_ABH_CLK_CTRL_IO_CONFIG; + subsystem_power(LPC_SYS_ABH_CLK_CTRL_IO_CONFIG, 1); } /* Handlers */ diff --git a/drivers/i2c.c b/drivers/i2c.c index 8f75684..60692ae 100644 --- a/drivers/i2c.c +++ b/drivers/i2c.c @@ -449,25 +449,23 @@ static void i2c_clock_on(uint32_t i2c_clk_freq) void set_i2c_pins(void) { - struct lpc_sys_control* sys_ctrl = LPC_SYS_CONTROL; struct lpc_io_control* ioctrl = LPC_IO_CONTROL; /* Make sure IO_Config is clocked */ - sys_ctrl->sys_AHB_clk_ctrl |= LPC_SYS_ABH_CLK_CTRL_IO_CONFIG; + subsystem_power(LPC_SYS_ABH_CLK_CTRL_IO_CONFIG, 1); ioctrl->pio0_10 = LPC_IO_FUNC_ALT(2) | LPC_IO_OPEN_DRAIN_ENABLE; /* True open drain */ ioctrl->pio0_11 = LPC_IO_FUNC_ALT(2) | LPC_IO_OPEN_DRAIN_ENABLE; /* Config done, power off IO_CONFIG block */ - sys_ctrl->sys_AHB_clk_ctrl &= ~LPC_SYS_ABH_CLK_CTRL_IO_CONFIG; + subsystem_power(LPC_SYS_ABH_CLK_CTRL_IO_CONFIG, 0); } void i2c_on(uint32_t i2c_clk_freq) { - struct lpc_sys_control* sys_ctrl = LPC_SYS_CONTROL; struct lpc_i2c* i2c = LPC_I2C0; NVIC_DisableIRQ(I2C0_IRQ); /* Power on I2C 0 block */ - sys_ctrl->sys_AHB_clk_ctrl |= LPC_SYS_ABH_CLK_CTRL_I2C; + subsystem_power(LPC_SYS_ABH_CLK_CTRL_I2C, 1); /* Set clock */ i2c_clock_on(i2c_clk_freq); mod_i2c.clock = i2c_clk_freq; @@ -486,9 +484,8 @@ void i2c_on(uint32_t i2c_clk_freq) void i2c_off(void) { - struct lpc_sys_control* sys_ctrl = LPC_SYS_CONTROL; NVIC_DisableIRQ(I2C0_IRQ); - sys_ctrl->sys_AHB_clk_ctrl &= ~(LPC_SYS_ABH_CLK_CTRL_I2C); + subsystem_power(LPC_SYS_ABH_CLK_CTRL_I2C, 0); mod_i2c.clock = 0; } diff --git a/drivers/serial.c b/drivers/serial.c index 9ae5d78..405e94b 100644 --- a/drivers/serial.c +++ b/drivers/serial.c @@ -196,16 +196,6 @@ static void uart_clk_off(uint32_t uart_num) sys_ctrl->uart_clk_div[uart_num] = 0; } -static void uart_power(uint32_t power_bit, uint32_t on_off) -{ - struct lpc_sys_control* sys_ctrl = LPC_SYS_CONTROL; - if (on_off == 1) { - sys_ctrl->sys_AHB_clk_ctrl |= power_bit; - } else { - sys_ctrl->sys_AHB_clk_ctrl &= ~(power_bit); - } -} - static uint32_t uart_mode_setup(uint32_t uart_num) { struct lpc_uart* uart = uarts[uart_num].regs; /* Get the right registers */ @@ -294,10 +284,10 @@ int uart_on(uint32_t uart_num, uint32_t baudrate) NVIC_DisableIRQ( uart->irq ); /* Setup pins, must be done before clock setup and with uart powered off. */ uart_clk_off(uart_num); - uart_power(uart->power_offset, 0); + subsystem_power(uart->power_offset, 0); uart_set_pin_func(uart_num); /* Turn On power */ - uart_power(uart->power_offset, 1); + subsystem_power(uart->power_offset, 1); /* Setup clock acording to baudrate */ uart_clk_on(uart_num, baudrate); /* Setup mode, fifo, ... */ @@ -318,7 +308,7 @@ void uart_off(uint32_t uart_num) NVIC_DisableIRQ( uart->irq ); uart_clk_off(uart_num); /* Turn Off power */ - uart_power(uart->power_offset, 0); + subsystem_power(uart->power_offset, 0); } diff --git a/include/core/system.h b/include/core/system.h index 8713806..2e92403 100644 --- a/include/core/system.h +++ b/include/core/system.h @@ -61,6 +61,9 @@ void stop_watchdog(void); /***************************************************************************** */ void enter_deep_sleep(void); +/* Power on or off a subsystem */ +void subsystem_power(uint32_t power_bit, uint32_t on_off); + /* Configure the brown-out detection */ void system_brown_out_detection_config(uint32_t level);