mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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