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