Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers daplink_debug.h Source File

daplink_debug.h

Go to the documentation of this file.
00001 /**
00002  * @file    daplink_debug.h
00003  * @brief   optional trace messages useful in development
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  * not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  * http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #ifndef DAPLINK_DEBUG_H
00023 #define DAPLINK_DEBUG_H
00024 
00025 #include <stdarg.h>
00026 #include <stdio.h>
00027 #include <string.h>
00028 #include <stdint.h>
00029 #include "cmsis_os2.h"
00030 #include "rl_usb.h"
00031 #include "util.h"
00032 
00033 #ifdef __cplusplus
00034 extern "C" {
00035 #endif
00036 
00037 #ifndef MSC_DEBUG
00038 //#define MSC_DEBUG
00039 #endif
00040 
00041 #if defined (MSC_DEBUG)
00042 
00043 static const char error_msg[] = "\r\n<OVERFLOW>\r\n";
00044 
00045 static inline uint32_t daplink_debug(uint8_t *buf, uint32_t size)
00046 {
00047     uint32_t total_free;
00048     uint32_t write_free;
00049     uint32_t error_len = strlen(error_msg);
00050     total_free = USBD_CDC_ACM_DataFree();
00051 
00052     if (total_free < error_len) {
00053         // No space
00054         return 0;
00055     }
00056 
00057     // Size available for writing
00058     write_free = total_free - error_len;
00059     size = MIN(write_free, size);
00060     USBD_CDC_ACM_DataSend(buf, size);
00061 
00062     if (write_free == size) {
00063         USBD_CDC_ACM_DataSend((uint8_t *)error_msg, error_len);
00064     }
00065 
00066     return size;
00067 }
00068 
00069 static char daplink_debug_buf[128] = {0};
00070 static inline uint32_t daplink_debug_print(const char *format, ...)
00071 {
00072     uint32_t ret;
00073     int32_t r = 0;
00074     va_list arg;
00075     ret = 1;
00076     va_start(arg, format);
00077     r = vsnprintf(daplink_debug_buf, sizeof(daplink_debug_buf), format, arg);
00078 
00079     if (r >= sizeof(daplink_debug_buf)) {
00080         r = snprintf(daplink_debug_buf, sizeof(daplink_debug_buf), "<Error - string length %i exceeds print buffer>\r\n", r);
00081         ret = 0;
00082     }
00083 
00084     va_end(arg);
00085     daplink_debug((uint8_t *)daplink_debug_buf, r);
00086     return ret;
00087 }
00088 
00089 #else
00090 
00091 static inline uint32_t daplink_debug_print(const char *format, ...)
00092 {
00093     return 1;
00094 }
00095 
00096 static inline uint32_t daplink_debug(uint8_t *data, uint32_t size)
00097 {
00098     return 1;
00099 }
00100 
00101 #endif
00102 
00103 #define debug_msg(fmt, args...) daplink_debug_print(fmt, ## args);
00104 #define debug_data(buf, size) daplink_debug(buf, size);
00105 
00106 #ifdef __cplusplus
00107 }
00108 #endif
00109 
00110 #endif