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