vfs_uart.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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 <sys/param.h>
  21. #include "esp_vfs.h"
  22. #include "esp_vfs_dev.h"
  23. #include "esp_attr.h"
  24. #include "soc/uart_struct.h"
  25. #include "driver/uart.h"
  26. #include "sdkconfig.h"
  27. #include "driver/uart_select.h"
  28. #include "rom/uart.h"
  29. // TODO: make the number of UARTs chip dependent
  30. #define UART_NUM 3
  31. // Token signifying that no character is available
  32. #define NONE -1
  33. // UART write bytes function type
  34. typedef void (*tx_func_t)(int, int);
  35. // UART read bytes function type
  36. typedef int (*rx_func_t)(int);
  37. // Basic functions for sending and receiving bytes over UART
  38. static void uart_tx_char(int fd, int c);
  39. static int uart_rx_char(int fd);
  40. // Functions for sending and receiving bytes which use UART driver
  41. static void uart_tx_char_via_driver(int fd, int c);
  42. static int uart_rx_char_via_driver(int fd);
  43. // Pointers to UART peripherals
  44. static uart_dev_t* s_uarts[UART_NUM] = {&UART0, &UART1, &UART2};
  45. // per-UART locks, lazily initialized
  46. static _lock_t s_uart_read_locks[UART_NUM];
  47. static _lock_t s_uart_write_locks[UART_NUM];
  48. // One-character buffer used for newline conversion code, per UART
  49. static int s_peek_char[UART_NUM] = { NONE, NONE, NONE };
  50. // Per-UART non-blocking flag. Note: default implementation does not honor this
  51. // flag, all reads are non-blocking. This option becomes effective if UART
  52. // driver is used.
  53. static bool s_non_blocking[UART_NUM];
  54. /* Lock ensuring that uart_select is used from only one task at the time */
  55. static _lock_t s_one_select_lock;
  56. static SemaphoreHandle_t *_signal_sem = NULL;
  57. static fd_set *_readfds = NULL;
  58. static fd_set *_writefds = NULL;
  59. static fd_set *_errorfds = NULL;
  60. static fd_set *_readfds_orig = NULL;
  61. static fd_set *_writefds_orig = NULL;
  62. static fd_set *_errorfds_orig = NULL;
  63. // Newline conversion mode when transmitting
  64. static esp_line_endings_t s_tx_mode =
  65. #if CONFIG_NEWLIB_STDOUT_LINE_ENDING_CRLF
  66. ESP_LINE_ENDINGS_CRLF;
  67. #elif CONFIG_NEWLIB_STDOUT_LINE_ENDING_CR
  68. ESP_LINE_ENDINGS_CR;
  69. #else
  70. ESP_LINE_ENDINGS_LF;
  71. #endif
  72. // Newline conversion mode when receiving
  73. static esp_line_endings_t s_rx_mode[UART_NUM] = { [0 ... UART_NUM-1] =
  74. #if CONFIG_NEWLIB_STDIN_LINE_ENDING_CRLF
  75. ESP_LINE_ENDINGS_CRLF
  76. #elif CONFIG_NEWLIB_STDIN_LINE_ENDING_CR
  77. ESP_LINE_ENDINGS_CR
  78. #else
  79. ESP_LINE_ENDINGS_LF
  80. #endif
  81. };
  82. static void uart_end_select();
  83. // Functions used to write bytes to UART. Default to "basic" functions.
  84. static tx_func_t s_uart_tx_func[UART_NUM] = {
  85. &uart_tx_char, &uart_tx_char, &uart_tx_char
  86. };
  87. // Functions used to read bytes from UART. Default to "basic" functions.
  88. static rx_func_t s_uart_rx_func[UART_NUM] = {
  89. &uart_rx_char, &uart_rx_char, &uart_rx_char
  90. };
  91. static int uart_open(const char * path, int flags, int mode)
  92. {
  93. // this is fairly primitive, we should check if file is opened read only,
  94. // and error out if write is requested
  95. int fd = -1;
  96. if (strcmp(path, "/0") == 0) {
  97. fd = 0;
  98. } else if (strcmp(path, "/1") == 0) {
  99. fd = 1;
  100. } else if (strcmp(path, "/2") == 0) {
  101. fd = 2;
  102. } else {
  103. errno = ENOENT;
  104. return fd;
  105. }
  106. s_non_blocking[fd] = ((flags & O_NONBLOCK) == O_NONBLOCK);
  107. return fd;
  108. }
  109. static void uart_tx_char(int fd, int c)
  110. {
  111. uart_dev_t* uart = s_uarts[fd];
  112. while (uart->status.txfifo_cnt >= 127) {
  113. ;
  114. }
  115. uart->fifo.rw_byte = c;
  116. }
  117. static void uart_tx_char_via_driver(int fd, int c)
  118. {
  119. char ch = (char) c;
  120. uart_write_bytes(fd, &ch, 1);
  121. }
  122. static int uart_rx_char(int fd)
  123. {
  124. uart_dev_t* uart = s_uarts[fd];
  125. if (uart->status.rxfifo_cnt == 0) {
  126. return NONE;
  127. }
  128. return uart->fifo.rw_byte;
  129. }
  130. static int uart_rx_char_via_driver(int fd)
  131. {
  132. uint8_t c;
  133. int timeout = s_non_blocking[fd] ? 0 : portMAX_DELAY;
  134. int n = uart_read_bytes(fd, &c, 1, timeout);
  135. if (n <= 0) {
  136. return NONE;
  137. }
  138. return c;
  139. }
  140. static ssize_t uart_write(int fd, const void * data, size_t size)
  141. {
  142. assert(fd >=0 && fd < 3);
  143. const char *data_c = (const char *)data;
  144. /* Even though newlib does stream locking on each individual stream, we need
  145. * a dedicated UART lock if two streams (stdout and stderr) point to the
  146. * same UART.
  147. */
  148. _lock_acquire_recursive(&s_uart_write_locks[fd]);
  149. for (size_t i = 0; i < size; i++) {
  150. int c = data_c[i];
  151. if (c == '\n' && s_tx_mode != ESP_LINE_ENDINGS_LF) {
  152. s_uart_tx_func[fd](fd, '\r');
  153. if (s_tx_mode == ESP_LINE_ENDINGS_CR) {
  154. continue;
  155. }
  156. }
  157. s_uart_tx_func[fd](fd, c);
  158. }
  159. _lock_release_recursive(&s_uart_write_locks[fd]);
  160. return size;
  161. }
  162. /* Helper function which returns a previous character or reads a new one from
  163. * UART. Previous character can be returned ("pushed back") using
  164. * uart_return_char function.
  165. */
  166. static int uart_read_char(int fd)
  167. {
  168. /* return character from peek buffer, if it is there */
  169. if (s_peek_char[fd] != NONE) {
  170. int c = s_peek_char[fd];
  171. s_peek_char[fd] = NONE;
  172. return c;
  173. }
  174. return s_uart_rx_func[fd](fd);
  175. }
  176. /* Push back a character; it will be returned by next call to uart_read_char */
  177. static void uart_return_char(int fd, int c)
  178. {
  179. assert(s_peek_char[fd] == NONE);
  180. s_peek_char[fd] = c;
  181. }
  182. static ssize_t uart_read(int fd, void* data, size_t size)
  183. {
  184. assert(fd >=0 && fd < 3);
  185. char *data_c = (char *) data;
  186. size_t received = 0;
  187. _lock_acquire_recursive(&s_uart_read_locks[fd]);
  188. while (received < size) {
  189. int c = uart_read_char(fd);
  190. if (c == '\r') {
  191. if (s_rx_mode[fd] == ESP_LINE_ENDINGS_CR) {
  192. c = '\n';
  193. } else if (s_rx_mode[fd] == ESP_LINE_ENDINGS_CRLF) {
  194. /* look ahead */
  195. int c2 = uart_read_char(fd);
  196. if (c2 == NONE) {
  197. /* could not look ahead, put the current character back */
  198. uart_return_char(fd, c);
  199. break;
  200. }
  201. if (c2 == '\n') {
  202. /* this was \r\n sequence. discard \r, return \n */
  203. c = '\n';
  204. } else {
  205. /* \r followed by something else. put the second char back,
  206. * it will be processed on next iteration. return \r now.
  207. */
  208. uart_return_char(fd, c2);
  209. }
  210. }
  211. } else if (c == NONE) {
  212. break;
  213. }
  214. data_c[received] = (char) c;
  215. ++received;
  216. if (c == '\n') {
  217. break;
  218. }
  219. }
  220. _lock_release_recursive(&s_uart_read_locks[fd]);
  221. if (received > 0) {
  222. return received;
  223. }
  224. errno = EWOULDBLOCK;
  225. return -1;
  226. }
  227. static int uart_fstat(int fd, struct stat * st)
  228. {
  229. assert(fd >=0 && fd < 3);
  230. st->st_mode = S_IFCHR;
  231. return 0;
  232. }
  233. static int uart_close(int fd)
  234. {
  235. assert(fd >=0 && fd < 3);
  236. return 0;
  237. }
  238. static int uart_fcntl(int fd, int cmd, va_list args)
  239. {
  240. assert(fd >=0 && fd < 3);
  241. int result = 0;
  242. if (cmd == F_GETFL) {
  243. if (s_non_blocking[fd]) {
  244. result |= O_NONBLOCK;
  245. }
  246. } else if (cmd == F_SETFL) {
  247. int arg = va_arg(args, int);
  248. s_non_blocking[fd] = (arg & O_NONBLOCK) != 0;
  249. } else {
  250. // unsupported operation
  251. result = -1;
  252. errno = ENOSYS;
  253. }
  254. return result;
  255. }
  256. static int uart_access(const char *path, int amode)
  257. {
  258. int ret = -1;
  259. if (strcmp(path, "/0") == 0 || strcmp(path, "/1") == 0 || strcmp(path, "/2") == 0) {
  260. if (F_OK == amode) {
  261. ret = 0; //path exists
  262. } else {
  263. if ((((amode & R_OK) == R_OK) || ((amode & W_OK) == W_OK)) && ((amode & X_OK) != X_OK)) {
  264. ret = 0; //path is readable and/or writable but not executable
  265. } else {
  266. errno = EACCES;
  267. }
  268. }
  269. } else {
  270. errno = ENOENT;
  271. }
  272. return ret;
  273. }
  274. static int uart_fsync(int fd)
  275. {
  276. assert(fd >= 0 && fd < 3);
  277. _lock_acquire_recursive(&s_uart_write_locks[fd]);
  278. uart_tx_wait_idle((uint8_t) fd);
  279. _lock_release_recursive(&s_uart_write_locks[fd]);
  280. return 0;
  281. }
  282. static void select_notif_callback(uart_port_t uart_num, uart_select_notif_t uart_select_notif, BaseType_t *task_woken)
  283. {
  284. switch (uart_select_notif) {
  285. case UART_SELECT_READ_NOTIF:
  286. if (FD_ISSET(uart_num, _readfds_orig)) {
  287. FD_SET(uart_num, _readfds);
  288. esp_vfs_select_triggered_isr(_signal_sem, task_woken);
  289. }
  290. break;
  291. case UART_SELECT_WRITE_NOTIF:
  292. if (FD_ISSET(uart_num, _writefds_orig)) {
  293. FD_SET(uart_num, _writefds);
  294. esp_vfs_select_triggered_isr(_signal_sem, task_woken);
  295. }
  296. break;
  297. case UART_SELECT_ERROR_NOTIF:
  298. if (FD_ISSET(uart_num, _errorfds_orig)) {
  299. FD_SET(uart_num, _errorfds);
  300. esp_vfs_select_triggered_isr(_signal_sem, task_woken);
  301. }
  302. break;
  303. }
  304. }
  305. static esp_err_t uart_start_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, SemaphoreHandle_t *signal_sem)
  306. {
  307. if (_lock_try_acquire(&s_one_select_lock)) {
  308. return ESP_ERR_INVALID_STATE;
  309. }
  310. const int max_fds = MIN(nfds, UART_NUM);
  311. portENTER_CRITICAL(uart_get_selectlock());
  312. if (_readfds || _writefds || _errorfds || _readfds_orig || _writefds_orig || _errorfds_orig || _signal_sem) {
  313. portEXIT_CRITICAL(uart_get_selectlock());
  314. uart_end_select();
  315. return ESP_ERR_INVALID_STATE;
  316. }
  317. if ((_readfds_orig = malloc(sizeof(fd_set))) == NULL) {
  318. portEXIT_CRITICAL(uart_get_selectlock());
  319. uart_end_select();
  320. return ESP_ERR_NO_MEM;
  321. }
  322. if ((_writefds_orig = malloc(sizeof(fd_set))) == NULL) {
  323. portEXIT_CRITICAL(uart_get_selectlock());
  324. uart_end_select();
  325. return ESP_ERR_NO_MEM;
  326. }
  327. if ((_errorfds_orig = malloc(sizeof(fd_set))) == NULL) {
  328. portEXIT_CRITICAL(uart_get_selectlock());
  329. uart_end_select();
  330. return ESP_ERR_NO_MEM;
  331. }
  332. //uart_set_select_notif_callback set the callbacks in UART ISR
  333. for (int i = 0; i < max_fds; ++i) {
  334. if (FD_ISSET(i, readfds) || FD_ISSET(i, writefds) || FD_ISSET(i, exceptfds)) {
  335. uart_set_select_notif_callback(i, select_notif_callback);
  336. }
  337. }
  338. _signal_sem = signal_sem;
  339. _readfds = readfds;
  340. _writefds = writefds;
  341. _errorfds = exceptfds;
  342. *_readfds_orig = *readfds;
  343. *_writefds_orig = *writefds;
  344. *_errorfds_orig = *exceptfds;
  345. FD_ZERO(readfds);
  346. FD_ZERO(writefds);
  347. FD_ZERO(exceptfds);
  348. for (int i = 0; i < max_fds; ++i) {
  349. if (FD_ISSET(i, _readfds_orig)) {
  350. size_t buffered_size;
  351. if (uart_get_buffered_data_len(i, &buffered_size) == ESP_OK && buffered_size > 0) {
  352. // signalize immediately when data is buffered
  353. FD_SET(i, _readfds);
  354. esp_vfs_select_triggered(_signal_sem);
  355. }
  356. }
  357. }
  358. portEXIT_CRITICAL(uart_get_selectlock());
  359. // s_one_select_lock is not released on successfull exit - will be
  360. // released in uart_end_select()
  361. return ESP_OK;
  362. }
  363. static void uart_end_select()
  364. {
  365. portENTER_CRITICAL(uart_get_selectlock());
  366. for (int i = 0; i < UART_NUM; ++i) {
  367. uart_set_select_notif_callback(i, NULL);
  368. }
  369. _signal_sem = NULL;
  370. _readfds = NULL;
  371. _writefds = NULL;
  372. _errorfds = NULL;
  373. if (_readfds_orig) {
  374. free(_readfds_orig);
  375. _readfds_orig = NULL;
  376. }
  377. if (_writefds_orig) {
  378. free(_writefds_orig);
  379. _writefds_orig = NULL;
  380. }
  381. if (_errorfds_orig) {
  382. free(_errorfds_orig);
  383. _errorfds_orig = NULL;
  384. }
  385. portEXIT_CRITICAL(uart_get_selectlock());
  386. _lock_release(&s_one_select_lock);
  387. }
  388. #ifdef CONFIG_SUPPORT_TERMIOS
  389. static int uart_tcsetattr(int fd, int optional_actions, const struct termios *p)
  390. {
  391. if (fd < 0 || fd >= UART_NUM) {
  392. errno = EBADF;
  393. return -1;
  394. }
  395. if (p == NULL) {
  396. errno = EINVAL;
  397. return -1;
  398. }
  399. switch (optional_actions) {
  400. case TCSANOW:
  401. // nothing to do
  402. break;
  403. case TCSADRAIN:
  404. if (uart_wait_tx_done(fd, portMAX_DELAY) != ESP_OK) {
  405. errno = EINVAL;
  406. return -1;
  407. }
  408. /* FALLTHRU */
  409. case TCSAFLUSH:
  410. if (uart_flush_input(fd) != ESP_OK) {
  411. errno = EINVAL;
  412. return -1;
  413. }
  414. break;
  415. default:
  416. errno = EINVAL;
  417. return -1;
  418. }
  419. if (p->c_iflag & IGNCR) {
  420. s_rx_mode[fd] = ESP_LINE_ENDINGS_CRLF;
  421. } else if (p->c_iflag & ICRNL) {
  422. s_rx_mode[fd] = ESP_LINE_ENDINGS_CR;
  423. } else {
  424. s_rx_mode[fd] = ESP_LINE_ENDINGS_LF;
  425. }
  426. // output line endings are not supported because there is no alternative in termios for converting LF to CR
  427. {
  428. uart_word_length_t data_bits;
  429. const tcflag_t csize_bits = p->c_cflag & CSIZE;
  430. switch (csize_bits) {
  431. case CS5:
  432. data_bits = UART_DATA_5_BITS;
  433. break;
  434. case CS6:
  435. data_bits = UART_DATA_6_BITS;
  436. break;
  437. case CS7:
  438. data_bits = UART_DATA_7_BITS;
  439. break;
  440. case CS8:
  441. data_bits = UART_DATA_8_BITS;
  442. break;
  443. default:
  444. errno = EINVAL;
  445. return -1;
  446. }
  447. if (uart_set_word_length(fd, data_bits) != ESP_OK) {
  448. errno = EINVAL;
  449. return -1;
  450. }
  451. }
  452. if (uart_set_stop_bits(fd, (p->c_cflag & CSTOPB) ? UART_STOP_BITS_2 : UART_STOP_BITS_1) != ESP_OK) {
  453. errno = EINVAL;
  454. return -1;
  455. }
  456. if (uart_set_parity(fd, (p->c_cflag & PARENB) ?
  457. ((p->c_cflag & PARODD) ? UART_PARITY_ODD : UART_PARITY_EVEN)
  458. :
  459. UART_PARITY_DISABLE) != ESP_OK) {
  460. errno = EINVAL;
  461. return -1;
  462. }
  463. if (p->c_cflag & (CBAUD | CBAUDEX)) {
  464. if (p->c_ispeed != p->c_ospeed) {
  465. errno = EINVAL;
  466. return -1;
  467. } else {
  468. uint32_t b;
  469. if (p->c_cflag & BOTHER) {
  470. b = p->c_ispeed;
  471. } else {
  472. switch (p->c_ispeed) {
  473. case B0:
  474. b = 0;
  475. break;
  476. case B50:
  477. b = 50;
  478. break;
  479. case B75:
  480. b = 75;
  481. break;
  482. case B110:
  483. b = 110;
  484. break;
  485. case B134:
  486. b = 134;
  487. break;
  488. case B150:
  489. b = 150;
  490. break;
  491. case B200:
  492. b = 200;
  493. break;
  494. case B300:
  495. b = 300;
  496. break;
  497. case B600:
  498. b = 600;
  499. break;
  500. case B1200:
  501. b = 1200;
  502. break;
  503. case B1800:
  504. b = 1800;
  505. break;
  506. case B2400:
  507. b = 2400;
  508. break;
  509. case B4800:
  510. b = 4800;
  511. break;
  512. case B9600:
  513. b = 9600;
  514. break;
  515. case B19200:
  516. b = 19200;
  517. break;
  518. case B38400:
  519. b = 38400;
  520. break;
  521. case B57600:
  522. b = 57600;
  523. break;
  524. case B115200:
  525. b = 115200;
  526. break;
  527. case B230400:
  528. b = 230400;
  529. break;
  530. case B460800:
  531. b = 460800;
  532. break;
  533. case B500000:
  534. b = 500000;
  535. break;
  536. case B576000:
  537. b = 576000;
  538. break;
  539. case B921600:
  540. b = 921600;
  541. break;
  542. case B1000000:
  543. b = 1000000;
  544. break;
  545. case B1152000:
  546. b = 1152000;
  547. break;
  548. case B1500000:
  549. b = 1500000;
  550. break;
  551. case B2000000:
  552. b = 2000000;
  553. break;
  554. case B2500000:
  555. b = 2500000;
  556. break;
  557. case B3000000:
  558. b = 3000000;
  559. break;
  560. case B3500000:
  561. b = 3500000;
  562. break;
  563. case B4000000:
  564. b = 4000000;
  565. break;
  566. default:
  567. errno = EINVAL;
  568. return -1;
  569. }
  570. }
  571. if (uart_set_baudrate(fd, b) != ESP_OK) {
  572. errno = EINVAL;
  573. return -1;
  574. }
  575. }
  576. }
  577. return 0;
  578. }
  579. static int uart_tcgetattr(int fd, struct termios *p)
  580. {
  581. if (fd < 0 || fd >= UART_NUM) {
  582. errno = EBADF;
  583. return -1;
  584. }
  585. if (p == NULL) {
  586. errno = EINVAL;
  587. return -1;
  588. }
  589. memset(p, 0, sizeof(struct termios));
  590. if (s_rx_mode[fd] == ESP_LINE_ENDINGS_CRLF) {
  591. p->c_iflag |= IGNCR;
  592. } else if (s_rx_mode[fd] == ESP_LINE_ENDINGS_CR) {
  593. p->c_iflag |= ICRNL;
  594. }
  595. {
  596. uart_word_length_t data_bits;
  597. if (uart_get_word_length(fd, &data_bits) != ESP_OK) {
  598. errno = EINVAL;
  599. return -1;
  600. }
  601. p->c_cflag &= (~CSIZE);
  602. switch (data_bits) {
  603. case UART_DATA_5_BITS:
  604. p->c_cflag |= CS5;
  605. break;
  606. case UART_DATA_6_BITS:
  607. p->c_cflag |= CS6;
  608. break;
  609. case UART_DATA_7_BITS:
  610. p->c_cflag |= CS7;
  611. break;
  612. case UART_DATA_8_BITS:
  613. p->c_cflag |= CS8;
  614. break;
  615. default:
  616. errno = ENOSYS;
  617. return -1;
  618. }
  619. }
  620. {
  621. uart_stop_bits_t stop_bits;
  622. if (uart_get_stop_bits(fd, &stop_bits) != ESP_OK) {
  623. errno = EINVAL;
  624. return -1;
  625. }
  626. switch (stop_bits) {
  627. case UART_STOP_BITS_1:
  628. // nothing to do
  629. break;
  630. case UART_STOP_BITS_2:
  631. p->c_cflag |= CSTOPB;
  632. break;
  633. default:
  634. // UART_STOP_BITS_1_5 is unsupported by termios
  635. errno = ENOSYS;
  636. return -1;
  637. }
  638. }
  639. {
  640. uart_parity_t parity_mode;
  641. if (uart_get_parity(fd, &parity_mode) != ESP_OK) {
  642. errno = EINVAL;
  643. return -1;
  644. }
  645. switch (parity_mode) {
  646. case UART_PARITY_EVEN:
  647. p->c_cflag |= PARENB;
  648. break;
  649. case UART_PARITY_ODD:
  650. p->c_cflag |= (PARENB | PARODD);
  651. break;
  652. case UART_PARITY_DISABLE:
  653. // nothing to do
  654. break;
  655. default:
  656. errno = ENOSYS;
  657. return -1;
  658. }
  659. }
  660. {
  661. uint32_t baudrate;
  662. if (uart_get_baudrate(fd, &baudrate) != ESP_OK) {
  663. errno = EINVAL;
  664. return -1;
  665. }
  666. p->c_cflag |= (CBAUD | CBAUDEX);
  667. speed_t sp;
  668. switch (baudrate) {
  669. case 0:
  670. sp = B0;
  671. break;
  672. case 50:
  673. sp = B50;
  674. break;
  675. case 75:
  676. sp = B75;
  677. break;
  678. case 110:
  679. sp = B110;
  680. break;
  681. case 134:
  682. sp = B134;
  683. break;
  684. case 150:
  685. sp = B150;
  686. break;
  687. case 200:
  688. sp = B200;
  689. break;
  690. case 300:
  691. sp = B300;
  692. break;
  693. case 600:
  694. sp = B600;
  695. break;
  696. case 1200:
  697. sp = B1200;
  698. break;
  699. case 1800:
  700. sp = B1800;
  701. break;
  702. case 2400:
  703. sp = B2400;
  704. break;
  705. case 4800:
  706. sp = B4800;
  707. break;
  708. case 9600:
  709. sp = B9600;
  710. break;
  711. case 19200:
  712. sp = B19200;
  713. break;
  714. case 38400:
  715. sp = B38400;
  716. break;
  717. case 57600:
  718. sp = B57600;
  719. break;
  720. case 115200:
  721. sp = B115200;
  722. break;
  723. case 230400:
  724. sp = B230400;
  725. break;
  726. case 460800:
  727. sp = B460800;
  728. break;
  729. case 500000:
  730. sp = B500000;
  731. break;
  732. case 576000:
  733. sp = B576000;
  734. break;
  735. case 921600:
  736. sp = B921600;
  737. break;
  738. case 1000000:
  739. sp = B1000000;
  740. break;
  741. case 1152000:
  742. sp = B1152000;
  743. break;
  744. case 1500000:
  745. sp = B1500000;
  746. break;
  747. case 2000000:
  748. sp = B2000000;
  749. break;
  750. case 2500000:
  751. sp = B2500000;
  752. break;
  753. case 3000000:
  754. sp = B3000000;
  755. break;
  756. case 3500000:
  757. sp = B3500000;
  758. break;
  759. case 4000000:
  760. sp = B4000000;
  761. break;
  762. default:
  763. p->c_cflag |= BOTHER;
  764. sp = baudrate;
  765. break;
  766. }
  767. p->c_ispeed = p->c_ospeed = sp;
  768. }
  769. return 0;
  770. }
  771. static int uart_tcdrain(int fd)
  772. {
  773. if (fd < 0 || fd >= UART_NUM) {
  774. errno = EBADF;
  775. return -1;
  776. }
  777. if (uart_wait_tx_done(fd, portMAX_DELAY) != ESP_OK) {
  778. errno = EINVAL;
  779. return -1;
  780. }
  781. return 0;
  782. }
  783. static int uart_tcflush(int fd, int select)
  784. {
  785. if (fd < 0 || fd >= UART_NUM) {
  786. errno = EBADF;
  787. return -1;
  788. }
  789. if (select == TCIFLUSH) {
  790. if (uart_flush_input(fd) != ESP_OK) {
  791. errno = EINVAL;
  792. return -1;
  793. }
  794. } else {
  795. // output flushing is not supported
  796. errno = EINVAL;
  797. return -1;
  798. }
  799. return 0;
  800. }
  801. #endif // CONFIG_SUPPORT_TERMIOS
  802. void esp_vfs_dev_uart_register()
  803. {
  804. esp_vfs_t vfs = {
  805. .flags = ESP_VFS_FLAG_DEFAULT,
  806. .write = &uart_write,
  807. .open = &uart_open,
  808. .fstat = &uart_fstat,
  809. .close = &uart_close,
  810. .read = &uart_read,
  811. .fcntl = &uart_fcntl,
  812. .fsync = &uart_fsync,
  813. .access = &uart_access,
  814. .start_select = &uart_start_select,
  815. .end_select = &uart_end_select,
  816. #ifdef CONFIG_SUPPORT_TERMIOS
  817. .tcsetattr = &uart_tcsetattr,
  818. .tcgetattr = &uart_tcgetattr,
  819. .tcdrain = &uart_tcdrain,
  820. .tcflush = &uart_tcflush,
  821. #endif // CONFIG_SUPPORT_TERMIOS
  822. };
  823. ESP_ERROR_CHECK(esp_vfs_register("/dev/uart", &vfs, NULL));
  824. }
  825. void esp_vfs_dev_uart_set_rx_line_endings(esp_line_endings_t mode)
  826. {
  827. for (int i = 0; i < UART_NUM; ++i) {
  828. s_rx_mode[i] = mode;
  829. }
  830. }
  831. void esp_vfs_dev_uart_set_tx_line_endings(esp_line_endings_t mode)
  832. {
  833. s_tx_mode = mode;
  834. }
  835. void esp_vfs_dev_uart_use_nonblocking(int uart_num)
  836. {
  837. _lock_acquire_recursive(&s_uart_read_locks[uart_num]);
  838. _lock_acquire_recursive(&s_uart_write_locks[uart_num]);
  839. s_uart_tx_func[uart_num] = uart_tx_char;
  840. s_uart_rx_func[uart_num] = uart_rx_char;
  841. _lock_release_recursive(&s_uart_write_locks[uart_num]);
  842. _lock_release_recursive(&s_uart_read_locks[uart_num]);
  843. }
  844. void esp_vfs_dev_uart_use_driver(int uart_num)
  845. {
  846. _lock_acquire_recursive(&s_uart_read_locks[uart_num]);
  847. _lock_acquire_recursive(&s_uart_write_locks[uart_num]);
  848. s_uart_tx_func[uart_num] = uart_tx_char_via_driver;
  849. s_uart_rx_func[uart_num] = uart_rx_char_via_driver;
  850. _lock_release_recursive(&s_uart_write_locks[uart_num]);
  851. _lock_release_recursive(&s_uart_read_locks[uart_num]);
  852. }