A board support package for the LPC4088 Display Module.

Dependencies:   DM_HttpServer DM_USBHost

Dependents:   lpc4088_displaymodule_emwin lpc4088_displaymodule_demo_sphere sampleGUI sampleEmptyGUI ... more

Fork of DMSupport by EmbeddedArtists AB

Committer:
embeddedartists
Date:
Wed Oct 23 06:59:29 2019 +0000
Revision:
41:e06e764ff4fd
Parent:
34:fc366bab393f
Updates related to mbed OS 5

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 41:e06e764ff4fd 91 _thr = new Thread();
embeddedartists 41:e06e764ff4fd 92 _thr->start(callback(&RtosLog::logTask, this));
embeddedartists 2:887c6b45e7fa 93 }
embeddedartists 2:887c6b45e7fa 94 }
embeddedartists 2:887c6b45e7fa 95
embeddedartists 2:887c6b45e7fa 96 int RtosLog::printf(const char* format, ...)
embeddedartists 2:887c6b45e7fa 97 {
embeddedartists 2:887c6b45e7fa 98 // The pool has no "wait for free message" so we use a Sempahore
embeddedartists 2:887c6b45e7fa 99 // to keep track of the number of free messages and, if needed,
embeddedartists 2:887c6b45e7fa 100 // block the caller until a message is free
embeddedartists 41:e06e764ff4fd 101 _sem.acquire();
embeddedartists 2:887c6b45e7fa 102
embeddedartists 2:887c6b45e7fa 103 // Allocate a null terminated message. Will always succeed due to
embeddedartists 2:887c6b45e7fa 104 // the semaphore above
embeddedartists 2:887c6b45e7fa 105 message_t *message = _mpool.calloc();
embeddedartists 2:887c6b45e7fa 106
embeddedartists 2:887c6b45e7fa 107 // Write the callers formatted message
embeddedartists 2:887c6b45e7fa 108 std::va_list args;
embeddedartists 2:887c6b45e7fa 109 va_start(args, format);
embeddedartists 2:887c6b45e7fa 110 int ret = vsnprintf(message->msg, MessageLen, format, args);
embeddedartists 2:887c6b45e7fa 111 va_end(args);
embeddedartists 2:887c6b45e7fa 112
embeddedartists 2:887c6b45e7fa 113 // If the entire message could not fit in the preallocated buffer
embeddedartists 2:887c6b45e7fa 114 // then allocate a new one and try again.
embeddedartists 2:887c6b45e7fa 115 if (ret > MessageLen) {
embeddedartists 2:887c6b45e7fa 116 message->ptr = (char*)malloc(ret + 1);
embeddedartists 2:887c6b45e7fa 117 if (message->ptr != NULL) {
embeddedartists 2:887c6b45e7fa 118 va_start(args, format);
embeddedartists 2:887c6b45e7fa 119 ret = vsnprintf(message->ptr, ret + 1, format, args);
embeddedartists 2:887c6b45e7fa 120 va_end(args);
embeddedartists 2:887c6b45e7fa 121 }
embeddedartists 2:887c6b45e7fa 122 }
embeddedartists 2:887c6b45e7fa 123
embeddedartists 2:887c6b45e7fa 124 // Send message
embeddedartists 2:887c6b45e7fa 125 _queue.put(message);
embeddedartists 2:887c6b45e7fa 126
embeddedartists 2:887c6b45e7fa 127 // Note that the Semaphore is not released here, that is done after
embeddedartists 2:887c6b45e7fa 128 // the message has been processed and released into the pool by
embeddedartists 2:887c6b45e7fa 129 // logTask()
embeddedartists 2:887c6b45e7fa 130 //_sem.release();
embeddedartists 2:887c6b45e7fa 131
embeddedartists 2:887c6b45e7fa 132 return ret;
embeddedartists 2:887c6b45e7fa 133 }
embeddedartists 2:887c6b45e7fa 134
embeddedartists 34:fc366bab393f 135 int RtosLog::isr_printf(const char* format, ...)
embeddedartists 34:fc366bab393f 136 {
embeddedartists 34:fc366bab393f 137 // The pool has no "wait for free message" so we use a Sempahore
embeddedartists 34:fc366bab393f 138 // to keep track of the number of free messages and, if needed,
embeddedartists 34:fc366bab393f 139 // block the caller until a message is free
embeddedartists 41:e06e764ff4fd 140 bool available = _sem.try_acquire();
embeddedartists 41:e06e764ff4fd 141 if (!available) {
embeddedartists 34:fc366bab393f 142 // no free messages and it is not good to wait in an ISR so
embeddedartists 34:fc366bab393f 143 // we discard the message
embeddedartists 34:fc366bab393f 144 return 0;
embeddedartists 34:fc366bab393f 145 }
embeddedartists 34:fc366bab393f 146
embeddedartists 34:fc366bab393f 147 // Allocate a null terminated message. Will always succeed due to
embeddedartists 34:fc366bab393f 148 // the semaphore above
embeddedartists 34:fc366bab393f 149 message_t *message = _mpool.calloc();
embeddedartists 34:fc366bab393f 150
embeddedartists 34:fc366bab393f 151 // Write the callers formatted message
embeddedartists 34:fc366bab393f 152 std::va_list args;
embeddedartists 34:fc366bab393f 153 va_start(args, format);
embeddedartists 34:fc366bab393f 154 int ret = vsnprintf(message->msg, MessageLen, format, args);
embeddedartists 34:fc366bab393f 155 va_end(args);
embeddedartists 34:fc366bab393f 156
embeddedartists 34:fc366bab393f 157 // If the entire message could not fit in the preallocated buffer
embeddedartists 34:fc366bab393f 158 // then allocate a new one and try again.
embeddedartists 34:fc366bab393f 159 if (ret > MessageLen) {
embeddedartists 34:fc366bab393f 160 message->ptr = (char*)malloc(ret + 1);
embeddedartists 34:fc366bab393f 161 if (message->ptr != NULL) {
embeddedartists 34:fc366bab393f 162 va_start(args, format);
embeddedartists 34:fc366bab393f 163 ret = vsnprintf(message->ptr, ret + 1, format, args);
embeddedartists 34:fc366bab393f 164 va_end(args);
embeddedartists 34:fc366bab393f 165 }
embeddedartists 34:fc366bab393f 166 }
embeddedartists 34:fc366bab393f 167
embeddedartists 34:fc366bab393f 168 // Send message
embeddedartists 34:fc366bab393f 169 _queue.put(message);
embeddedartists 34:fc366bab393f 170
embeddedartists 34:fc366bab393f 171 // Note that the Semaphore is not released here, that is done after
embeddedartists 34:fc366bab393f 172 // the message has been processed and released into the pool by
embeddedartists 34:fc366bab393f 173 // logTask()
embeddedartists 34:fc366bab393f 174 //_sem.release();
embeddedartists 34:fc366bab393f 175
embeddedartists 34:fc366bab393f 176 return ret;
embeddedartists 34:fc366bab393f 177 }