]> granicus.if.org Git - esp-idf/commitdiff
i2c_tool: add i2c_tool example test
authormorris <maoshengrong@espressif.com>
Thu, 18 Oct 2018 03:25:22 +0000 (11:25 +0800)
committermorris <maoshengrong@espressif.com>
Fri, 30 Nov 2018 02:08:28 +0000 (10:08 +0800)
1. add example test for i2c-tools
2. make command line arguments number configurable

.gitlab-ci.yml
components/driver/i2c.c
examples/peripherals/i2c/i2c_tools/README.md
examples/peripherals/i2c/i2c_tools/example_test.py [new file with mode: 0644]
examples/peripherals/i2c/i2c_tools/main/Kconfig.projbuild
examples/peripherals/i2c/i2c_tools/main/cmd_i2ctools.c
examples/peripherals/i2c/i2c_tools/main/i2ctools_example_main.c

index d6d66fd50d9dee385728a4e3cc98a0ce664f85a5..2e40d3b54ffa7ff874fe289feb78ce1dad250c38 100644 (file)
@@ -1009,6 +1009,12 @@ example_test_006_01:
     - ESP32
     - Example_ShieldBox
 
+example_test_007_01:
+  <<: *example_test_template
+  tags:
+    - ESP32
+    - Example_I2C_CCS811_SENSOR
+
 UT_001_01:
   <<: *unit_test_template
   tags:
index 1fec11589613701253253b2711ac0684363171d0..c69fbe697c3d79644a6a7f5cd655028171927ff1 100644 (file)
@@ -1087,7 +1087,7 @@ esp_err_t i2c_master_read(i2c_cmd_handle_t cmd_handle, uint8_t* data, size_t dat
         return i2c_master_read_static(cmd_handle, data, data_len, ack);
     } else {
         if(data_len == 1) {
-            return i2c_master_read_byte(cmd_handle, data, I2C_MASTER_NACK);    
+            return i2c_master_read_byte(cmd_handle, data, I2C_MASTER_NACK);
         } else {
             esp_err_t ret;
             if((ret =  i2c_master_read_static(cmd_handle, data, data_len - 1, I2C_MASTER_ACK)) != ESP_OK) {
@@ -1095,7 +1095,7 @@ esp_err_t i2c_master_read(i2c_cmd_handle_t cmd_handle, uint8_t* data, size_t dat
             }
             return i2c_master_read_byte(cmd_handle, data + data_len - 1, I2C_MASTER_NACK);
         }
-    }   
+    }
 }
 
 static void IRAM_ATTR i2c_master_cmd_begin_static(i2c_port_t i2c_num)
@@ -1306,7 +1306,7 @@ esp_err_t i2c_master_cmd_begin(i2c_port_t i2c_num, i2c_cmd_handle_t cmd_handle,
                     clear_bus_cnt++;
                     if(clear_bus_cnt >= I2C_ACKERR_CNT_MAX) {
                         i2c_master_clear_bus(i2c_num);
-                        clear_bus_cnt = 0;   
+                        clear_bus_cnt = 0;
                     }
                     ret = ESP_FAIL;
                 } else {
@@ -1400,4 +1400,4 @@ int i2c_slave_read_buffer(i2c_port_t i2c_num, uint8_t* data, size_t max_size, Ti
     }
     xSemaphoreGive(p_i2c->slv_rx_mux);
     return cnt;
-}
\ No newline at end of file
+}
index 0f8a7b5efad6f9b724065cb64de23beb33092bc3..ca150d838b1ffb57dd9007dd3129702ee5121a68 100644 (file)
@@ -24,17 +24,10 @@ To run this example, you should have one ESP32 dev board (e.g. ESP32-WROVER Kit)
 
 **Note:** The following pin assignments are used by default, you can change them with `i2cconfig` command at any time.
 
-|                  | SDA    | SCL    |
-| ---------------- | ------ | ------ |
-| ESP32 I2C Master | GPIO18 | GPIO19 |
-| Sensor           | SDA    | SCL    |
-
-- master:
-  - GPIO18 is assigned as the data signal of I2C master port
-  - GPIO19 is assigned as the clock signal of I2C master port
-
-- connection:
-  - connect SDA/SCL of CCS811 sensor with GPIO18/GPIO19
+|                  | SDA    | SCL    | GND  | Other | VCC  |
+| ---------------- | ------ | ------ | ---- | ----- | ---- |
+| ESP32 I2C Master | GPIO18 | GPIO19 | GND  | GND   | 3.3V |
+| Sensor           | SDA    | SCL    | GND  | WAK   | VCC  |
 
 **Note: ** There’s no need to add an external pull-up resistors for SDA/SCL pin, because the driver will enable the internal pull-up resistors itself.
 
@@ -43,6 +36,8 @@ To run this example, you should have one ESP32 dev board (e.g. ESP32-WROVER Kit)
 Enter `make menuconfig` if you are using GNU Make based build system or enter `idf.py menuconfig` if you are using CMake based build system. Then go into `Example Configuration` menu.
 
 - You can choose whether or not to save command history into flash in `Store command history in flash` option.
+- You can set the maximum number of command line arguments under `Maximum number of command line arguments` option.
+- You can set the command line buffer length under `Command line buffer length` option.
 
 ### Build and Flash
 
@@ -199,9 +194,11 @@ esp32> i2cget -c 0x5b -r 0x02 -l 8
 * I don’t find any available address when running `i2cdetect` command.
   * Make sure your wiring connection is right.
   * Some sensor will have a “wake up” pin, via which user can put the sensor into a sleep mode. So make sure your sensor in **not** in the sleep state.
