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