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