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