t

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Mon Oct 02 15:33:19 2017 +0100
Revision:
174:b96e65c34a4d
Parent:
160:d5399cc887bb
Child:
175:af195413fb11
This updates the lib to the mbed lib v 152

Who changed what in which revision?

UserRevisionLine numberNew 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"
AnnaBridge 174:b96e65c34a4d 19 #include "platform/mbed_sleep.h"
<> 149:156823d33999 20
<> 149:156823d33999 21 #if DEVICE_SERIAL
<> 149:156823d33999 22
<> 149:156823d33999 23 namespace mbed {
<> 149:156823d33999 24
<> 149:156823d33999 25 SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
<> 149:156823d33999 26 #if DEVICE_SERIAL_ASYNCH
<> 149:156823d33999 27 _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
AnnaBridge 174:b96e65c34a4d 28 _rx_usage(DMA_USAGE_NEVER), _tx_callback(NULL),
AnnaBridge 174:b96e65c34a4d 29 _rx_callback(NULL),
<> 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++) {
AnnaBridge 174:b96e65c34a4d 35 _irq[i] = NULL;
<> 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) {
AnnaBridge 174:b96e65c34a4d 76 // lock deep sleep only the first time
AnnaBridge 174:b96e65c34a4d 77 if (!_irq[type]) {
AnnaBridge 174:b96e65c34a4d 78 sleep_manager_lock_deep_sleep();
AnnaBridge 174:b96e65c34a4d 79 }
<> 160:d5399cc887bb 80 _irq[type] = func;
<> 149:156823d33999 81 serial_irq_set(&_serial, (SerialIrq)type, 1);
<> 149:156823d33999 82 } else {
AnnaBridge 174:b96e65c34a4d 83 // unlock deep sleep only the first time
AnnaBridge 174:b96e65c34a4d 84 if (_irq[type]) {
AnnaBridge 174:b96e65c34a4d 85 sleep_manager_unlock_deep_sleep();
AnnaBridge 174:b96e65c34a4d 86 }
AnnaBridge 174:b96e65c34a4d 87 _irq[type] = NULL;
<> 149:156823d33999 88 serial_irq_set(&_serial, (SerialIrq)type, 0);
<> 149:156823d33999 89 }
<> 149:156823d33999 90 core_util_critical_section_exit();
<> 149:156823d33999 91 unlock();
<> 149:156823d33999 92 }
<> 149:156823d33999 93
<> 149:156823d33999 94 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) {
<> 149:156823d33999 95 SerialBase *handler = (SerialBase*)id;
AnnaBridge 174:b96e65c34a4d 96 if (handler->_irq[irq_type]) {
AnnaBridge 174:b96e65c34a4d 97 handler->_irq[irq_type]();
AnnaBridge 174:b96e65c34a4d 98 }
<> 149:156823d33999 99 }
<> 149:156823d33999 100
<> 149:156823d33999 101 int SerialBase::_base_getc() {
<> 149:156823d33999 102 // Mutex is already held
<> 149:156823d33999 103 return serial_getc(&_serial);
<> 149:156823d33999 104 }
<> 149:156823d33999 105
<> 149:156823d33999 106 int SerialBase::_base_putc(int c) {
<> 149:156823d33999 107 // Mutex is already held
<> 149:156823d33999 108 serial_putc(&_serial, c);
<> 149:156823d33999 109 return c;
<> 149:156823d33999 110 }
<> 149:156823d33999 111
<> 149:156823d33999 112 void SerialBase::send_break() {
<> 149:156823d33999 113 lock();
<> 149:156823d33999 114 // Wait for 1.5 frames before clearing the break condition
<> 149:156823d33999 115 // This will have different effects on our platforms, but should
<> 149:156823d33999 116 // ensure that we keep the break active for at least one frame.
<> 149:156823d33999 117 // We consider a full frame (1 start bit + 8 data bits bits +
<> 149:156823d33999 118 // 1 parity bit + 2 stop bits = 12 bits) for computation.
<> 149:156823d33999 119 // One bit time (in us) = 1000000/_baud
<> 149:156823d33999 120 // Twelve bits: 12000000/baud delay
<> 149:156823d33999 121 // 1.5 frames: 18000000/baud delay
<> 149:156823d33999 122 serial_break_set(&_serial);
<> 149:156823d33999 123 wait_us(18000000/_baud);
<> 149:156823d33999 124 serial_break_clear(&_serial);
<> 149:156823d33999 125 unlock();
<> 149:156823d33999 126 }
<> 149:156823d33999 127
<> 149:156823d33999 128 void SerialBase::lock() {
<> 149:156823d33999 129 // Stub
<> 149:156823d33999 130 }
<> 149:156823d33999 131
<> 149:156823d33999 132 void SerialBase:: unlock() {
<> 149:156823d33999 133 // Stub
<> 149:156823d33999 134 }
<> 149:156823d33999 135
<> 149:156823d33999 136 #if DEVICE_SERIAL_FC
<> 149:156823d33999 137 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
<> 149:156823d33999 138 lock();
<> 149:156823d33999 139 FlowControl flow_type = (FlowControl)type;
<> 149:156823d33999 140 switch(type) {
<> 149:156823d33999 141 case RTS:
<> 149:156823d33999 142 serial_set_flow_control(&_serial, flow_type, flow1, NC);
<> 149:156823d33999 143 break;
<> 149:156823d33999 144
<> 149:156823d33999 145 case CTS:
<> 149:156823d33999 146 serial_set_flow_control(&_serial, flow_type, NC, flow1);
<> 149:156823d33999 147 break;
<> 149:156823d33999 148
<> 149:156823d33999 149 case RTSCTS:
<> 149:156823d33999 150 case Disabled:
<> 149:156823d33999 151 serial_set_flow_control(&_serial, flow_type, flow1, flow2);
<> 149:156823d33999 152 break;
<> 149:156823d33999 153
<> 149:156823d33999 154 default:
<> 149:156823d33999 155 break;
<> 149:156823d33999 156 }
<> 149:156823d33999 157 unlock();
<> 149:156823d33999 158 }
<> 149:156823d33999 159 #endif
<> 149:156823d33999 160
<> 149:156823d33999 161 #if DEVICE_SERIAL_ASYNCH
<> 149:156823d33999 162
<> 149:156823d33999 163 int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t& callback, int event)
<> 149:156823d33999 164 {
<> 149:156823d33999 165 if (serial_tx_active(&_serial)) {
<> 149:156823d33999 166 return -1; // transaction ongoing
<> 149:156823d33999 167 }
<> 149:156823d33999 168 start_write((void *)buffer, length, 8, callback, event);
<> 149:156823d33999 169 return 0;
<> 149:156823d33999 170 }
<> 149:156823d33999 171
<> 149:156823d33999 172 int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t& callback, int event)
<> 149:156823d33999 173 {
<> 149:156823d33999 174 if (serial_tx_active(&_serial)) {
<> 149:156823d33999 175 return -1; // transaction ongoing
<> 149:156823d33999 176 }
<> 149:156823d33999 177 start_write((void *)buffer, length, 16, callback, event);
<> 149:156823d33999 178 return 0;
<> 149:156823d33999 179 }
<> 149:156823d33999 180
<> 149:156823d33999 181 void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event)
<> 149:156823d33999 182 {
<> 149:156823d33999 183 _tx_callback = callback;
<> 149:156823d33999 184
<> 149:156823d33999 185 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
AnnaBridge 174:b96e65c34a4d 186 sleep_manager_lock_deep_sleep();
<> 149:156823d33999 187 serial_tx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, _tx_usage);
<> 149:156823d33999 188 }
<> 149:156823d33999 189
<> 149:156823d33999 190 void SerialBase::abort_write(void)
<> 149:156823d33999 191 {
AnnaBridge 174:b96e65c34a4d 192 // rx might still be active
AnnaBridge 174:b96e65c34a4d 193 if (_rx_callback) {
AnnaBridge 174:b96e65c34a4d 194 sleep_manager_unlock_deep_sleep();
AnnaBridge 174:b96e65c34a4d 195 }
AnnaBridge 174:b96e65c34a4d 196 _tx_callback = NULL;
<> 149:156823d33999 197 serial_tx_abort_asynch(&_serial);
<> 149:156823d33999 198 }
<> 149:156823d33999 199
<> 149:156823d33999 200 void SerialBase::abort_read(void)
<> 149:156823d33999 201 {
AnnaBridge 174:b96e65c34a4d 202 // tx might still be active
AnnaBridge 174:b96e65c34a4d 203 if (_tx_callback) {
AnnaBridge 174:b96e65c34a4d 204 sleep_manager_unlock_deep_sleep();
AnnaBridge 174:b96e65c34a4d 205 }
AnnaBridge 174:b96e65c34a4d 206 _rx_callback = NULL;
<> 149:156823d33999 207 serial_rx_abort_asynch(&_serial);
<> 149:156823d33999 208 }
<> 149:156823d33999 209
<> 149:156823d33999 210 int SerialBase::set_dma_usage_tx(DMAUsage usage)
<> 149:156823d33999 211 {
<> 149:156823d33999 212 if (serial_tx_active(&_serial)) {
<> 149:156823d33999 213 return -1;
<> 149:156823d33999 214 }
<> 149:156823d33999 215 _tx_usage = usage;
<> 149:156823d33999 216 return 0;
<> 149:156823d33999 217 }
<> 149:156823d33999 218
<> 149:156823d33999 219 int SerialBase::set_dma_usage_rx(DMAUsage usage)
<> 149:156823d33999 220 {
<> 149:156823d33999 221 if (serial_tx_active(&_serial)) {
<> 149:156823d33999 222 return -1;
<> 149:156823d33999 223 }
<> 149:156823d33999 224 _rx_usage = usage;
<> 149:156823d33999 225 return 0;
<> 149:156823d33999 226 }
<> 149:156823d33999 227
<> 149:156823d33999 228 int SerialBase::read(uint8_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match)
<> 149:156823d33999 229 {
<> 149:156823d33999 230 if (serial_rx_active(&_serial)) {
<> 149:156823d33999 231 return -1; // transaction ongoing
<> 149:156823d33999 232 }
<> 149:156823d33999 233 start_read((void*)buffer, length, 8, callback, event, char_match);
<> 149:156823d33999 234 return 0;
<> 149:156823d33999 235 }
<> 149:156823d33999 236
<> 149:156823d33999 237
<> 149:156823d33999 238 int SerialBase::read(uint16_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match)
<> 149:156823d33999 239 {
<> 149:156823d33999 240 if (serial_rx_active(&_serial)) {
<> 149:156823d33999 241 return -1; // transaction ongoing
<> 149:156823d33999 242 }
<> 149:156823d33999 243 start_read((void*)buffer, length, 16, callback, event, char_match);
<> 149:156823d33999 244 return 0;
<> 149:156823d33999 245 }
<> 149:156823d33999 246
<> 149:156823d33999 247
<> 149:156823d33999 248 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 249 {
<> 149:156823d33999 250 _rx_callback = callback;
<> 149:156823d33999 251 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
AnnaBridge 174:b96e65c34a4d 252 sleep_manager_lock_deep_sleep();
<> 149:156823d33999 253 serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, char_match, _rx_usage);
<> 149:156823d33999 254 }
<> 149:156823d33999 255
<> 149:156823d33999 256 void SerialBase::interrupt_handler_asynch(void)
<> 149:156823d33999 257 {
<> 149:156823d33999 258 int event = serial_irq_handler_asynch(&_serial);
<> 149:156823d33999 259 int rx_event = event & SERIAL_EVENT_RX_MASK;
AnnaBridge 174:b96e65c34a4d 260 bool unlock_deepsleep = false;
AnnaBridge 174:b96e65c34a4d 261
<> 149:156823d33999 262 if (_rx_callback && rx_event) {
AnnaBridge 174:b96e65c34a4d 263 unlock_deepsleep = true;
<> 149:156823d33999 264 _rx_callback.call(rx_event);
<> 149:156823d33999 265 }
<> 149:156823d33999 266
<> 149:156823d33999 267 int tx_event = event & SERIAL_EVENT_TX_MASK;
<> 149:156823d33999 268 if (_tx_callback && tx_event) {
AnnaBridge 174:b96e65c34a4d 269 unlock_deepsleep = true;
<> 149:156823d33999 270 _tx_callback.call(tx_event);
<> 149:156823d33999 271 }
AnnaBridge 174:b96e65c34a4d 272 // unlock if tx or rx events are generated
AnnaBridge 174:b96e65c34a4d 273 if (unlock_deepsleep) {
AnnaBridge 174:b96e65c34a4d 274 sleep_manager_unlock_deep_sleep();
AnnaBridge 174:b96e65c34a4d 275 }
<> 149:156823d33999 276 }
<> 149:156823d33999 277
<> 149:156823d33999 278 #endif
<> 149:156823d33999 279
<> 149:156823d33999 280 } // namespace mbed
<> 149:156823d33999 281
<> 149:156823d33999 282 #endif