i2ctools_example_main.c 3.0 KB

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