Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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