lib
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 "drivers/RawSerial.h" 00017 #include "platform/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, int baud) : SerialBase(tx, rx, baud) { 00027 // No lock needed in the constructor 00028 } 00029 00030 int RawSerial::getc() { 00031 lock(); 00032 int ret = _base_getc(); 00033 unlock(); 00034 return ret; 00035 } 00036 00037 int RawSerial::putc(int c) { 00038 lock(); 00039 int ret = _base_putc(c); 00040 unlock(); 00041 return ret; 00042 } 00043 00044 int RawSerial::puts(const char *str) { 00045 lock(); 00046 while (*str) 00047 putc(*str ++); 00048 unlock(); 00049 return 0; 00050 } 00051 00052 // Experimental support for printf in RawSerial. No Stream inheritance 00053 // means we can't call printf() directly, so we use sprintf() instead. 00054 // We only call malloc() for the sprintf() buffer if the buffer 00055 // length is above a certain threshold, otherwise we use just the stack. 00056 int RawSerial::printf(const char *format, ...) { 00057 lock(); 00058 std::va_list arg; 00059 va_start(arg, format); 00060 // ARMCC microlib does not properly handle a size of 0. 00061 // As a workaround supply a dummy buffer with a size of 1. 00062 char dummy_buf[1]; 00063 int len = vsnprintf(dummy_buf, sizeof(dummy_buf), format, arg); 00064 if (len < STRING_STACK_LIMIT) { 00065 char temp[STRING_STACK_LIMIT]; 00066 vsprintf(temp, format, arg); 00067 puts(temp); 00068 } else { 00069 char *temp = new char[len + 1]; 00070 vsprintf(temp, format, arg); 00071 puts(temp); 00072 delete[] temp; 00073 } 00074 va_end(arg); 00075 unlock(); 00076 return len; 00077 } 00078 00079 /** Acquire exclusive access to this serial port 00080 */ 00081 void RawSerial::lock() { 00082 // No lock used - external synchronization required 00083 } 00084 00085 /** Release exclusive access to this serial port 00086 */ 00087 void RawSerial::unlock() { 00088 // No lock used - external synchronization required 00089 } 00090 00091 } // namespace mbed 00092 00093 #endif
Generated on Tue Jul 12 2022 20:00:14 by 1.7.2