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