implement subsystem_power() instead of a specific function in each subsystem
authorNathael Pajani <nathael.pajani@ed3l.fr>
Mon, 22 Jul 2013 23:29:36 +0000 (01:29 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Tue, 8 Nov 2022 16:03:04 +0000 (17:03 +0100)
core/system.c
drivers/gpio.c
drivers/i2c.c
drivers/serial.c
include/core/system.h

index 7f26794..e0c588e 100644 (file)
@@ -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                                           */
 /***************************************************************************** */
index 973b81a..bf60cfc 100644 (file)
 
 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 */
index 8f75684..60692ae 100644 (file)
@@ -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;
 }
 
index 9ae5d78..405e94b 100644 (file)
@@ -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);
 }
 
 
index 8713806..2e92403 100644 (file)
@@ -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);