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