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