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