Bird
Raised Fist0
Embedded Cprogramming~20 mins

Clock gating for power saving in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Clock Gating Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this clock gating simulation code?

Consider the following embedded C code simulating clock gating behavior. What will be printed when this code runs?

Embedded C
#include <stdio.h>

int main() {
    int clock_enabled = 0;
    int power_consumption = 100; // base power

    if (clock_enabled) {
        power_consumption += 50;
        printf("Clock is enabled. Power consumption: %d\n", power_consumption);
    } else {
        printf("Clock is gated. Power consumption: %d\n", power_consumption);
    }
    return 0;
}
AClock is gated. Power consumption: 150
BClock is enabled. Power consumption: 150
CClock is gated. Power consumption: 100
DClock is enabled. Power consumption: 100
Attempts:
2 left
💡 Hint

Check the value of clock_enabled and how it affects power consumption.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes clock gating?

Choose the statement that correctly explains the purpose of clock gating in embedded systems.

AClock gating duplicates the clock signal to multiple modules for synchronization.
BClock gating disables the clock signal to unused modules to reduce power consumption.
CClock gating permanently turns off the power supply to a module.
DClock gating increases the clock frequency to improve performance.
Attempts:
2 left
💡 Hint

Think about how clock gating helps save power without turning off the entire module.

🔧 Debug
advanced
2:00remaining
Identify the error in this clock gating code snippet

What error will this embedded C code produce when compiled?

Embedded C
void clock_gate(int enable) {
    if enable {
        // Enable clock
    } else {
        // Disable clock
    }
}

int main() {
    clock_gate(1);
    return 0;
}
ASyntaxError: Missing parentheses in if statement
BTypeError: Invalid argument type for clock_gate
CRuntimeError: Infinite loop detected
DNo error, code compiles and runs
Attempts:
2 left
💡 Hint

Check the syntax of the if statement in C.

🚀 Application
advanced
1:30remaining
How many clock gating states are possible in this code?

Given this code controlling clock gating for two modules, how many unique clock gating states can the system have?

Embedded C
typedef struct {
    int module1_clock;
    int module2_clock;
} ClockControl;

int main() {
    ClockControl cc = {0, 0};
    // module1_clock and module2_clock can be 0 (gated) or 1 (enabled)
    return 0;
}
A4
B3
C2
D1
Attempts:
2 left
💡 Hint

Each module clock can be either gated or enabled independently.

❓ Predict Output
expert
2:30remaining
What is the output of this clock gating control function?

Analyze the following embedded C code that simulates clock gating control. What will be printed?

Embedded C
#include <stdio.h>

void clock_gate(int *clock, int enable) {
    *clock = enable ? 1 : 0;
}

int main() {
    int clock_signal = 0;
    clock_gate(&clock_signal, 1);
    clock_gate(&clock_signal, 0);
    if (clock_signal) {
        printf("Clock enabled\n");
    } else {
        printf("Clock gated\n");
    }
    return 0;
}
AClock enabled
BCompilation error
CNo output
DClock gated
Attempts:
2 left
💡 Hint

Trace the changes to clock_signal after each function call.

Practice

(1/5)
1. What is the main purpose of clock gating in embedded systems?
easy
A. To enable all hardware modules simultaneously
B. To increase the clock speed of the processor
C. To reset the microcontroller
D. To save power by turning off clocks to unused modules

Solution

  1. Step 1: Understand clock gating concept

    Clock gating disables the clock signal to parts of a chip that are not in use.
  2. Step 2: Identify the main benefit

    Disabling clocks reduces power consumption and heat generation.
  3. Final Answer:

    To save power by turning off clocks to unused modules -> Option D
  4. Quick Check:

    Clock gating = power saving [OK]
Hint: Clock gating means stopping clocks to save power [OK]
Common Mistakes:
  • Thinking clock gating speeds up the processor
  • Confusing clock gating with resetting hardware
  • Assuming clock gating enables all modules
2. Which of the following is the correct way to enable clock gating for a peripheral in embedded C?
easy
A. CLK_GATE_REG |= (1 << PERIPHERAL_BIT);
B. CLK_GATE_REG &= ~(1 << PERIPHERAL_BIT);
C. CLK_GATE_REG = 0;
D. CLK_GATE_REG ^= (1 << PERIPHERAL_BIT);

