Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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