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