Bird
Raised Fist0
Embedded Cprogramming~20 mins

Clock polarity and phase (CPOL, CPHA) in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
SPI Clock Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
SPI Clock Behavior with CPOL=0 and CPHA=0

Consider an SPI communication configured with CPOL=0 and CPHA=0. What is the clock idle state and when is data sampled?

Embedded C
/* SPI clock line behavior simulation */
// CPOL=0 means clock idle low
// CPHA=0 means data sampled on first clock edge (rising edge)
// Simulate clock and data sampling
#include <stdio.h>

int main() {
    int clock = 0; // idle low
    int data_sampled = 0;
    // Clock cycle: 0->1 rising edge, 1->0 falling edge
    // Data sampled on rising edge
    for (int cycle = 0; cycle < 2; cycle++) {
        clock = 1; // rising edge
        data_sampled++;
        clock = 0; // falling edge
    }
    printf("Data sampled %d times\n", data_sampled);
    return 0;
}
AClock idle state is low; data is sampled on the rising edge of the clock.
BClock idle state is high; data is sampled on the falling edge of the clock.
CClock idle state is high; data is sampled on the rising edge of the clock.
DClock idle state is low; data is sampled on the falling edge of the clock.
Attempts:
2 left
💡 Hint

Remember, CPOL=0 means clock line is low when idle. CPHA=0 means sampling on the first clock edge.

❓ Predict Output
intermediate
2:00remaining
SPI Clock Behavior with CPOL=1 and CPHA=1

For SPI configured with CPOL=1 and CPHA=1, what is the clock idle state and when is data sampled?

Embedded C
/* SPI clock line behavior simulation */
// CPOL=1 means clock idle high
// CPHA=1 means data sampled on second clock edge (falling edge)
#include <stdio.h>

int main() {
    int clock = 1; // idle high
    int data_sampled = 0;
    // Clock cycle: 1->0 falling edge, 0->1 rising edge
    // Data sampled on falling edge
    for (int cycle = 0; cycle < 2; cycle++) {
        clock = 0; // falling edge
        data_sampled++;
        clock = 1; // rising edge
    }
    printf("Data sampled %d times\n", data_sampled);
    return 0;
}
AClock idle state is low; data is sampled on the rising edge of the clock.
BClock idle state is low; data is sampled on the falling edge of the clock.
CClock idle state is high; data is sampled on the falling edge of the clock.
DClock idle state is high; data is sampled on the rising edge of the clock.
Attempts:
2 left
💡 Hint

CPOL=1 means clock line is high when idle. CPHA=1 means sampling on the second clock edge.

🧠 Conceptual
advanced
1:30remaining
Effect of CPHA on Data Sampling Edge

Which statement correctly describes the effect of CPHA on the SPI data sampling edge?

ACPHA=0 means data is sampled on the second clock edge; CPHA=1 means data is sampled on the first clock edge.
BCPHA=0 means data is sampled on the first clock edge; CPHA=1 means data is sampled on the second clock edge.
CCPHA controls the clock polarity, not the sampling edge.
DCPHA=1 disables data sampling during clock edges.
Attempts:
2 left
💡 Hint

Think about how CPHA shifts the sampling point between edges.

🔧 Debug
advanced
1:30remaining
Identify the SPI Configuration Error

Given the following SPI configuration code snippet, what error will occur?

Embedded C
spi_config.cpol = 2;
spi_config.cpha = 0;
// Start SPI communication
spi_start(&spi_config);
ARuntime error due to invalid CPOL value.
BNo error; CPOL=2 is valid and means clock idle high.
CCompile-time syntax error due to missing semicolon.
DLogical error: CPHA must be 1 if CPOL is 2.
Attempts:
2 left
💡 Hint

CPOL can only be 0 or 1 in SPI standards.

🚀 Application
expert
2:00remaining
Determine SPI Mode from CPOL and CPHA

In SPI communication, the mode number (0 to 3) is determined by CPOL and CPHA values. Which option correctly maps CPOL=1 and CPHA=0 to the SPI mode number?

AMode 0
BMode 1
CMode 3
DMode 2
Attempts:
2 left
💡 Hint

SPI mode = CPOL * 2 + CPHA

Practice

(1/5)
1.

What does the CPOL setting control in SPI communication?

easy
A. The speed of the clock signal
B. The idle level of the clock signal (high or low)
C. The data bit order (MSB or LSB first)
D. The voltage level of the data line

Solution

  1. Step 1: Understand CPOL meaning

    CPOL stands for Clock Polarity and it defines the idle state of the clock line in SPI communication.
  2. Step 2: Identify what CPOL controls

    CPOL sets whether the clock signal is low or high when idle (not toggling).
  3. Final Answer:

    The idle level of the clock signal (high or low) -> Option B
  4. Quick Check:

    CPOL = Clock idle level [OK]
