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