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