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