0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Use printf with UART in Embedded C

To use printf with UART in embedded C, you need to redirect the standard output to the UART transmit function by implementing the _write or fputc function depending on your toolchain. This allows printf to send characters over UART, enabling easy serial debugging or communication.
📐

Syntax

To use printf with UART, you must override the low-level output function that printf uses internally. Commonly, this is done by implementing the _write function or fputc function depending on your compiler and standard library.

  • _write: Used by many ARM toolchains (e.g., GCC with newlib). It sends a buffer of characters to UART.
  • fputc: Used in some environments to send a single character to UART.

Inside these functions, you call your UART transmit function to send characters one by one.

c
int _write(int file, char *ptr, int len) {
    for (int i = 0; i < len; i++) {
        UART_Transmit(ptr[i]); // Your UART send function
    }
    return len;
}
💻

Example

This example shows how to redirect printf output to UART by implementing the _write function. It assumes you have a function UART_Transmit(char c) that sends one character over UART.

c
#include <stdio.h>

// Mock UART transmit function
void UART_Transmit(char c) {
    // Hardware-specific code to send 'c' over UART
    // For example, write to UART data register and wait for transmit complete
}

// Redirect printf output to UART
int _write(int file, char *ptr, int len) {
    for (int i = 0; i < len; i++) {
        UART_Transmit(ptr[i]);
    }
    return len;
}

int main() {
    // Initialize UART hardware here

    printf("Hello UART!\n");
    return 0;
}
Output
Hello UART!
⚠️

Common Pitfalls

  • Not implementing the low-level output function (_write or fputc) causes printf to not send data over UART.
  • Forgetting to initialize UART hardware before using printf leads to no output.
  • Using blocking UART transmit without timeout can hang the program if UART is not ready.
  • Not handling newline characters properly; some terminals expect \r\n instead of just \n.
c
/* Wrong: printf without _write implementation */
#include <stdio.h>

int main() {
    printf("No UART output\n"); // This prints nowhere
    return 0;
}

/* Right: Implement _write to send chars */
int _write(int file, char *ptr, int len) {
    for (int i = 0; i < len; i++) {
        UART_Transmit(ptr[i]);
    }
    return len;
}
📊

Quick Reference

Summary tips for using printf with UART:

  • Implement _write or fputc to redirect output.
  • Call your UART transmit function inside that implementation.
  • Initialize UART hardware before printing.
  • Handle newline characters for your terminal.
  • Test with simple strings first.

Key Takeaways

Implement the low-level output function (_write or fputc) to redirect printf to UART.
Always initialize UART hardware before using printf for output.
Use your UART transmit function inside the output redirection function.
Handle newline characters properly for your terminal display.
Test with simple printf statements to verify UART output.