mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

UserRevisionLine numberNew contents of line
modtronix 1:71204b8406f2 1 /* mbed Microcontroller Library
modtronix 1:71204b8406f2 2 * Copyright (c) 2006-2013 ARM Limited
modtronix 1:71204b8406f2 3 *
modtronix 1:71204b8406f2 4 * Licensed under the Apache License, Version 2.0 (the "License");
modtronix 1:71204b8406f2 5 * you may not use this file except in compliance with the License.
modtronix 1:71204b8406f2 6 * You may obtain a copy of the License at
modtronix 1:71204b8406f2 7 *
modtronix 1:71204b8406f2 8 * http://www.apache.org/licenses/LICENSE-2.0
modtronix 1:71204b8406f2 9 *
modtronix 1:71204b8406f2 10 * Unless required by applicable law or agreed to in writing, software
modtronix 1:71204b8406f2 11 * distributed under the License is distributed on an "AS IS" BASIS,
modtronix 1:71204b8406f2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
modtronix 1:71204b8406f2 13 * See the License for the specific language governing permissions and
modtronix 1:71204b8406f2 14 * limitations under the License.
modtronix 1:71204b8406f2 15 */
modtronix 1:71204b8406f2 16 #include "RawSerial.h"
modtronix 1:71204b8406f2 17 #include "wait_api.h"
modtronix 1:71204b8406f2 18 #include <cstdarg>
modtronix 1:71204b8406f2 19
modtronix 1:71204b8406f2 20 #if DEVICE_SERIAL
modtronix 1:71204b8406f2 21
modtronix 1:71204b8406f2 22 #define STRING_STACK_LIMIT 120
modtronix 1:71204b8406f2 23
modtronix 1:71204b8406f2 24 namespace mbed {
modtronix 1:71204b8406f2 25
modtronix 1:71204b8406f2 26 RawSerial::RawSerial(PinName tx, PinName rx) : SerialBase(tx, rx) {
modtronix 1:71204b8406f2 27 }
modtronix 1:71204b8406f2 28
modtronix 1:71204b8406f2 29 int RawSerial::getc() {
modtronix 1:71204b8406f2 30 return _base_getc();
modtronix 1:71204b8406f2 31 }
modtronix 1:71204b8406f2 32
modtronix 1:71204b8406f2 33 int RawSerial::putc(int c) {
modtronix 1:71204b8406f2 34 return _base_putc(c);
modtronix 1:71204b8406f2 35 }
modtronix 1:71204b8406f2 36
modtronix 1:71204b8406f2 37 int RawSerial::puts(const char *str) {
modtronix 1:71204b8406f2 38 while (*str)
modtronix 1:71204b8406f2 39 putc(*str ++);
modtronix 1:71204b8406f2 40 return 0;
modtronix 1:71204b8406f2 41 }
modtronix 1:71204b8406f2 42
modtronix 1:71204b8406f2 43 // Experimental support for printf in RawSerial. No Stream inheritance
modtronix 1:71204b8406f2 44 // means we can't call printf() directly, so we use sprintf() instead.
modtronix 1:71204b8406f2 45 // We only call malloc() for the sprintf() buffer if the buffer
modtronix 1:71204b8406f2 46 // length is above a certain threshold, otherwise we use just the stack.
modtronix 1:71204b8406f2 47 int RawSerial::printf(const char *format, ...) {
modtronix 1:71204b8406f2 48 std::va_list arg;
modtronix 1:71204b8406f2 49 va_start(arg, format);
modtronix 1:71204b8406f2 50 int len = vsnprintf(NULL, 0, format, arg);
modtronix 1:71204b8406f2 51 if (len < STRING_STACK_LIMIT) {
modtronix 1:71204b8406f2 52 char temp[STRING_STACK_LIMIT];
modtronix 1:71204b8406f2 53 vsprintf(temp, format, arg);
modtronix 1:71204b8406f2 54 puts(temp);
modtronix 1:71204b8406f2 55 } else {
modtronix 1:71204b8406f2 56 char *temp = new char[len + 1];
modtronix 1:71204b8406f2 57 vsprintf(temp, format, arg);
modtronix 1:71204b8406f2 58 puts(temp);
modtronix 1:71204b8406f2 59 delete[] temp;
modtronix 1:71204b8406f2 60 }
modtronix 1:71204b8406f2 61 va_end(arg);
modtronix 1:71204b8406f2 62 return len;
modtronix 1:71204b8406f2 63 }
modtronix 1:71204b8406f2 64
modtronix 1:71204b8406f2 65 } // namespace mbed
modtronix 1:71204b8406f2 66
modtronix 1:71204b8406f2 67 #endif