main.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Flash multiple partitions 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 <sys/param.h>
  8. #include <string.h>
  9. #include "esp_err.h"
  10. #include "esp_log.h"
  11. #include "esp_task_wdt.h"
  12. #include "driver/uart.h"
  13. #include "driver/gpio.h"
  14. #include "esp32_port.h"
  15. #include "esp_loader.h"
  16. #include "example_common.h"
  17. #include "freertos/FreeRTOS.h"
  18. // This can be set to a higher baud rate, but because it takes some time to
  19. // switch the uart baud rate in SlaveMonitor task, the log at slave starup
  20. // time will be lost or garbled.
  21. #define HIGHER_BAUDRATE 115200
  22. // Max line size
  23. #define BUF_LEN 128
  24. static char buf[BUF_LEN] = {0};
  25. void SlaveMonitor(){
  26. #if (HIGHER_BAUDRATE != 115200)
  27. uart_flush_input(UART_NUM_1);
  28. uart_flush(UART_NUM_1);
  29. uart_set_baudrate(UART_NUM_1, 115200);
  30. #endif
  31. while (1) {
  32. int rxBytes = uart_read_bytes(UART_NUM_1, buf, BUF_LEN, 100 / portTICK_PERIOD_MS);
  33. buf[rxBytes] = '\0';
  34. printf("%s", buf);
  35. }
  36. }
  37. void app_main(void)
  38. {
  39. example_ram_app_binary_t bin;
  40. const loader_esp32_config_t config = {
  41. .baud_rate = 115200,
  42. .uart_port = UART_NUM_1,
  43. .uart_rx_pin = GPIO_NUM_5,
  44. .uart_tx_pin = GPIO_NUM_4,
  45. .reset_trigger_pin = GPIO_NUM_25,
  46. .gpio0_trigger_pin = GPIO_NUM_26,
  47. };
  48. if (loader_port_esp32_init(&config) != ESP_LOADER_SUCCESS) {
  49. ESP_LOGE("example", "serial initialization failed.");
  50. abort();
  51. }
  52. if (connect_to_target(HIGHER_BAUDRATE) == ESP_LOADER_SUCCESS) {
  53. get_example_ram_app_binary(esp_loader_get_target(), &bin);
  54. printf("\e[1;32mLoading app to RAM ...\n\e[0m");
  55. esp_loader_error_t err = load_ram_binary(bin.ram_app.data);
  56. if (err == ESP_LOADER_SUCCESS) {
  57. // Forward slave's serial output
  58. printf("\e[1;33m********************************************\n\e[0m");
  59. printf("\e[1;33m*** Logs below are print from slave .... ***\n\e[0m");
  60. printf("\e[1;33m********************************************\n\e[0m");
  61. xTaskCreate(SlaveMonitor, "SlaveMonitor", 2048, NULL, configMAX_PRIORITIES, NULL);
  62. } else {
  63. printf("\e[1;31mLoading to ram failed ...\e[0m\n");
  64. }
  65. }
  66. vTaskDelete(NULL);
  67. }