mbed-dev-f303

Committer:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4
Date:
Tue Jun 14 09:21:18 2022 +0000
Revision:
0:bdf663c61a82
lib

Who changed what in which revision?

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