Elijah Stanger-Jones / mbed-dev-f303
Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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