esp32_spi_port.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "esp32_spi_port.h"
  16. #include "esp_log.h"
  17. #include "driver/gpio.h"
  18. #include "esp_timer.h"
  19. #include "esp_log.h"
  20. #include "esp_idf_version.h"
  21. #include <unistd.h>
  22. // #define SERIAL_DEBUG_ENABLE
  23. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 0)
  24. #define DMA_CHAN SPI_DMA_CH_AUTO
  25. #else
  26. #define DMA_CHAN 1
  27. #endif
  28. #define WORD_ALIGNED(ptr) ((size_t)ptr % sizeof(size_t) == 0)
  29. #ifdef SERIAL_DEBUG_ENABLE
  30. static void dec_to_hex_str(const uint8_t dec, uint8_t hex_str[3])
  31. {
  32. static const uint8_t dec_to_hex[] = {
  33. '0', '1', '2', '3', '4', '5', '6', '7',
  34. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  35. };
  36. hex_str[0] = dec_to_hex[dec >> 4];
  37. hex_str[1] = dec_to_hex[dec & 0xF];
  38. hex_str[2] = '\0';
  39. }
  40. static void serial_debug_print(const uint8_t *data, uint16_t size, bool write)
  41. {
  42. static bool write_prev = false;
  43. uint8_t hex_str[3];
  44. if(write_prev != write) {
  45. write_prev = write;
  46. printf("\n--- %s ---\n", write ? "WRITE" : "READ");
  47. }
  48. for(uint32_t i = 0; i < size; i++) {
  49. dec_to_hex_str(data[i], hex_str);
  50. printf("%s ", hex_str);
  51. }
  52. }
  53. #else
  54. static void serial_debug_print(const uint8_t *data, uint16_t size, bool write) { }
  55. #endif
  56. static spi_host_device_t s_spi_bus;
  57. static spi_bus_config_t s_spi_config;
  58. static spi_device_handle_t s_device_h;
  59. static spi_device_interface_config_t s_device_config;
  60. static int64_t s_time_end;
  61. static uint32_t s_reset_trigger_pin;
  62. static uint32_t s_strap_bit0_pin;
  63. static uint32_t s_strap_bit1_pin;
  64. static uint32_t s_strap_bit2_pin;
  65. static uint32_t s_strap_bit3_pin;
  66. static uint32_t s_spi_cs_pin;
  67. esp_loader_error_t loader_port_esp32_spi_init(const loader_esp32_spi_config_t *config)
  68. {
  69. /* Initialize the global static variables*/
  70. s_spi_bus = config->spi_bus;
  71. s_reset_trigger_pin = config->reset_trigger_pin;
  72. s_strap_bit0_pin = config->strap_bit0_pin;
  73. s_strap_bit1_pin = config->strap_bit1_pin;
  74. s_strap_bit2_pin = config->strap_bit2_pin;
  75. s_strap_bit3_pin = config->strap_bit3_pin;
  76. s_spi_cs_pin = config->spi_cs_pin;
  77. /* Configure and initialize the SPI bus*/
  78. s_spi_config.mosi_io_num = config->spi_mosi_pin;
  79. s_spi_config.miso_io_num = config->spi_miso_pin;
  80. s_spi_config.sclk_io_num = config->spi_clk_pin;
  81. s_spi_config.quadwp_io_num = config->spi_quadwp_pin;
  82. s_spi_config.quadhd_io_num = config->spi_quadhd_pin;
  83. s_spi_config.max_transfer_sz = 4096 * 4;
  84. if (spi_bus_initialize(s_spi_bus, &s_spi_config, DMA_CHAN) != ESP_OK) {
  85. return ESP_LOADER_ERROR_FAIL;
  86. }
  87. /* Configure and add the device */
  88. s_device_config.clock_speed_hz = config->frequency;
  89. s_device_config.spics_io_num = -1; /* We're using the chip select pin as GPIO as we need to
  90. chain multiple transactions with CS pulled down */
  91. s_device_config.flags = SPI_DEVICE_HALFDUPLEX;
  92. s_device_config.queue_size = 16;
  93. if (spi_bus_add_device(s_spi_bus, &s_device_config, &s_device_h) != ESP_OK) {
  94. return ESP_LOADER_ERROR_FAIL;
  95. }
  96. /* Initialize the pins except for the strapping ones */
  97. gpio_reset_pin(s_reset_trigger_pin);
  98. gpio_set_pull_mode(s_reset_trigger_pin, GPIO_PULLUP_ONLY);
  99. gpio_set_direction(s_reset_trigger_pin, GPIO_MODE_OUTPUT);
  100. gpio_set_level(s_reset_trigger_pin, 1);
  101. gpio_reset_pin(s_spi_cs_pin);
  102. gpio_set_pull_mode(s_spi_cs_pin, GPIO_PULLUP_ONLY);
  103. gpio_set_direction(s_spi_cs_pin, GPIO_MODE_OUTPUT);
  104. gpio_set_level(s_spi_cs_pin, 1);
  105. return ESP_LOADER_SUCCESS;
  106. }
  107. void loader_port_esp32_spi_deinit(void)
  108. {
  109. gpio_reset_pin(s_reset_trigger_pin);
  110. gpio_reset_pin(s_spi_cs_pin);
  111. spi_bus_remove_device(s_device_h);
  112. spi_bus_free(s_spi_bus);
  113. }
  114. void loader_port_spi_set_cs(const uint32_t level) {
  115. gpio_set_level(s_spi_cs_pin, level);
  116. }
  117. esp_loader_error_t loader_port_write(const uint8_t *data, const uint16_t size, const uint32_t timeout)
  118. {
  119. /* Due to the fact that the SPI driver uses DMA for larger transfers,
  120. and the DMA requirements, the buffer must be word aligned */
  121. if (data == NULL || !WORD_ALIGNED(data)) {
  122. return ESP_LOADER_ERROR_INVALID_PARAM;
  123. }
  124. serial_debug_print(data, size, true);
  125. spi_transaction_t transaction = {
  126. .tx_buffer = data,
  127. .rx_buffer = NULL,
  128. .length = size * 8U,
  129. .rxlength = 0,
  130. };
  131. esp_err_t err = spi_device_transmit(s_device_h, &transaction);
  132. if (err == ESP_OK) {
  133. serial_debug_print(data, size, false);
  134. return ESP_LOADER_SUCCESS;
  135. } else if (err == ESP_ERR_TIMEOUT) {
  136. return ESP_LOADER_ERROR_TIMEOUT;
  137. } else {
  138. return ESP_LOADER_ERROR_FAIL;
  139. }
  140. }
  141. esp_loader_error_t loader_port_read(uint8_t *data, const uint16_t size, const uint32_t timeout)
  142. {
  143. /* Due to the fact that the SPI driver uses DMA for larger transfers,
  144. and the DMA requirements, the buffer must be word aligned */
  145. if (data == NULL || !WORD_ALIGNED(data)) {
  146. return ESP_LOADER_ERROR_INVALID_PARAM;
  147. }
  148. serial_debug_print(data, size, true);
  149. spi_transaction_t transaction = {
  150. .tx_buffer = NULL,
  151. .rx_buffer = data,
  152. .rxlength = size * 8,
  153. };
  154. esp_err_t err = spi_device_transmit(s_device_h, &transaction);
  155. if (err == ESP_OK) {
  156. serial_debug_print(data, size, false);
  157. return ESP_LOADER_SUCCESS;
  158. } else if (err == ESP_ERR_TIMEOUT) {
  159. return ESP_LOADER_ERROR_TIMEOUT;
  160. } else {
  161. return ESP_LOADER_ERROR_FAIL;
  162. }
  163. }
  164. void loader_port_enter_bootloader(void)
  165. {
  166. /*
  167. We have to initialize the GPIO pins for the target strapping pins here,
  168. as they may overlap with target SPI pins.
  169. For instance in the case of ESP32C3 MISO and strapping bit 0 pins overlap.
  170. */
  171. spi_bus_remove_device(s_device_h);
  172. spi_bus_free(s_spi_bus);
  173. gpio_reset_pin(s_strap_bit0_pin);
  174. gpio_set_pull_mode(s_strap_bit0_pin, GPIO_PULLUP_ONLY);
  175. gpio_set_direction(s_strap_bit0_pin, GPIO_MODE_OUTPUT);
  176. gpio_reset_pin(s_strap_bit1_pin);
  177. gpio_set_pull_mode(s_strap_bit1_pin, GPIO_PULLUP_ONLY);
  178. gpio_set_direction(s_strap_bit1_pin, GPIO_MODE_OUTPUT);
  179. gpio_reset_pin(s_strap_bit2_pin);
  180. gpio_set_pull_mode(s_strap_bit2_pin, GPIO_PULLUP_ONLY);
  181. gpio_set_direction(s_strap_bit2_pin, GPIO_MODE_OUTPUT);
  182. gpio_reset_pin(s_strap_bit3_pin);
  183. gpio_set_pull_mode(s_strap_bit3_pin, GPIO_PULLUP_ONLY);
  184. gpio_set_direction(s_strap_bit3_pin, GPIO_MODE_OUTPUT);
  185. /* Set the strapping pins and perform the reset sequence */
  186. gpio_set_level(s_strap_bit0_pin, 1);
  187. gpio_set_level(s_strap_bit1_pin, 0);
  188. gpio_set_level(s_strap_bit2_pin, 0);
  189. gpio_set_level(s_strap_bit3_pin, 0);
  190. loader_port_reset_target();
  191. loader_port_delay_ms(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
  192. gpio_set_level(s_strap_bit3_pin, 1);
  193. gpio_set_level(s_strap_bit0_pin, 0);
  194. /* Disable the strapping pins so they can be used by the slave later */
  195. gpio_reset_pin(s_strap_bit0_pin);
  196. gpio_reset_pin(s_strap_bit1_pin);
  197. gpio_reset_pin(s_strap_bit2_pin);
  198. gpio_reset_pin(s_strap_bit3_pin);
  199. /* Restore the SPI bus pins */
  200. spi_bus_initialize(s_spi_bus, &s_spi_config, DMA_CHAN);
  201. spi_bus_add_device(s_spi_bus, &s_device_config, &s_device_h);
  202. }
  203. void loader_port_reset_target(void)
  204. {
  205. gpio_set_level(s_reset_trigger_pin, 0);
  206. loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
  207. gpio_set_level(s_reset_trigger_pin, 1);
  208. }
  209. void loader_port_delay_ms(const uint32_t ms)
  210. {
  211. usleep(ms * 1000);
  212. }
  213. void loader_port_start_timer(const uint32_t ms)
  214. {
  215. s_time_end = esp_timer_get_time() + ms * 1000;
  216. }
  217. uint32_t loader_port_remaining_time(void)
  218. {
  219. int64_t remaining = (s_time_end - esp_timer_get_time()) / 1000;
  220. return (remaining > 0) ? (uint32_t)remaining : 0;
  221. }
  222. void loader_port_debug_print(const char *str)
  223. {
  224. printf("DEBUG: %s\n", str);
  225. }
  226. esp_loader_error_t loader_port_change_transmission_rate(const uint32_t frequency)
  227. {
  228. if (spi_bus_remove_device(s_device_h) != ESP_OK) {
  229. return ESP_LOADER_ERROR_FAIL;
  230. }
  231. uint32_t old_frequency = s_device_config.clock_speed_hz;
  232. s_device_config.clock_speed_hz = frequency;
  233. if (spi_bus_add_device(s_spi_bus, &s_device_config, &s_device_h) != ESP_OK) {
  234. s_device_config.clock_speed_hz = old_frequency;
  235. return ESP_LOADER_ERROR_FAIL;
  236. }
  237. return ESP_LOADER_SUCCESS;
  238. }