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