vfs_uart.c 26 KB

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