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