---
title: "UART Hello World"
description: "The canonical \"printf via UART\" walkthrough: open a port with alpuartopen(), send a greeting plus a monotonically-increasing counter every second, handle the error paths. The producer-only variant most vendor SDKs ship as their first tutorial — uart-echo is the bidirectional companion."
source: https://docs.alplab.ai/sdk/examples/uart-hello-world
---

# `uart-hello-world`

The canonical "printf via UART" walkthrough: open a port with `alp_uart_open()`, send a greeting plus a monotonically-increasing counter every second, handle the error paths. The producer-only variant most vendor SDKs ship as their first tutorial — [`uart-echo`](https://docs.alplab.ai/sdk/examples/uart-echo) is the bidirectional companion.

Source: [`examples/peripheral-io/uart-hello-world/`](https://github.com/alplabai/alp-sdk/tree/main/examples/peripheral-io/uart-hello-world).

## Why not just `printf`?

Zephyr's `printf` lands on the *console* UART — a single, Kconfig-pinned device. Real apps often need to drive a *different* one: a Bluetooth modem on a secondary port, a GPS module, a stepper driver expecting TMC2209 datagrams, a debug pin that isn't the console.

`alp_uart_*()` opens any UART by portable instance ID (`ALP_E1M_UART0`, `ALP_E1M_UART1`) without touching `CONFIG_CONSOLE_*`. It also takes a byte buffer plus an explicit length — which is what you want for binary protocols. `printf` is text-only.

## What it does

1. `alp_init()`, then `alp_uart_open()` on `ALP_E1M_UART0`.
2. Builds its config from `ALP_UART_CONFIG_DEFAULT(ALP_E1M_UART0)` — the macro fills identity from the ID and every other field with its canonical default, which is exactly 115200 8N1. Nothing to override for this app; override a field *after* the macro when a chip demands different framing:

   ```c
   alp_uart_config_t cfg = ALP_UART_CONFIG_DEFAULT(ALP_E1M_UART0);
   cfg.baudrate = 9600;   /* then pass &cfg to alp_uart_open */
   ```
3. `alp_uart_write()` ships a `static const` greeting with an explicit `\r\n` — serial terminals expect CR+LF, unlike stdout.
4. Loops five times: `snprintf` into a bounded stack buffer, clamp the length (snprintf returns what *would* have been written), write it, wait a second.
5. `alp_uart_close()` releases the SDK handle. It does **not** power down the controller or drop the line — on real hardware the UART keeps idling high. Use Zephyr's `pm_device_action_run` after close if you truly need it off.

The example deliberately keeps the raw `ALP_E1M_UART0` instance ID in view; `uart-echo` opens the same port by its board-macro alias. Both styles are first-class.

## `board.yaml`

```yaml
som:
  sku: E1M-AEN801

preset: e1m-evk

pins:
  - { e1m: E1M_UART0, macro: EVK_UART_PORT_DEBUG, doc: "Console UART exposed on the debug header" }

cores:
  m55_hp:
    app: ./src
    peripherals:
      - uart

diagnostics:
  log_level: info
```

The `uart` entry becomes `CONFIG_SERIAL=y` in `alp.conf`, which also activates the SDK's `alp_uart_*` wrapper layer.

## Expected output

On the SDK log console:

```
[uart-hello] open ALP_E1M_UART0 @ 115200 8N1
[uart-hello] greeting written
[uart-hello] tick 0 written
[uart-hello] tick 1 written
...
[uart-hello] done
```

On a terminal attached to `ALP_E1M_UART0` itself:

```
Alp SDK uart-hello-world
tick 0
tick 1
tick 2
tick 3
tick 4
```

If the port can't open the example prints `open failed: alp_last_error=<n>` and exits cleanly. Most common causes: no `alp-uart0` devicetree alias on this board, or the driver rejected the baudrate/framing combination with `-ENOTSUP`.

## Status

Runs on `native_sim/native/64`; the harness latches on `[uart-hello] done`.

## See also

- [`<alp/peripheral.h>` reference — UART](https://docs.alplab.ai/sdk/api/peripheral#uart)
- [`uart-echo`](https://docs.alplab.ai/sdk/examples/uart-echo) — bidirectional companion
- [`uart-rx-ringbuf`](https://docs.alplab.ai/sdk/examples/uart-rx-ringbuf) — interrupt-driven RX into a ring buffer
- [`hello-world`](https://docs.alplab.ai/sdk/examples/hello-world) — the same heartbeat over the console
- [Examples overview](https://docs.alplab.ai/sdk/examples/overview)
