raspberry_port.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 "esp_loader_io.h"
  16. #include "protocol.h"
  17. #include <pigpio.h>
  18. #include "raspberry_port.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. #include <termios.h>
  25. #include <time.h>
  26. #include <stdlib.h>
  27. #include <stdint.h>
  28. #include <stdarg.h>
  29. #include <sys/ioctl.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <sys/param.h>
  33. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  34. static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write)
  35. {
  36. static bool write_prev = false;
  37. if (write_prev != write) {
  38. write_prev = write;
  39. printf("\n--- %s ---\n", write ? "WRITE" : "READ");
  40. }
  41. for (uint32_t i = 0; i < size; i++) {
  42. printf("%02x ", data[i]);
  43. }
  44. }
  45. #endif
  46. static int serial;
  47. static int64_t s_time_end;
  48. static int32_t s_reset_trigger_pin;
  49. static int32_t s_gpio0_trigger_pin;
  50. static speed_t convert_baudrate(int baud)
  51. {
  52. switch (baud) {
  53. case 50: return B50;
  54. case 75: return B75;
  55. case 110: return B110;
  56. case 134: return B134;
  57. case 150: return B150;
  58. case 200: return B200;
  59. case 300: return B300;
  60. case 600: return B600;
  61. case 1200: return B1200;
  62. case 1800: return B1800;
  63. case 2400: return B2400;
  64. case 4800: return B4800;
  65. case 9600: return B9600;
  66. case 19200: return B19200;
  67. case 38400: return B38400;
  68. case 57600: return B57600;
  69. case 115200: return B115200;
  70. case 230400: return B230400;
  71. case 460800: return B460800;
  72. case 500000: return B500000;
  73. case 576000: return B576000;
  74. case 921600: return B921600;
  75. case 1000000: return B1000000;
  76. case 1152000: return B1152000;
  77. case 1500000: return B1500000;
  78. case 2000000: return B2000000;
  79. case 2500000: return B2500000;
  80. case 3000000: return B3000000;
  81. case 3500000: return B3500000;
  82. case 4000000: return B4000000;
  83. default: return -1;
  84. }
  85. }
  86. static int serialOpen (const char *device, uint32_t baudrate)
  87. {
  88. struct termios options;
  89. int status, fd;
  90. if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1) {
  91. printf("Error occured while opening serial port !\n");
  92. return -1 ;
  93. }
  94. fcntl (fd, F_SETFL, O_RDWR) ;
  95. // Get and modify current options:
  96. tcgetattr (fd, &options);
  97. speed_t baud = convert_baudrate(baudrate);
  98. if(baud < 0) {
  99. printf("Invalid baudrate!\n");
  100. return -1;
  101. }
  102. cfmakeraw (&options) ;
  103. cfsetispeed (&options, baud) ;
  104. cfsetospeed (&options, baud) ;
  105. options.c_cflag |= (CLOCAL | CREAD) ;
  106. options.c_cflag &= ~(PARENB | CSTOPB | CSIZE) ;
  107. options.c_cflag |= CS8 ;
  108. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
  109. options.c_oflag &= ~OPOST ;
  110. options.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
  111. options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // Disable any special handling of received bytes
  112. options.c_cc [VMIN] = 0 ;
  113. options.c_cc [VTIME] = 10 ; // 1 Second
  114. tcsetattr (fd, TCSANOW, &options) ;
  115. ioctl (fd, TIOCMGET, &status);
  116. status |= TIOCM_DTR ;
  117. status |= TIOCM_RTS ;
  118. ioctl (fd, TIOCMSET, &status);
  119. usleep (10000) ; // 10mS
  120. return fd ;
  121. }
  122. static esp_loader_error_t change_baudrate(int file_desc, int baudrate)
  123. {
  124. struct termios options;
  125. speed_t baud = convert_baudrate(baudrate);
  126. if(baud < 0) {
  127. return ESP_LOADER_ERROR_INVALID_PARAM;
  128. }
  129. tcgetattr (file_desc, &options);
  130. cfmakeraw (&options) ;
  131. cfsetispeed (&options, baud);
  132. cfsetospeed (&options, baud);
  133. tcsetattr (file_desc, TCSANOW, &options);
  134. return ESP_LOADER_SUCCESS;
  135. }
  136. static void set_timeout(uint32_t timeout)
  137. {
  138. struct termios options;
  139. timeout /= 100;
  140. timeout = MAX(timeout, 1);
  141. tcgetattr(serial, &options);
  142. options.c_cc[VTIME] = timeout;
  143. tcsetattr(serial, TCSANOW, &options);
  144. }
  145. static esp_loader_error_t read_char(char *c, uint32_t timeout)
  146. {
  147. set_timeout(timeout);
  148. int read_bytes = read(serial, c, 1);
  149. if (read_bytes == 1) {
  150. return ESP_LOADER_SUCCESS;
  151. } else if (read_bytes == 0) {
  152. return ESP_LOADER_ERROR_TIMEOUT;
  153. } else {
  154. return ESP_LOADER_ERROR_FAIL;
  155. }
  156. }
  157. static esp_loader_error_t read_data(char *buffer, uint32_t size)
  158. {
  159. for (int i = 0; i < size; i++) {
  160. uint32_t remaining_time = loader_port_remaining_time();
  161. RETURN_ON_ERROR( read_char(&buffer[i], remaining_time) );
  162. }
  163. return ESP_LOADER_SUCCESS;
  164. }
  165. esp_loader_error_t loader_port_raspberry_init(const loader_raspberry_config_t *config)
  166. {
  167. s_reset_trigger_pin = config->reset_trigger_pin;
  168. s_gpio0_trigger_pin = config->gpio0_trigger_pin;
  169. serial = serialOpen(config->device, config->baudrate);
  170. if (serial < 0) {
  171. printf("Serial port could not be opened!\n");
  172. return ESP_LOADER_ERROR_FAIL;
  173. }
  174. if (gpioInitialise() < 0) {
  175. printf("pigpio initialisation failed\n");
  176. return ESP_LOADER_ERROR_FAIL;
  177. }
  178. gpioSetMode(config->reset_trigger_pin, PI_OUTPUT);
  179. gpioSetMode(config->gpio0_trigger_pin, PI_OUTPUT);
  180. return ESP_LOADER_SUCCESS;
  181. }
  182. esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout)
  183. {
  184. int written = write(serial, data, size);
  185. if (written < 0) {
  186. return ESP_LOADER_ERROR_FAIL;
  187. } else if (written < size) {
  188. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  189. transfer_debug_print(data, written, true);
  190. #endif
  191. return ESP_LOADER_ERROR_TIMEOUT;
  192. } else {
  193. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  194. transfer_debug_print(data, written, true);
  195. #endif
  196. return ESP_LOADER_SUCCESS;
  197. }
  198. }
  199. esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
  200. {
  201. RETURN_ON_ERROR( read_data(data, size) );
  202. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  203. transfer_debug_print(data, size, false);
  204. #endif
  205. return ESP_LOADER_SUCCESS;
  206. }
  207. // Set GPIO0 LOW, then assert reset pin for 50 milliseconds.
  208. void loader_port_enter_bootloader(void)
  209. {
  210. gpioWrite(s_gpio0_trigger_pin, 0);
  211. loader_port_reset_target();
  212. loader_port_delay_ms(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
  213. gpioWrite(s_gpio0_trigger_pin, 1);
  214. }
  215. void loader_port_reset_target(void)
  216. {
  217. gpioWrite(s_reset_trigger_pin, 0);
  218. loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
  219. gpioWrite(s_reset_trigger_pin, 1);
  220. }
  221. void loader_port_delay_ms(uint32_t ms)
  222. {
  223. usleep(ms * 1000);
  224. }
  225. void loader_port_start_timer(uint32_t ms)
  226. {
  227. s_time_end = clock() + (ms * (CLOCKS_PER_SEC / 1000));
  228. }
  229. uint32_t loader_port_remaining_time(void)
  230. {
  231. int64_t remaining = (s_time_end - clock()) / 1000;
  232. return (remaining > 0) ? (uint32_t)remaining : 0;
  233. }
  234. void loader_port_debug_print(const char *str)
  235. {
  236. printf("DEBUG: %s\n", str);
  237. }
  238. esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
  239. {
  240. return change_baudrate(serial, baudrate);
  241. }