Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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