,,
Fork of Application by
mbed-dev/drivers/RawSerial.cpp@10:41552d038a69, 2017-01-10 (annotated)
- Committer:
- Zaitsev
- Date:
- Tue Jan 10 20:42:26 2017 +0000
- Revision:
- 10:41552d038a69
USB Serial bi-directional bridge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Zaitsev | 10:41552d038a69 | 1 | /* mbed Microcontroller Library |
Zaitsev | 10:41552d038a69 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Zaitsev | 10:41552d038a69 | 3 | * |
Zaitsev | 10:41552d038a69 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Zaitsev | 10:41552d038a69 | 5 | * you may not use this file except in compliance with the License. |
Zaitsev | 10:41552d038a69 | 6 | * You may obtain a copy of the License at |
Zaitsev | 10:41552d038a69 | 7 | * |
Zaitsev | 10:41552d038a69 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Zaitsev | 10:41552d038a69 | 9 | * |
Zaitsev | 10:41552d038a69 | 10 | * Unless required by applicable law or agreed to in writing, software |
Zaitsev | 10:41552d038a69 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Zaitsev | 10:41552d038a69 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Zaitsev | 10:41552d038a69 | 13 | * See the License for the specific language governing permissions and |
Zaitsev | 10:41552d038a69 | 14 | * limitations under the License. |
Zaitsev | 10:41552d038a69 | 15 | */ |
Zaitsev | 10:41552d038a69 | 16 | #include "drivers/RawSerial.h" |
Zaitsev | 10:41552d038a69 | 17 | #include "platform/wait_api.h" |
Zaitsev | 10:41552d038a69 | 18 | #include <cstdarg> |
Zaitsev | 10:41552d038a69 | 19 | |
Zaitsev | 10:41552d038a69 | 20 | #if DEVICE_SERIAL |
Zaitsev | 10:41552d038a69 | 21 | |
Zaitsev | 10:41552d038a69 | 22 | #define STRING_STACK_LIMIT 120 |
Zaitsev | 10:41552d038a69 | 23 | |
Zaitsev | 10:41552d038a69 | 24 | namespace mbed { |
Zaitsev | 10:41552d038a69 | 25 | |
Zaitsev | 10:41552d038a69 | 26 | RawSerial::RawSerial(PinName tx, PinName rx, int baud) : SerialBase(tx, rx, baud) { |
Zaitsev | 10:41552d038a69 | 27 | // No lock needed in the constructor |
Zaitsev | 10:41552d038a69 | 28 | } |
Zaitsev | 10:41552d038a69 | 29 | |
Zaitsev | 10:41552d038a69 | 30 | int RawSerial::getc() { |
Zaitsev | 10:41552d038a69 | 31 | lock(); |
Zaitsev | 10:41552d038a69 | 32 | int ret = _base_getc(); |
Zaitsev | 10:41552d038a69 | 33 | unlock(); |
Zaitsev | 10:41552d038a69 | 34 | return ret; |
Zaitsev | 10:41552d038a69 | 35 | } |
Zaitsev | 10:41552d038a69 | 36 | |
Zaitsev | 10:41552d038a69 | 37 | int RawSerial::putc(int c) { |
Zaitsev | 10:41552d038a69 | 38 | lock(); |
Zaitsev | 10:41552d038a69 | 39 | int ret = _base_putc(c); |
Zaitsev | 10:41552d038a69 | 40 | unlock(); |
Zaitsev | 10:41552d038a69 | 41 | return ret; |
Zaitsev | 10:41552d038a69 | 42 | } |
Zaitsev | 10:41552d038a69 | 43 | |
Zaitsev | 10:41552d038a69 | 44 | int RawSerial::puts(const char *str) { |
Zaitsev | 10:41552d038a69 | 45 | lock(); |
Zaitsev | 10:41552d038a69 | 46 | while (*str) |
Zaitsev | 10:41552d038a69 | 47 | putc(*str ++); |
Zaitsev | 10:41552d038a69 | 48 | unlock(); |
Zaitsev | 10:41552d038a69 | 49 | return 0; |
Zaitsev | 10:41552d038a69 | 50 | } |
Zaitsev | 10:41552d038a69 | 51 | |
Zaitsev | 10:41552d038a69 | 52 | // Experimental support for printf in RawSerial. No Stream inheritance |
Zaitsev | 10:41552d038a69 | 53 | // means we can't call printf() directly, so we use sprintf() instead. |
Zaitsev | 10:41552d038a69 | 54 | // We only call malloc() for the sprintf() buffer if the buffer |
Zaitsev | 10:41552d038a69 | 55 | // length is above a certain threshold, otherwise we use just the stack. |
Zaitsev | 10:41552d038a69 | 56 | int RawSerial::printf(const char *format, ...) { |
Zaitsev | 10:41552d038a69 | 57 | lock(); |
Zaitsev | 10:41552d038a69 | 58 | std::va_list arg; |
Zaitsev | 10:41552d038a69 | 59 | va_start(arg, format); |
Zaitsev | 10:41552d038a69 | 60 | // ARMCC microlib does not properly handle a size of 0. |
Zaitsev | 10:41552d038a69 | 61 | // As a workaround supply a dummy buffer with a size of 1. |
Zaitsev | 10:41552d038a69 | 62 | char dummy_buf[1]; |
Zaitsev | 10:41552d038a69 | 63 | int len = vsnprintf(dummy_buf, sizeof(dummy_buf), format, arg); |
Zaitsev | 10:41552d038a69 | 64 | if (len < STRING_STACK_LIMIT) { |
Zaitsev | 10:41552d038a69 | 65 | char temp[STRING_STACK_LIMIT]; |
Zaitsev | 10:41552d038a69 | 66 | vsprintf(temp, format, arg); |
Zaitsev | 10:41552d038a69 | 67 | puts(temp); |
Zaitsev | 10:41552d038a69 | 68 | } else { |
Zaitsev | 10:41552d038a69 | 69 | char *temp = new char[len + 1]; |
Zaitsev | 10:41552d038a69 | 70 | vsprintf(temp, format, arg); |
Zaitsev | 10:41552d038a69 | 71 | puts(temp); |
Zaitsev | 10:41552d038a69 | 72 | delete[] temp; |
Zaitsev | 10:41552d038a69 | 73 | } |
Zaitsev | 10:41552d038a69 | 74 | va_end(arg); |
Zaitsev | 10:41552d038a69 | 75 | unlock(); |
Zaitsev | 10:41552d038a69 | 76 | return len; |
Zaitsev | 10:41552d038a69 | 77 | } |
Zaitsev | 10:41552d038a69 | 78 | |
Zaitsev | 10:41552d038a69 | 79 | /** Acquire exclusive access to this serial port |
Zaitsev | 10:41552d038a69 | 80 | */ |
Zaitsev | 10:41552d038a69 | 81 | void RawSerial::lock() { |
Zaitsev | 10:41552d038a69 | 82 | // No lock used - external synchronization required |
Zaitsev | 10:41552d038a69 | 83 | } |
Zaitsev | 10:41552d038a69 | 84 | |
Zaitsev | 10:41552d038a69 | 85 | /** Release exclusive access to this serial port |
Zaitsev | 10:41552d038a69 | 86 | */ |
Zaitsev | 10:41552d038a69 | 87 | void RawSerial::unlock() { |
Zaitsev | 10:41552d038a69 | 88 | // No lock used - external synchronization required |
Zaitsev | 10:41552d038a69 | 89 | } |
Zaitsev | 10:41552d038a69 | 90 | |
Zaitsev | 10:41552d038a69 | 91 | } // namespace mbed |
Zaitsev | 10:41552d038a69 | 92 | |
Zaitsev | 10:41552d038a69 | 93 | #endif |