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