GPIO register configuration in Embedded C - Time & Space Complexity
We want to understand how the time to configure GPIO registers changes as we configure more pins.
How does the work grow when we set up many pins one by one?
Analyze the time complexity of the following code snippet.
// Configure GPIO pins one by one
for (int i = 0; i < num_pins; i++) {
GPIO->MODER &= ~(0x3 << (2 * i)); // Clear mode bits
GPIO->MODER |= (0x1 << (2 * i)); // Set pin as output
}
This code sets each GPIO pin mode to output by updating register bits in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop that updates GPIO register bits for each pin.
- How many times: Exactly once per pin, so
num_pinstimes.
Each pin requires a fixed number of steps to configure, so total work grows directly with number of pins.
| Input Size (num_pins) | Approx. Operations |
|---|---|
| 10 | About 10 register updates |
| 100 | About 100 register updates |
| 1000 | About 1000 register updates |
Pattern observation: The work grows steadily and directly with the number of pins.
Time Complexity: O(n)
This means the time to configure GPIO pins grows linearly as you add more pins.
[X] Wrong: "Configuring multiple pins at once takes the same time as one pin."
[OK] Correct: Each pin requires separate register updates, so more pins mean more work.
Understanding how loops affect time helps you explain embedded code efficiency clearly and confidently.
"What if we configure all pins by writing once to the register instead of looping? How would the time complexity change?"
Practice
Solution
Step 1: Understand GPIO register role
GPIO registers control the mode and state of microcontroller pins.Step 2: Identify the main use
Setting pins as input or output allows the microcontroller to read sensors or control devices.Final Answer:
To set pins as input or output for hardware interaction -> Option DQuick Check:
GPIO config = pin mode setup [OK]
- Confusing GPIO with clock or memory settings
- Thinking GPIO handles interrupts directly
- Assuming GPIO sets processor speed
GPIO_DIR to configure pin 3 as output?Solution
Step 1: Understand bitwise OR for setting bits
Using OR with (1 << 3) sets bit 3 without changing other bits.Step 2: Analyze each option
GPIO_DIR = GPIO_DIR | (1 << 3); correctly sets bit 3. GPIO_DIR = GPIO_DIR & (1 << 3); clears other bits. GPIO_DIR = (1 << 3); overwrites all bits. GPIO_DIR = ~GPIO_DIR | (1 << 3); inverts bits incorrectly.Final Answer:
GPIO_DIR = GPIO_DIR | (1 << 3); -> Option CQuick Check:
Set bit with OR = correct [OK]
- Using AND (&) instead of OR (|) to set bits
- Overwriting register instead of modifying bits
- Using NOT (~) incorrectly
uint8_t GPIO_DATA = 0x00;
GPIO_DATA |= (1 << 2);
GPIO_DATA &= ~(1 << 0);
GPIO_DATA |= (1 << 0);
What is the final value of
GPIO_DATA in hexadecimal?Solution
Step 1: Apply each operation step-by-step
Start: GPIO_DATA = 0x00 (binary 0000 0000).
After GPIO_DATA |= (1 << 2): sets bit 2 -> 0000 0100 (0x04).
After GPIO_DATA &= ~(1 << 0): clears bit 0 -> still 0000 0100 (0x04).
After GPIO_DATA |= (1 << 0): sets bit 0 -> 0000 0101 (0x05).Step 2: Convert final binary to hex
Binary 0000 0101 equals hex 0x05.Final Answer:
0x05 -> Option BQuick Check:
Bit 2 and 0 set = 0x05 [OK]
- Ignoring bit clear operation
- Confusing bit positions
- Not converting binary to hex correctly
GPIO_DIR &= ~(1 << 5); // Set pin 5 as input
GPIO_DIR |= (1 << 5); // Set pin 5 as output
Solution
Step 1: Analyze the two lines
First line clears bit 5 to set pin 5 as input.
Second line sets bit 5 to set pin 5 as output.Step 2: Understand the conflict
Both lines contradict each other immediately, so pin 5 cannot be both input and output at the same time.Final Answer:
Pin 5 is configured as both input and output, causing conflict -> Option AQuick Check:
Conflicting pin modes = error [OK]
- Overwriting pin mode without delay or condition
- Misunderstanding bitwise operators
- Assuming GPIO_DIR is read-only
GPIO_DIR register. Which code snippet correctly sets this configuration without affecting other bits?Solution
Step 1: Identify bits for outputs and inputs
Outputs: pins 0,2,4 -> bits 0,2,4 set to 1 -> binary 0001 0101 (0x15).
Inputs: pins 1,3,5 -> bits 1,3,5 preserved.Step 2: Use mask to clear bits 0,2,4 before setting
Mask 0xAA (1010 1010) clears bits 0,2,4 when ANDed.
Then OR with 0x15 sets output bits.
So: GPIO_DIR = (GPIO_DIR & 0xAA) | 0x15;Final Answer:
GPIO_DIR = (GPIO_DIR & 0xAA) | 0x15; -> Option AQuick Check:
Mask inputs, set outputs correctly [OK]
- Using wrong mask to clear bits
- Overwriting entire register losing other bits
- Confusing input bits with output bits
Ready to prove it?
Pass the test and this pattern moves unknown
