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