vfs_uart.c 29 KB

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