@@ -36,21 +36,68 @@ static int cmd_uart_write(const struct shell *sh, size_t argc, char **argv)
36
36
return 0 ;
37
37
}
38
38
39
+
40
+ static int cmd_uart_read (const struct shell * sh , size_t argc , char * * argv )
41
+ {
42
+ char * s_dev_name = argv [1 ];
43
+ const struct device * dev = shell_device_get_binding (s_dev_name );
44
+ int ret = 0 ;
45
+ char chr ;
46
+ k_timepoint_t end ;
47
+ uint64_t seconds ;
48
+
49
+ if (!dev || !device_is_uart (dev )) {
50
+ shell_error (sh , "UART: Device driver %s not found." , s_dev_name );
51
+ return - ENODEV ;
52
+ }
53
+
54
+ seconds = shell_strtoul (argv [2 ], 10 , & ret );
55
+ if (ret != 0 ) {
56
+ shell_help (sh );
57
+ return SHELL_CMD_HELP_PRINTED ;
58
+ }
59
+ if (seconds < 1 ) {
60
+ return - EINVAL ;
61
+ }
62
+ shell_info (sh , "UART: Read for %lli seconds from %s." , seconds , s_dev_name );
63
+
64
+ end = sys_timepoint_calc (K_SECONDS (seconds ));
65
+ while (!sys_timepoint_expired (end )) {
66
+ ret = uart_poll_in (dev , & chr );
67
+ if (ret == 0 ) {
68
+ shell_fprintf_normal (sh , "%c" , chr );
69
+ }
70
+ if (ret != 0 && ret != -1 ) {
71
+ shell_error (sh , "Failed to read from UART (%d)" , ret );
72
+ break ;
73
+ }
74
+ }
75
+
76
+ shell_fprintf_normal (sh , "\n" );
77
+
78
+ return ret ;
79
+ }
80
+
81
+
39
82
static int cmd_uart_baudrate (const struct shell * sh , size_t argc , char * * argv )
40
83
{
41
84
char * s_dev_name = argv [1 ];
42
85
const struct device * dev ;
43
86
struct uart_config cfg ;
44
87
uint32_t baudrate ;
45
- int ret ;
88
+ int ret = 0 ;
46
89
47
90
dev = shell_device_get_binding (s_dev_name );
48
91
if (!dev || !device_is_uart (dev )) {
49
92
shell_error (sh , "UART: Device driver %s not found." , s_dev_name );
50
93
return - ENODEV ;
51
94
}
52
95
53
- baudrate = strtol (argv [2 ], NULL , 10 );
96
+ baudrate = shell_strtoul (argv [2 ], 10 , & ret );
97
+ if (ret != 0 ) {
98
+ shell_help (sh );
99
+ return SHELL_CMD_HELP_PRINTED ;
100
+ }
54
101
ret = uart_config_get (dev , & cfg );
55
102
if (ret < 0 ) {
56
103
shell_error (sh , "UART: Failed to get current configuration: %d" , ret );
@@ -126,6 +173,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_uart_cmds,
126
173
"Write data to the UART device\n"
127
174
"Usage: write <device> <data>" ,
128
175
cmd_uart_write , 3 , 0 ),
176
+ SHELL_CMD_ARG (read , & dsub_device_name ,
177
+ "read data from the UART device\n"
178
+ "Usage: read <device> <duration in secs>" ,
179
+ cmd_uart_read , 3 , 0 ),
129
180
SHELL_CMD_ARG (baudrate , & dsub_device_name ,
130
181
"Configure UART device baudrate\n"
131
182
"Usage: baudrate <device> <baudrate>" ,
0 commit comments