Skip to content

Commit 8df64c3

Browse files
authored
debug: add RTT output support (#899)
1 parent 35919e9 commit 8df64c3

File tree

6 files changed

+3131
-54
lines changed

6 files changed

+3131
-54
lines changed

source/daplink/daplink_debug.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @file daplink_debug.c
3+
* @brief optional trace messages useful in development
4+
*
5+
* DAPLink Interface Firmware
6+
* Copyright (c) 2009-2021, Arm Limited, All Rights Reserved
7+
* SPDX-License-Identifier: Apache-2.0
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
10+
* not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*/
21+
22+
#include <stdarg.h>
23+
#include <stdio.h>
24+
#include <string.h>
25+
#include <stdint.h>
26+
#include "cmsis_os2.h"
27+
#include "rl_usb.h"
28+
#include "util.h"
29+
30+
#if defined (DAPLINK_DEBUG)
31+
32+
#if defined (DAPLINK_DEBUG_RTT)
33+
34+
#include "debug/SEGGER_RTT.c"
35+
36+
uint32_t daplink_debug(uint8_t *buf, uint32_t size)
37+
{
38+
return SEGGER_RTT_Write(0, buf, size);
39+
}
40+
41+
#else
42+
43+
static const char error_msg[] = "\r\n<OVERFLOW>\r\n";
44+
45+
uint32_t daplink_debug(uint8_t *buf, uint32_t size)
46+
{
47+
uint32_t total_free;
48+
uint32_t write_free;
49+
uint32_t error_len = strlen(error_msg);
50+
total_free = USBD_CDC_ACM_DataFree();
51+
52+
if (total_free < error_len) {
53+
// No space
54+
return 0;
55+
}
56+
57+
// Size available for writing
58+
write_free = total_free - error_len;
59+
size = MIN(write_free, size);
60+
USBD_CDC_ACM_DataSend(buf, size);
61+
62+
if (write_free == size) {
63+
USBD_CDC_ACM_DataSend((uint8_t *)error_msg, error_len);
64+
}
65+
66+
return size;
67+
}
68+
#endif
69+
70+
static char daplink_debug_buf[128] = {0};
71+
uint32_t daplink_debug_print(const char *format, ...)
72+
{
73+
uint32_t ret;
74+
int32_t r = 0;
75+
va_list arg;
76+
ret = 1;
77+
va_start(arg, format);
78+
r = vsnprintf(daplink_debug_buf, sizeof(daplink_debug_buf), format, arg);
79+
80+
if (r >= sizeof(daplink_debug_buf)) {
81+
r = snprintf(daplink_debug_buf, sizeof(daplink_debug_buf), "<Error - string length %i exceeds print buffer>\r\n", r);
82+
ret = 0;
83+
}
84+
85+
va_end(arg);
86+
daplink_debug((uint8_t *)daplink_debug_buf, r);
87+
return ret;
88+
}
89+
90+
#endif

source/daplink/daplink_debug.h

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @brief optional trace messages useful in development
44
*
55
* DAPLink Interface Firmware
6-
* Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
6+
* Copyright (c) 2009-2021, Arm Limited, All Rights Reserved
77
* SPDX-License-Identifier: Apache-2.0
88
*
99
* Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -26,65 +26,19 @@
2626
#include <stdio.h>
2727
#include <string.h>
2828
#include <stdint.h>
29-
#include "cmsis_os2.h"
30-
#include "rl_usb.h"
31-
#include "util.h"
3229

3330
#ifdef __cplusplus
3431
extern "C" {
3532
#endif
3633

37-
#ifndef MSC_DEBUG
38-
//#define MSC_DEBUG
34+
#ifndef DAPLINK_DEBUG
35+
// #define DAPLINK_DEBUG
3936
#endif
4037

41-
#if defined (MSC_DEBUG)
38+
#if defined (DAPLINK_DEBUG)
4239

43-
static const char error_msg[] = "\r\n<OVERFLOW>\r\n";
44-
45-
static inline uint32_t daplink_debug(uint8_t *buf, uint32_t size)
46-
{
47-
uint32_t total_free;
48-
uint32_t write_free;
49-
uint32_t error_len = strlen(error_msg);
50-
total_free = USBD_CDC_ACM_DataFree();
51-
52-
if (total_free < error_len) {
53-
// No space
54-
return 0;
55-
}
56-
57-
// Size available for writing
58-
write_free = total_free - error_len;
59-
size = MIN(write_free, size);
60-
USBD_CDC_ACM_DataSend(buf, size);
61-
62-
if (write_free == size) {
63-
USBD_CDC_ACM_DataSend((uint8_t *)error_msg, error_len);
64-
}
65-
66-
return size;
67-
}
68-
69-
static char daplink_debug_buf[128] = {0};
70-
static inline uint32_t daplink_debug_print(const char *format, ...)
71-
{
72-
uint32_t ret;
73-
int32_t r = 0;
74-
va_list arg;
75-
ret = 1;
76-
va_start(arg, format);
77-
r = vsnprintf(daplink_debug_buf, sizeof(daplink_debug_buf), format, arg);
78-
79-
if (r >= sizeof(daplink_debug_buf)) {
80-
r = snprintf(daplink_debug_buf, sizeof(daplink_debug_buf), "<Error - string length %i exceeds print buffer>\r\n", r);
81-
ret = 0;
82-
}
83-
84-
va_end(arg);
85-
daplink_debug((uint8_t *)daplink_debug_buf, r);
86-
return ret;
87-
}
40+
uint32_t daplink_debug_print(const char *format, ...);
41+
uint32_t daplink_debug(uint8_t *data, uint32_t size);
8842

8943
#else
9044

@@ -103,8 +57,8 @@ static inline uint32_t daplink_debug(uint8_t *data, uint32_t size)
10357
#define debug_msg(fmt, args...) daplink_debug_print(fmt, ## args);
10458
#define debug_data(buf, size) daplink_debug(buf, size);
10559

106-
#ifdef __cplusplus
107-
}
10860
#endif
10961

62+
#ifdef __cplusplus
63+
}
11064
#endif

source/daplink/debug/LICENSE.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
SEGGER Microcontroller GmbH
3+
The Embedded Experts
4+
5+
(c) 1995 - 2021 SEGGER Microcontroller GmbH
6+
www.segger.com Support: [email protected]
7+
8+
SEGGER RTT Real Time Transfer for embedded targets
9+
10+
11+
All rights reserved.
12+
13+
SEGGER strongly recommends to not make any changes
14+
to or modify the source code of this software in order to stay
15+
compatible with the RTT protocol and J-Link.
16+
17+
Redistribution and use in source and binary forms, with or
18+
without modification, are permitted provided that the following
19+
condition is met:
20+
21+
- Redistributions of source code must retain the above copyright
22+
notice, this condition and the following disclaimer.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25+
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28+
DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR
29+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31+
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32+
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35+
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
36+
DAMAGE.

0 commit comments

Comments
 (0)