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