i2ctools_example_main.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
  38. #if CONFIG_EXAMPLE_STORE_HISTORY
  39. initialize_filesystem();
  40. repl_config.history_save_path = HISTORY_PATH;
  41. #endif
  42. repl_config.prompt = "i2c-tools>";
  43. // init console REPL environment
  44. ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
  45. register_i2ctools();
  46. register_system();
  47. printf("\n ==============================================================\n");
  48. printf(" | Steps to Use i2c-tools |\n");
  49. printf(" | |\n");
  50. printf(" | 1. Try 'help', check all supported commands |\n");
  51. printf(" | 2. Try 'i2cconfig' to configure your I2C bus |\n");
  52. printf(" | 3. Try 'i2cdetect' to scan devices on the bus |\n");
  53. printf(" | 4. Try 'i2cget' to get the content of specific register |\n");
  54. printf(" | 5. Try 'i2cset' to set the value of specific register |\n");
  55. printf(" | 6. Try 'i2cdump' to dump all the register (Experiment) |\n");
  56. printf(" | |\n");
  57. printf(" ==============================================================\n\n");
  58. // start console REPL
  59. ESP_ERROR_CHECK(esp_console_start_repl(repl));
  60. }