console_uart.c

nRF52833-DK (boards.json id: nrf52833-dk) · File : console_uart.c · Language : C · Use case : Tracer des mesures sur la console série

The serial console is a nRF firmware’s first lifeline: this sketch prints counter, uptime and a sensor value at 1 Hz on the console UART. On the DK it is the connector’s « USB UART »; on the MDK, the native USB CDC.

Hardware

An nRF52833-DK board (Nordic, nRF52833 Cortex-M4F SoC, BLE 5) powered over USB: 4 LEDs on P0.13-P0.16 (active-low) and 4 buttons on P0.11, P0.12, P0.24, P0.25 (active-low, internal pull-up must be enabled). The board and a micro-USB/USB-C cable; a serial terminal (minicom, PuTTY, west uart_console) at 115200 baud.

Wiring

No wiring: the console rides the programming USB. The header UART pins stay free for a second port.

How it works

printk is routed to the device chosen by zephyr,console in the devicetree; k_uptime_get returns milliseconds since boot, handy for dating events.

Running it

Zephyr project: west build -b nrf52833dk/nrf52833 then west flash (on-board J-Link); 115200 serial console on the USB UART connector.

Variants

Switch to the logging subsystem (LOG_INF) for filterable levels, add a second UART via overlay for an external module, or format as CSV for logging.

console_uart.c

/* Console série Zephyr : trame périodique sur l'UART console.
 * DK : l'USB « USB UART » du connecteur micro-B (115200 8N1).
 * MDK : l'USB natif du nRF52840 (CDC) selon le BSP. */
#include <zephyr/kernel.h>

int main(void)
{
	int n = 0;

	printk("Console prête — mesure 1 Hz\n");
	while (1) {
		/* Exemple de trame : compteur + uptime + une fausse
		   valeur capteur dérivée du compteur. */
		int valeur = 100 + (n % 25);
		printk("[%05d] uptime=%lld ms valeur=%d\n",
		       n, k_uptime_get(), valeur);
		n++;
		k_msleep(1000);
	}
	return 0;
}

← Back to the board