-  * Have a try resetting you I2C device, and then re-run `i2cdetect`.
+  * Reset you I2C device, and then run `i2cdetect` again.
 * I can’t get the right content when running `i2cdump` command.
-  * Currently the `i2cdump` only support those who have the same content length of registers inside the I2C device. For example, if a device have three legal addresses, and the content length at these address are 1 byte, 2 bytes and 4 bytes. Then you should not expect this command to dump the register correctly.
+  * Currently the `i2cdump` only support those who have the same content length of registers inside the I2C device. For example, if a device have three register addresses, and the content length at these address are 1 byte, 2 bytes and 4 bytes. In this case you should not expect this command to dump the register correctly.
+* I really input argument correctly, but the command line “discard” the last few arguements from time to time.
+  * Enlarge the maximum number of arguments in the menuconfig.
 
 
 
diff --git a/examples/peripherals/i2c/i2c_tools/example_test.py b/examples/peripherals/i2c/i2c_tools/example_test.py
new file mode 100644 (file)
index 0000000..2e8a109
--- /dev/null
@@ -0,0 +1,42 @@
+from __future__ import print_function
+import os
+import sys
+test_fw_path = os.getenv("TEST_FW_PATH")
+if test_fw_path and test_fw_path not in sys.path:
+    sys.path.insert(0, test_fw_path)
+import TinyFW
+import IDF
+
+EXPECT_TIMEOUT = 20
+
+
+@IDF.idf_example_test(env_tag='Example_I2C_CCS811_SENSOR')
+def test_i2ctools_example(env, extra_data):
+    # Get device under test, flash and start example. "i2ctool" must be defined in EnvConfig
+    dut = env.get_dut('i2ctools', 'examples/peripherals/i2c/i2c_tools')
+    dut.start_app()
+    dut.expect("esp32>", timeout=EXPECT_TIMEOUT)
+    # Get i2c address
+    dut.write("i2cdetect")
+    dut.expect("5b", timeout=EXPECT_TIMEOUT)
+    # Get chip ID
+    dut.write("i2cget -c 0x5b -r 0x20 -l 1")
+    dut.expect("0x81", timeout=EXPECT_TIMEOUT)
+    # Reset sensor
+    dut.write("i2cset -c 0x5b -r 0xFF 0x11 0xE5 0x72 0x8A")
+    dut.expect("OK", timeout=EXPECT_TIMEOUT)
+    # Get status
+    dut.write("i2cget -c 0x5b -r 0x00 -l 1")
+    dut.expect_any("0x10", timeout=EXPECT_TIMEOUT)
+    # Change work mode
+    dut.write("i2cset -c 0x5b -r 0xF4")
+    dut.expect("OK", timeout=EXPECT_TIMEOUT)
+    dut.write("i2cset -c 0x5b -r 0x01 0x10")
+    dut.expect("OK", timeout=EXPECT_TIMEOUT)
+    # Get new status
+    dut.write("i2cget -c 0x5b -r 0x00 -l 1")
+    dut.expect_any("0x98", "0x90", timeout=EXPECT_TIMEOUT)
+
+
+if __name__ == '__main__':
+    test_i2ctools_example()
index 1b1b9632dc8859fe58b31099c6f4d855a69e947a..3ff2662a7849efcf3641f24a90c754a3755bad13 100644 (file)
@@ -8,4 +8,18 @@ config STORE_HISTORY
         command history. If this option is enabled, initalizes a FAT filesystem
         and uses it to store command history.
 
+config MAX_CMD_ARGUMENTS
+    int "Maximum number of command line arguments"
+    default 16
+    range 8 256
+    help
+        maximum number of command line arguments to parse
+
+config MAX_CMD_LENGTH
+    int "Command line buffer length"
+    default 256
+    range 256 512
+    help
+        length of command line buffer, in bytes
+
 endmenu
index eae93a9d1d619e558ed69a82bbf1e4d1b60d8494..b9c87369c8fd4c6cd57b1e9111723658f094732b 100644 (file)
@@ -255,7 +255,7 @@ static int do_i2cset_cmd(int argc, char **argv)
     /* Check chip address: "-c" option */
     int chip_addr = i2cset_args.chip_address->ival[0];
     /* Check register address: "-r" option */
-    int data_addr = -1;
+    int data_addr = 0;
     if (i2cset_args.register_address->count) {
         data_addr = i2cset_args.register_address->ival[0];
     }
@@ -267,7 +267,7 @@ static int do_i2cset_cmd(int argc, char **argv)
     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
     i2c_master_start(cmd);
     i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
-    if (data_addr != -1) {
+    if (i2cset_args.register_address->count) {
         i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
     }
     for (int i = 0; i < len; i++) {
index 302a1d387973fd3599bacd02e24e7a512641f03c..eacf80c38b51be8cc0925a900c642499e5ea88b1 100644 (file)
@@ -86,8 +86,8 @@ static void initialize_console()
 
     /* Initialize the console */
     esp_console_config_t console_config = {
-        .max_cmdline_args = 8,
-        .max_cmdline_length = 256,
+        .max_cmdline_args = CONFIG_MAX_CMD_ARGUMENTS,
+        .max_cmdline_length = CONFIG_MAX_CMD_LENGTH,
 #if CONFIG_LOG_COLORS
         .hint_color = atoi(LOG_COLOR_CYAN)
 #endif