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