Hint: CPOL = Clock idle level, remember idle means 'rest' state [OK]
Common Mistakes:
  • Confusing CPOL with clock speed
  • Thinking CPOL controls data order
  • Mixing CPOL with voltage levels
2.

Which of the following is the correct way to set CPHA to 1 in embedded C?

SPI->CR1 = ...;
easy
A. SPI->CR1 |= (1 << 1);
B. SPI->CR1 |= (1 << 2);
C. SPI->CR1 |= (1 << 0);
D. SPI->CR1 |= (1 << 3);

Solution

  1. Step 1: Identify CPHA bit position

    In many SPI controllers, CPHA is bit 1 (second bit) in the control register.
  2. Step 2: Set CPHA bit correctly

    Setting CPHA to 1 means setting bit 1, so use (1 << 1) to set that bit.
  3. Final Answer:

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

    CPHA bit = 1 << 1 [OK]
Hint: CPHA usually bit 1, so shift 1 by 1 [OK]
Common Mistakes:
  • Using wrong bit shift for CPHA
  • Confusing CPHA with CPOL bit position
  • Forgetting to use bitwise OR to set bit
3.

Given the following code snippet, what is the SPI mode configured?

SPI->CR1 = (1 << 1) | (1 << 0);

Assuming bit 0 = CPOL and bit 1 = CPHA.

medium
A. CPOL=0, CPHA=1 (Mode 1)
B. CPOL=1, CPHA=0 (Mode 2)
C. CPOL=1, CPHA=1 (Mode 3)
D. CPOL=0, CPHA=0 (Mode 0)

Solution

  1. Step 1: Decode bits from CR1 value

    The value sets bit 0 and bit 1 to 1, so CPOL=1 and CPHA=1.
  2. Step 2: Match CPOL and CPHA to SPI mode

    CPOL=1 and CPHA=1 corresponds to SPI Mode 3.
  3. Final Answer:

    CPOL=1, CPHA=1 (Mode 3) -> Option C
  4. Quick Check:

    Bits 0 and 1 set = CPOL=1, CPHA=1 [OK]
Hint: Bit 0 = CPOL, bit 1 = CPHA, both 1 means Mode 3 [OK]
Common Mistakes:
  • Mixing up which bit is CPOL or CPHA
  • Confusing SPI mode numbers
  • Ignoring bit positions in register
4.

Identify the error in this SPI configuration code snippet:

SPI->CR1 = (1 << 2); // Set CPHA to 1

Assuming CPHA is bit 1 and CPOL is bit 0.

medium
A. CPHA bit is incorrectly set at bit 2 instead of bit 1
B. CPOL bit should be set before CPHA
C. The code should clear other bits before setting CPHA
D. CPHA cannot be set using bit shifts

Solution

  1. Step 1: Check CPHA bit position

    CPHA is bit 1, but code sets bit 2, which is incorrect.
  2. Step 2: Understand correct bit setting

    To set CPHA, bit 1 must be set using (1 << 1), not (1 << 2).
  3. Final Answer:

    CPHA bit is incorrectly set at bit 2 instead of bit 1 -> Option A
  4. Quick Check:

    CPHA = bit 1, code sets bit 2 [OK]
Hint: CPHA is bit 1, not bit 2; check bit positions carefully [OK]
Common Mistakes:
  • Setting wrong bit for CPHA
  • Assuming CPHA is bit 2 by mistake
  • Not verifying hardware documentation
5.

You need to configure SPI to Mode 0 (CPOL=0, CPHA=0) but your device only works if data is sampled on the second clock edge. How should you set CPOL and CPHA?

hard
A. CPOL=0, CPHA=0 to sample on second edge
B. CPOL=1, CPHA=0 to sample on second edge
C. CPOL=1, CPHA=1 to sample on second edge
D. CPOL=0, CPHA=1 to sample on second edge

Solution

  1. Step 1: Understand SPI Mode 0 settings

    Mode 0 means CPOL=0 (clock idle low) and CPHA=0 (sample on first edge).
  2. Step 2: Adjust CPHA to sample on second edge

    To sample on the second clock edge, CPHA must be set to 1, keeping CPOL=0.
  3. Step 3: Confirm final settings

    Set CPOL=0 and CPHA=1 to keep clock idle low but sample on second edge.
  4. Final Answer:

    CPOL=0, CPHA=1 to sample on second edge -> Option D
  5. Quick Check:

    Sample second edge = CPHA=1 [OK]
Hint: CPHA=1 means sample on second edge, CPOL controls idle level [OK]
Common Mistakes:
  • Assuming Mode 0 always means sample on first edge
  • Changing CPOL when only CPHA needed
  • Not knowing which edge is first or second
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