vfs_uart.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2015-2016 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 "esp_vfs.h"
  16. #include "esp_attr.h"
  17. #include "sys/errno.h"
  18. #include "sys/lock.h"
  19. #include "soc/uart_struct.h"
  20. #include "sdkconfig.h"
  21. static uart_dev_t* s_uarts[3] = {&UART0, &UART1, &UART2};
  22. static _lock_t s_uart_locks[3]; // per-UART locks, lazily initialized
  23. static int IRAM_ATTR uart_open(const char * path, int flags, int mode)
  24. {
  25. // this is fairly primitive, we should check if file is opened read only,
  26. // and error out if write is requested
  27. if (strcmp(path, "/0") == 0) {
  28. return 0;
  29. } else if (strcmp(path, "/1") == 0) {
  30. return 1;
  31. } else if (strcmp(path, "/2") == 0) {
  32. return 2;
  33. }
  34. errno = ENOENT;
  35. return -1;
  36. }
  37. static void IRAM_ATTR uart_tx_char(uart_dev_t* uart, int c)
  38. {
  39. while (uart->status.txfifo_cnt >= 127) {
  40. ;
  41. }
  42. uart->fifo.rw_byte = c;
  43. }
  44. static size_t IRAM_ATTR uart_write(int fd, const void * data, size_t size)
  45. {
  46. assert(fd >=0 && fd < 3);
  47. const char *data_c = (const char *)data;
  48. uart_dev_t* uart = s_uarts[fd];
  49. /*
  50. * Even though newlib does stream locking on each individual stream, we need
  51. * a dedicated UART lock if two streams (stdout and stderr) point to the
  52. * same UART.
  53. */
  54. _lock_acquire_recursive(&s_uart_locks[fd]);
  55. for (size_t i = 0; i < size; i++) {
  56. #if CONFIG_NEWLIB_STDOUT_ADDCR
  57. if (data_c[i]=='\n') {
  58. uart_tx_char(uart, '\r');
  59. }
  60. #endif
  61. uart_tx_char(uart, data_c[i]);
  62. }
  63. _lock_release_recursive(&s_uart_locks[fd]);
  64. return size;
  65. }
  66. static ssize_t IRAM_ATTR uart_read(int fd, void* data, size_t size)
  67. {
  68. assert(fd >=0 && fd < 3);
  69. uint8_t *data_c = (uint8_t *) data;
  70. uart_dev_t* uart = s_uarts[fd];
  71. size_t received = 0;
  72. _lock_acquire_recursive(&s_uart_locks[fd]);
  73. while (uart->status.rxfifo_cnt > 0 && received < size) {
  74. uint8_t c = uart->fifo.rw_byte;
  75. #if CONFIG_NEWLIB_STDOUT_ADDCR
  76. /* Convert \r\n sequences to \n.
  77. * If \r is received, it is put into 'buffered_char' until the next
  78. * character is received. Then depending on the character, we either
  79. * drop \r (if the next one is \n) or output \r and then proceed to output
  80. * the new character.
  81. */
  82. const int NONE = -1;
  83. static int buffered_char = NONE;
  84. if (buffered_char != NONE) {
  85. if (buffered_char == '\r' && c == '\n') {
  86. buffered_char = NONE;
  87. } else {
  88. data_c[received] = buffered_char;
  89. buffered_char = NONE;
  90. ++received;
  91. if (received == size) {
  92. /* We have placed the buffered character into the output buffer
  93. * but there won't be enough space for the newly received one.
  94. * Keep the new character in buffered_char until read is called
  95. * again.
  96. */
  97. buffered_char = c;
  98. break;
  99. }
  100. }
  101. }
  102. if (c == '\r') {
  103. buffered_char = c;
  104. continue;
  105. }
  106. #endif //CONFIG_NEWLIB_STDOUT_ADDCR
  107. data_c[received] = c;
  108. ++received;
  109. }
  110. _lock_release_recursive(&s_uart_locks[fd]);
  111. if (received > 0) {
  112. return received;
  113. }
  114. errno = EWOULDBLOCK;
  115. return -1;
  116. }
  117. static int IRAM_ATTR uart_fstat(int fd, struct stat * st)
  118. {
  119. assert(fd >=0 && fd < 3);
  120. st->st_mode = S_IFCHR;
  121. return 0;
  122. }
  123. static int IRAM_ATTR uart_close(int fd)
  124. {
  125. assert(fd >=0 && fd < 3);
  126. return 0;
  127. }
  128. void esp_vfs_dev_uart_register()
  129. {
  130. esp_vfs_t vfs = {
  131. .fd_offset = 0,
  132. .flags = ESP_VFS_FLAG_DEFAULT,
  133. .write = &uart_write,
  134. .open = &uart_open,
  135. .fstat = &uart_fstat,
  136. .close = &uart_close,
  137. .read = &uart_read,
  138. .lseek = NULL,
  139. .stat = NULL,
  140. .link = NULL,
  141. .unlink = NULL,
  142. .rename = NULL
  143. };
  144. ESP_ERROR_CHECK(esp_vfs_register("/dev/uart", &vfs, NULL));
  145. }