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