Printf redirect to UART in Embedded C - Time & Space Complexity
When we redirect printf to UART, we want to know how the time taken changes as we print more characters.
We ask: How does printing more data affect the time the program spends sending it?
Analyze the time complexity of the following code snippet.
int _write(int file, char *ptr, int len) {
for (int i = 0; i < len; i++) {
while (!(UART_STATUS_REG & UART_TX_READY)) {
; // wait until UART is ready
}
UART_DATA_REG = ptr[i];
}
return len;
}
This code sends each character one by one over UART, waiting for the hardware to be ready before sending the next.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each character in the string to send.
- How many times: The loop runs once for every character in the input length.
As the number of characters to print grows, the time spent sending grows in the same way.
| 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 grows directly with the number of characters; doubling characters doubles the time.
Time Complexity: O(n)
This means the time to send data grows linearly with the number of characters printed.
[X] Wrong: "Sending more characters takes the same time as sending one."
[OK] Correct: Each character must be sent separately, and the code waits for UART readiness each time, so more characters mean more waiting and sending steps.
Understanding how hardware communication time grows with data size helps you write efficient embedded code and explain your reasoning clearly in interviews.
What if we changed the code to send characters in larger blocks without waiting for each one? How would the time complexity change?
Practice
fputc function in embedded C when redirecting printf output to UART?Solution
Step 1: Understand
Theprintfoutput mechanismprintffunction sends characters one by one tofputcfor output.Step 2: Role of overriding
By overridingfputcfputc, we redirect each character to UART transmit function, sending it out via UART.Final Answer:
To send each character fromprintfto the UART hardware for transmission -> Option DQuick Check:
Overridingfputcredirects output to UART [OK]
printf calls fputc for output [OK]- Confusing
fputcwith UART initialization - Trying to change UART settings inside
fputc - Ignoring that
printfusesfputcinternally
fputc to redirect printf output to UART in embedded C?Solution
Step 1: Recall standard
The standardfputcsignaturefputcfunction prototype isint fputc(int ch, FILE *stream);.Step 2: Match options with correct signature
char fputc(int ch, FILE *f); matches the correct order and types: returns char (though standard is int), takes int and FILE pointer.Final Answer:
char fputc(int ch, FILE *f); -> Option AQuick Check:
Correctfputcprototype returns int and takes int, FILE* [OK]
fputc signature [OK]- Swapping parameter order
- Using void return type
- Using wrong parameter types
int fputc(int ch, FILE *f) {
UART_SendChar((char)ch);
return ch;
}
int main() {
printf("Hi\n");
return 0;
}Solution
Step 1: Understand
Each character of "Hi\n" is sent tofputcrole inprintffputc, which callsUART_SendCharto send it.Step 2: Output on UART terminal
Characters 'H', 'i', and newline '\n' are sent sequentially, so "Hi" and a newline appear on UART terminal.Final Answer:
Hi followed by a newline on UART terminal -> Option CQuick Check:
printfoutput redirected viafputcto UART [OK]
- Assuming no output without UART initialization code shown
- Thinking only first char is sent
- Confusing compile errors with runtime behavior
fputc override code that tries to redirect printf to UART:int fputc(int ch, FILE *f) {
UART_SendChar(ch);
return 0;
}Solution
Step 1: Check return value of
Thefputcfputcfunction must return the character written on success, or EOF on failure.Step 2: Analyze the code's return statement
Returning 0 (null char) instead of the character can causeprintfto misbehave or stop output prematurely.Final Answer:
Returning 0 instead of the character causes incorrectprintfbehavior -> Option AQuick Check:
Return written char fromfputcto keepprintfworking [OK]
fputc [OK]- Returning 0 instead of the character
- Ignoring return value importance
- Assuming void return type is allowed
printf output to UART and also support multiple UART ports. Which approach correctly extends fputc to select UART port dynamically?Solution
Step 1: Understand
Only onefputclimitationfputcfunction can be linked, so multiple versions are not possible.Step 2: Use global variable for UART selection
By storing the current UART port in a global variable,fputccan send characters to the selected UART dynamically.Step 3: Evaluate other options
Modifyingprintfis complex; interrupts do not switch ports automatically; multiplefputcfunctions cause conflicts.Final Answer:
Use a global variable to store current UART port and send chars accordingly insidefputc-> Option BQuick Check:
Global UART selector enables dynamic port choice infputc[OK]
fputc [OK]- Trying multiple
fputcfunctions - Changing
printfsignature - Relying on interrupts to switch UART ports
Ready to prove it?
Pass the test and this pattern moves unknown
