mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 1:9db0e321a9f4 1 /* mbed Microcontroller Library
kenjiArai 1:9db0e321a9f4 2 * Copyright (c) 2006-2019 ARM Limited
kenjiArai 1:9db0e321a9f4 3 * SPDX-License-Identifier: Apache-2.0
kenjiArai 1:9db0e321a9f4 4 *
kenjiArai 1:9db0e321a9f4 5 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 1:9db0e321a9f4 6 * you may not use this file except in compliance with the License.
kenjiArai 1:9db0e321a9f4 7 * You may obtain a copy of the License at
kenjiArai 1:9db0e321a9f4 8 *
kenjiArai 1:9db0e321a9f4 9 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 1:9db0e321a9f4 10 *
kenjiArai 1:9db0e321a9f4 11 * Unless required by applicable law or agreed to in writing, software
kenjiArai 1:9db0e321a9f4 12 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 1:9db0e321a9f4 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 1:9db0e321a9f4 14 * See the License for the specific language governing permissions and
kenjiArai 1:9db0e321a9f4 15 * limitations under the License.
kenjiArai 1:9db0e321a9f4 16 */
kenjiArai 1:9db0e321a9f4 17 #include "drivers/RawSerial.h"
kenjiArai 1:9db0e321a9f4 18 #include <stdio.h>
kenjiArai 1:9db0e321a9f4 19
kenjiArai 1:9db0e321a9f4 20
kenjiArai 1:9db0e321a9f4 21 #if DEVICE_SERIAL
kenjiArai 1:9db0e321a9f4 22
kenjiArai 1:9db0e321a9f4 23 #define STRING_STACK_LIMIT 120
kenjiArai 1:9db0e321a9f4 24
kenjiArai 1:9db0e321a9f4 25 namespace mbed {
kenjiArai 1:9db0e321a9f4 26
kenjiArai 1:9db0e321a9f4 27 RawSerial::RawSerial(PinName tx, PinName rx, int baud) : SerialBase(tx, rx, baud)
kenjiArai 1:9db0e321a9f4 28 {
kenjiArai 1:9db0e321a9f4 29 // No lock needed in the constructor
kenjiArai 1:9db0e321a9f4 30 }
kenjiArai 1:9db0e321a9f4 31
kenjiArai 1:9db0e321a9f4 32 int RawSerial::getc()
kenjiArai 1:9db0e321a9f4 33 {
kenjiArai 1:9db0e321a9f4 34 lock();
kenjiArai 1:9db0e321a9f4 35 int ret = _base_getc();
kenjiArai 1:9db0e321a9f4 36 unlock();
kenjiArai 1:9db0e321a9f4 37 return ret;
kenjiArai 1:9db0e321a9f4 38 }
kenjiArai 1:9db0e321a9f4 39
kenjiArai 1:9db0e321a9f4 40 int RawSerial::putc(int c)
kenjiArai 1:9db0e321a9f4 41 {
kenjiArai 1:9db0e321a9f4 42 lock();
kenjiArai 1:9db0e321a9f4 43 int ret = _base_putc(c);
kenjiArai 1:9db0e321a9f4 44 unlock();
kenjiArai 1:9db0e321a9f4 45 return ret;
kenjiArai 1:9db0e321a9f4 46 }
kenjiArai 1:9db0e321a9f4 47
kenjiArai 1:9db0e321a9f4 48 int RawSerial::puts(const char *str)
kenjiArai 1:9db0e321a9f4 49 {
kenjiArai 1:9db0e321a9f4 50 lock();
kenjiArai 1:9db0e321a9f4 51 while (*str) {
kenjiArai 1:9db0e321a9f4 52 putc(*str ++);
kenjiArai 1:9db0e321a9f4 53 }
kenjiArai 1:9db0e321a9f4 54 unlock();
kenjiArai 1:9db0e321a9f4 55 return 0;
kenjiArai 1:9db0e321a9f4 56 }
kenjiArai 1:9db0e321a9f4 57
kenjiArai 1:9db0e321a9f4 58 // Experimental support for printf in RawSerial. No Stream inheritance
kenjiArai 1:9db0e321a9f4 59 // means we can't call printf() directly, so we use sprintf() instead.
kenjiArai 1:9db0e321a9f4 60 // We only call malloc() for the sprintf() buffer if the buffer
kenjiArai 1:9db0e321a9f4 61 // length is above a certain threshold, otherwise we use just the stack.
kenjiArai 1:9db0e321a9f4 62 int RawSerial::printf(const char *format, ...)
kenjiArai 1:9db0e321a9f4 63 {
kenjiArai 1:9db0e321a9f4 64 std::va_list arg;
kenjiArai 1:9db0e321a9f4 65 va_start(arg, format);
kenjiArai 1:9db0e321a9f4 66 int len = this->vprintf(format, arg);
kenjiArai 1:9db0e321a9f4 67 va_end(arg);
kenjiArai 1:9db0e321a9f4 68 return len;
kenjiArai 1:9db0e321a9f4 69 }
kenjiArai 1:9db0e321a9f4 70
kenjiArai 1:9db0e321a9f4 71 int RawSerial::vprintf(const char *format, std::va_list arg)
kenjiArai 1:9db0e321a9f4 72 {
kenjiArai 1:9db0e321a9f4 73 lock();
kenjiArai 1:9db0e321a9f4 74 // ARMCC microlib does not properly handle a size of 0.
kenjiArai 1:9db0e321a9f4 75 // As a workaround supply a dummy buffer with a size of 1.
kenjiArai 1:9db0e321a9f4 76 char dummy_buf[1];
kenjiArai 1:9db0e321a9f4 77 int len = vsnprintf(dummy_buf, sizeof(dummy_buf), format, arg);
kenjiArai 1:9db0e321a9f4 78 if (len < STRING_STACK_LIMIT) {
kenjiArai 1:9db0e321a9f4 79 char temp[STRING_STACK_LIMIT];
kenjiArai 1:9db0e321a9f4 80 vsprintf(temp, format, arg);
kenjiArai 1:9db0e321a9f4 81 puts(temp);
kenjiArai 1:9db0e321a9f4 82 } else {
kenjiArai 1:9db0e321a9f4 83 char *temp = new char[len + 1];
kenjiArai 1:9db0e321a9f4 84 vsprintf(temp, format, arg);
kenjiArai 1:9db0e321a9f4 85 puts(temp);
kenjiArai 1:9db0e321a9f4 86 delete[] temp;
kenjiArai 1:9db0e321a9f4 87 }
kenjiArai 1:9db0e321a9f4 88 unlock();
kenjiArai 1:9db0e321a9f4 89 return len;
kenjiArai 1:9db0e321a9f4 90 }
kenjiArai 1:9db0e321a9f4 91
kenjiArai 1:9db0e321a9f4 92 /** Acquire exclusive access to this serial port
kenjiArai 1:9db0e321a9f4 93 */
kenjiArai 1:9db0e321a9f4 94 void RawSerial::lock()
kenjiArai 1:9db0e321a9f4 95 {
kenjiArai 1:9db0e321a9f4 96 // No lock used - external synchronization required
kenjiArai 1:9db0e321a9f4 97 }
kenjiArai 1:9db0e321a9f4 98
kenjiArai 1:9db0e321a9f4 99 /** Release exclusive access to this serial port
kenjiArai 1:9db0e321a9f4 100 */
kenjiArai 1:9db0e321a9f4 101 void RawSerial::unlock()
kenjiArai 1:9db0e321a9f4 102 {
kenjiArai 1:9db0e321a9f4 103 // No lock used - external synchronization required
kenjiArai 1:9db0e321a9f4 104 }
kenjiArai 1:9db0e321a9f4 105
kenjiArai 1:9db0e321a9f4 106 } // namespace mbed
kenjiArai 1:9db0e321a9f4 107
kenjiArai 1:9db0e321a9f4 108 #endif