vfs_uart.c 28 KB

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