Try Embedded

Nucleo-H743ZI2: non-blocking serial with the USART's FIFO

Bare-metal C on the STM32H743 (Cortex-M7 at up to 480 MHz): queue text in a ring buffer and let the USART's FIFO threshold interrupt send it, so printing…

Communication · Advanced

In a real product: Diagnostic log port on a motor drive (Industrial): Log lines go out in the background, so a long message never delays the control loop.

The Nucleo-H743ZI2 carries an STM32H743ZI: an Arm Cortex-M7 that runs at up to 480 MHz, with 2 MB of flash and 1 MB of RAM. Its USARTs have an eight-byte FIFO each way. Here text is queued in a ring buffer in RAM and USART3's TX FIFO threshold interrupt moves it into the FIFO whenever half of it is free, so log_write() returns at once however long the message is. LD1 blinks at the same time to show the main loop never stalls.

What you'll learn

  • FIFO mode (FIFOEN, set while the USART is off) and the TX FIFO threshold (TXFTCFG, TXFTIE)
  • A ring buffer between code and an interrupt: one side only writes head, the other only tail
  • Turning an interrupt on when there is work and off when there is none

How it works

After reset the H743 runs from its 64 MHz HSI, and USART3 counts the same clock. In FIFO mode, TXE_TXFNF in ISR means "the TX FIFO is not full", and TXFT means "at least the threshold is free". With TXFTCFG = 010 the threshold is half the FIFO, four bytes.

log_write() copies text into ring[] at head and enables TXFTIE. The interrupt then fires (half the FIFO is free), and USART3_IRQHandler moves bytes from tail into TDR until the FIFO is full or the ring is empty. When the ring is empty it turns TXFTIE off, or the interrupt would fire for ever with nothing to send. While the FIFO's eight bytes go out on the wire, about 0.7 ms at 115200 baud, the core is free.

head is only written by log_write() and tail only by the interrupt, so neither can undo the other's work. A message that does not fit in the ring is cut short rather than overwriting text not yet sent.

Try this

  • Make the report longer than the ring (128 bytes): the end is cut, and dropped counts what was lost.
  • Set TXFTCFG to 000 (an eighth): the interrupt fires more often, with less to send each time.
  • Replace log_write() with a loop that waits on TXE_TXFNF for each byte, and watch LD1's blink stretch.

main.c

// Bare-metal C on the Nucleo-H743ZI2: no Arduino core and no HAL. CMSIS names every register.
// log_write() queues text in a ring buffer; USART3's TX FIFO threshold interrupt sends it in the background.
#include <stdio.h>
#include <string.h>
#include "stm32h7xx.h"

#define RING 128
static char ring[RING];
static volatile unsigned head, tail;   // head: written by log_write; tail: by the interrupt
static volatile unsigned dropped;
static volatile uint32_t ticks;        // milliseconds since SysTick started

void SysTick_Handler(void) {
  ticks++;
}

// At least half the TX FIFO is free: fill it from the ring.
void USART3_IRQHandler(void) {
  while (tail != head && (USART3->ISR & USART_ISR_TXE_TXFNF)) {
    USART3->TDR = ring[tail];
    tail = (tail + 1) % RING;
  }
  if (tail == head) USART3->CR3 &= ~USART_CR3_TXFTIE;   // nothing left: stop asking
}

// Queues text and returns at once; what does not fit is dropped.
static void log_write(const char *text) {
  for (size_t i = 0; i < strlen(text); i++) {
    unsigned next = (head + 1) % RING;
    if (next == tail) { dropped++; continue; }
    ring[head] = text[i];
    head = next;
  }
  USART3->CR3 |= USART_CR3_TXFTIE;
}

int main(void) {
  RCC->AHB4ENR |= RCC_AHB4ENR_GPIOBEN | RCC_AHB4ENR_GPIODEN;
  RCC->APB1LENR |= RCC_APB1LENR_USART3EN;
  GPIOB->MODER = (GPIOB->MODER & ~GPIO_MODER_MODE0) | GPIO_MODER_MODE0_0;   // PB0 (LD1): output
  GPIOD->MODER = (GPIOD->MODER & ~GPIO_MODER_MODE8) | GPIO_MODER_MODE8_1;   // PD8: USART3 TX
  GPIOD->AFR[1] = (GPIOD->AFR[1] & ~0xFu) | 7u;                            // AF7

  USART3->BRR = SystemCoreClock / 115200;                                  // 64 MHz HSI after reset
  USART3->CR3 = 2u << USART_CR3_TXFTCFG_Pos;                               // threshold: half the FIFO free
  USART3->CR1 = USART_CR1_FIFOEN | USART_CR1_TE | USART_CR1_UE;            // FIFOEN while UE was 0
  NVIC_EnableIRQ(USART3_IRQn);

  SysTick->LOAD = SystemCoreClock / 1000 - 1;   // 1 ms
  SysTick->VAL = 0;
  SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;

  log_write("Logging from the TX FIFO interrupt\r\n");
  uint32_t next_report = 0, next_blink = 0;
  while (1) {
    if (ticks >= next_blink) {             // LD1 keeps its rhythm: log_write never waits
      next_blink += 250;
      GPIOB->ODR ^= GPIO_ODR_OD0;
    }
    if (ticks >= next_report) {
      next_report += 500;
      char line[64];
      snprintf(line, sizeof line, "uptime %lu ms, dropped %u bytes\r\n", (unsigned long)ticks, dropped);
      log_write(line);
    }
  }
}

Lessons behind it