mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
187:0387e8f68319
mbed library release version 165

Who changed what in which revision?

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