raspberry_port.c 7.5 KB

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