vfs_uart.c 28 KB

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