mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
drivers/SerialBase.cpp@181:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 181:57724642e740
- Parent:
- 175:af195413fb11
- Child:
- 184:08ed48f1de7f
mbed-dev library. Release version 159.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #include "drivers/SerialBase.h" |
<> | 160:d5399cc887bb | 17 | #include "platform/mbed_wait_api.h" |
<> | 160:d5399cc887bb | 18 | #include "platform/mbed_critical.h" |
AnnaBridge | 174:b96e65c34a4d | 19 | #include "platform/mbed_sleep.h" |
<> | 149:156823d33999 | 20 | |
<> | 149:156823d33999 | 21 | #if DEVICE_SERIAL |
<> | 149:156823d33999 | 22 | |
<> | 149:156823d33999 | 23 | namespace mbed { |
<> | 149:156823d33999 | 24 | |
<> | 149:156823d33999 | 25 | SerialBase::SerialBase(PinName tx, PinName rx, int baud) : |
<> | 149:156823d33999 | 26 | #if DEVICE_SERIAL_ASYNCH |
<> | 149:156823d33999 | 27 | _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER), |
AnnaBridge | 174:b96e65c34a4d | 28 | _rx_usage(DMA_USAGE_NEVER), _tx_callback(NULL), |
AnnaBridge | 174:b96e65c34a4d | 29 | _rx_callback(NULL), |
<> | 149:156823d33999 | 30 | #endif |
<> | 149:156823d33999 | 31 | _serial(), _baud(baud) { |
<> | 149:156823d33999 | 32 | // No lock needed in the constructor |
<> | 149:156823d33999 | 33 | |
<> | 149:156823d33999 | 34 | for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { |
AnnaBridge | 174:b96e65c34a4d | 35 | _irq[i] = NULL; |
<> | 149:156823d33999 | 36 | } |
<> | 149:156823d33999 | 37 | |
<> | 149:156823d33999 | 38 | serial_init(&_serial, tx, rx); |
<> | 149:156823d33999 | 39 | serial_baud(&_serial, _baud); |
<> | 149:156823d33999 | 40 | serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this); |
<> | 149:156823d33999 | 41 | } |
<> | 149:156823d33999 | 42 | |
<> | 149:156823d33999 | 43 | void SerialBase::baud(int baudrate) { |
<> | 149:156823d33999 | 44 | lock(); |
<> | 149:156823d33999 | 45 | serial_baud(&_serial, baudrate); |
<> | 149:156823d33999 | 46 | _baud = baudrate; |
<> | 149:156823d33999 | 47 | unlock(); |
<> | 149:156823d33999 | 48 | } |
<> | 149:156823d33999 | 49 | |
<> | 149:156823d33999 | 50 | void SerialBase::format(int bits, Parity parity, int stop_bits) { |
<> | 149:156823d33999 | 51 | lock(); |
<> | 149:156823d33999 | 52 | serial_format(&_serial, bits, (SerialParity)parity, stop_bits); |
<> | 149:156823d33999 | 53 | unlock(); |
<> | 149:156823d33999 | 54 | } |
<> | 149:156823d33999 | 55 | |
<> | 149:156823d33999 | 56 | int SerialBase::readable() { |
<> | 149:156823d33999 | 57 | lock(); |
<> | 149:156823d33999 | 58 | int ret = serial_readable(&_serial); |
<> | 149:156823d33999 | 59 | unlock(); |
<> | 149:156823d33999 | 60 | return ret; |
<> | 149:156823d33999 | 61 | } |
<> | 149:156823d33999 | 62 | |
<> | 149:156823d33999 | 63 | |
<> | 149:156823d33999 | 64 | int SerialBase::writeable() { |
<> | 149:156823d33999 | 65 | lock(); |
<> | 149:156823d33999 | 66 | int ret = serial_writable(&_serial); |
<> | 149:156823d33999 | 67 | unlock(); |
<> | 149:156823d33999 | 68 | return ret; |
<> | 149:156823d33999 | 69 | } |
<> | 149:156823d33999 | 70 | |
<> | 149:156823d33999 | 71 | void SerialBase::attach(Callback<void()> func, IrqType type) { |
<> | 149:156823d33999 | 72 | lock(); |
<> | 149:156823d33999 | 73 | // Disable interrupts when attaching interrupt handler |
<> | 149:156823d33999 | 74 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 75 | if (func) { |
AnnaBridge | 174:b96e65c34a4d | 76 | // lock deep sleep only the first time |
AnnaBridge | 174:b96e65c34a4d | 77 | if (!_irq[type]) { |
AnnaBridge | 174:b96e65c34a4d | 78 | sleep_manager_lock_deep_sleep(); |
AnnaBridge | 174:b96e65c34a4d | 79 | } |
<> | 160:d5399cc887bb | 80 | _irq[type] = func; |
<> | 149:156823d33999 | 81 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
<> | 149:156823d33999 | 82 | } else { |
AnnaBridge | 174:b96e65c34a4d | 83 | // unlock deep sleep only the first time |
AnnaBridge | 174:b96e65c34a4d | 84 | if (_irq[type]) { |
AnnaBridge | 174:b96e65c34a4d | 85 | sleep_manager_unlock_deep_sleep(); |
AnnaBridge | 174:b96e65c34a4d | 86 | } |
AnnaBridge | 174:b96e65c34a4d | 87 | _irq[type] = NULL; |
<> | 149:156823d33999 | 88 | serial_irq_set(&_serial, (SerialIrq)type, 0); |
<> | 149:156823d33999 | 89 | } |
<> | 149:156823d33999 | 90 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 91 | unlock(); |
<> | 149:156823d33999 | 92 | } |
<> | 149:156823d33999 | 93 | |
<> | 149:156823d33999 | 94 | void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) { |
<> | 149:156823d33999 | 95 | SerialBase *handler = (SerialBase*)id; |
AnnaBridge | 174:b96e65c34a4d | 96 | if (handler->_irq[irq_type]) { |
AnnaBridge | 174:b96e65c34a4d | 97 | handler->_irq[irq_type](); |
AnnaBridge | 174:b96e65c34a4d | 98 | } |
<> | 149:156823d33999 | 99 | } |
<> | 149:156823d33999 | 100 | |
<> | 149:156823d33999 | 101 | int SerialBase::_base_getc() { |
<> | 149:156823d33999 | 102 | // Mutex is already held |
<> | 149:156823d33999 | 103 | return serial_getc(&_serial); |
<> | 149:156823d33999 | 104 | } |
<> | 149:156823d33999 | 105 | |
<> | 149:156823d33999 | 106 | int SerialBase::_base_putc(int c) { |
<> | 149:156823d33999 | 107 | // Mutex is already held |
<> | 149:156823d33999 | 108 | serial_putc(&_serial, c); |
<> | 149:156823d33999 | 109 | return c; |
<> | 149:156823d33999 | 110 | } |
<> | 149:156823d33999 | 111 | |
<> | 149:156823d33999 | 112 | void SerialBase::send_break() { |
<> | 149:156823d33999 | 113 | lock(); |
<> | 149:156823d33999 | 114 | // Wait for 1.5 frames before clearing the break condition |
<> | 149:156823d33999 | 115 | // This will have different effects on our platforms, but should |
<> | 149:156823d33999 | 116 | // ensure that we keep the break active for at least one frame. |
<> | 149:156823d33999 | 117 | // We consider a full frame (1 start bit + 8 data bits bits + |
<> | 149:156823d33999 | 118 | // 1 parity bit + 2 stop bits = 12 bits) for computation. |
<> | 149:156823d33999 | 119 | // One bit time (in us) = 1000000/_baud |
<> | 149:156823d33999 | 120 | // Twelve bits: 12000000/baud delay |
<> | 149:156823d33999 | 121 | // 1.5 frames: 18000000/baud delay |
<> | 149:156823d33999 | 122 | serial_break_set(&_serial); |
<> | 149:156823d33999 | 123 | wait_us(18000000/_baud); |
<> | 149:156823d33999 | 124 | serial_break_clear(&_serial); |
<> | 149:156823d33999 | 125 | unlock(); |
<> | 149:156823d33999 | 126 | } |
<> | 149:156823d33999 | 127 | |
<> | 149:156823d33999 | 128 | void SerialBase::lock() { |
<> | 149:156823d33999 | 129 | // Stub |
<> | 149:156823d33999 | 130 | } |
<> | 149:156823d33999 | 131 | |
<> | 149:156823d33999 | 132 | void SerialBase:: unlock() { |
<> | 149:156823d33999 | 133 | // Stub |
<> | 149:156823d33999 | 134 | } |
<> | 149:156823d33999 | 135 | |
AnnaBridge | 175:af195413fb11 | 136 | SerialBase::~SerialBase() |
AnnaBridge | 175:af195413fb11 | 137 | { |
AnnaBridge | 175:af195413fb11 | 138 | // No lock needed in destructor |
AnnaBridge | 175:af195413fb11 | 139 | |
AnnaBridge | 175:af195413fb11 | 140 | // Detaching interrupts releases the sleep lock if it was locked |
AnnaBridge | 175:af195413fb11 | 141 | for (int irq = 0; irq < IrqCnt; irq++) { |
AnnaBridge | 175:af195413fb11 | 142 | attach(NULL, (IrqType)irq); |
AnnaBridge | 175:af195413fb11 | 143 | } |
AnnaBridge | 175:af195413fb11 | 144 | } |
AnnaBridge | 175:af195413fb11 | 145 | |
<> | 149:156823d33999 | 146 | #if DEVICE_SERIAL_FC |
<> | 149:156823d33999 | 147 | void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) { |
<> | 149:156823d33999 | 148 | lock(); |
<> | 149:156823d33999 | 149 | FlowControl flow_type = (FlowControl)type; |
<> | 149:156823d33999 | 150 | switch(type) { |
<> | 149:156823d33999 | 151 | case RTS: |
<> | 149:156823d33999 | 152 | serial_set_flow_control(&_serial, flow_type, flow1, NC); |
<> | 149:156823d33999 | 153 | break; |
<> | 149:156823d33999 | 154 | |
<> | 149:156823d33999 | 155 | case CTS: |
<> | 149:156823d33999 | 156 | serial_set_flow_control(&_serial, flow_type, NC, flow1); |
<> | 149:156823d33999 | 157 | break; |
<> | 149:156823d33999 | 158 | |
<> | 149:156823d33999 | 159 | case RTSCTS: |
<> | 149:156823d33999 | 160 | case Disabled: |
<> | 149:156823d33999 | 161 | serial_set_flow_control(&_serial, flow_type, flow1, flow2); |
<> | 149:156823d33999 | 162 | break; |
<> | 149:156823d33999 | 163 | |
<> | 149:156823d33999 | 164 | default: |
<> | 149:156823d33999 | 165 | break; |
<> | 149:156823d33999 | 166 | } |
<> | 149:156823d33999 | 167 | unlock(); |
<> | 149:156823d33999 | 168 | } |
<> | 149:156823d33999 | 169 | #endif |
<> | 149:156823d33999 | 170 | |
<> | 149:156823d33999 | 171 | #if DEVICE_SERIAL_ASYNCH |
<> | 149:156823d33999 | 172 | |
<> | 149:156823d33999 | 173 | int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t& callback, int event) |
<> | 149:156823d33999 | 174 | { |
<> | 149:156823d33999 | 175 | if (serial_tx_active(&_serial)) { |
<> | 149:156823d33999 | 176 | return -1; // transaction ongoing |
<> | 149:156823d33999 | 177 | } |
<> | 149:156823d33999 | 178 | start_write((void *)buffer, length, 8, callback, event); |
<> | 149:156823d33999 | 179 | return 0; |
<> | 149:156823d33999 | 180 | } |
<> | 149:156823d33999 | 181 | |
<> | 149:156823d33999 | 182 | int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t& callback, int event) |
<> | 149:156823d33999 | 183 | { |
<> | 149:156823d33999 | 184 | if (serial_tx_active(&_serial)) { |
<> | 149:156823d33999 | 185 | return -1; // transaction ongoing |
<> | 149:156823d33999 | 186 | } |
<> | 149:156823d33999 | 187 | start_write((void *)buffer, length, 16, callback, event); |
<> | 149:156823d33999 | 188 | return 0; |
<> | 149:156823d33999 | 189 | } |
<> | 149:156823d33999 | 190 | |
<> | 149:156823d33999 | 191 | void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event) |
<> | 149:156823d33999 | 192 | { |
<> | 149:156823d33999 | 193 | _tx_callback = callback; |
<> | 149:156823d33999 | 194 | |
<> | 149:156823d33999 | 195 | _thunk_irq.callback(&SerialBase::interrupt_handler_asynch); |
AnnaBridge | 174:b96e65c34a4d | 196 | sleep_manager_lock_deep_sleep(); |
<> | 149:156823d33999 | 197 | serial_tx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, _tx_usage); |
<> | 149:156823d33999 | 198 | } |
<> | 149:156823d33999 | 199 | |
<> | 149:156823d33999 | 200 | void SerialBase::abort_write(void) |
<> | 149:156823d33999 | 201 | { |
AnnaBridge | 174:b96e65c34a4d | 202 | // rx might still be active |
AnnaBridge | 174:b96e65c34a4d | 203 | if (_rx_callback) { |
AnnaBridge | 174:b96e65c34a4d | 204 | sleep_manager_unlock_deep_sleep(); |
AnnaBridge | 174:b96e65c34a4d | 205 | } |
AnnaBridge | 174:b96e65c34a4d | 206 | _tx_callback = NULL; |
<> | 149:156823d33999 | 207 | serial_tx_abort_asynch(&_serial); |
<> | 149:156823d33999 | 208 | } |
<> | 149:156823d33999 | 209 | |
<> | 149:156823d33999 | 210 | void SerialBase::abort_read(void) |
<> | 149:156823d33999 | 211 | { |
AnnaBridge | 174:b96e65c34a4d | 212 | // tx might still be active |
AnnaBridge | 174:b96e65c34a4d | 213 | if (_tx_callback) { |
AnnaBridge | 174:b96e65c34a4d | 214 | sleep_manager_unlock_deep_sleep(); |
AnnaBridge | 174:b96e65c34a4d | 215 | } |
AnnaBridge | 174:b96e65c34a4d | 216 | _rx_callback = NULL; |
<> | 149:156823d33999 | 217 | serial_rx_abort_asynch(&_serial); |
<> | 149:156823d33999 | 218 | } |
<> | 149:156823d33999 | 219 | |
<> | 149:156823d33999 | 220 | int SerialBase::set_dma_usage_tx(DMAUsage usage) |
<> | 149:156823d33999 | 221 | { |
<> | 149:156823d33999 | 222 | if (serial_tx_active(&_serial)) { |
<> | 149:156823d33999 | 223 | return -1; |
<> | 149:156823d33999 | 224 | } |
<> | 149:156823d33999 | 225 | _tx_usage = usage; |
<> | 149:156823d33999 | 226 | return 0; |
<> | 149:156823d33999 | 227 | } |
<> | 149:156823d33999 | 228 | |
<> | 149:156823d33999 | 229 | int SerialBase::set_dma_usage_rx(DMAUsage usage) |
<> | 149:156823d33999 | 230 | { |
<> | 149:156823d33999 | 231 | if (serial_tx_active(&_serial)) { |
<> | 149:156823d33999 | 232 | return -1; |
<> | 149:156823d33999 | 233 | } |
<> | 149:156823d33999 | 234 | _rx_usage = usage; |
<> | 149:156823d33999 | 235 | return 0; |
<> | 149:156823d33999 | 236 | } |
<> | 149:156823d33999 | 237 | |
<> | 149:156823d33999 | 238 | int SerialBase::read(uint8_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match) |
<> | 149:156823d33999 | 239 | { |
<> | 149:156823d33999 | 240 | if (serial_rx_active(&_serial)) { |
<> | 149:156823d33999 | 241 | return -1; // transaction ongoing |
<> | 149:156823d33999 | 242 | } |
<> | 149:156823d33999 | 243 | start_read((void*)buffer, length, 8, callback, event, char_match); |
<> | 149:156823d33999 | 244 | return 0; |
<> | 149:156823d33999 | 245 | } |
<> | 149:156823d33999 | 246 | |
<> | 149:156823d33999 | 247 | |
<> | 149:156823d33999 | 248 | int SerialBase::read(uint16_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match) |
<> | 149:156823d33999 | 249 | { |
<> | 149:156823d33999 | 250 | if (serial_rx_active(&_serial)) { |
<> | 149:156823d33999 | 251 | return -1; // transaction ongoing |
<> | 149:156823d33999 | 252 | } |
<> | 149:156823d33999 | 253 | start_read((void*)buffer, length, 16, callback, event, char_match); |
<> | 149:156823d33999 | 254 | return 0; |
<> | 149:156823d33999 | 255 | } |
<> | 149:156823d33999 | 256 | |
<> | 149:156823d33999 | 257 | |
<> | 149:156823d33999 | 258 | void SerialBase::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match) |
<> | 149:156823d33999 | 259 | { |
<> | 149:156823d33999 | 260 | _rx_callback = callback; |
<> | 149:156823d33999 | 261 | _thunk_irq.callback(&SerialBase::interrupt_handler_asynch); |
AnnaBridge | 174:b96e65c34a4d | 262 | sleep_manager_lock_deep_sleep(); |
<> | 149:156823d33999 | 263 | serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, char_match, _rx_usage); |
<> | 149:156823d33999 | 264 | } |
<> | 149:156823d33999 | 265 | |
<> | 149:156823d33999 | 266 | void SerialBase::interrupt_handler_asynch(void) |
<> | 149:156823d33999 | 267 | { |
<> | 149:156823d33999 | 268 | int event = serial_irq_handler_asynch(&_serial); |
<> | 149:156823d33999 | 269 | int rx_event = event & SERIAL_EVENT_RX_MASK; |
AnnaBridge | 174:b96e65c34a4d | 270 | bool unlock_deepsleep = false; |
AnnaBridge | 174:b96e65c34a4d | 271 | |
<> | 149:156823d33999 | 272 | if (_rx_callback && rx_event) { |
AnnaBridge | 174:b96e65c34a4d | 273 | unlock_deepsleep = true; |
<> | 149:156823d33999 | 274 | _rx_callback.call(rx_event); |
<> | 149:156823d33999 | 275 | } |
<> | 149:156823d33999 | 276 | |
<> | 149:156823d33999 | 277 | int tx_event = event & SERIAL_EVENT_TX_MASK; |
<> | 149:156823d33999 | 278 | if (_tx_callback && tx_event) { |
AnnaBridge | 174:b96e65c34a4d | 279 | unlock_deepsleep = true; |
<> | 149:156823d33999 | 280 | _tx_callback.call(tx_event); |
<> | 149:156823d33999 | 281 | } |
AnnaBridge | 174:b96e65c34a4d | 282 | // unlock if tx or rx events are generated |
AnnaBridge | 174:b96e65c34a4d | 283 | if (unlock_deepsleep) { |
AnnaBridge | 174:b96e65c34a4d | 284 | sleep_manager_unlock_deep_sleep(); |
AnnaBridge | 174:b96e65c34a4d | 285 | } |
<> | 149:156823d33999 | 286 | } |
<> | 149:156823d33999 | 287 | |
<> | 149:156823d33999 | 288 | #endif |
<> | 149:156823d33999 | 289 | |
<> | 149:156823d33999 | 290 | } // namespace mbed |
<> | 149:156823d33999 | 291 | |
<> | 149:156823d33999 | 292 | #endif |