vfs_uart.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright 2015-2017 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. #include <string.h>
  15. #include <stdbool.h>
  16. #include <stdarg.h>
  17. #include <sys/errno.h>
  18. #include <sys/lock.h>
  19. #include <sys/fcntl.h>
  20. #include "esp_vfs.h"
  21. #include "esp_vfs_dev.h"
  22. #include "esp_attr.h"
  23. #include "soc/uart_struct.h"
  24. #include "driver/uart.h"
  25. #include "sdkconfig.h"
  26. // TODO: make the number of UARTs chip dependent
  27. #define UART_NUM 3
  28. // Token signifying that no character is available
  29. #define NONE -1
  30. // UART write bytes function type
  31. typedef void (*tx_func_t)(int, int);
  32. // UART read bytes function type
  33. typedef int (*rx_func_t)(int);
  34. // Basic functions for sending and receiving bytes over UART
  35. static void uart_tx_char(int fd, int c);
  36. static int uart_rx_char(int fd);
  37. // Functions for sending and receiving bytes which use UART driver
  38. static void uart_tx_char_via_driver(int fd, int c);
  39. static int uart_rx_char_via_driver(int fd);
  40. // Pointers to UART peripherals
  41. static uart_dev_t* s_uarts[UART_NUM] = {&UART0, &UART1, &UART2};
  42. // per-UART locks, lazily initialized
  43. static _lock_t s_uart_read_locks[UART_NUM];
  44. static _lock_t s_uart_write_locks[UART_NUM];
  45. // One-character buffer used for newline conversion code, per UART
  46. static int s_peek_char[UART_NUM] = { NONE, NONE, NONE };
  47. // Per-UART non-blocking flag. Note: default implementation does not honor this
  48. // flag, all reads are non-blocking. This option becomes effective if UART
  49. // driver is used.
  50. static bool s_non_blocking[UART_NUM];
  51. // Newline conversion mode when transmitting
  52. static esp_line_endings_t s_tx_mode =
  53. #if CONFIG_NEWLIB_STDOUT_LINE_ENDING_CRLF
  54. ESP_LINE_ENDINGS_CRLF;
  55. #elif CONFIG_NEWLIB_STDOUT_LINE_ENDING_CR
  56. ESP_LINE_ENDINGS_CR;
  57. #else
  58. ESP_LINE_ENDINGS_LF;
  59. #endif
  60. // Newline conversion mode when receiving
  61. static esp_line_endings_t s_rx_mode =
  62. #if CONFIG_NEWLIB_STDIN_LINE_ENDING_CRLF
  63. ESP_LINE_ENDINGS_CRLF;
  64. #elif CONFIG_NEWLIB_STDIN_LINE_ENDING_CR
  65. ESP_LINE_ENDINGS_CR;
  66. #else
  67. ESP_LINE_ENDINGS_LF;
  68. #endif
  69. // Functions used to write bytes to UART. Default to "basic" functions.
  70. static tx_func_t s_uart_tx_func[UART_NUM] = {
  71. &uart_tx_char, &uart_tx_char, &uart_tx_char
  72. };
  73. // Functions used to read bytes from UART. Default to "basic" functions.
  74. static rx_func_t s_uart_rx_func[UART_NUM] = {
  75. &uart_rx_char, &uart_rx_char, &uart_rx_char
  76. };
  77. static int uart_open(const char * path, int flags, int mode)
  78. {
  79. // this is fairly primitive, we should check if file is opened read only,
  80. // and error out if write is requested
  81. if (strcmp(path, "/0") == 0) {
  82. return 0;
  83. } else if (strcmp(path, "/1") == 0) {
  84. return 1;
  85. } else if (strcmp(path, "/2") == 0) {
  86. return 2;
  87. }
  88. errno = ENOENT;
  89. return -1;
  90. }
  91. static void uart_tx_char(int fd, int c)
  92. {
  93. uart_dev_t* uart = s_uarts[fd];
  94. while (uart->status.txfifo_cnt >= 127) {
  95. ;
  96. }
  97. uart->fifo.rw_byte = c;
  98. }
  99. static void uart_tx_char_via_driver(int fd, int c)
  100. {
  101. char ch = (char) c;
  102. uart_write_bytes(fd, &ch, 1);
  103. }
  104. static int uart_rx_char(int fd)
  105. {
  106. uart_dev_t* uart = s_uarts[fd];
  107. if (uart->status.rxfifo_cnt == 0) {
  108. return NONE;
  109. }
  110. return uart->fifo.rw_byte;
  111. }
  112. static int uart_rx_char_via_driver(int fd)
  113. {
  114. uint8_t c;
  115. int timeout = s_non_blocking[fd] ? 0 : portMAX_DELAY;
  116. int n = uart_read_bytes(fd, &c, 1, timeout);
  117. if (n <= 0) {
  118. return NONE;
  119. }
  120. return c;
  121. }
  122. static ssize_t uart_write(int fd, const void * data, size_t size)
  123. {
  124. assert(fd >=0 && fd < 3);
  125. const char *data_c = (const char *)data;
  126. /* Even though newlib does stream locking on each individual stream, we need
  127. * a dedicated UART lock if two streams (stdout and stderr) point to the
  128. * same UART.
  129. */
  130. _lock_acquire_recursive(&s_uart_write_locks[fd]);
  131. for (size_t i = 0; i < size; i++) {
  132. int c = data_c[i];
  133. if (c == '\n' && s_tx_mode != ESP_LINE_ENDINGS_LF) {
  134. s_uart_tx_func[fd](fd, '\r');
  135. if (s_tx_mode == ESP_LINE_ENDINGS_CR) {
  136. continue;
  137. }
  138. }
  139. s_uart_tx_func[fd](fd, c);
  140. }
  141. _lock_release_recursive(&s_uart_write_locks[fd]);
  142. return size;
  143. }
  144. /* Helper function which returns a previous character or reads a new one from
  145. * UART. Previous character can be returned ("pushed back") using
  146. * uart_return_char function.
  147. */
  148. static int uart_read_char(int fd)
  149. {
  150. /* return character from peek buffer, if it is there */
  151. if (s_peek_char[fd] != NONE) {
  152. int c = s_peek_char[fd];
  153. s_peek_char[fd] = NONE;
  154. return c;
  155. }
  156. return s_uart_rx_func[fd](fd);
  157. }
  158. /* Push back a character; it will be returned by next call to uart_read_char */
  159. static void uart_return_char(int fd, int c)
  160. {
  161. assert(s_peek_char[fd] == NONE);
  162. s_peek_char[fd] = c;
  163. }
  164. static ssize_t uart_read(int fd, void* data, size_t size)
  165. {
  166. assert(fd >=0 && fd < 3);
  167. char *data_c = (char *) data;
  168. size_t received = 0;
  169. _lock_acquire_recursive(&s_uart_read_locks[fd]);
  170. while (received < size) {
  171. int c = uart_read_char(fd);
  172. if (c == '\r') {
  173. if (s_rx_mode == ESP_LINE_ENDINGS_CR) {
  174. c = '\n';
  175. } else if (s_rx_mode == ESP_LINE_ENDINGS_CRLF) {
  176. /* look ahead */
  177. int c2 = uart_read_char(fd);
  178. if (c2 == NONE) {
  179. /* could not look ahead, put the current character back */
  180. uart_return_char(fd, c);
  181. break;
  182. }
  183. if (c2 == '\n') {
  184. /* this was \r\n sequence. discard \r, return \n */
  185. c = '\n';
  186. } else {
  187. /* \r followed by something else. put the second char back,
  188. * it will be processed on next iteration. return \r now.
  189. */
  190. uart_return_char(fd, c2);
  191. }
  192. }
  193. } else if (c == NONE) {
  194. break;
  195. }
  196. data_c[received] = (char) c;
  197. ++received;
  198. if (c == '\n') {
  199. break;
  200. }
  201. }
  202. _lock_release_recursive(&s_uart_read_locks[fd]);
  203. if (received > 0) {
  204. return received;
  205. }
  206. errno = EWOULDBLOCK;
  207. return -1;
  208. }
  209. static int uart_fstat(int fd, struct stat * st)
  210. {
  211. assert(fd >=0 && fd < 3);
  212. st->st_mode = S_IFCHR;
  213. return 0;
  214. }
  215. static int uart_close(int fd)
  216. {
  217. assert(fd >=0 && fd < 3);
  218. return 0;
  219. }
  220. static int uart_fcntl(int fd, int cmd, va_list args)
  221. {
  222. assert(fd >=0 && fd < 3);
  223. int result = 0;
  224. if (cmd == F_GETFL) {
  225. if (s_non_blocking[fd]) {
  226. result |= O_NONBLOCK;
  227. }
  228. } else if (cmd == F_SETFL) {
  229. int arg = va_arg(args, int);
  230. s_non_blocking[fd] = (arg & O_NONBLOCK) != 0;
  231. } else {
  232. // unsupported operation
  233. result = -1;
  234. errno = ENOSYS;
  235. }
  236. return result;
  237. }
  238. void esp_vfs_dev_uart_register()
  239. {
  240. esp_vfs_t vfs = {
  241. .flags = ESP_VFS_FLAG_DEFAULT,
  242. .write = &uart_write,
  243. .open = &uart_open,
  244. .fstat = &uart_fstat,
  245. .close = &uart_close,
  246. .read = &uart_read,
  247. .fcntl = &uart_fcntl
  248. };
  249. ESP_ERROR_CHECK(esp_vfs_register("/dev/uart", &vfs, NULL));
  250. }
  251. void esp_vfs_dev_uart_set_rx_line_endings(esp_line_endings_t mode)
  252. {
  253. s_rx_mode = mode;
  254. }
  255. void esp_vfs_dev_uart_set_tx_line_endings(esp_line_endings_t mode)
  256. {
  257. s_tx_mode = mode;
  258. }
  259. void esp_vfs_dev_uart_use_nonblocking(int fd)
  260. {
  261. _lock_acquire_recursive(&s_uart_read_locks[fd]);
  262. _lock_acquire_recursive(&s_uart_write_locks[fd]);
  263. s_uart_tx_func[fd] = uart_tx_char;
  264. s_uart_rx_func[fd] = uart_rx_char;
  265. _lock_release_recursive(&s_uart_write_locks[fd]);
  266. _lock_release_recursive(&s_uart_read_locks[fd]);
  267. }
  268. void esp_vfs_dev_uart_use_driver(int fd)
  269. {
  270. _lock_acquire_recursive(&s_uart_read_locks[fd]);
  271. _lock_acquire_recursive(&s_uart_write_locks[fd]);
  272. s_uart_tx_func[fd] = uart_tx_char_via_driver;
  273. s_uart_rx_func[fd] = uart_rx_char_via_driver;
  274. _lock_release_recursive(&s_uart_write_locks[fd]);
  275. _lock_release_recursive(&s_uart_read_locks[fd]);
  276. }