1 /* i2c-tools example
2 
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4 
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9 
10 #include <stdio.h>
11 #include <string.h>
12 #include "sdkconfig.h"
13 #include "esp_log.h"
14 #include "esp_console.h"
15 #include "esp_vfs_fat.h"
16 #include "cmd_system.h"
17 #include "cmd_i2ctools.h"
18 
19 static const char *TAG = "i2c-tools";
20 
21 #if CONFIG_EXAMPLE_STORE_HISTORY
22 
23 #define MOUNT_PATH "/data"
24 #define HISTORY_PATH MOUNT_PATH "/history.txt"
25 
initialize_filesystem(void)26 static void initialize_filesystem(void)
27 {
28     static wl_handle_t wl_handle;
29     const esp_vfs_fat_mount_config_t mount_config = {
30         .max_files = 4,
31         .format_if_mount_failed = true
32     };
33     esp_err_t err = esp_vfs_fat_spiflash_mount(MOUNT_PATH, "storage", &mount_config, &wl_handle);
34     if (err != ESP_OK) {
35         ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
36         return;
37     }
38 }
39 #endif // CONFIG_EXAMPLE_STORE_HISTORY
40 
app_main(void)41 void app_main(void)
42 {
43     esp_console_repl_t *repl = NULL;
44     esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
45 
46 #if CONFIG_EXAMPLE_STORE_HISTORY
47     initialize_filesystem();
48     repl_config.history_save_path = HISTORY_PATH;
49 #endif
50     repl_config.prompt = "i2c-tools>";
51 
52     // install console REPL environment
53 #if CONFIG_ESP_CONSOLE_UART
54     esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
55     ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
56 #elif CONFIG_ESP_CONSOLE_USB_CDC
57     esp_console_dev_usb_cdc_config_t cdc_config = ESP_CONSOLE_DEV_CDC_CONFIG_DEFAULT();
58     ESP_ERROR_CHECK(esp_console_new_repl_usb_cdc(&cdc_config, &repl_config, &repl));
59 #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
60     esp_console_dev_usb_serial_jtag_config_t usbjtag_config = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT();
61     ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&usbjtag_config, &repl_config, &repl));
62 #endif
63 
64     register_i2ctools();
65     register_system();
66 
67     printf("\n ==============================================================\n");
68     printf(" |             Steps to Use i2c-tools                         |\n");
69     printf(" |                                                            |\n");
70     printf(" |  1. Try 'help', check all supported commands               |\n");
71     printf(" |  2. Try 'i2cconfig' to configure your I2C bus              |\n");
72     printf(" |  3. Try 'i2cdetect' to scan devices on the bus             |\n");
73     printf(" |  4. Try 'i2cget' to get the content of specific register   |\n");
74     printf(" |  5. Try 'i2cset' to set the value of specific register     |\n");
75     printf(" |  6. Try 'i2cdump' to dump all the register (Experiment)    |\n");
76     printf(" |                                                            |\n");
77     printf(" ==============================================================\n\n");
78 
79     // start console REPL
80     ESP_ERROR_CHECK(esp_console_start_repl(repl));
81 }
82