Skip to main content

Subsection 3.4.3 The Retarget Layer

The function __io_putchar near the top of uart.c is the subtlest part of the driver. In the ARM C library, __io_putchar is declared as a weak symbol: the linker uses the library’s built-in (no-op) version unless you supply your own stronger definition. By defining it in uart.c, we override the weak version, and every character that printf (or putchar) emits is automatically routed to uart2_write, no changes to application code required.
Notice that __io_putchar passes the character straight through without any modification. This means a bare '\n' arrives at the terminal as ASCII 10 (line feed) only, and most terminal emulators, including CoolTerm, interpret a lone line feed as "move cursor down" but not "return to column 0." The result is the classic staircase effect, where successive lines march diagonally down and to the right. To avoid it, always end your format strings with "\r\n" rather than just "\n":
printf("Voltage: %d mV\r\n", mV);   // correct: CR+LF
printf("Voltage: %d mV\n",  mV);   // wrong: LF only → staircase