Solution

  1. Step 1: Understand bit setting for enabling clock gating

    Setting a bit (using |=) enables clock gating for that peripheral.
  2. Step 2: Analyze each option

    CLK_GATE_REG |= (1 << PERIPHERAL_BIT); sets the bit; CLK_GATE_REG &= ~(1 << PERIPHERAL_BIT); clears it; CLK_GATE_REG = 0; clears all bits; CLK_GATE_REG ^= (1 << PERIPHERAL_BIT); toggles bits.
  3. Final Answer:

    CLK_GATE_REG |= (1 << PERIPHERAL_BIT); -> Option A
  4. Quick Check:

    Set bit to enable clock gating [OK]
Hint: Use |= to set bits for enabling clock gating [OK]
Common Mistakes:
  • Using &=~ which clears bits instead of setting
  • Assigning 0 clears all bits
  • Using ^= toggles bits unpredictably
3. Given the code snippet:
uint32_t CLK_GATE_REG = 0x0;
#define PERIPH1_BIT 2
CLK_GATE_REG |= (1 << PERIPH1_BIT);
CLK_GATE_REG &= ~(1 << PERIPH1_BIT);
What is the final value of CLK_GATE_REG?
medium
A. 0x4
B. 0x0
C. 0x2
D. 0x6

Solution

  1. Step 1: Set bit 2 in CLK_GATE_REG

    CLK_GATE_REG |= (1 << 2) sets bit 2, so value becomes 0x4.
  2. Step 2: Clear bit 2 in CLK_GATE_REG

    CLK_GATE_REG &= ~(1 << 2) clears bit 2, so value returns to 0x0.
  3. Final Answer:

    0x0 -> Option B
  4. Quick Check:

    Set then clear bit 2 = 0x0 [OK]
Hint: Set then clear same bit results in zero [OK]
Common Mistakes:
  • Forgetting that clearing bit resets value
  • Confusing bit positions
  • Assuming bits accumulate without clearing
4. Identify the error in this clock gating code snippet:
uint32_t CLK_GATE_REG = 0xFF;
#define PERIPH2_BIT 3
CLK_GATE_REG = (1 << PERIPH2_BIT);
medium
A. It toggles bit 3 instead of setting it
B. It correctly enables clock gating for peripheral 2
C. It clears all bits except bit 3, disabling other clocks
D. It causes a syntax error due to missing semicolon

Solution

  1. Step 1: Analyze the assignment operation

    Assigning CLK_GATE_REG = (1 << 3) overwrites previous bits, clearing all except bit 3.
  2. Step 2: Understand impact on clock gating

    This disables clock gating for other peripherals unintentionally.
  3. Final Answer:

    It clears all bits except bit 3, disabling other clocks -> Option C
  4. Quick Check:

    Direct assignment clears other bits [OK]
Hint: Use |= to set bits, not = to avoid clearing others [OK]
Common Mistakes:
  • Using = instead of |= to set bits
  • Ignoring impact on other bits
  • Thinking this toggles bits
5. You want to save power by gating clocks to peripherals only when they are idle. Which approach correctly implements this in embedded C?
void update_clock_gating(bool peripheral_active) {
    if (peripheral_active) {
        CLK_GATE_REG &= ~(1 << PERIPH_BIT); // Disable gating
    } else {
        CLK_GATE_REG |= (1 << PERIPH_BIT);  // Enable gating
    }
}
hard
A. This code correctly enables gating when peripheral is idle
B. This code enables gating when peripheral is active, which wastes power
C. This code disables gating in both cases, no power saving
D. This code causes syntax errors due to missing variable declarations

Solution

  1. Step 1: Understand gating logic in code

    When peripheral_active is true, gating is disabled (clock runs). When false, gating is enabled (clock off).
  2. Step 2: Confirm power saving behavior

    Enabling gating only when peripheral is idle saves power correctly.
  3. Final Answer:

    This code correctly enables gating when peripheral is idle -> Option A
  4. Quick Check:

    Gating enabled when idle = power saving [OK]
Hint: Enable gating only when peripheral is idle [OK]
Common Mistakes:
  • Reversing active and idle logic
  • Forgetting to use |= and &=~ correctly
  • Assuming code has syntax errors without checking
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard