Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 { 00030 // No lock needed in the constructor 00031 } 00032 00033 int RawSerial::getc() 00034 { 00035 lock(); 00036 int ret = _base_getc(); 00037 unlock(); 00038 return ret; 00039 } 00040 00041 int RawSerial::putc(int c) 00042 { 00043 lock(); 00044 int ret = _base_putc(c); 00045 unlock(); 00046 return ret; 00047 } 00048 00049 int RawSerial::puts(const char *str) 00050 { 00051 lock(); 00052 while (*str) { 00053 putc(*str ++); 00054 } 00055 unlock(); 00056 return 0; 00057 } 00058 00059 // Experimental support for printf in RawSerial. No Stream inheritance 00060 // means we can't call printf() directly, so we use sprintf() instead. 00061 // We only call malloc() for the sprintf() buffer if the buffer 00062 // length is above a certain threshold, otherwise we use just the stack. 00063 int RawSerial::printf(const char *format, ...) 00064 { 00065 lock(); 00066 std::va_list arg; 00067 va_start(arg, format); 00068 // ARMCC microlib does not properly handle a size of 0. 00069 // As a workaround supply a dummy buffer with a size of 1. 00070 char dummy_buf[1]; 00071 int len = vsnprintf(dummy_buf, sizeof(dummy_buf), format, arg); 00072 if (len < STRING_STACK_LIMIT) { 00073 char temp[STRING_STACK_LIMIT]; 00074 vsprintf(temp, format, arg); 00075 puts(temp); 00076 } else { 00077 char *temp = new char[len + 1]; 00078 vsprintf(temp, format, arg); 00079 puts(temp); 00080 delete[] temp; 00081 } 00082 va_end(arg); 00083 unlock(); 00084 return len; 00085 } 00086 00087 /** Acquire exclusive access to this serial port 00088 */ 00089 void RawSerial::lock() 00090 { 00091 // No lock used - external synchronization required 00092 } 00093 00094 /** Release exclusive access to this serial port 00095 */ 00096 void RawSerial::unlock() 00097 { 00098 // No lock used - external synchronization required 00099 } 00100 00101 } // namespace mbed 00102 00103 #endif
Generated on Tue Aug 9 2022 00:37:18 by
