mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Thu Sep 06 13:40:20 2018 +0100
Revision:
187:0387e8f68319
Parent:
167:e84263d55307
Child:
189:f392fc9709a3
mbed-dev library. Release version 163

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