mbed
Fork of mbed-dev by
drivers/UARTSerial.cpp@168:9672193075cf, 2017-07-06 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Jul 06 15:42:05 2017 +0100
- Revision:
- 168:9672193075cf
- Parent:
- 167:e84263d55307
- Child:
- 171:19eb464bc2be
This updates the lib to the mbed lib v 146
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 | 167:e84263d55307 | 22 | #include "platform/mbed_wait_api.h" |
AnnaBridge | 167:e84263d55307 | 23 | |
AnnaBridge | 167:e84263d55307 | 24 | namespace mbed { |
AnnaBridge | 167:e84263d55307 | 25 | |
AnnaBridge | 167:e84263d55307 | 26 | UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) : |
AnnaBridge | 167:e84263d55307 | 27 | SerialBase(tx, rx, baud), |
AnnaBridge | 167:e84263d55307 | 28 | _blocking(true), |
AnnaBridge | 167:e84263d55307 | 29 | _tx_irq_enabled(false), |
AnnaBridge | 167:e84263d55307 | 30 | _dcd_irq(NULL) |
AnnaBridge | 167:e84263d55307 | 31 | { |
AnnaBridge | 167:e84263d55307 | 32 | /* Attatch IRQ routines to the serial device. */ |
AnnaBridge | 167:e84263d55307 | 33 | SerialBase::attach(callback(this, &UARTSerial::rx_irq), RxIrq); |
AnnaBridge | 167:e84263d55307 | 34 | } |
AnnaBridge | 167:e84263d55307 | 35 | |
AnnaBridge | 167:e84263d55307 | 36 | UARTSerial::~UARTSerial() |
AnnaBridge | 167:e84263d55307 | 37 | { |
AnnaBridge | 167:e84263d55307 | 38 | delete _dcd_irq; |
AnnaBridge | 167:e84263d55307 | 39 | } |
AnnaBridge | 167:e84263d55307 | 40 | |
AnnaBridge | 167:e84263d55307 | 41 | void UARTSerial::dcd_irq() |
AnnaBridge | 167:e84263d55307 | 42 | { |
AnnaBridge | 167:e84263d55307 | 43 | wake(); |
AnnaBridge | 167:e84263d55307 | 44 | } |
AnnaBridge | 167:e84263d55307 | 45 | |
AnnaBridge | 167:e84263d55307 | 46 | void UARTSerial::set_data_carrier_detect(PinName dcd_pin, bool active_high) |
AnnaBridge | 167:e84263d55307 | 47 | { |
AnnaBridge | 167:e84263d55307 | 48 | delete _dcd_irq; |
AnnaBridge | 167:e84263d55307 | 49 | _dcd_irq = NULL; |
AnnaBridge | 167:e84263d55307 | 50 | |
AnnaBridge | 167:e84263d55307 | 51 | if (dcd_pin != NC) { |
AnnaBridge | 167:e84263d55307 | 52 | _dcd_irq = new InterruptIn(dcd_pin); |
AnnaBridge | 167:e84263d55307 | 53 | if (active_high) { |
AnnaBridge | 167:e84263d55307 | 54 | _dcd_irq->fall(callback(this, &UARTSerial::dcd_irq)); |
AnnaBridge | 167:e84263d55307 | 55 | } else { |
AnnaBridge | 167:e84263d55307 | 56 | _dcd_irq->rise(callback(this, &UARTSerial::dcd_irq)); |
AnnaBridge | 167:e84263d55307 | 57 | } |
AnnaBridge | 167:e84263d55307 | 58 | } |
AnnaBridge | 167:e84263d55307 | 59 | } |
AnnaBridge | 167:e84263d55307 | 60 | |
AnnaBridge | 167:e84263d55307 | 61 | int UARTSerial::close() |
AnnaBridge | 167:e84263d55307 | 62 | { |
AnnaBridge | 167:e84263d55307 | 63 | /* Does not let us pass a file descriptor. So how to close ? |
AnnaBridge | 167:e84263d55307 | 64 | * Also, does it make sense to close a device type file descriptor*/ |
AnnaBridge | 167:e84263d55307 | 65 | return 0; |
AnnaBridge | 167:e84263d55307 | 66 | } |
AnnaBridge | 167:e84263d55307 | 67 | |
AnnaBridge | 167:e84263d55307 | 68 | int UARTSerial::isatty() |
AnnaBridge | 167:e84263d55307 | 69 | { |
AnnaBridge | 167:e84263d55307 | 70 | return 1; |
AnnaBridge | 167:e84263d55307 | 71 | |
AnnaBridge | 167:e84263d55307 | 72 | } |
AnnaBridge | 167:e84263d55307 | 73 | |
AnnaBridge | 167:e84263d55307 | 74 | off_t UARTSerial::seek(off_t offset, int whence) |
AnnaBridge | 167:e84263d55307 | 75 | { |
AnnaBridge | 167:e84263d55307 | 76 | /*XXX lseek can be done theoratically, but is it sane to mark positions on a dynamically growing/shrinking |
AnnaBridge | 167:e84263d55307 | 77 | * buffer system (from an interrupt context) */ |
AnnaBridge | 167:e84263d55307 | 78 | return -ESPIPE; |
AnnaBridge | 167:e84263d55307 | 79 | } |
AnnaBridge | 167:e84263d55307 | 80 | |
AnnaBridge | 167:e84263d55307 | 81 | int UARTSerial::sync() |
AnnaBridge | 167:e84263d55307 | 82 | { |
AnnaBridge | 168:9672193075cf | 83 | api_lock(); |
AnnaBridge | 167:e84263d55307 | 84 | |
AnnaBridge | 167:e84263d55307 | 85 | while (!_txbuf.empty()) { |
AnnaBridge | 168:9672193075cf | 86 | api_unlock(); |
AnnaBridge | 167:e84263d55307 | 87 | // Doing better than wait would require TxIRQ to also do wake() when becoming empty. Worth it? |
AnnaBridge | 167:e84263d55307 | 88 | wait_ms(1); |
AnnaBridge | 168:9672193075cf | 89 | api_lock(); |
AnnaBridge | 167:e84263d55307 | 90 | } |
AnnaBridge | 167:e84263d55307 | 91 | |
AnnaBridge | 168:9672193075cf | 92 | api_unlock(); |
AnnaBridge | 167:e84263d55307 | 93 | |
AnnaBridge | 167:e84263d55307 | 94 | return 0; |
AnnaBridge | 167:e84263d55307 | 95 | } |
AnnaBridge | 167:e84263d55307 | 96 | |
AnnaBridge | 167:e84263d55307 | 97 | void UARTSerial::sigio(Callback<void()> func) { |
AnnaBridge | 167:e84263d55307 | 98 | core_util_critical_section_enter(); |
AnnaBridge | 167:e84263d55307 | 99 | _sigio_cb = func; |
AnnaBridge | 167:e84263d55307 | 100 | if (_sigio_cb) { |
AnnaBridge | 167:e84263d55307 | 101 | short current_events = poll(0x7FFF); |
AnnaBridge | 167:e84263d55307 | 102 | if (current_events) { |
AnnaBridge | 167:e84263d55307 | 103 | _sigio_cb(); |
AnnaBridge | 167:e84263d55307 | 104 | } |
AnnaBridge | 167:e84263d55307 | 105 | } |
AnnaBridge | 167:e84263d55307 | 106 | core_util_critical_section_exit(); |
AnnaBridge | 167:e84263d55307 | 107 | } |
AnnaBridge | 167:e84263d55307 | 108 | |
AnnaBridge | 167:e84263d55307 | 109 | ssize_t UARTSerial::write(const void* buffer, size_t length) |
AnnaBridge | 167:e84263d55307 | 110 | { |
AnnaBridge | 167:e84263d55307 | 111 | size_t data_written = 0; |
AnnaBridge | 167:e84263d55307 | 112 | const char *buf_ptr = static_cast<const char *>(buffer); |
AnnaBridge | 167:e84263d55307 | 113 | |
AnnaBridge | 168:9672193075cf | 114 | api_lock(); |
AnnaBridge | 167:e84263d55307 | 115 | |
AnnaBridge | 167:e84263d55307 | 116 | while (_txbuf.full()) { |
AnnaBridge | 167:e84263d55307 | 117 | if (!_blocking) { |
AnnaBridge | 168:9672193075cf | 118 | api_unlock(); |
AnnaBridge | 167:e84263d55307 | 119 | return -EAGAIN; |
AnnaBridge | 167:e84263d55307 | 120 | } |
AnnaBridge | 168:9672193075cf | 121 | api_unlock(); |
AnnaBridge | 167:e84263d55307 | 122 | wait_ms(1); // XXX todo - proper wait, WFE for non-rtos ? |
AnnaBridge | 168:9672193075cf | 123 | api_lock(); |
AnnaBridge | 167:e84263d55307 | 124 | } |
AnnaBridge | 167:e84263d55307 | 125 | |
AnnaBridge | 167:e84263d55307 | 126 | while (data_written < length && !_txbuf.full()) { |
AnnaBridge | 167:e84263d55307 | 127 | _txbuf.push(*buf_ptr++); |
AnnaBridge | 167:e84263d55307 | 128 | data_written++; |
AnnaBridge | 167:e84263d55307 | 129 | } |
AnnaBridge | 167:e84263d55307 | 130 | |
AnnaBridge | 167:e84263d55307 | 131 | core_util_critical_section_enter(); |
AnnaBridge | 167:e84263d55307 | 132 | if (!_tx_irq_enabled) { |
AnnaBridge | 167:e84263d55307 | 133 | UARTSerial::tx_irq(); // only write to hardware in one place |
AnnaBridge | 167:e84263d55307 | 134 | if (!_txbuf.empty()) { |
AnnaBridge | 167:e84263d55307 | 135 | SerialBase::attach(callback(this, &UARTSerial::tx_irq), TxIrq); |
AnnaBridge | 167:e84263d55307 | 136 | _tx_irq_enabled = true; |
AnnaBridge | 167:e84263d55307 | 137 | } |
AnnaBridge | 167:e84263d55307 | 138 | } |
AnnaBridge | 167:e84263d55307 | 139 | core_util_critical_section_exit(); |
AnnaBridge | 167:e84263d55307 | 140 | |
AnnaBridge | 168:9672193075cf | 141 | api_unlock(); |
AnnaBridge | 167:e84263d55307 | 142 | |
AnnaBridge | 167:e84263d55307 | 143 | return data_written; |
AnnaBridge | 167:e84263d55307 | 144 | } |
AnnaBridge | 167:e84263d55307 | 145 | |
AnnaBridge | 167:e84263d55307 | 146 | ssize_t UARTSerial::read(void* buffer, size_t length) |
AnnaBridge | 167:e84263d55307 | 147 | { |
AnnaBridge | 167:e84263d55307 | 148 | size_t data_read = 0; |
AnnaBridge | 167:e84263d55307 | 149 | |
AnnaBridge | 167:e84263d55307 | 150 | char *ptr = static_cast<char *>(buffer); |
AnnaBridge | 167:e84263d55307 | 151 | |
AnnaBridge | 168:9672193075cf | 152 | api_lock(); |
AnnaBridge | 167:e84263d55307 | 153 | |
AnnaBridge | 167:e84263d55307 | 154 | while (_rxbuf.empty()) { |
AnnaBridge | 167:e84263d55307 | 155 | if (!_blocking) { |
AnnaBridge | 168:9672193075cf | 156 | api_unlock(); |
AnnaBridge | 167:e84263d55307 | 157 | return -EAGAIN; |
AnnaBridge | 167:e84263d55307 | 158 | } |
AnnaBridge | 168:9672193075cf | 159 | api_unlock(); |
AnnaBridge | 167:e84263d55307 | 160 | wait_ms(1); // XXX todo - proper wait, WFE for non-rtos ? |
AnnaBridge | 168:9672193075cf | 161 | api_lock(); |
AnnaBridge | 167:e84263d55307 | 162 | } |
AnnaBridge | 167:e84263d55307 | 163 | |
AnnaBridge | 167:e84263d55307 | 164 | while (data_read < length && !_rxbuf.empty()) { |
AnnaBridge | 167:e84263d55307 | 165 | _rxbuf.pop(*ptr++); |
AnnaBridge | 167:e84263d55307 | 166 | data_read++; |
AnnaBridge | 167:e84263d55307 | 167 | } |
AnnaBridge | 167:e84263d55307 | 168 | |
AnnaBridge | 168:9672193075cf | 169 | api_unlock(); |
AnnaBridge | 167:e84263d55307 | 170 | |
AnnaBridge | 167:e84263d55307 | 171 | return data_read; |
AnnaBridge | 167:e84263d55307 | 172 | } |
AnnaBridge | 167:e84263d55307 | 173 | |
AnnaBridge | 167:e84263d55307 | 174 | bool UARTSerial::hup() const |
AnnaBridge | 167:e84263d55307 | 175 | { |
AnnaBridge | 167:e84263d55307 | 176 | return _dcd_irq && _dcd_irq->read() != 0; |
AnnaBridge | 167:e84263d55307 | 177 | } |
AnnaBridge | 167:e84263d55307 | 178 | |
AnnaBridge | 167:e84263d55307 | 179 | void UARTSerial::wake() |
AnnaBridge | 167:e84263d55307 | 180 | { |
AnnaBridge | 167:e84263d55307 | 181 | if (_sigio_cb) { |
AnnaBridge | 167:e84263d55307 | 182 | _sigio_cb(); |
AnnaBridge | 167:e84263d55307 | 183 | } |
AnnaBridge | 167:e84263d55307 | 184 | } |
AnnaBridge | 167:e84263d55307 | 185 | |
AnnaBridge | 167:e84263d55307 | 186 | short UARTSerial::poll(short events) const { |
AnnaBridge | 167:e84263d55307 | 187 | |
AnnaBridge | 167:e84263d55307 | 188 | short revents = 0; |
AnnaBridge | 167:e84263d55307 | 189 | /* Check the Circular Buffer if space available for writing out */ |
AnnaBridge | 167:e84263d55307 | 190 | |
AnnaBridge | 167:e84263d55307 | 191 | |
AnnaBridge | 167:e84263d55307 | 192 | if (!_rxbuf.empty()) { |
AnnaBridge | 167:e84263d55307 | 193 | revents |= POLLIN; |
AnnaBridge | 167:e84263d55307 | 194 | } |
AnnaBridge | 167:e84263d55307 | 195 | |
AnnaBridge | 167:e84263d55307 | 196 | /* POLLHUP and POLLOUT are mutually exclusive */ |
AnnaBridge | 167:e84263d55307 | 197 | if (hup()) { |
AnnaBridge | 167:e84263d55307 | 198 | revents |= POLLHUP; |
AnnaBridge | 167:e84263d55307 | 199 | } else if (!_txbuf.full()) { |
AnnaBridge | 167:e84263d55307 | 200 | revents |= POLLOUT; |
AnnaBridge | 167:e84263d55307 | 201 | } |
AnnaBridge | 167:e84263d55307 | 202 | |
AnnaBridge | 167:e84263d55307 | 203 | /*TODO Handle other event types */ |
AnnaBridge | 167:e84263d55307 | 204 | |
AnnaBridge | 167:e84263d55307 | 205 | return revents; |
AnnaBridge | 167:e84263d55307 | 206 | } |
AnnaBridge | 167:e84263d55307 | 207 | |
AnnaBridge | 168:9672193075cf | 208 | void UARTSerial::lock() |
AnnaBridge | 168:9672193075cf | 209 | { |
AnnaBridge | 168:9672193075cf | 210 | // This is the override for SerialBase. |
AnnaBridge | 168:9672193075cf | 211 | // No lock required as we only use SerialBase from interrupt or from |
AnnaBridge | 168:9672193075cf | 212 | // inside our own critical section. |
AnnaBridge | 168:9672193075cf | 213 | } |
AnnaBridge | 168:9672193075cf | 214 | |
AnnaBridge | 168:9672193075cf | 215 | void UARTSerial::unlock() |
AnnaBridge | 168:9672193075cf | 216 | { |
AnnaBridge | 168:9672193075cf | 217 | // This is the override for SerialBase. |
AnnaBridge | 168:9672193075cf | 218 | } |
AnnaBridge | 168:9672193075cf | 219 | |
AnnaBridge | 168:9672193075cf | 220 | void UARTSerial::api_lock(void) |
AnnaBridge | 167:e84263d55307 | 221 | { |
AnnaBridge | 167:e84263d55307 | 222 | _mutex.lock(); |
AnnaBridge | 167:e84263d55307 | 223 | } |
AnnaBridge | 167:e84263d55307 | 224 | |
AnnaBridge | 168:9672193075cf | 225 | void UARTSerial::api_unlock(void) |
AnnaBridge | 167:e84263d55307 | 226 | { |
AnnaBridge | 167:e84263d55307 | 227 | _mutex.unlock(); |
AnnaBridge | 167:e84263d55307 | 228 | } |
AnnaBridge | 167:e84263d55307 | 229 | |
AnnaBridge | 167:e84263d55307 | 230 | void UARTSerial::rx_irq(void) |
AnnaBridge | 167:e84263d55307 | 231 | { |
AnnaBridge | 167:e84263d55307 | 232 | bool was_empty = _rxbuf.empty(); |
AnnaBridge | 167:e84263d55307 | 233 | |
AnnaBridge | 167:e84263d55307 | 234 | /* Fill in the receive buffer if the peripheral is readable |
AnnaBridge | 167:e84263d55307 | 235 | * and receive buffer is not full. */ |
AnnaBridge | 167:e84263d55307 | 236 | while (SerialBase::readable()) { |
AnnaBridge | 167:e84263d55307 | 237 | char data = SerialBase::_base_getc(); |
AnnaBridge | 167:e84263d55307 | 238 | if (!_rxbuf.full()) { |
AnnaBridge | 167:e84263d55307 | 239 | _rxbuf.push(data); |
AnnaBridge | 167:e84263d55307 | 240 | } else { |
AnnaBridge | 167:e84263d55307 | 241 | /* Drop - can we report in some way? */ |
AnnaBridge | 167:e84263d55307 | 242 | } |
AnnaBridge | 167:e84263d55307 | 243 | } |
AnnaBridge | 167:e84263d55307 | 244 | |
AnnaBridge | 167:e84263d55307 | 245 | /* Report the File handler that data is ready to be read from the buffer. */ |
AnnaBridge | 167:e84263d55307 | 246 | if (was_empty && !_rxbuf.empty()) { |
AnnaBridge | 167:e84263d55307 | 247 | wake(); |
AnnaBridge | 167:e84263d55307 | 248 | } |
AnnaBridge | 167:e84263d55307 | 249 | } |
AnnaBridge | 167:e84263d55307 | 250 | |
AnnaBridge | 167:e84263d55307 | 251 | // Also called from write to start transfer |
AnnaBridge | 167:e84263d55307 | 252 | void UARTSerial::tx_irq(void) |
AnnaBridge | 167:e84263d55307 | 253 | { |
AnnaBridge | 167:e84263d55307 | 254 | bool was_full = _txbuf.full(); |
AnnaBridge | 167:e84263d55307 | 255 | |
AnnaBridge | 167:e84263d55307 | 256 | /* Write to the peripheral if there is something to write |
AnnaBridge | 167:e84263d55307 | 257 | * and if the peripheral is available to write. */ |
AnnaBridge | 167:e84263d55307 | 258 | while (!_txbuf.empty() && SerialBase::writeable()) { |
AnnaBridge | 167:e84263d55307 | 259 | char data; |
AnnaBridge | 167:e84263d55307 | 260 | _txbuf.pop(data); |
AnnaBridge | 167:e84263d55307 | 261 | SerialBase::_base_putc(data); |
AnnaBridge | 167:e84263d55307 | 262 | } |
AnnaBridge | 167:e84263d55307 | 263 | |
AnnaBridge | 167:e84263d55307 | 264 | if (_tx_irq_enabled && _txbuf.empty()) { |
AnnaBridge | 167:e84263d55307 | 265 | SerialBase::attach(NULL, TxIrq); |
AnnaBridge | 167:e84263d55307 | 266 | _tx_irq_enabled = false; |
AnnaBridge | 167:e84263d55307 | 267 | } |
AnnaBridge | 167:e84263d55307 | 268 | |
AnnaBridge | 167:e84263d55307 | 269 | /* Report the File handler that data can be written to peripheral. */ |
AnnaBridge | 167:e84263d55307 | 270 | if (was_full && !_txbuf.full() && !hup()) { |
AnnaBridge | 167:e84263d55307 | 271 | wake(); |
AnnaBridge | 167:e84263d55307 | 272 | } |
AnnaBridge | 167:e84263d55307 | 273 | } |
AnnaBridge | 167:e84263d55307 | 274 | |
AnnaBridge | 167:e84263d55307 | 275 | } //namespace mbed |
AnnaBridge | 167:e84263d55307 | 276 | |
AnnaBridge | 168:9672193075cf | 277 | #endif //(DEVICE_SERIAL && DEVICE_INTERRUPTIN) |