@@ -48,3 +48,112 @@ Serial Error Reporting
4848
4949It is possible to check if there are some frame, parity, overrun, break, or
5050other error using the ioctl TIOCGICOUNT just like on Linux.
51+
52+ Serial Debug Structure (TIOCSERGSTRUCT)
53+ ---------------------------------------
54+
55+ .. note ::
56+ This is a **debug-only ** ioctl. The internal structures it exposes are
57+ driver-specific, may change without notice, and must not be relied upon
58+ as a stable ABI.
59+
60+ The ``TIOCSERGSTRUCT `` ioctl allows a developer to retrieve a copy of the
61+ serial driver's internal state structure for diagnostic and debugging purposes.
62+ It is defined in ``include/nuttx/serial/tioctl.h ``::
63+
64+ #define TIOCSERGSTRUCT _TIOC(0x0032) /* Get device TTY structure */
65+
66+ Enabling ``TIOCSERGSTRUCT ``
67+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
68+
69+ Support is controlled by the Kconfig option ``CONFIG_SERIAL_TIOCSERGSTRUCT ``.
70+ To enable it:
71+
72+ 1. ``CONFIG_DEBUG_FEATURES `` must be enabled (the option depends on it).
73+ 2. Either ``CONFIG_MCU_SERIAL `` or ``CONFIG_16550_UART `` must be active (i.e.,
74+ the board must use an MCU serial driver or the generic 16550 UART driver).
75+ 3. The specific low-level serial driver for your hardware must implement the
76+ ``TIOCSERGSTRUCT `` case in its ``ioctl `` method. Most serial drivers in
77+ the tree already do (63+ drivers across ARM, ARM64, RISC-V, Xtensa, and
78+ MIPS architectures).
79+
80+ Via ``menuconfig ``, navigate to:
81+
82+ .. code-block :: text
83+
84+ Device Drivers --->
85+ Serial Driver Support --->
86+ [*] Support TIOCSERGSTRUCT
87+
88+ If the option is not visible, ensure that ``CONFIG_DEBUG_FEATURES `` is enabled
89+ first.
90+
91+ How It Works
92+ ~~~~~~~~~~~~
93+
94+ Because the exact layout depends on the serial driver selected for your board,
95+ there is no single portable structure definition. The caller must consult the
96+ driver source for the struct definition and size the buffer accordingly.
97+
98+ If ``arg `` is ``NULL ``, the ioctl returns ``-EINVAL ``.
99+
100+ Example Usage
101+ ~~~~~~~~~~~~~
102+
103+ The following example shows how an application might use ``TIOCSERGSTRUCT ``
104+ with the 16550 UART driver to inspect internal state. Adapt the structure type
105+ and header to match the serial driver used on your board.
106+
107+ .. code-block :: c
108+
109+ #include <stdio.h>
110+ #include <fcntl.h>
111+ #include <unistd.h>
112+ #include <sys/ioctl.h>
113+ #include <nuttx/serial/tioctl.h>
114+
115+ /* Include the driver-specific header for the struct definition.
116+ * This example uses the 16550 UART; replace with the header that
117+ * defines your board's serial driver state structure.
118+ */
119+
120+ #include <nuttx/serial/uart_16550.h>
121+
122+ int main(int argc, char *argv[])
123+ {
124+ struct u16550_s devstate;
125+ int fd;
126+ int ret;
127+
128+ fd = open("/dev/ttyS0", O_RDONLY);
129+ if (fd < 0)
130+ {
131+ perror("open");
132+ return 1;
133+ }
134+
135+ ret = ioctl(fd, TIOCSERGSTRUCT, (unsigned long)&devstate);
136+ if (ret < 0)
137+ {
138+ perror("ioctl TIOCSERGSTRUCT");
139+ close(fd);
140+ return 1;
141+ }
142+
143+ /* Inspect driver-internal fields for debugging. Field names
144+ * are specific to the driver; consult the driver source for
145+ * the struct definition.
146+ */
147+
148+ printf("UART base address: 0x%08lx\n",
149+ (unsigned long)devstate.uartbase);
150+
151+ close(fd);
152+ return 0;
153+ }
154+
155+ .. warning ::
156+ The structure layout and field names are internal to each driver
157+ implementation and **may change between NuttX releases **. Use this ioctl
158+ for interactive debugging and diagnostics only — never in production
159+ application logic.
0 commit comments