vfs_uart.c 29 KB

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