Lancaster University's (short term!) clone of mbed-src for micro:bit. This is a copy of the github branch https://github.com/lancaster-university/mbed-classic
Fork of mbed-src by
common/SerialBase.cpp@563:536c9fb088a0, 2015-06-10 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jun 10 10:00:08 2015 +0100
- Revision:
- 563:536c9fb088a0
- Parent:
- 525:c320967f86b9
Synchronized with git revision 4778e33fa11602202ae3a57ced5d026e8053cedf
Full URL: https://github.com/mbedmicro/mbed/commit/4778e33fa11602202ae3a57ced5d026e8053cedf/
Fix asynch methods constness
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 36:ab3ee77451e7 | 1 | /* mbed Microcontroller Library |
mbed_official | 36:ab3ee77451e7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 36:ab3ee77451e7 | 3 | * |
mbed_official | 36:ab3ee77451e7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 36:ab3ee77451e7 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 36:ab3ee77451e7 | 6 | * You may obtain a copy of the License at |
mbed_official | 36:ab3ee77451e7 | 7 | * |
mbed_official | 36:ab3ee77451e7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 36:ab3ee77451e7 | 9 | * |
mbed_official | 36:ab3ee77451e7 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 36:ab3ee77451e7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 36:ab3ee77451e7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 36:ab3ee77451e7 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 36:ab3ee77451e7 | 14 | * limitations under the License. |
mbed_official | 36:ab3ee77451e7 | 15 | */ |
mbed_official | 36:ab3ee77451e7 | 16 | #include "SerialBase.h" |
mbed_official | 36:ab3ee77451e7 | 17 | #include "wait_api.h" |
mbed_official | 36:ab3ee77451e7 | 18 | |
mbed_official | 36:ab3ee77451e7 | 19 | #if DEVICE_SERIAL |
mbed_official | 36:ab3ee77451e7 | 20 | |
mbed_official | 36:ab3ee77451e7 | 21 | namespace mbed { |
mbed_official | 36:ab3ee77451e7 | 22 | |
mbed_official | 525:c320967f86b9 | 23 | SerialBase::SerialBase(PinName tx, PinName rx) : |
mbed_official | 525:c320967f86b9 | 24 | #if DEVICE_SERIAL_ASYNCH |
mbed_official | 525:c320967f86b9 | 25 | _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER), |
mbed_official | 525:c320967f86b9 | 26 | _rx_usage(DMA_USAGE_NEVER), |
mbed_official | 525:c320967f86b9 | 27 | #endif |
mbed_official | 525:c320967f86b9 | 28 | _serial(), _baud(9600) { |
mbed_official | 36:ab3ee77451e7 | 29 | serial_init(&_serial, tx, rx); |
mbed_official | 36:ab3ee77451e7 | 30 | serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this); |
mbed_official | 36:ab3ee77451e7 | 31 | } |
mbed_official | 36:ab3ee77451e7 | 32 | |
mbed_official | 36:ab3ee77451e7 | 33 | void SerialBase::baud(int baudrate) { |
mbed_official | 36:ab3ee77451e7 | 34 | serial_baud(&_serial, baudrate); |
mbed_official | 36:ab3ee77451e7 | 35 | _baud = baudrate; |
mbed_official | 36:ab3ee77451e7 | 36 | } |
mbed_official | 36:ab3ee77451e7 | 37 | |
mbed_official | 36:ab3ee77451e7 | 38 | void SerialBase::format(int bits, Parity parity, int stop_bits) { |
mbed_official | 36:ab3ee77451e7 | 39 | serial_format(&_serial, bits, (SerialParity)parity, stop_bits); |
mbed_official | 36:ab3ee77451e7 | 40 | } |
mbed_official | 36:ab3ee77451e7 | 41 | |
mbed_official | 36:ab3ee77451e7 | 42 | int SerialBase::readable() { |
mbed_official | 36:ab3ee77451e7 | 43 | return serial_readable(&_serial); |
mbed_official | 36:ab3ee77451e7 | 44 | } |
mbed_official | 36:ab3ee77451e7 | 45 | |
mbed_official | 36:ab3ee77451e7 | 46 | |
mbed_official | 36:ab3ee77451e7 | 47 | int SerialBase::writeable() { |
mbed_official | 36:ab3ee77451e7 | 48 | return serial_writable(&_serial); |
mbed_official | 36:ab3ee77451e7 | 49 | } |
mbed_official | 36:ab3ee77451e7 | 50 | |
mbed_official | 36:ab3ee77451e7 | 51 | void SerialBase::attach(void (*fptr)(void), IrqType type) { |
mbed_official | 36:ab3ee77451e7 | 52 | if (fptr) { |
mbed_official | 36:ab3ee77451e7 | 53 | _irq[type].attach(fptr); |
mbed_official | 36:ab3ee77451e7 | 54 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
mbed_official | 36:ab3ee77451e7 | 55 | } else { |
mbed_official | 36:ab3ee77451e7 | 56 | serial_irq_set(&_serial, (SerialIrq)type, 0); |
mbed_official | 36:ab3ee77451e7 | 57 | } |
mbed_official | 36:ab3ee77451e7 | 58 | } |
mbed_official | 36:ab3ee77451e7 | 59 | |
mbed_official | 36:ab3ee77451e7 | 60 | void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) { |
mbed_official | 36:ab3ee77451e7 | 61 | SerialBase *handler = (SerialBase*)id; |
mbed_official | 36:ab3ee77451e7 | 62 | handler->_irq[irq_type].call(); |
mbed_official | 36:ab3ee77451e7 | 63 | } |
mbed_official | 36:ab3ee77451e7 | 64 | |
mbed_official | 36:ab3ee77451e7 | 65 | int SerialBase::_base_getc() { |
mbed_official | 36:ab3ee77451e7 | 66 | return serial_getc(&_serial); |
mbed_official | 36:ab3ee77451e7 | 67 | } |
mbed_official | 36:ab3ee77451e7 | 68 | |
mbed_official | 36:ab3ee77451e7 | 69 | int SerialBase::_base_putc(int c) { |
mbed_official | 36:ab3ee77451e7 | 70 | serial_putc(&_serial, c); |
mbed_official | 36:ab3ee77451e7 | 71 | return c; |
mbed_official | 36:ab3ee77451e7 | 72 | } |
mbed_official | 36:ab3ee77451e7 | 73 | |
mbed_official | 36:ab3ee77451e7 | 74 | void SerialBase::send_break() { |
mbed_official | 36:ab3ee77451e7 | 75 | // Wait for 1.5 frames before clearing the break condition |
mbed_official | 36:ab3ee77451e7 | 76 | // This will have different effects on our platforms, but should |
mbed_official | 36:ab3ee77451e7 | 77 | // ensure that we keep the break active for at least one frame. |
mbed_official | 212:34d62c0b2af6 | 78 | // We consider a full frame (1 start bit + 8 data bits bits + |
mbed_official | 36:ab3ee77451e7 | 79 | // 1 parity bit + 2 stop bits = 12 bits) for computation. |
mbed_official | 36:ab3ee77451e7 | 80 | // One bit time (in us) = 1000000/_baud |
mbed_official | 36:ab3ee77451e7 | 81 | // Twelve bits: 12000000/baud delay |
mbed_official | 36:ab3ee77451e7 | 82 | // 1.5 frames: 18000000/baud delay |
mbed_official | 36:ab3ee77451e7 | 83 | serial_break_set(&_serial); |
mbed_official | 36:ab3ee77451e7 | 84 | wait_us(18000000/_baud); |
mbed_official | 36:ab3ee77451e7 | 85 | serial_break_clear(&_serial); |
mbed_official | 36:ab3ee77451e7 | 86 | } |
mbed_official | 36:ab3ee77451e7 | 87 | |
mbed_official | 192:42b1a45e48aa | 88 | #if DEVICE_SERIAL_FC |
mbed_official | 64:7b352733b00a | 89 | void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) { |
mbed_official | 64:7b352733b00a | 90 | FlowControl flow_type = (FlowControl)type; |
mbed_official | 64:7b352733b00a | 91 | switch(type) { |
mbed_official | 64:7b352733b00a | 92 | case RTS: |
mbed_official | 64:7b352733b00a | 93 | serial_set_flow_control(&_serial, flow_type, flow1, NC); |
mbed_official | 64:7b352733b00a | 94 | break; |
mbed_official | 212:34d62c0b2af6 | 95 | |
mbed_official | 64:7b352733b00a | 96 | case CTS: |
mbed_official | 64:7b352733b00a | 97 | serial_set_flow_control(&_serial, flow_type, NC, flow1); |
mbed_official | 64:7b352733b00a | 98 | break; |
mbed_official | 212:34d62c0b2af6 | 99 | |
mbed_official | 64:7b352733b00a | 100 | case RTSCTS: |
mbed_official | 65:46be7cdf873f | 101 | case Disabled: |
mbed_official | 64:7b352733b00a | 102 | serial_set_flow_control(&_serial, flow_type, flow1, flow2); |
mbed_official | 64:7b352733b00a | 103 | break; |
mbed_official | 212:34d62c0b2af6 | 104 | |
mbed_official | 64:7b352733b00a | 105 | default: |
mbed_official | 64:7b352733b00a | 106 | break; |
mbed_official | 64:7b352733b00a | 107 | } |
mbed_official | 64:7b352733b00a | 108 | } |
mbed_official | 64:7b352733b00a | 109 | #endif |
mbed_official | 64:7b352733b00a | 110 | |
mbed_official | 525:c320967f86b9 | 111 | #if DEVICE_SERIAL_ASYNCH |
mbed_official | 525:c320967f86b9 | 112 | |
mbed_official | 563:536c9fb088a0 | 113 | int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t& callback, int event) |
mbed_official | 525:c320967f86b9 | 114 | { |
mbed_official | 525:c320967f86b9 | 115 | if (serial_tx_active(&_serial)) { |
mbed_official | 525:c320967f86b9 | 116 | return -1; // transaction ongoing |
mbed_official | 525:c320967f86b9 | 117 | } |
mbed_official | 525:c320967f86b9 | 118 | start_write((void *)buffer, length, 8, callback, event); |
mbed_official | 525:c320967f86b9 | 119 | return 0; |
mbed_official | 525:c320967f86b9 | 120 | } |
mbed_official | 525:c320967f86b9 | 121 | |
mbed_official | 563:536c9fb088a0 | 122 | int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t& callback, int event) |
mbed_official | 525:c320967f86b9 | 123 | { |
mbed_official | 525:c320967f86b9 | 124 | if (serial_tx_active(&_serial)) { |
mbed_official | 525:c320967f86b9 | 125 | return -1; // transaction ongoing |
mbed_official | 525:c320967f86b9 | 126 | } |
mbed_official | 525:c320967f86b9 | 127 | start_write((void *)buffer, length, 16, callback, event); |
mbed_official | 525:c320967f86b9 | 128 | return 0; |
mbed_official | 525:c320967f86b9 | 129 | } |
mbed_official | 525:c320967f86b9 | 130 | |
mbed_official | 563:536c9fb088a0 | 131 | void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event) |
mbed_official | 525:c320967f86b9 | 132 | { |
mbed_official | 525:c320967f86b9 | 133 | _tx_callback = callback; |
mbed_official | 525:c320967f86b9 | 134 | |
mbed_official | 525:c320967f86b9 | 135 | _thunk_irq.callback(&SerialBase::interrupt_handler_asynch); |
mbed_official | 525:c320967f86b9 | 136 | serial_tx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, _tx_usage); |
mbed_official | 525:c320967f86b9 | 137 | } |
mbed_official | 525:c320967f86b9 | 138 | |
mbed_official | 525:c320967f86b9 | 139 | void SerialBase::abort_write(void) |
mbed_official | 525:c320967f86b9 | 140 | { |
mbed_official | 525:c320967f86b9 | 141 | serial_tx_abort_asynch(&_serial); |
mbed_official | 525:c320967f86b9 | 142 | } |
mbed_official | 525:c320967f86b9 | 143 | |
mbed_official | 525:c320967f86b9 | 144 | void SerialBase::abort_read(void) |
mbed_official | 525:c320967f86b9 | 145 | { |
mbed_official | 525:c320967f86b9 | 146 | serial_rx_abort_asynch(&_serial); |
mbed_official | 525:c320967f86b9 | 147 | } |
mbed_official | 525:c320967f86b9 | 148 | |
mbed_official | 525:c320967f86b9 | 149 | int SerialBase::set_dma_usage_tx(DMAUsage usage) |
mbed_official | 525:c320967f86b9 | 150 | { |
mbed_official | 525:c320967f86b9 | 151 | if (serial_tx_active(&_serial)) { |
mbed_official | 525:c320967f86b9 | 152 | return -1; |
mbed_official | 525:c320967f86b9 | 153 | } |
mbed_official | 525:c320967f86b9 | 154 | _tx_usage = usage; |
mbed_official | 525:c320967f86b9 | 155 | return 0; |
mbed_official | 525:c320967f86b9 | 156 | } |
mbed_official | 525:c320967f86b9 | 157 | |
mbed_official | 525:c320967f86b9 | 158 | int SerialBase::set_dma_usage_rx(DMAUsage usage) |
mbed_official | 525:c320967f86b9 | 159 | { |
mbed_official | 525:c320967f86b9 | 160 | if (serial_tx_active(&_serial)) { |
mbed_official | 525:c320967f86b9 | 161 | return -1; |
mbed_official | 525:c320967f86b9 | 162 | } |
mbed_official | 525:c320967f86b9 | 163 | _rx_usage = usage; |
mbed_official | 525:c320967f86b9 | 164 | return 0; |
mbed_official | 525:c320967f86b9 | 165 | } |
mbed_official | 525:c320967f86b9 | 166 | |
mbed_official | 525:c320967f86b9 | 167 | int SerialBase::read(uint8_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match) |
mbed_official | 525:c320967f86b9 | 168 | { |
mbed_official | 525:c320967f86b9 | 169 | if (serial_rx_active(&_serial)) { |
mbed_official | 525:c320967f86b9 | 170 | return -1; // transaction ongoing |
mbed_official | 525:c320967f86b9 | 171 | } |
mbed_official | 525:c320967f86b9 | 172 | start_read((void*)buffer, length, 8, callback, event, char_match); |
mbed_official | 525:c320967f86b9 | 173 | return 0; |
mbed_official | 525:c320967f86b9 | 174 | } |
mbed_official | 525:c320967f86b9 | 175 | |
mbed_official | 525:c320967f86b9 | 176 | |
mbed_official | 525:c320967f86b9 | 177 | int SerialBase::read(uint16_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match) |
mbed_official | 525:c320967f86b9 | 178 | { |
mbed_official | 525:c320967f86b9 | 179 | if (serial_rx_active(&_serial)) { |
mbed_official | 525:c320967f86b9 | 180 | return -1; // transaction ongoing |
mbed_official | 525:c320967f86b9 | 181 | } |
mbed_official | 525:c320967f86b9 | 182 | start_read((void*)buffer, length, 16, callback, event, char_match); |
mbed_official | 525:c320967f86b9 | 183 | return 0; |
mbed_official | 525:c320967f86b9 | 184 | } |
mbed_official | 525:c320967f86b9 | 185 | |
mbed_official | 525:c320967f86b9 | 186 | |
mbed_official | 525:c320967f86b9 | 187 | void SerialBase::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match) |
mbed_official | 525:c320967f86b9 | 188 | { |
mbed_official | 525:c320967f86b9 | 189 | _rx_callback = callback; |
mbed_official | 525:c320967f86b9 | 190 | _thunk_irq.callback(&SerialBase::interrupt_handler_asynch); |
mbed_official | 525:c320967f86b9 | 191 | serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, char_match, _rx_usage); |
mbed_official | 525:c320967f86b9 | 192 | } |
mbed_official | 525:c320967f86b9 | 193 | |
mbed_official | 525:c320967f86b9 | 194 | void SerialBase::interrupt_handler_asynch(void) |
mbed_official | 525:c320967f86b9 | 195 | { |
mbed_official | 525:c320967f86b9 | 196 | int event = serial_irq_handler_asynch(&_serial); |
mbed_official | 525:c320967f86b9 | 197 | int rx_event = event & SERIAL_EVENT_RX_MASK; |
mbed_official | 525:c320967f86b9 | 198 | if (_rx_callback && rx_event) { |
mbed_official | 525:c320967f86b9 | 199 | _rx_callback.call(rx_event); |
mbed_official | 525:c320967f86b9 | 200 | } |
mbed_official | 525:c320967f86b9 | 201 | |
mbed_official | 525:c320967f86b9 | 202 | int tx_event = event & SERIAL_EVENT_TX_MASK; |
mbed_official | 525:c320967f86b9 | 203 | if (_tx_callback && tx_event) { |
mbed_official | 525:c320967f86b9 | 204 | _tx_callback.call(tx_event); |
mbed_official | 525:c320967f86b9 | 205 | } |
mbed_official | 525:c320967f86b9 | 206 | } |
mbed_official | 525:c320967f86b9 | 207 | |
mbed_official | 525:c320967f86b9 | 208 | #endif |
mbed_official | 525:c320967f86b9 | 209 | |
mbed_official | 36:ab3ee77451e7 | 210 | } // namespace mbed |
mbed_official | 36:ab3ee77451e7 | 211 | |
mbed_official | 36:ab3ee77451e7 | 212 | #endif |