mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
Embed:
(wiki syntax)
Show/hide line numbers
RawSerial.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "RawSerial.h" 00017 #include "wait_api.h" 00018 #include <cstdarg> 00019 00020 #if DEVICE_SERIAL 00021 00022 #define STRING_STACK_LIMIT 120 00023 00024 namespace mbed { 00025 00026 RawSerial::RawSerial(PinName tx, PinName rx) : SerialBase(tx, rx) { 00027 } 00028 00029 int RawSerial::getc() { 00030 return _base_getc(); 00031 } 00032 00033 int RawSerial::putc(int c) { 00034 return _base_putc(c); 00035 } 00036 00037 int RawSerial::puts(const char *str) { 00038 while (*str) 00039 putc(*str ++); 00040 return 0; 00041 } 00042 00043 // Experimental support for printf in RawSerial. No Stream inheritance 00044 // means we can't call printf() directly, so we use sprintf() instead. 00045 // We only call malloc() for the sprintf() buffer if the buffer 00046 // length is above a certain threshold, otherwise we use just the stack. 00047 int RawSerial::printf(const char *format, ...) { 00048 std::va_list arg; 00049 va_start(arg, format); 00050 // ARMCC microlib does not properly handle a size of 0. 00051 // As a workaround supply a dummy buffer with a size of 1. 00052 char dummy_buf[1]; 00053 int len = vsnprintf(dummy_buf, sizeof(dummy_buf), format, arg); 00054 if (len < STRING_STACK_LIMIT) { 00055 char temp[STRING_STACK_LIMIT]; 00056 vsprintf(temp, format, arg); 00057 puts(temp); 00058 } else { 00059 char *temp = new char[len + 1]; 00060 vsprintf(temp, format, arg); 00061 puts(temp); 00062 delete[] temp; 00063 } 00064 va_end(arg); 00065 return len; 00066 } 00067 00068 } // namespace mbed 00069 00070 #endif
Generated on Tue Jul 12 2022 20:02:09 by 1.7.2