main.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * ESP Flasher Library Example for Zephyr
  3. * Written in 2022 by KT-Elektronik, Klaucke und Partner GmbH
  4. * This example code is in the Public Domain (or CC0 licensed, at your option.)
  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. * Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
  10. */
  11. #include <zephyr/kernel.h>
  12. #include <zephyr/device.h>
  13. #include <zephyr/devicetree.h>
  14. #include <zephyr/drivers/gpio.h>
  15. #include <zephyr/sys/util.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <zephyr_port.h>
  19. #include <esp_loader.h>
  20. #include "example_common.h"
  21. #define HIGHER_BAUDRATE 230400
  22. #define DEFAULT_BAUDRATE 115200
  23. /* Get UART DTS entry used as flash interface */
  24. static const struct device *esp_uart_dev = DEVICE_DT_GET(DT_ALIAS(uart));
  25. /* Get GPIO pin connected to the ESP's enable pin. */
  26. static const struct gpio_dt_spec esp_enable_spec = GPIO_DT_SPEC_GET(DT_ALIAS(en), gpios);
  27. /* Get GPIO pin connected to the ESP's boot pin. */
  28. static const struct gpio_dt_spec esp_boot_spec = GPIO_DT_SPEC_GET(DT_ALIAS(boot), gpios);
  29. int main(void)
  30. {
  31. example_binaries_t bin;
  32. const loader_zephyr_config_t config = {
  33. .uart_dev = esp_uart_dev,
  34. .enable_spec = esp_enable_spec,
  35. .boot_spec = esp_boot_spec
  36. };
  37. printk("Running ESP Flasher from Zephyr\r\n");
  38. if (!device_is_ready(esp_uart_dev)) {
  39. printk("ESP UART not ready");
  40. return;
  41. }
  42. if (!device_is_ready(esp_boot_spec.port)) {
  43. printk("ESP boot GPIO not ready");
  44. return;
  45. }
  46. if (!device_is_ready(esp_enable_spec.port)) {
  47. printk("Bluetooth Enable GPIO not ready");
  48. return;
  49. }
  50. gpio_pin_configure_dt(&esp_boot_spec, GPIO_OUTPUT_ACTIVE);
  51. gpio_pin_configure_dt(&esp_enable_spec, GPIO_OUTPUT_INACTIVE);
  52. if (loader_port_zephyr_init(&config) != ESP_LOADER_SUCCESS) {
  53. printk("ESP loader init failed");
  54. return;
  55. }
  56. if (connect_to_target(HIGHER_BAUDRATE) == ESP_LOADER_SUCCESS) {
  57. get_example_binaries(esp_loader_get_target(), &bin);
  58. flash_binary(bin.boot.data, bin.boot.size, bin.boot.addr);
  59. flash_binary(bin.part.data, bin.part.size, bin.part.addr);
  60. flash_binary(bin.app.data, bin.app.size, bin.app.addr);
  61. }
  62. esp_loader_reset_target();
  63. return 0;
  64. }