vfs_uart.c 28 KB

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