UART protocol fundamentals in Embedded C - Time & Space Complexity
When working with UART communication, it's important to understand how the time to send data grows as the amount of data increases.
We want to know how the time needed changes when sending more bytes over UART.
Analyze the time complexity of the following UART send function.
void uart_send_bytes(const uint8_t* data, size_t length) {
for (size_t i = 0; i < length; i++) {
while (!uart_tx_ready()) {
// wait until UART is ready to send
}
uart_write_byte(data[i]);
}
}
This code sends each byte one by one over UART, waiting for the transmitter to be ready before sending the next byte.
Look at what repeats in this code.
- Primary operation: Sending each byte in the data array.
- How many times: Exactly once per byte, so it repeats
lengthtimes.
As you send more bytes, the time grows in a straight line with the number of bytes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 waits and sends |
| 100 | About 100 waits and sends |
| 1000 | About 1000 waits and sends |
Pattern observation: The time increases 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 you want to send.
[X] Wrong: "Sending multiple bytes over UART takes the same time no matter how many bytes there are."
[OK] Correct: Each byte must be sent one after another, so more bytes mean more time spent waiting and sending.
Understanding how UART sending time grows helps you design efficient communication in embedded systems and shows you can think about how code scales.
"What if we used DMA (Direct Memory Access) to send data instead of sending byte by byte? How would the time complexity change?"
Practice
Solution
Step 1: Understand UART acronym
UART is a communication protocol used in embedded systems for serial communication.Step 2: Match full form
UART stands for Universal Asynchronous Receiver/Transmitter, describing its function.Final Answer:
Universal Asynchronous Receiver/Transmitter -> Option DQuick Check:
UART = Universal Asynchronous Receiver/Transmitter [OK]
- Confusing UART with synchronous protocols
- Mixing up the acronym words
- Thinking UART is analog communication
Solution
Step 1: Identify common UART init function
In embedded C, uart_init with baud rate parameter is a common initialization call.Step 2: Check syntax correctness
uart_init(9600); uses correct function name and parameter style without extra keywords.Final Answer:
uart_init(9600); -> Option AQuick Check:
Correct UART init syntax = uart_init(9600); [OK]
- Using incorrect function names
- Adding parameter names inside call
- Using camelCase instead of snake_case
uart_init(9600);
uart_send_char('A');
uart_send_char('B');Solution
Step 1: Initialize UART at 9600 baud
The uart_init(9600) sets baud rate for communication.Step 2: Send characters in order
uart_send_char('A') sends 'A', then uart_send_char('B') sends 'B' sequentially.Final Answer:
AB -> Option CQuick Check:
Characters sent in order = AB [OK]
- Assuming characters reverse order
- Expecting spaces between characters
- Thinking uart_send_char is undefined
void setup() {
uart_init(115200);
uart_init(9600);
}Solution
Step 1: Review uart_init calls
Two uart_init calls with different baud rates cause conflict in configuration.Step 2: Understand UART config rules
UART baud rate must be set once before communication; multiple calls overwrite settings.Final Answer:
Multiple uart_init calls cause baud rate conflict -> Option BQuick Check:
Multiple init calls cause conflict = A [OK]
- Thinking multiple init calls are allowed
- Assuming uart_init has no parameters
- Ignoring case sensitivity in function names
Solution
Step 1: Understand string indexing in C
In C, single quotes ' ' denote characters, double quotes \" \" denote strings (char arrays).Step 2: Check each option for correct character access
for(int i=0; i<5; i++) uart_send_char(\"HELLO\"[i]); correctly indexes the string literal to access each character 'H','E','L','L','O'.Final Answer:
for(int i=0; i<5; i++) uart_send_char("HELLO"[i]); -> Option AQuick Check:
Correct string indexing uses double quotes \"HELLO\"[i] [OK]
- Using single quotes for multi-character string literals
- Adding integers to characters ('H' + i sends HIJKL)
- Using pointer arithmetic without dereferencing
Ready to prove it?
Pass the test and this pattern moves unknown
