001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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