Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

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