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