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