0
0
Embedded Cprogramming~5 mins

Clock gating for power saving in Embedded C

Choose your learning style9 modes available
Introduction

Clock gating helps save power by turning off the clock signal to parts of a chip when they are not in use. This stops unnecessary switching and reduces energy use.

When a microcontroller has modules that are idle and don't need to run all the time.
In battery-powered devices to extend battery life by reducing power consumption.
When designing low-power embedded systems that must run efficiently.
To reduce heat generation by lowering switching activity inside the chip.
Syntax
Embedded C
void enable_clock_gating(ModuleName module);
void disable_clock_gating(ModuleName module);

Clock gating is usually controlled by setting or clearing bits in special registers.

Exact syntax depends on the microcontroller and its hardware registers.

Examples
This turns off the clock to the UART module when it is idle, saving power.
Embedded C
/* Enable clock gating for UART module */
enable_clock_gating(UART_MODULE);
This turns on the clock to the ADC module so it can work.
Embedded C
/* Disable clock gating for ADC module to start conversion */
disable_clock_gating(ADC_MODULE);
Sample Program

This program simulates clock gating by setting bits in a register. Setting a bit means the clock is off for that module, clearing it means the clock is on.

We start with all clocks on (register 0). Then we turn off UART clock, then ADC clock, then turn UART clock back on.

Embedded C
#include <stdint.h>
#include <stdio.h>

// Simulated hardware register for clock gating control
uint32_t CLOCK_GATING_REG = 0;

// Define bit positions for modules
#define UART_MODULE_BIT 0
#define ADC_MODULE_BIT 1

void enable_clock_gating(uint8_t module_bit) {
    // Set bit to 1 to gate (turn off) clock for that module
    CLOCK_GATING_REG |= (1U << module_bit);
}

void disable_clock_gating(uint8_t module_bit) {
    // Clear bit to 0 to enable clock for that module
    CLOCK_GATING_REG &= ~(1U << module_bit);
}

int main() {
    printf("Initial CLOCK_GATING_REG: 0x%X\n", CLOCK_GATING_REG);

    // Enable clock gating for UART (turn off UART clock)
    enable_clock_gating(UART_MODULE_BIT);
    printf("After gating UART clock: 0x%X\n", CLOCK_GATING_REG);

    // Enable clock gating for ADC (turn off ADC clock)
    enable_clock_gating(ADC_MODULE_BIT);
    printf("After gating ADC clock: 0x%X\n", CLOCK_GATING_REG);

    // Disable clock gating for UART (turn on UART clock)
    disable_clock_gating(UART_MODULE_BIT);
    printf("After ungating UART clock: 0x%X\n", CLOCK_GATING_REG);

    return 0;
}
OutputSuccess
Important Notes

Clock gating saves power by stopping the clock signal to unused parts of a chip.

Always check your microcontroller's datasheet for the correct registers and bits to control clock gating.

Improper clock gating can cause modules to stop working or cause bugs, so use carefully.

Summary

Clock gating turns off clocks to unused modules to save power.

It is done by setting or clearing bits in hardware registers.

Use clock gating in embedded systems to extend battery life and reduce heat.