UARTSerial.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2017 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if (DEVICE_SERIAL && DEVICE_INTERRUPTIN)
  17. #include <errno.h>
  18. #include "UARTSerial.h"
  19. #include "platform/mbed_poll.h"
  20. #if MBED_CONF_RTOS_PRESENT
  21. #include "rtos/Thread.h"
  22. #else
  23. #include "platform/mbed_wait_api.h"
  24. #endif
  25. namespace mbed {
  26. UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) :
  27. SerialBase(tx, rx, baud),
  28. _blocking(true),
  29. _tx_irq_enabled(false),
  30. _rx_irq_enabled(true),
  31. _dcd_irq(NULL)
  32. {
  33. /* Attatch IRQ routines to the serial device. */
  34. SerialBase::attach(callback(this, &UARTSerial::rx_irq), RxIrq);
  35. }
  36. UARTSerial::~UARTSerial()
  37. {
  38. delete _dcd_irq;
  39. }
  40. void UARTSerial::dcd_irq()
  41. {
  42. wake();
  43. }
  44. void UARTSerial::set_baud(int baud)
  45. {
  46. SerialBase::baud(baud);
  47. }
  48. void UARTSerial::set_data_carrier_detect(PinName dcd_pin, bool active_high)
  49. {
  50. delete _dcd_irq;
  51. _dcd_irq = NULL;
  52. if (dcd_pin != NC) {
  53. _dcd_irq = new InterruptIn(dcd_pin);
  54. if (active_high) {
  55. _dcd_irq->fall(callback(this, &UARTSerial::dcd_irq));
  56. } else {
  57. _dcd_irq->rise(callback(this, &UARTSerial::dcd_irq));
  58. }
  59. }
  60. }
  61. void UARTSerial::set_format(int bits, Parity parity, int stop_bits)
  62. {
  63. api_lock();
  64. SerialBase::format(bits, parity, stop_bits);
  65. api_unlock();
  66. }
  67. #if DEVICE_SERIAL_FC
  68. void UARTSerial::set_flow_control(Flow type, PinName flow1, PinName flow2)
  69. {
  70. api_lock();
  71. SerialBase::set_flow_control(type, flow1, flow2);
  72. api_unlock();
  73. }
  74. #endif
  75. int UARTSerial::close()
  76. {
  77. /* Does not let us pass a file descriptor. So how to close ?
  78. * Also, does it make sense to close a device type file descriptor*/
  79. return 0;
  80. }
  81. int UARTSerial::isatty()
  82. {
  83. return 1;
  84. }
  85. off_t UARTSerial::seek(off_t offset, int whence)
  86. {
  87. /*XXX lseek can be done theoratically, but is it sane to mark positions on a dynamically growing/shrinking
  88. * buffer system (from an interrupt context) */
  89. return -ESPIPE;
  90. }
  91. int UARTSerial::sync()
  92. {
  93. api_lock();
  94. while (!_txbuf.empty()) {
  95. api_unlock();
  96. // Doing better than wait would require TxIRQ to also do wake() when becoming empty. Worth it?
  97. wait_ms(1);
  98. api_lock();
  99. }
  100. api_unlock();
  101. return 0;
  102. }
  103. void UARTSerial::sigio(Callback<void()> func)
  104. {
  105. core_util_critical_section_enter();
  106. _sigio_cb = func;
  107. if (_sigio_cb) {
  108. short current_events = poll(0x7FFF);
  109. if (current_events) {
  110. _sigio_cb();
  111. }
  112. }
  113. core_util_critical_section_exit();
  114. }
  115. ssize_t UARTSerial::write(const void *buffer, size_t length)
  116. {
  117. size_t data_written = 0;
  118. const char *buf_ptr = static_cast<const char *>(buffer);
  119. if (length == 0) {
  120. return 0;
  121. }
  122. api_lock();
  123. // Unlike read, we should write the whole thing if blocking. POSIX only
  124. // allows partial as a side-effect of signal handling; it normally tries to
  125. // write everything if blocking. Without signals we can always write all.
  126. while (data_written < length) {
  127. if (_txbuf.full()) {
  128. if (!_blocking) {
  129. break;
  130. }
  131. do {
  132. api_unlock();
  133. wait_ms(1); // XXX todo - proper wait, WFE for non-rtos ?
  134. api_lock();
  135. } while (_txbuf.full());
  136. }
  137. while (data_written < length && !_txbuf.full()) {
  138. _txbuf.push(*buf_ptr++);
  139. data_written++;
  140. }
  141. core_util_critical_section_enter();
  142. if (!_tx_irq_enabled) {
  143. UARTSerial::tx_irq(); // only write to hardware in one place
  144. if (!_txbuf.empty()) {
  145. SerialBase::attach(callback(this, &UARTSerial::tx_irq), TxIrq);
  146. _tx_irq_enabled = true;
  147. }
  148. }
  149. core_util_critical_section_exit();
  150. }
  151. api_unlock();
  152. return data_written != 0 ? (ssize_t) data_written : (ssize_t) - EAGAIN;
  153. }
  154. ssize_t UARTSerial::read(void *buffer, size_t length)
  155. {
  156. size_t data_read = 0;
  157. char *ptr = static_cast<char *>(buffer);
  158. if (length == 0) {
  159. return 0;
  160. }
  161. api_lock();
  162. while (_rxbuf.empty()) {
  163. if (!_blocking) {
  164. api_unlock();
  165. return -EAGAIN;
  166. }
  167. api_unlock();
  168. wait_ms(1); // XXX todo - proper wait, WFE for non-rtos ?
  169. api_lock();
  170. }
  171. while (data_read < length && !_rxbuf.empty()) {
  172. _rxbuf.pop(*ptr++);
  173. data_read++;
  174. }
  175. core_util_critical_section_enter();
  176. if (!_rx_irq_enabled) {
  177. UARTSerial::rx_irq(); // only read from hardware in one place
  178. if (!_rxbuf.full()) {
  179. SerialBase::attach(callback(this, &UARTSerial::rx_irq), RxIrq);
  180. _rx_irq_enabled = true;
  181. }
  182. }
  183. core_util_critical_section_exit();
  184. api_unlock();
  185. return data_read;
  186. }
  187. bool UARTSerial::hup() const
  188. {
  189. return _dcd_irq && _dcd_irq->read() != 0;
  190. }
  191. void UARTSerial::wake()
  192. {
  193. if (_sigio_cb) {
  194. _sigio_cb();
  195. }
  196. }
  197. short UARTSerial::poll(short events) const
  198. {
  199. short revents = 0;
  200. /* Check the Circular Buffer if space available for writing out */
  201. if (!_rxbuf.empty()) {
  202. revents |= POLLIN;
  203. }
  204. /* POLLHUP and POLLOUT are mutually exclusive */
  205. if (hup()) {
  206. revents |= POLLHUP;
  207. } else if (!_txbuf.full()) {
  208. revents |= POLLOUT;
  209. }
  210. /*TODO Handle other event types */
  211. return revents;
  212. }
  213. void UARTSerial::lock()
  214. {
  215. // This is the override for SerialBase.
  216. // No lock required as we only use SerialBase from interrupt or from
  217. // inside our own critical section.
  218. }
  219. void UARTSerial::unlock()
  220. {
  221. // This is the override for SerialBase.
  222. }
  223. void UARTSerial::api_lock(void)
  224. {
  225. _mutex.lock();
  226. }
  227. void UARTSerial::api_unlock(void)
  228. {
  229. _mutex.unlock();
  230. }
  231. void UARTSerial::rx_irq(void)
  232. {
  233. bool was_empty = _rxbuf.empty();
  234. /* Fill in the receive buffer if the peripheral is readable
  235. * and receive buffer is not full. */
  236. while (!_rxbuf.full() && SerialBase::readable()) {
  237. char data = SerialBase::_base_getc();
  238. _rxbuf.push(data);
  239. }
  240. if (_rx_irq_enabled && _rxbuf.full()) {
  241. SerialBase::attach(NULL, RxIrq);
  242. _rx_irq_enabled = false;
  243. }
  244. /* Report the File handler that data is ready to be read from the buffer. */
  245. if (was_empty && !_rxbuf.empty()) {
  246. wake();
  247. }
  248. }
  249. // Also called from write to start transfer
  250. void UARTSerial::tx_irq(void)
  251. {
  252. bool was_full = _txbuf.full();
  253. char data;
  254. /* Write to the peripheral if there is something to write
  255. * and if the peripheral is available to write. */
  256. while (SerialBase::writeable() && _txbuf.pop(data)) {
  257. SerialBase::_base_putc(data);
  258. }
  259. if (_tx_irq_enabled && _txbuf.empty()) {
  260. SerialBase::attach(NULL, TxIrq);
  261. _tx_irq_enabled = false;
  262. }
  263. /* Report the File handler that data can be written to peripheral. */
  264. if (was_full && !_txbuf.full() && !hup()) {
  265. wake();
  266. }
  267. }
  268. void UARTSerial::wait_ms(uint32_t millisec)
  269. {
  270. /* wait_ms implementation for RTOS spins until exact microseconds - we
  271. * want to just sleep until next tick.
  272. */
  273. #if MBED_CONF_RTOS_PRESENT
  274. rtos::Thread::wait(millisec);
  275. #else
  276. ::wait_ms(millisec);
  277. #endif
  278. }
  279. } //namespace mbed
  280. #endif //(DEVICE_SERIAL && DEVICE_INTERRUPTIN)