@@ -19,6 +19,9 @@ LOG_MODULE_REGISTER(i2c_shell, CONFIG_LOG_DEFAULT_LEVEL);
1919extern struct device __device_start [];
2020extern struct device __device_end [];
2121
22+ /* Maximum bytes we can write or read at once */
23+ #define MAX_I2C_BYTES 16
24+
2225static int cmd_i2c_scan (const struct shell * shell ,
2326 size_t argc , char * * argv )
2427{
@@ -88,6 +91,42 @@ static int cmd_i2c_recover(const struct shell *shell,
8891 return 0 ;
8992}
9093
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+
91130static int cmd_i2c_write_byte (const struct shell * shell ,
92131 size_t argc , char * * argv )
93132{
@@ -143,6 +182,41 @@ static int cmd_i2c_read_byte(const struct shell *shell,
143182 return 0 ;
144183}
145184
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+
146220static void device_name_get (size_t idx , struct shell_static_entry * entry );
147221
148222SHELL_DYNAMIC_CMD_CREATE (dsub_device_name , device_name_get );
@@ -175,9 +249,15 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_i2c_cmds,
175249 "Scan I2C devices" , cmd_i2c_scan ),
176250 SHELL_CMD (recover , & dsub_device_name ,
177251 "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 ),
178255 SHELL_CMD_ARG (read_byte , & dsub_device_name ,
179256 "Read a byte from an I2C device" ,
180257 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 ),
181261 SHELL_CMD_ARG (write_byte , & dsub_device_name ,
182262 "Write a byte to an I2C device" ,
183263 cmd_i2c_write_byte , 4 , 1 ),
0 commit comments