This fork captures the mbed lib v125 for ease of integration into older projects.

Fork of mbed-dev by mbed official

Committer:
apluscw
Date:
Fri Jul 20 21:24:42 2018 +0000
Revision:
187:92cbb9eec47b
Mbed library with source code from mbed lib v125. Posted to ease integration with some older projects.

Who changed what in which revision?

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