Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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