@@ -19,6 +19,9 @@ LOG_MODULE_REGISTER(i2c_shell, CONFIG_LOG_DEFAULT_LEVEL);
19
19
extern struct device __device_start [];
20
20
extern struct device __device_end [];
21
21
22
+ /* Maximum bytes we can write or read at once */
23
+ #define MAX_I2C_BYTES 16
24
+
22
25
static int cmd_i2c_scan (const struct shell * shell ,
23
26
size_t argc , char * * argv )
24
27
{
@@ -88,6 +91,42 @@ static int cmd_i2c_recover(const struct shell *shell,
88
91
return 0 ;
89
92
}
90
93
94
+ /* i2c write <device> <dev_addr> [<byte1>, ...] */
95
+ static int cmd_i2c_write (const struct shell * shell , size_t argc , char * * argv )
96
+ {
97
+ u8_t buf [MAX_I2C_BYTES ];
98
+ struct device * dev ;
99
+ int num_bytes ;
100
+ int reg_addr ;
101
+ int dev_addr ;
102
+ int i ;
103
+
104
+ dev = device_get_binding (argv [1 ]);
105
+ if (!dev ) {
106
+ shell_error (shell , "I2C: Device driver %s not found." , argv [1 ]);
107
+ return - ENODEV ;
108
+ }
109
+
110
+ dev_addr = strtol (argv [2 ], NULL , 16 );
111
+ reg_addr = strtol (argv [3 ], NULL , 16 );
112
+ num_bytes = argc - 4 ;
113
+ if (num_bytes < 0 )
114
+ return 0 ;
115
+ if (num_bytes > MAX_I2C_BYTES ) {
116
+ num_bytes = MAX_I2C_BYTES ;
117
+ }
118
+ for (i = 0 ; i < num_bytes ; i ++ ) {
119
+ buf [i ] = (uint8_t )strtol (argv [4 + i ], NULL , 16 );
120
+ }
121
+
122
+ if (i2c_burst_write (dev , dev_addr , reg_addr , buf , num_bytes ) < 0 ) {
123
+ shell_error (shell , "Failed to write to device: %s" , argv [1 ]);
124
+ return - EIO ;
125
+ }
126
+
127
+ return 0 ;
128
+ }
129
+
91
130
static int cmd_i2c_write_byte (const struct shell * shell ,
92
131
size_t argc , char * * argv )
93
132
{
@@ -143,6 +182,41 @@ static int cmd_i2c_read_byte(const struct shell *shell,
143
182
return 0 ;
144
183
}
145
184
185
+ /* i2c read <device> <dev_addr> [<numbytes>] */
186
+ static int cmd_i2c_read (const struct shell * shell , size_t argc , char * * argv )
187
+ {
188
+ u8_t buf [MAX_I2C_BYTES ];
189
+ struct device * dev ;
190
+ int num_bytes ;
191
+ int reg_addr ;
192
+ int dev_addr ;
193
+
194
+ dev = device_get_binding (argv [1 ]);
195
+ if (!dev ) {
196
+ shell_error (shell , "I2C: Device driver %s not found." , argv [1 ]);
197
+ return - ENODEV ;
198
+ }
199
+
200
+ dev_addr = strtol (argv [2 ], NULL , 16 );
201
+ reg_addr = strtol (argv [3 ], NULL , 16 );
202
+ if (argc > 4 ) {
203
+ num_bytes = strtol (argv [4 ], NULL , 16 );
204
+ if (num_bytes > MAX_I2C_BYTES )
205
+ num_bytes = MAX_I2C_BYTES ;
206
+ } else {
207
+ num_bytes = MAX_I2C_BYTES ;
208
+ }
209
+
210
+ if (i2c_burst_read (dev , dev_addr , reg_addr , buf , num_bytes ) < 0 ) {
211
+ shell_error (shell , "Failed to read from device: %s" , argv [1 ]);
212
+ return - EIO ;
213
+ }
214
+
215
+ shell_hexdump (shell , buf , num_bytes );
216
+
217
+ return 0 ;
218
+ }
219
+
146
220
static void device_name_get (size_t idx , struct shell_static_entry * entry );
147
221
148
222
SHELL_DYNAMIC_CMD_CREATE (dsub_device_name , device_name_get );
@@ -175,9 +249,15 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_i2c_cmds,
175
249
"Scan I2C devices" , cmd_i2c_scan ),
176
250
SHELL_CMD (recover , & dsub_device_name ,
177
251
"Recover I2C bus" , cmd_i2c_recover ),
252
+ SHELL_CMD_ARG (read , & dsub_device_name ,
253
+ "Read bytes from an I2C device" ,
254
+ cmd_i2c_read , 3 , MAX_I2C_BYTES ),
178
255
SHELL_CMD_ARG (read_byte , & dsub_device_name ,
179
256
"Read a byte from an I2C device" ,
180
257
cmd_i2c_read_byte , 3 , 1 ),
258
+ SHELL_CMD_ARG (write , & dsub_device_name ,
259
+ "Write bytes to an I2C device" ,
260
+ cmd_i2c_write , 3 , MAX_I2C_BYTES ),
181
261
SHELL_CMD_ARG (write_byte , & dsub_device_name ,
182
262
"Write a byte to an I2C device" ,
183
263
cmd_i2c_write_byte , 4 , 1 ),
0 commit comments