Check Your Understanding3.4.4Check Your Understanding
1.
uart2_write spins waiting for the TXE flag before writing to TDR. What does TXE indicate, and what would happen if you removed that check and wrote to TDR immediately on every call?
TXE (Transmit Data Register Empty) means the hardware shift register has consumed the previous byte and TDR can accept a new one. Skipping the check and writing TDR while the previous byte is still being shifted out would overwrite it, silently dropping that character.
Correct. TXE is the ready handshake between your code and the hardware shift register. Ignoring it causes data loss at high data rates or back-to-back writes.
TXE means a transmit error occurred; skipping the check causes the USART to assert a fault interrupt.
TXE stands for Transmit Data Register Empty. It is a readiness flag, not an error flag. Transmit errors use different flags (e.g. framing error, noise error).
Removing the check would make transmission faster with no side effects because the USART hardware buffers bytes automatically.
USART2 has only a one-byte TDR, with no FIFO. Consecutive writes without checking TXE overwrite the previous byte before it leaves the chip.
The retarget layer sends each character to uart2_write without modification. If you call printf("Alpha\nBeta\nGamma\n"), what will the output look like in CoolTerm, and what change to the format string would fix it?
CoolTerm treats '\n' (line feed, ASCII 10) as "move cursor down" but not "return to column 0." The three words appear in a staircase, each starting where the one above ended. Changing the format string to use "\r\n" instead of "\n" sends a carriage return first, which moves the cursor to column 0 before the line feed advances to the next line.
Correct. '\r' (ASCII 13) is the carriage return that moves the cursor to column 0. Because the retarget layer does not insert it automatically, your format strings must include it explicitly.
CoolTerm automatically converts bare '\n' to CR+LF, so the output looks correct with no change needed.
CoolTerm’s default mode does not perform LF→CRLF translation. Some terminal applications offer this as an option, but relying on it makes your code non-portable and hides the underlying issue.
The terminal displays a literal backslash-n between the words instead of a newline.
'\n' in a C string literal is the control character ASCII 10, not the two printable characters \ and n. The terminal does receive a line feed and does advance the cursor, just not back to column 0.
The driver enables USART2 with RCC->APBENR1. When you adapt the driver for USART1 in the lab exercise, you will need a different register. How would you find out which APB bus a given peripheral is connected to?
Look at the STM32C031 reference manual’s clock tree diagram or the RCC chapter’s register descriptions. Each APBENR register lists the peripherals on that bus; searching the register map for "USART1EN" reveals which register contains the enable bit.
Correct. The reference manual is the authoritative source. Note that USART1’s enable bit is in RCC->APBENR2, not APBENR1.
All USART peripherals are on the same bus, so you can always use the same RCC register.
USART1’s enable bit is in APBENR2 while USART2’s is in APBENR1. Using APBENR1 to try to enable USART1 sets a different bit (or none at all), and the USART1 clock remains disabled.
The peripheral number tells you the bus: odd-numbered USARTs are on APB1, even-numbered on APB2.
This is a convenient rule of thumb for some MCU families but it does not hold for the STM32C031. Always verify with the datasheet or reference manual.