raspberry_port.c 7.4 KB

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