main.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Example of loading the program into RAM through SPI
  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/spi_master.h"
  13. #include "driver/uart.h"
  14. #include "driver/gpio.h"
  15. #include "esp32_spi_port.h"
  16. #include "esp_loader.h"
  17. #include "example_common.h"
  18. #include "freertos/FreeRTOS.h"
  19. static const char *TAG = "spi_ram_loader";
  20. // Max line size
  21. #define BUF_LEN 128
  22. static uint8_t buf[BUF_LEN] = {0};
  23. void slave_monitor(void *arg)
  24. {
  25. // Initialize UART
  26. uart_config_t uart_config = {
  27. .baud_rate = 115200,
  28. .data_bits = UART_DATA_8_BITS,
  29. .parity = UART_PARITY_DISABLE,
  30. .stop_bits = UART_STOP_BITS_1,
  31. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  32. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  33. .source_clk = UART_SCLK_DEFAULT,
  34. #endif
  35. };
  36. ESP_ERROR_CHECK(uart_param_config(UART_NUM_2, &uart_config));
  37. ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, GPIO_NUM_6, GPIO_NUM_7,
  38. UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  39. ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, BUF_LEN * 4, BUF_LEN * 4, 0, NULL, 0));
  40. while (1) {
  41. int rxBytes = uart_read_bytes(UART_NUM_2, buf, BUF_LEN, 100 / portTICK_PERIOD_MS);
  42. buf[rxBytes] = '\0';
  43. printf("%s", buf);
  44. }
  45. }
  46. void app_main(void)
  47. {
  48. example_ram_app_binary_t bin;
  49. const loader_esp32_spi_config_t config = {
  50. .spi_bus = SPI2_HOST,
  51. .frequency = 20 * 1000000,
  52. .reset_trigger_pin = GPIO_NUM_5,
  53. .spi_clk_pin = GPIO_NUM_12,
  54. .spi_cs_pin = GPIO_NUM_10,
  55. .spi_miso_pin = GPIO_NUM_13,
  56. .spi_mosi_pin = GPIO_NUM_11,
  57. .spi_quadwp_pin = GPIO_NUM_14,
  58. .spi_quadhd_pin = GPIO_NUM_9,
  59. .strap_bit0_pin = GPIO_NUM_13,
  60. .strap_bit1_pin = GPIO_NUM_2,
  61. .strap_bit2_pin = GPIO_NUM_3,
  62. .strap_bit3_pin = GPIO_NUM_4,
  63. };
  64. if (loader_port_esp32_spi_init(&config) != ESP_LOADER_SUCCESS) {
  65. ESP_LOGE(TAG, "SPI initialization failed.");
  66. abort();
  67. }
  68. if (connect_to_target(0) == ESP_LOADER_SUCCESS) {
  69. get_example_ram_app_binary(esp_loader_get_target(), &bin);
  70. ESP_LOGI(TAG, "Loading app to RAM ...");
  71. esp_loader_error_t err = load_ram_binary(bin.ram_app.data);
  72. if (err == ESP_LOADER_SUCCESS) {
  73. // Forward slave's serial output
  74. ESP_LOGI(TAG, "********************************************");
  75. ESP_LOGI(TAG, "*** Logs below are print from slave .... ***");
  76. ESP_LOGI(TAG, "********************************************");
  77. xTaskCreate(slave_monitor, "slave_monitor", 2048, NULL, configMAX_PRIORITIES, NULL);
  78. } else {
  79. ESP_LOGE(TAG, "Loading to RAM failed ...");
  80. }
  81. }
  82. vTaskDelete(NULL);
  83. }