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