vfs_uart.c 29 KB

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