From 2b165f99c4ad21a32c1d2013c03a4d6afc61431b Mon Sep 17 00:00:00 2001 From: morris Date: Thu, 18 Oct 2018 11:25:22 +0800 Subject: [PATCH] i2c_tool: add i2c_tool example test 1. add example test for i2c-tools 2. make command line arguments number configurable --- .gitlab-ci.yml | 6 +++ components/driver/i2c.c | 8 ++-- examples/peripherals/i2c/i2c_tools/README.md | 23 +++++----- .../peripherals/i2c/i2c_tools/example_test.py | 42 +++++++++++++++++++ .../i2c/i2c_tools/main/Kconfig.projbuild | 14 +++++++ .../i2c/i2c_tools/main/cmd_i2ctools.c | 4 +- .../i2c_tools/main/i2ctools_example_main.c | 4 +- 7 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 examples/peripherals/i2c/i2c_tools/example_test.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d6d66fd50..2e40d3b54 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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: diff --git a/components/driver/i2c.c b/components/driver/i2c.c index 1fec11589..c69fbe697 100644 --- a/components/driver/i2c.c +++ b/components/driver/i2c.c @@ -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 +} diff --git a/examples/peripherals/i2c/i2c_tools/README.md b/examples/peripherals/i2c/i2c_tools/README.md index 0f8a7b5ef..ca150d838 100644 --- a/examples/peripherals/i2c/i2c_tools/README.md +++ b/examples/peripherals/i2c/i2c_tools/README.md @@ -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 index 000000000..2e8a109f7 --- /dev/null +++ b/examples/peripherals/i2c/i2c_tools/example_test.py @@ -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() diff --git a/examples/peripherals/i2c/i2c_tools/main/Kconfig.projbuild b/examples/peripherals/i2c/i2c_tools/main/Kconfig.projbuild index 1b1b9632d..3ff2662a7 100644 --- a/examples/peripherals/i2c/i2c_tools/main/Kconfig.projbuild +++ b/examples/peripherals/i2c/i2c_tools/main/Kconfig.projbuild @@ -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 diff --git a/examples/peripherals/i2c/i2c_tools/main/cmd_i2ctools.c b/examples/peripherals/i2c/i2c_tools/main/cmd_i2ctools.c index eae93a9d1..b9c87369c 100644 --- a/examples/peripherals/i2c/i2c_tools/main/cmd_i2ctools.c +++ b/examples/peripherals/i2c/i2c_tools/main/cmd_i2ctools.c @@ -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++) { diff --git a/examples/peripherals/i2c/i2c_tools/main/i2ctools_example_main.c b/examples/peripherals/i2c/i2c_tools/main/i2ctools_example_main.c index 302a1d387..eacf80c38 100644 --- a/examples/peripherals/i2c/i2c_tools/main/i2ctools_example_main.c +++ b/examples/peripherals/i2c/i2c_tools/main/i2ctools_example_main.c @@ -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