Daniel Vizcaya
/
04_RTOS_Embebidos
Entrega 3er corte - sistemas embebidos
mbed-os/drivers/RawSerial.cpp@1:fcdb45ee95b9, 2018-05-30 (annotated)
- Committer:
- Bethory
- Date:
- Wed May 30 04:46:28 2018 +0000
- Revision:
- 1:fcdb45ee95b9
- Parent:
- 0:6ad07c9019fd
Entrega Final
Who changed what in which revision?
User | Revision | Line number | New 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/RawSerial.h" |
Bethory | 0:6ad07c9019fd | 17 | #include "platform/mbed_wait_api.h" |
Bethory | 0:6ad07c9019fd | 18 | #include <stdio.h> |
Bethory | 0:6ad07c9019fd | 19 | #include <cstdarg> |
Bethory | 0:6ad07c9019fd | 20 | |
Bethory | 0:6ad07c9019fd | 21 | |
Bethory | 0:6ad07c9019fd | 22 | #if DEVICE_SERIAL |
Bethory | 0:6ad07c9019fd | 23 | |
Bethory | 0:6ad07c9019fd | 24 | #define STRING_STACK_LIMIT 120 |
Bethory | 0:6ad07c9019fd | 25 | |
Bethory | 0:6ad07c9019fd | 26 | namespace mbed { |
Bethory | 0:6ad07c9019fd | 27 | |
Bethory | 0:6ad07c9019fd | 28 | RawSerial::RawSerial(PinName tx, PinName rx, int baud) : SerialBase(tx, rx, baud) { |
Bethory | 0:6ad07c9019fd | 29 | // No lock needed in the constructor |
Bethory | 0:6ad07c9019fd | 30 | } |
Bethory | 0:6ad07c9019fd | 31 | |
Bethory | 0:6ad07c9019fd | 32 | int RawSerial::getc() { |
Bethory | 0:6ad07c9019fd | 33 | lock(); |
Bethory | 0:6ad07c9019fd | 34 | int ret = _base_getc(); |
Bethory | 0:6ad07c9019fd | 35 | unlock(); |
Bethory | 0:6ad07c9019fd | 36 | return ret; |
Bethory | 0:6ad07c9019fd | 37 | } |
Bethory | 0:6ad07c9019fd | 38 | |
Bethory | 0:6ad07c9019fd | 39 | int RawSerial::putc(int c) { |
Bethory | 0:6ad07c9019fd | 40 | lock(); |
Bethory | 0:6ad07c9019fd | 41 | int ret = _base_putc(c); |
Bethory | 0:6ad07c9019fd | 42 | unlock(); |
Bethory | 0:6ad07c9019fd | 43 | return ret; |
Bethory | 0:6ad07c9019fd | 44 | } |
Bethory | 0:6ad07c9019fd | 45 | |
Bethory | 0:6ad07c9019fd | 46 | int RawSerial::puts(const char *str) { |
Bethory | 0:6ad07c9019fd | 47 | lock(); |
Bethory | 0:6ad07c9019fd | 48 | while (*str) |
Bethory | 0:6ad07c9019fd | 49 | putc(*str ++); |
Bethory | 0:6ad07c9019fd | 50 | unlock(); |
Bethory | 0:6ad07c9019fd | 51 | return 0; |
Bethory | 0:6ad07c9019fd | 52 | } |
Bethory | 0:6ad07c9019fd | 53 | |
Bethory | 0:6ad07c9019fd | 54 | // Experimental support for printf in RawSerial. No Stream inheritance |
Bethory | 0:6ad07c9019fd | 55 | // means we can't call printf() directly, so we use sprintf() instead. |
Bethory | 0:6ad07c9019fd | 56 | // We only call malloc() for the sprintf() buffer if the buffer |
Bethory | 0:6ad07c9019fd | 57 | // length is above a certain threshold, otherwise we use just the stack. |
Bethory | 0:6ad07c9019fd | 58 | int RawSerial::printf(const char *format, ...) { |
Bethory | 0:6ad07c9019fd | 59 | lock(); |
Bethory | 0:6ad07c9019fd | 60 | std::va_list arg; |
Bethory | 0:6ad07c9019fd | 61 | va_start(arg, format); |
Bethory | 0:6ad07c9019fd | 62 | // ARMCC microlib does not properly handle a size of 0. |
Bethory | 0:6ad07c9019fd | 63 | // As a workaround supply a dummy buffer with a size of 1. |
Bethory | 0:6ad07c9019fd | 64 | char dummy_buf[1]; |
Bethory | 0:6ad07c9019fd | 65 | int len = vsnprintf(dummy_buf, sizeof(dummy_buf), format, arg); |
Bethory | 0:6ad07c9019fd | 66 | if (len < STRING_STACK_LIMIT) { |
Bethory | 0:6ad07c9019fd | 67 | char temp[STRING_STACK_LIMIT]; |
Bethory | 0:6ad07c9019fd | 68 | vsprintf(temp, format, arg); |
Bethory | 0:6ad07c9019fd | 69 | puts(temp); |
Bethory | 0:6ad07c9019fd | 70 | } else { |
Bethory | 0:6ad07c9019fd | 71 | char *temp = new char[len + 1]; |
Bethory | 0:6ad07c9019fd | 72 | vsprintf(temp, format, arg); |
Bethory | 0:6ad07c9019fd | 73 | puts(temp); |
Bethory | 0:6ad07c9019fd | 74 | delete[] temp; |
Bethory | 0:6ad07c9019fd | 75 | } |
Bethory | 0:6ad07c9019fd | 76 | va_end(arg); |
Bethory | 0:6ad07c9019fd | 77 | unlock(); |
Bethory | 0:6ad07c9019fd | 78 | return len; |
Bethory | 0:6ad07c9019fd | 79 | } |
Bethory | 0:6ad07c9019fd | 80 | |
Bethory | 0:6ad07c9019fd | 81 | /** Acquire exclusive access to this serial port |
Bethory | 0:6ad07c9019fd | 82 | */ |
Bethory | 0:6ad07c9019fd | 83 | void RawSerial::lock() { |
Bethory | 0:6ad07c9019fd | 84 | // No lock used - external synchronization required |
Bethory | 0:6ad07c9019fd | 85 | } |
Bethory | 0:6ad07c9019fd | 86 | |
Bethory | 0:6ad07c9019fd | 87 | /** Release exclusive access to this serial port |
Bethory | 0:6ad07c9019fd | 88 | */ |
Bethory | 0:6ad07c9019fd | 89 | void RawSerial::unlock() { |
Bethory | 0:6ad07c9019fd | 90 | // No lock used - external synchronization required |
Bethory | 0:6ad07c9019fd | 91 | } |
Bethory | 0:6ad07c9019fd | 92 | |
Bethory | 0:6ad07c9019fd | 93 | } // namespace mbed |
Bethory | 0:6ad07c9019fd | 94 | |
Bethory | 0:6ad07c9019fd | 95 | #endif |