Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RawSerial.cpp Source File

RawSerial.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "drivers/RawSerial.h"
00018 #include <stdio.h>
00019 
00020 
00021 #if DEVICE_SERIAL
00022 
00023 #define STRING_STACK_LIMIT    120
00024 
00025 namespace mbed {
00026 
00027 RawSerial::RawSerial(PinName tx, PinName rx, int baud) : SerialBase(tx, rx, baud)
00028 {
00029     // No lock needed in the constructor
00030 }
00031 
00032 int RawSerial::getc()
00033 {
00034     lock();
00035     int ret = _base_getc();
00036     unlock();
00037     return ret;
00038 }
00039 
00040 int RawSerial::putc(int c)
00041 {
00042     lock();
00043     int ret = _base_putc(c);
00044     unlock();
00045     return ret;
00046 }
00047 
00048 int RawSerial::puts(const char *str)
00049 {
00050     lock();
00051     while (*str) {
00052         putc(*str ++);
00053     }
00054     unlock();
00055     return 0;
00056 }
00057 
00058 // Experimental support for printf in RawSerial. No Stream inheritance
00059 // means we can't call printf() directly, so we use sprintf() instead.
00060 // We only call malloc() for the sprintf() buffer if the buffer
00061 // length is above a certain threshold, otherwise we use just the stack.
00062 int RawSerial::printf(const char *format, ...)
00063 {
00064     std::va_list arg;
00065     va_start(arg, format);
00066     int len = this->vprintf(format, arg);
00067     va_end(arg);
00068     return len;
00069 }
00070 
00071 int RawSerial::vprintf(const char *format, std::va_list arg)
00072 {
00073     lock();
00074     // ARMCC microlib does not properly handle a size of 0.
00075     // As a workaround supply a dummy buffer with a size of 1.
00076     char dummy_buf[1];
00077     int len = vsnprintf(dummy_buf, sizeof(dummy_buf), format, arg);
00078     if (len < STRING_STACK_LIMIT) {
00079         char temp[STRING_STACK_LIMIT];
00080         vsprintf(temp, format, arg);
00081         puts(temp);
00082     } else {
00083         char *temp = new char[len + 1];
00084         vsprintf(temp, format, arg);
00085         puts(temp);
00086         delete[] temp;
00087     }
00088     unlock();
00089     return len;
00090 }
00091 
00092 /** Acquire exclusive access to this serial port
00093  */
00094 void RawSerial::lock()
00095 {
00096     // No lock used - external synchronization required
00097 }
00098 
00099 /** Release exclusive access to this serial port
00100  */
00101 void RawSerial::unlock()
00102 {
00103     // No lock used - external synchronization required
00104 }
00105 
00106 } // namespace mbed
00107 
00108 #endif