1 |
2 |
ZTEX |
/*%
|
2 |
|
|
ZTEX Firmware Kit for EZ-USB FX3 Microcontrollers
|
3 |
|
|
Copyright (C) 2009-2017 ZTEX GmbH.
|
4 |
|
|
http://www.ztex.de
|
5 |
|
|
|
6 |
|
|
This Source Code Form is subject to the terms of the Mozilla Public
|
7 |
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
8 |
|
|
You can obtain one at http://mozilla.org/MPL/2.0/.
|
9 |
|
|
|
10 |
|
|
Alternatively, the contents of this file may be used under the terms
|
11 |
|
|
of the GNU General Public License Version 3, as described below:
|
12 |
|
|
|
13 |
|
|
This program is free software; you can redistribute it and/or modify
|
14 |
|
|
it under the terms of the GNU General Public License version 3 as
|
15 |
|
|
published by the Free Software Foundation.
|
16 |
|
|
|
17 |
|
|
This program is distributed in the hope that it will be useful, but
|
18 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
20 |
|
|
General Public License for more details.
|
21 |
|
|
|
22 |
|
|
You should have received a copy of the GNU General Public License
|
23 |
|
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
24 |
|
|
%*/
|
25 |
|
|
/*
|
26 |
|
|
Debugging and logging functions.
|
27 |
|
|
*/
|
28 |
|
|
|
29 |
|
|
#include "cyu3system.h"
|
30 |
|
|
#include "cyu3os.h"
|
31 |
|
|
#include "cyu3usb.h"
|
32 |
|
|
|
33 |
|
|
#define ZTEX_DEBUG_MSG_SIZE_MAX 32
|
34 |
|
|
#define ZTEX_DEBUG_MSG_LIMIT ZTEX_DEBUG_MSG_SIZE_MAX // maximum amount of messages between two get debug messages requests
|
35 |
|
|
|
36 |
|
|
#define ZTEX_LOG( args... ) { \
|
37 |
|
|
uint8_t* buf = CyU3PMemAlloc(512); \
|
38 |
|
|
CyU3PDebugStringPrint(buf, 512, args ); \
|
39 |
|
|
ztex_debug_add(1, strlen((char*)buf), buf, 2); \
|
40 |
|
|
CyU3PMemFree(buf); \
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
#define ZTEX_ASSERT( a ) { \
|
44 |
|
|
if (!(a)) ztex_runtime_error( 65530, __FILE__, __LINE__ ); \
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
#define ZTEX_ASSERT_RET( a ) { \
|
48 |
|
|
if (!(a)) { \
|
49 |
|
|
ztex_runtime_error( 65530, __FILE__, __LINE__ ); \
|
50 |
|
|
return 255; \
|
51 |
|
|
} \
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
#define ZTEX_REC(a) { \
|
55 |
|
|
uint16_t ec=(a); \
|
56 |
|
|
if ( ec != 0 ) { \
|
57 |
|
|
ztex_runtime_error( ec, __FILE__, __LINE__ ); \
|
58 |
|
|
} \
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
#define ZTEX_REC_RET(a) { \
|
62 |
|
|
uint16_t ec=(a); \
|
63 |
|
|
if ( ec != 0 ) { \
|
64 |
|
|
ztex_runtime_error( ec, __FILE__, __LINE__ ); \
|
65 |
|
|
return 255; \
|
66 |
|
|
} \
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
#define ZTEX_REC_CONT(a) { \
|
70 |
|
|
uint16_t ec=(a); \
|
71 |
|
|
if ( ec != 0 ) { \
|
72 |
|
|
ztex_runtime_error( ec, __FILE__, __LINE__ ); \
|
73 |
|
|
continue; \
|
74 |
|
|
} \
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
uint8_t ztex_ec = 0; // 1 if firmware halted due to fatal error
|
78 |
|
|
uint32_t debug_msg_last = 0; // index of last debug message + 1
|
79 |
|
|
uint16_t debug_msg_cnt = 0; // number of stored messages
|
80 |
|
|
uint16_t debug_msg_limit = ZTEX_DEBUG_MSG_LIMIT; // current message limit
|
81 |
|
|
|
82 |
|
|
uint8_t* debug_msg[ZTEX_DEBUG_MSG_SIZE_MAX]; // debug messages
|
83 |
|
|
uint16_t debug_msg_size[ZTEX_DEBUG_MSG_SIZE_MAX]; // length of the message
|
84 |
|
|
uint8_t debug_msg_type[ZTEX_DEBUG_MSG_SIZE_MAX]; // 1: string; 2: line info, other values: data
|
85 |
|
|
uint8_t debug_msg_action[ZTEX_DEBUG_MSG_SIZE_MAX]; // 0: do nothing, 1 free memory if overwritten, >1 copy + free
|
86 |
|
|
|
87 |
|
|
void ztex_debug_add(uint8_t type, uint16_t size, uint8_t* buf, uint8_t action) {
|
88 |
|
|
if (debug_msg_last >= debug_msg_limit ) {
|
89 |
|
|
if ( action >= 1 ) CyU3PMemFree(buf);
|
90 |
|
|
return;
|
91 |
|
|
}
|
92 |
|
|
uint16_t j = debug_msg_last % ZTEX_DEBUG_MSG_SIZE_MAX;
|
93 |
|
|
if ( debug_msg_last >= ZTEX_DEBUG_MSG_SIZE_MAX ) {
|
94 |
|
|
if ( debug_msg_action[j] >= 1 ) CyU3PMemFree(debug_msg[j]);
|
95 |
|
|
}
|
96 |
|
|
else {
|
97 |
|
|
debug_msg_cnt++;
|
98 |
|
|
}
|
99 |
|
|
debug_msg_last++;
|
100 |
|
|
|
101 |
|
|
if ( action>1 ) {
|
102 |
|
|
debug_msg[j] = CyU3PMemAlloc(size);
|
103 |
|
|
CyU3PMemCopy(debug_msg[j], buf, size);
|
104 |
|
|
}
|
105 |
|
|
else {
|
106 |
|
|
debug_msg[j]=buf;
|
107 |
|
|
}
|
108 |
|
|
debug_msg_size[j] = size;
|
109 |
|
|
debug_msg_type[j] = type;
|
110 |
|
|
debug_msg_action[j] = action;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
uint8_t ztex_log ( const char* str ) {
|
115 |
|
|
ztex_debug_add(1, strlen(str), (uint8_t*) str, 0);
|
116 |
|
|
return(255);
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
void ztex_runtime_error ( uint16_t ec, const char* fn, uint16_t line ) {
|
121 |
|
|
uint8_t size=strlen(fn)+4;
|
122 |
|
|
uint8_t* buf=CyU3PMemAlloc(size);
|
123 |
|
|
buf[0]=ec;
|
124 |
|
|
buf[1]=ec>>8;
|
125 |
|
|
buf[2]=line;
|
126 |
|
|
buf[3]=line>>8;
|
127 |
|
|
CyU3PMemCopy(&buf[4], (uint8_t*)fn, size-4);
|
128 |
|
|
ztex_debug_add(2, size, buf, 1);
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
// VR 0x28
|
132 |
|
|
uint8_t ztex_vr_debug_send(uint16_t value, uint16_t index, uint16_t length ) {
|
133 |
|
|
uint32_t idx = index | (value << 16);
|
134 |
|
|
if ( length < 10 ) length = 10;
|
135 |
|
|
if ( length > 4096 ) length = 4096;
|
136 |
|
|
|
137 |
|
|
ztex_ep0buf[0]=ztex_ec;
|
138 |
|
|
ztex_ep0buf[1]=debug_msg_last;
|
139 |
|
|
ztex_ep0buf[2]=debug_msg_last>>8;
|
140 |
|
|
ztex_ep0buf[3]=debug_msg_last>>16;
|
141 |
|
|
ztex_ep0buf[4]=debug_msg_last>>24;
|
142 |
|
|
ztex_ep0buf[5]=debug_msg_cnt;
|
143 |
|
|
ztex_ep0buf[6]=debug_msg_cnt>>8;
|
144 |
|
|
|
145 |
|
|
if ( (idx<debug_msg_last-debug_msg_cnt) || idx>=debug_msg_last) {
|
146 |
|
|
ztex_ep0buf[7]=0;
|
147 |
|
|
ztex_ep0buf[8]=0;
|
148 |
|
|
ztex_ep0buf[9]=255;
|
149 |
|
|
length=10;
|
150 |
|
|
}
|
151 |
|
|
else {
|
152 |
|
|
uint16_t j = idx % ZTEX_DEBUG_MSG_SIZE_MAX;
|
153 |
|
|
ztex_ep0buf[7]=debug_msg_size[j];
|
154 |
|
|
ztex_ep0buf[8]=debug_msg_size[j]>>8;
|
155 |
|
|
ztex_ep0buf[9]=debug_msg_type[j];
|
156 |
|
|
for ( uint16_t i = 0; i < debug_msg_size[j]; i++ )
|
157 |
|
|
if ( length>i+10) ztex_ep0buf[i+10]=debug_msg[j][i];
|
158 |
|
|
length=10+debug_msg_size[j];
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
idx+=ZTEX_DEBUG_MSG_LIMIT;
|
162 |
|
|
if (idx>=debug_msg_limit) debug_msg_limit=idx;
|
163 |
|
|
|
164 |
|
|
ZTEX_REC_RET( CyU3PUsbSendEP0Data( length, ztex_ep0buf ) );
|
165 |
|
|
return 0;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
// VR 0x29
|
169 |
|
|
uint8_t ztex_vr_usb3_errors(uint16_t value, uint16_t index, uint16_t length ) {
|
170 |
|
|
uint16_t w;
|
171 |
|
|
ztex_ep0buf[0] = w = ZTEX_USB3_SND_ERROR_COUNT;
|
172 |
|
|
ztex_ep0buf[1] = w >> 8;
|
173 |
|
|
ztex_ep0buf[2] = w = ZTEX_USB3_RCV_ERROR_COUNT;
|
174 |
|
|
ztex_ep0buf[3] = w >> 8;
|
175 |
|
|
ztex_ep0buf[3] = 0; // reserved for future use
|
176 |
|
|
ztex_ep0buf[4] = 0; // reserved for future use
|
177 |
|
|
ztex_ep0buf[5] = 0; // reserved for future use
|
178 |
|
|
ztex_ep0buf[6] = 0; // reserved for future use
|
179 |
|
|
ztex_ep0buf[7] = 0; // reserved for future use
|
180 |
|
|
if (length>8) length = 8;
|
181 |
|
|
ZTEX_REC_RET( CyU3PUsbSendEP0Data( length, ztex_ep0buf ) );
|
182 |
|
|
return 0;
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
uint8_t ztex_register_vendor_req(uint8_t idx, ztex_vendor_func f);
|
186 |
|
|
void ztex_debug_init () {
|
187 |
|
|
ztex_register_vendor_req(0x28, ztex_vr_debug_send);
|
188 |
|
|
ztex_register_vendor_req(0x29, ztex_vr_usb3_errors);
|
189 |
|
|
}
|