main.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Example of loading the program into RAM
  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. static const char *TAG = "serial_ram_loader";
  19. // This can be set to a higher baud rate, but because it takes some time to
  20. // switch the uart baud rate in slave_monitor task, the log at slave starup
  21. // time will be lost or garbled.
  22. #define HIGHER_BAUDRATE 115200
  23. // Max line size
  24. #define BUF_LEN 128
  25. static char buf[BUF_LEN] = {0};
  26. void slave_monitor(void *arg)
  27. {
  28. #if (HIGHER_BAUDRATE != 115200)
  29. uart_flush_input(UART_NUM_1);
  30. uart_flush(UART_NUM_1);
  31. uart_set_baudrate(UART_NUM_1, 115200);
  32. #endif
  33. while (1) {
  34. int rxBytes = uart_read_bytes(UART_NUM_1, buf, BUF_LEN, 100 / portTICK_PERIOD_MS);
  35. buf[rxBytes] = '\0';
  36. printf("%s", buf);
  37. }
  38. }
  39. void app_main(void)
  40. {
  41. example_ram_app_binary_t bin;
  42. const loader_esp32_config_t config = {
  43. .baud_rate = 115200,
  44. .uart_port = UART_NUM_1,
  45. .uart_rx_pin = GPIO_NUM_5,
  46. .uart_tx_pin = GPIO_NUM_4,
  47. .reset_trigger_pin = GPIO_NUM_25,
  48. .gpio0_trigger_pin = GPIO_NUM_26,
  49. };
  50. if (loader_port_esp32_init(&config) != ESP_LOADER_SUCCESS) {
  51. ESP_LOGE(TAG, "serial initialization failed.");
  52. abort();
  53. }
  54. if (connect_to_target(HIGHER_BAUDRATE) == ESP_LOADER_SUCCESS) {
  55. get_example_ram_app_binary(esp_loader_get_target(), &bin);
  56. ESP_LOGI(TAG, "Loading app to RAM ...");
  57. esp_loader_error_t err = load_ram_binary(bin.ram_app.data);
  58. if (err == ESP_LOADER_SUCCESS) {
  59. // Forward slave's serial output
  60. ESP_LOGI(TAG, "********************************************");
  61. ESP_LOGI(TAG, "*** Logs below are print from slave .... ***");
  62. ESP_LOGI(TAG, "********************************************");
  63. xTaskCreate(slave_monitor, "slave_monitor", 2048, NULL, configMAX_PRIORITIES, NULL);
  64. } else {
  65. ESP_LOGE(TAG, "Loading to ram failed ...");
  66. }
  67. }
  68. vTaskDelete(NULL);
  69. }