Transmitting a byte over UART in Embedded C - Time & Space Complexity
We want to understand how long it takes to send a byte using UART communication.
How does the time to send data change as we send more bytes?
Analyze the time complexity of the following code snippet.
void UART_TransmitByte(unsigned char data) {
while (!(UART_STATUS & UART_TX_READY)) {
// wait until transmitter is ready
}
UART_DATA = data; // send the byte
}
This code waits until the UART transmitter is ready, then sends one byte.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop waiting for the UART transmitter to be ready.
- How many times: It repeats until the hardware signals readiness, usually a small fixed number of times per byte.
Sending one byte takes some fixed time waiting for readiness and transmitting.
| Input Size (n bytes) | Approx. Operations |
|---|---|
| 10 | About 10 waits and sends |
| 100 | About 100 waits and sends |
| 1000 | About 1000 waits and sends |
Pattern observation: The time grows directly with the number of bytes sent.
Time Complexity: O(n)
This means the time to send data grows linearly with the number of bytes.
[X] Wrong: "Sending one byte takes the same time no matter how many bytes I send."
[OK] Correct: Each byte requires waiting and sending, so total time adds up as you send more bytes.
Understanding how hardware communication time scales helps you write efficient embedded code and explain performance clearly.
"What if we used interrupts instead of waiting in a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand UART transmit buffer role
The UART transmit buffer holds the byte to be sent. It must be empty before loading a new byte.Step 2: Reason why waiting is necessary
If you send a byte before the buffer is empty, the previous byte might be overwritten, causing data loss.Final Answer:
To ensure the previous byte has been sent and avoid data loss -> Option DQuick Check:
Wait for buffer empty = prevent data loss [OK]
- Ignoring the transmit buffer status
- Confusing transmit and receive buffers
- Trying to send bytes too fast
data using a register UART_STATUS with bit 5 as TX_READY flag?Solution
Step 1: Understand the TX_READY flag meaning
Bit 5 set means transmit buffer is ready (empty), so wait until it is set.Step 2: Analyze each code snippet
while (!(UART_STATUS & (1 << 5))); UART_DATA = data; waits in a loop until bit 5 is set, then sends data. This is correct. if (UART_STATUS & (1 << 5)) UART_DATA = data; sends data only if ready once, no wait. UART_DATA = data; while (!(UART_STATUS & (1 << 5))); sends data before waiting, risking overwrite. while (UART_STATUS & (1 << 5)) UART_DATA = data; loops while bit 5 is set, which is opposite logic.Final Answer:
while (!(UART_STATUS & (1 << 5))); UART_DATA = data; -> Option AQuick Check:
Wait until TX_READY set, then send [OK]
- Using if instead of while to wait
- Sending data before waiting
- Checking wrong bit or wrong condition
UART_STATUS initially has bit 5 cleared and then set after some time?while (!(UART_STATUS & (1 << 5))) {
// wait
}
UART_DATA = 0xAB;Solution
Step 1: Analyze the while loop condition
The loop waits while bit 5 of UART_STATUS is 0 (not ready). Initially bit 5 is cleared, so it waits.Step 2: Consider bit 5 changing to set
When bit 5 becomes set, the condition becomes false, loop exits, and UART_DATA is assigned 0xAB.Final Answer:
The byte 0xAB is sent after waiting for TX_READY -> Option BQuick Check:
Wait loop exits when ready, then send byte [OK]
- Assuming immediate send without waiting
- Thinking loop never ends
- Confusing bit set/clear logic
UART_DATA = data;
while (!(UART_STATUS & (1 << 5))) {
// wait
}Solution
Step 1: Check order of operations
The code writes data to UART_DATA before waiting for TX_READY flag.Step 2: Understand risk of overwriting
If UART is not ready, writing data first can overwrite previous byte, causing data loss.Final Answer:
The data is sent before checking if UART is ready, risking overwrite -> Option CQuick Check:
Send only after waiting for TX_READY [OK]
- Writing data before waiting
- Checking wrong status bit
- Missing wait loop entirely
data[5] = {0x10, 0x20, 0x30, 0x40, 0x50} over UART. Which code snippet correctly transmits all bytes ensuring no data loss?Solution
Step 1: Understand the need to wait before sending each byte
Each byte must be sent only after UART transmit buffer is ready to avoid overwriting.Step 2: Analyze each option
for (int i = 0; i < 5; i++) { while (!(UART_STATUS & (1 << 5))); UART_DATA = data[i]; } waits before sending each byte, ensuring safe transmission. for (int i = 0; i < 5; i++) { UART_DATA = data[i]; while (!(UART_STATUS & (1 << 5))); } sends byte first then waits, risking overwrite. int i = 0; while (i < 5) { UART_DATA = data[i]; i++; } sends bytes without waiting. for (int i = 0; i < 5; i++) { UART_DATA = data[i]; } sends all bytes without any wait, causing data loss.Final Answer:
for (int i = 0; i < 5; i++) { while (!(UART_STATUS & (1 << 5))); UART_DATA = data[i]; } -> Option AQuick Check:
Wait before each send to avoid data loss [OK]
- Sending bytes without waiting
- Waiting after sending bytes
- Ignoring transmit buffer status
Ready to prove it?
Pass the test and this pattern moves unknown
