The code from https://github.com/vpcola/Nucleo
mbed-src/common/RawSerial.cpp@0:5464d5e415e5, 2014-10-08 (annotated)
- Committer:
- sinrab
- Date:
- Wed Oct 08 11:00:24 2014 +0000
- Revision:
- 0:5464d5e415e5
The code from https://github.com/vpcola/Nucleo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sinrab | 0:5464d5e415e5 | 1 | /* mbed Microcontroller Library |
sinrab | 0:5464d5e415e5 | 2 | * Copyright (c) 2006-2013 ARM Limited |
sinrab | 0:5464d5e415e5 | 3 | * |
sinrab | 0:5464d5e415e5 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sinrab | 0:5464d5e415e5 | 5 | * you may not use this file except in compliance with the License. |
sinrab | 0:5464d5e415e5 | 6 | * You may obtain a copy of the License at |
sinrab | 0:5464d5e415e5 | 7 | * |
sinrab | 0:5464d5e415e5 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sinrab | 0:5464d5e415e5 | 9 | * |
sinrab | 0:5464d5e415e5 | 10 | * Unless required by applicable law or agreed to in writing, software |
sinrab | 0:5464d5e415e5 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sinrab | 0:5464d5e415e5 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sinrab | 0:5464d5e415e5 | 13 | * See the License for the specific language governing permissions and |
sinrab | 0:5464d5e415e5 | 14 | * limitations under the License. |
sinrab | 0:5464d5e415e5 | 15 | */ |
sinrab | 0:5464d5e415e5 | 16 | #include "RawSerial.h" |
sinrab | 0:5464d5e415e5 | 17 | #include "wait_api.h" |
sinrab | 0:5464d5e415e5 | 18 | #include <cstdarg> |
sinrab | 0:5464d5e415e5 | 19 | |
sinrab | 0:5464d5e415e5 | 20 | #if DEVICE_SERIAL |
sinrab | 0:5464d5e415e5 | 21 | |
sinrab | 0:5464d5e415e5 | 22 | #define STRING_STACK_LIMIT 120 |
sinrab | 0:5464d5e415e5 | 23 | |
sinrab | 0:5464d5e415e5 | 24 | namespace mbed { |
sinrab | 0:5464d5e415e5 | 25 | |
sinrab | 0:5464d5e415e5 | 26 | RawSerial::RawSerial(PinName tx, PinName rx) : SerialBase(tx, rx) { |
sinrab | 0:5464d5e415e5 | 27 | } |
sinrab | 0:5464d5e415e5 | 28 | |
sinrab | 0:5464d5e415e5 | 29 | int RawSerial::getc() { |
sinrab | 0:5464d5e415e5 | 30 | return _base_getc(); |
sinrab | 0:5464d5e415e5 | 31 | } |
sinrab | 0:5464d5e415e5 | 32 | |
sinrab | 0:5464d5e415e5 | 33 | int RawSerial::putc(int c) { |
sinrab | 0:5464d5e415e5 | 34 | return _base_putc(c); |
sinrab | 0:5464d5e415e5 | 35 | } |
sinrab | 0:5464d5e415e5 | 36 | |
sinrab | 0:5464d5e415e5 | 37 | int RawSerial::puts(const char *str) { |
sinrab | 0:5464d5e415e5 | 38 | while (*str) |
sinrab | 0:5464d5e415e5 | 39 | putc(*str ++); |
sinrab | 0:5464d5e415e5 | 40 | return 0; |
sinrab | 0:5464d5e415e5 | 41 | } |
sinrab | 0:5464d5e415e5 | 42 | |
sinrab | 0:5464d5e415e5 | 43 | // Experimental support for printf in RawSerial. No Stream inheritance |
sinrab | 0:5464d5e415e5 | 44 | // means we can't call printf() directly, so we use sprintf() instead. |
sinrab | 0:5464d5e415e5 | 45 | // We only call malloc() for the sprintf() buffer if the buffer |
sinrab | 0:5464d5e415e5 | 46 | // length is above a certain threshold, otherwise we use just the stack. |
sinrab | 0:5464d5e415e5 | 47 | int RawSerial::printf(const char *format, ...) { |
sinrab | 0:5464d5e415e5 | 48 | std::va_list arg; |
sinrab | 0:5464d5e415e5 | 49 | va_start(arg, format); |
sinrab | 0:5464d5e415e5 | 50 | int len = vsnprintf(NULL, 0, format, arg); |
sinrab | 0:5464d5e415e5 | 51 | if (len < STRING_STACK_LIMIT) { |
sinrab | 0:5464d5e415e5 | 52 | char temp[STRING_STACK_LIMIT]; |
sinrab | 0:5464d5e415e5 | 53 | vsprintf(temp, format, arg); |
sinrab | 0:5464d5e415e5 | 54 | puts(temp); |
sinrab | 0:5464d5e415e5 | 55 | } else { |
sinrab | 0:5464d5e415e5 | 56 | char *temp = new char[len + 1]; |
sinrab | 0:5464d5e415e5 | 57 | vsprintf(temp, format, arg); |
sinrab | 0:5464d5e415e5 | 58 | puts(temp); |
sinrab | 0:5464d5e415e5 | 59 | delete[] temp; |
sinrab | 0:5464d5e415e5 | 60 | } |
sinrab | 0:5464d5e415e5 | 61 | va_end(arg); |
sinrab | 0:5464d5e415e5 | 62 | return len; |
sinrab | 0:5464d5e415e5 | 63 | } |
sinrab | 0:5464d5e415e5 | 64 | |
sinrab | 0:5464d5e415e5 | 65 | } // namespace mbed |
sinrab | 0:5464d5e415e5 | 66 | |
sinrab | 0:5464d5e415e5 | 67 | #endif |
sinrab | 0:5464d5e415e5 | 68 |