t

Dependencies:   DM_FATFileSystem DM_HttpServer DM_USBHost EthernetInterface USBDevice mbed-rpc mbed-rtos

Fork of DMSupport by Embedded Artists

Committer:
JVI_1556
Date:
Fri Oct 26 11:19:35 2018 +0000
Revision:
41:096931c776eb
Parent:
34:fc366bab393f
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 2:887c6b45e7fa 1 /*
embeddedartists 2:887c6b45e7fa 2 * Copyright 2014 Embedded Artists AB
embeddedartists 2:887c6b45e7fa 3 *
embeddedartists 2:887c6b45e7fa 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 2:887c6b45e7fa 5 * you may not use this file except in compliance with the License.
embeddedartists 2:887c6b45e7fa 6 * You may obtain a copy of the License at
embeddedartists 2:887c6b45e7fa 7 *
embeddedartists 2:887c6b45e7fa 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 2:887c6b45e7fa 9 *
embeddedartists 2:887c6b45e7fa 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 2:887c6b45e7fa 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 2:887c6b45e7fa 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 2:887c6b45e7fa 13 * See the License for the specific language governing permissions and
embeddedartists 2:887c6b45e7fa 14 * limitations under the License.
embeddedartists 2:887c6b45e7fa 15 */
embeddedartists 2:887c6b45e7fa 16
embeddedartists 2:887c6b45e7fa 17 #include "mbed.h"
embeddedartists 2:887c6b45e7fa 18 #include "RtosLog.h"
embeddedartists 2:887c6b45e7fa 19 #include <cstdarg>
embeddedartists 2:887c6b45e7fa 20
embeddedartists 2:887c6b45e7fa 21 /******************************************************************************
embeddedartists 2:887c6b45e7fa 22 * Defines and typedefs
embeddedartists 2:887c6b45e7fa 23 *****************************************************************************/
embeddedartists 2:887c6b45e7fa 24
embeddedartists 2:887c6b45e7fa 25
embeddedartists 2:887c6b45e7fa 26 /******************************************************************************
embeddedartists 2:887c6b45e7fa 27 * Local variables
embeddedartists 2:887c6b45e7fa 28 *****************************************************************************/
embeddedartists 2:887c6b45e7fa 29
embeddedartists 2:887c6b45e7fa 30 /******************************************************************************
embeddedartists 2:887c6b45e7fa 31 * Private Functions
embeddedartists 2:887c6b45e7fa 32 *****************************************************************************/
embeddedartists 2:887c6b45e7fa 33
embeddedartists 2:887c6b45e7fa 34 void RtosLog::logTask(void const* args)
embeddedartists 2:887c6b45e7fa 35 {
embeddedartists 2:887c6b45e7fa 36 RtosLog* instance = (RtosLog*)args;
embeddedartists 2:887c6b45e7fa 37
embeddedartists 2:887c6b45e7fa 38 while (true) {
embeddedartists 2:887c6b45e7fa 39 osEvent evt = instance->_queue.get();
embeddedartists 2:887c6b45e7fa 40 if (evt.status == osEventMessage) {
embeddedartists 2:887c6b45e7fa 41 message_t *message = (message_t*)evt.value.p;
embeddedartists 2:887c6b45e7fa 42 if (message->ptr != NULL) {
embeddedartists 2:887c6b45e7fa 43 instance->_serial.printf(message->ptr);
embeddedartists 2:887c6b45e7fa 44 free(message->ptr);
embeddedartists 2:887c6b45e7fa 45 } else {
embeddedartists 2:887c6b45e7fa 46 instance->_serial.printf(message->msg);
embeddedartists 2:887c6b45e7fa 47 }
embeddedartists 2:887c6b45e7fa 48
embeddedartists 2:887c6b45e7fa 49 instance->_mpool.free(message);
embeddedartists 2:887c6b45e7fa 50
embeddedartists 2:887c6b45e7fa 51 // Increase the number of available messages in the pool
embeddedartists 2:887c6b45e7fa 52 instance->_sem.release();
embeddedartists 2:887c6b45e7fa 53 }
embeddedartists 2:887c6b45e7fa 54 }
embeddedartists 2:887c6b45e7fa 55 }
embeddedartists 2:887c6b45e7fa 56
embeddedartists 2:887c6b45e7fa 57 /******************************************************************************
embeddedartists 2:887c6b45e7fa 58 * Public Functions
embeddedartists 2:887c6b45e7fa 59 *****************************************************************************/
embeddedartists 2:887c6b45e7fa 60
embeddedartists 34:fc366bab393f 61 #if defined(DM_BOARD_USE_USBSERIAL_IN_RTOSLOG)
embeddedartists 34:fc366bab393f 62 RtosLog::RtosLog() :
embeddedartists 34:fc366bab393f 63 _sem(NumMessages), _serial(), _thr(NULL)
embeddedartists 34:fc366bab393f 64 {
embeddedartists 34:fc366bab393f 65 }
embeddedartists 34:fc366bab393f 66 #else
embeddedartists 2:887c6b45e7fa 67 RtosLog::RtosLog() :
embeddedartists 2:887c6b45e7fa 68 _sem(NumMessages), _serial(USBTX, USBRX), _thr(NULL)
embeddedartists 2:887c6b45e7fa 69 {
embeddedartists 2:887c6b45e7fa 70 #if defined(DM_BOARD_USE_FAST_UART)
embeddedartists 2:887c6b45e7fa 71 // This works because both the default serial (used by printf) and the s instance
embeddedartists 2:887c6b45e7fa 72 // (used by s.printf) would use the same underlying UART code so setting the baudrate
embeddedartists 2:887c6b45e7fa 73 // in one affects the other.
embeddedartists 2:887c6b45e7fa 74 _serial.baud(115200);
embeddedartists 2:887c6b45e7fa 75 #endif
embeddedartists 2:887c6b45e7fa 76 }
embeddedartists 34:fc366bab393f 77 #endif
embeddedartists 2:887c6b45e7fa 78
embeddedartists 2:887c6b45e7fa 79 RtosLog::~RtosLog()
embeddedartists 2:887c6b45e7fa 80 {
embeddedartists 2:887c6b45e7fa 81 if (_thr != NULL) {
embeddedartists 2:887c6b45e7fa 82 _thr->terminate();
embeddedartists 2:887c6b45e7fa 83 delete _thr;
embeddedartists 2:887c6b45e7fa 84 _thr = NULL;
embeddedartists 2:887c6b45e7fa 85 }
embeddedartists 2:887c6b45e7fa 86 }
embeddedartists 2:887c6b45e7fa 87
embeddedartists 2:887c6b45e7fa 88 void RtosLog::init()
embeddedartists 2:887c6b45e7fa 89 {
embeddedartists 2:887c6b45e7fa 90 if (_thr == NULL) {
embeddedartists 2:887c6b45e7fa 91 _thr = new Thread(&RtosLog::logTask, this);
embeddedartists 2:887c6b45e7fa 92 }
embeddedartists 2:887c6b45e7fa 93 }
embeddedartists 2:887c6b45e7fa 94
embeddedartists 2:887c6b45e7fa 95 int RtosLog::printf(const char* format, ...)
embeddedartists 2:887c6b45e7fa 96 {
embeddedartists 2:887c6b45e7fa 97 // The pool has no "wait for free message" so we use a Sempahore
embeddedartists 2:887c6b45e7fa 98 // to keep track of the number of free messages and, if needed,
embeddedartists 2:887c6b45e7fa 99 // block the caller until a message is free
embeddedartists 2:887c6b45e7fa 100 _sem.wait();
embeddedartists 2:887c6b45e7fa 101
embeddedartists 2:887c6b45e7fa 102 // Allocate a null terminated message. Will always succeed due to
embeddedartists 2:887c6b45e7fa 103 // the semaphore above
embeddedartists 2:887c6b45e7fa 104 message_t *message = _mpool.calloc();
embeddedartists 2:887c6b45e7fa 105
embeddedartists 2:887c6b45e7fa 106 // Write the callers formatted message
embeddedartists 2:887c6b45e7fa 107 std::va_list args;
embeddedartists 2:887c6b45e7fa 108 va_start(args, format);
embeddedartists 2:887c6b45e7fa 109 int ret = vsnprintf(message->msg, MessageLen, format, args);
embeddedartists 2:887c6b45e7fa 110 va_end(args);
embeddedartists 2:887c6b45e7fa 111
embeddedartists 2:887c6b45e7fa 112 // If the entire message could not fit in the preallocated buffer
embeddedartists 2:887c6b45e7fa 113 // then allocate a new one and try again.
embeddedartists 2:887c6b45e7fa 114 if (ret > MessageLen) {
embeddedartists 2:887c6b45e7fa 115 message->ptr = (char*)malloc(ret + 1);
embeddedartists 2:887c6b45e7fa 116 if (message->ptr != NULL) {
embeddedartists 2:887c6b45e7fa 117 va_start(args, format);
embeddedartists 2:887c6b45e7fa 118 ret = vsnprintf(message->ptr, ret + 1, format, args);
embeddedartists 2:887c6b45e7fa 119 va_end(args);
embeddedartists 2:887c6b45e7fa 120 }
embeddedartists 2:887c6b45e7fa 121 }
embeddedartists 2:887c6b45e7fa 122
embeddedartists 2:887c6b45e7fa 123 // Send message
embeddedartists 2:887c6b45e7fa 124 _queue.put(message);
embeddedartists 2:887c6b45e7fa 125
embeddedartists 2:887c6b45e7fa 126 // Note that the Semaphore is not released here, that is done after
embeddedartists 2:887c6b45e7fa 127 // the message has been processed and released into the pool by
embeddedartists 2:887c6b45e7fa 128 // logTask()
embeddedartists 2:887c6b45e7fa 129 //_sem.release();
embeddedartists 2:887c6b45e7fa 130
embeddedartists 2:887c6b45e7fa 131 return ret;
embeddedartists 2:887c6b45e7fa 132 }
embeddedartists 2:887c6b45e7fa 133
embeddedartists 34:fc366bab393f 134 int RtosLog::isr_printf(const char* format, ...)
embeddedartists 34:fc366bab393f 135 {
embeddedartists 34:fc366bab393f 136 // The pool has no "wait for free message" so we use a Sempahore
embeddedartists 34:fc366bab393f 137 // to keep track of the number of free messages and, if needed,
embeddedartists 34:fc366bab393f 138 // block the caller until a message is free
embeddedartists 34:fc366bab393f 139 int available = _sem.wait(0);
embeddedartists 34:fc366bab393f 140 if (available <= 0) {
embeddedartists 34:fc366bab393f 141 // no free messages and it is not good to wait in an ISR so
embeddedartists 34:fc366bab393f 142 // we discard the message
embeddedartists 34:fc366bab393f 143 return 0;
embeddedartists 34:fc366bab393f 144 }
embeddedartists 34:fc366bab393f 145
embeddedartists 34:fc366bab393f 146 // Allocate a null terminated message. Will always succeed due to
embeddedartists 34:fc366bab393f 147 // the semaphore above
embeddedartists 34:fc366bab393f 148 message_t *message = _mpool.calloc();
embeddedartists 34:fc366bab393f 149
embeddedartists 34:fc366bab393f 150 // Write the callers formatted message
embeddedartists 34:fc366bab393f 151 std::va_list args;
embeddedartists 34:fc366bab393f 152 va_start(args, format);
embeddedartists 34:fc366bab393f 153 int ret = vsnprintf(message->msg, MessageLen, format, args);
embeddedartists 34:fc366bab393f 154 va_end(args);
embeddedartists 34:fc366bab393f 155
embeddedartists 34:fc366bab393f 156 // If the entire message could not fit in the preallocated buffer
embeddedartists 34:fc366bab393f 157 // then allocate a new one and try again.
embeddedartists 34:fc366bab393f 158 if (ret > MessageLen) {
embeddedartists 34:fc366bab393f 159 message->ptr = (char*)malloc(ret + 1);
embeddedartists 34:fc366bab393f 160 if (message->ptr != NULL) {
embeddedartists 34:fc366bab393f 161 va_start(args, format);
embeddedartists 34:fc366bab393f 162 ret = vsnprintf(message->ptr, ret + 1, format, args);
embeddedartists 34:fc366bab393f 163 va_end(args);
embeddedartists 34:fc366bab393f 164 }
embeddedartists 34:fc366bab393f 165 }
embeddedartists 34:fc366bab393f 166
embeddedartists 34:fc366bab393f 167 // Send message
embeddedartists 34:fc366bab393f 168 _queue.put(message);
embeddedartists 34:fc366bab393f 169
embeddedartists 34:fc366bab393f 170 // Note that the Semaphore is not released here, that is done after
embeddedartists 34:fc366bab393f 171 // the message has been processed and released into the pool by
embeddedartists 34:fc366bab393f 172 // logTask()
embeddedartists 34:fc366bab393f 173 //_sem.release();
embeddedartists 34:fc366bab393f 174
embeddedartists 34:fc366bab393f 175 return ret;
embeddedartists 34:fc366bab393f 176 }