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