libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
daplink_v2_def.hpp
1#pragma once
2
3#include <cstdint>
4
5#include "libxr_def.hpp"
6#include "timebase.hpp"
7
8namespace LibXR::USB
9{
10
18namespace DapLinkV2Def
19{
20
21// ==============================
22// Limits / 限制
23// ==============================
24
25static constexpr std::uint16_t MAX_REQUEST_SIZE =
26 512u;
27static constexpr std::uint16_t MAX_RESPONSE_SIZE =
28 512u;
29
30// ==============================
31// CMSIS-DAP v2 Command IDs / 命令号
32// 注意:按规范值定义(SWO_Data=0x1C, SWD_Sequence=0x1D)
33// Note: Values follow the CMSIS-DAP specification (SWO_Data=0x1C, SWD_Sequence=0x1D).
34// ==============================
35
36enum class CommandId : std::uint8_t
37{
38 // Core (0x00-0x0F)
39 INFO = 0x00,
40 HOST_STATUS = 0x01,
41 CONNECT = 0x02,
42 DISCONNECT = 0x03,
43 TRANSFER_CONFIGURE = 0x04,
44 TRANSFER = 0x05,
45 TRANSFER_BLOCK = 0x06,
46 TRANSFER_ABORT = 0x07,
47 WRITE_ABORT = 0x08,
48 DELAY = 0x09,
49 RESET_TARGET = 0x0A,
50
51 // SWJ (0x10-0x1F)
52 SWJ_PINS = 0x10,
53 SWJ_CLOCK = 0x11,
54 SWJ_SEQUENCE = 0x12,
55 SWD_CONFIGURE = 0x13,
56 JTAG_SEQUENCE = 0x14,
57 JTAG_CONFIGURE = 0x15,
58 JTAG_IDCODE = 0x16,
59
60 // SWO (v2)
61 SWO_TRANSPORT = 0x17,
62 SWO_MODE = 0x18,
63 SWO_BAUDRATE = 0x19,
64 SWO_CONTROL = 0x1A,
65 SWO_STATUS = 0x1B,
66 SWO_DATA = 0x1C,
67
68 // SWD sequence (v2)
69 SWD_SEQUENCE = 0x1D,
70
71 // Queue (0x7E-0x7F)
72 QUEUE_COMMANDS = 0x7E,
73 EXECUTE_COMMANDS = 0x7F,
74
75 INVALID = 0xFF
76};
77
78// ==============================
79// DAP_Info IDs / 信息 ID
80// ==============================
81
82enum class InfoId : std::uint8_t
83{
84 VENDOR = 0x01,
85 PRODUCT = 0x02,
86 SERIAL_NUMBER = 0x03,
87 FIRMWARE_VERSION = 0x04,
88
89 DEVICE_VENDOR = 0x05,
90 DEVICE_NAME = 0x06,
91 BOARD_VENDOR = 0x07,
92 BOARD_NAME = 0x08,
93 PRODUCT_FIRMWARE_VERSION = 0x09,
94
95 CAPABILITIES = 0xF0,
96 TIMESTAMP_CLOCK = 0xF1,
97 SWO_BUFFER_SIZE = 0xFD,
98 PACKET_COUNT = 0xFE,
99 PACKET_SIZE = 0xFF
100};
101
102// ==============================
103// Capabilities bits / 能力位(DAP_Info: Capabilities)
104// ==============================
105
106static constexpr std::uint8_t DAP_CAP_SWD = 0x01u;
107static constexpr std::uint8_t DAP_CAP_JTAG = 0x02u;
108static constexpr std::uint8_t DAP_CAP_SWO = 0x04u;
109
110// ==============================
111// Status / Port / 状态与端口
112// ==============================
113
114enum class Status : std::uint8_t
115{
116 OK = 0x00,
117 ERROR = 0xFF
118};
119
120enum class Port : std::uint8_t
121{
122 DISABLED = 0x00,
123 SWD = 0x01,
124 JTAG = 0x02
125};
126
127enum class DebugPort : std::uint8_t
128{
129 DISABLED = 0,
130 SWD = 1,
131 JTAG = 2
132};
133
134// ==============================
135// Transfer Request bits / 传输请求位
136// ==============================
137
138static constexpr std::uint8_t DAP_TRANSFER_APNDP = (1u << 0);
139static constexpr std::uint8_t DAP_TRANSFER_RNW = (1u << 1);
140static constexpr std::uint8_t DAP_TRANSFER_A2 = (1u << 2);
141static constexpr std::uint8_t DAP_TRANSFER_A3 = (1u << 3);
142static constexpr std::uint8_t DAP_TRANSFER_MATCH_VALUE = (1u << 4);
143static constexpr std::uint8_t DAP_TRANSFER_MATCH_MASK = (1u << 5);
144static constexpr std::uint8_t DAP_TRANSFER_TIMESTAMP = (1u << 7);
145
146// Transfer Response bits / 传输响应位
147static constexpr std::uint8_t DAP_TRANSFER_OK = (1u << 0);
148static constexpr std::uint8_t DAP_TRANSFER_WAIT = (1u << 1);
149static constexpr std::uint8_t DAP_TRANSFER_FAULT = (1u << 2);
150static constexpr std::uint8_t DAP_TRANSFER_ERROR = (1u << 3);
151static constexpr std::uint8_t DAP_TRANSFER_MISMATCH = (1u << 4);
152static constexpr std::uint8_t DAP_TRANSFER_NO_TARGET = (1u << 7);
153
154// ==============================
155// SWJ Pins bits / SWJ 引脚位
156// ==============================
157
158static constexpr std::uint8_t DAP_SWJ_SWCLK_TCK = (1u << 0);
159static constexpr std::uint8_t DAP_SWJ_SWDIO_TMS = (1u << 1);
160static constexpr std::uint8_t DAP_SWJ_TDI = (1u << 2);
161static constexpr std::uint8_t DAP_SWJ_TDO = (1u << 3);
162static constexpr std::uint8_t DAP_SWJ_NTRST = (1u << 5);
163static constexpr std::uint8_t DAP_SWJ_NRESET = (1u << 7);
164
165// ==============================
166// SWD / JTAG Sequence fields / 序列字段
167// ==============================
168
169static constexpr std::uint8_t SWD_SEQUENCE_CLK = 0x3Fu;
170static constexpr std::uint8_t SWD_SEQUENCE_DIN = (1u << 7);
171
172static constexpr std::uint8_t JTAG_SEQUENCE_TCK = 0x3Fu;
173static constexpr std::uint8_t JTAG_SEQUENCE_TMS = (1u << 6);
174static constexpr std::uint8_t JTAG_SEQUENCE_TDO = (1u << 7);
175
176// ==============================
177// Helpers / 工具函数
178// ==============================
179
182static inline constexpr std::uint8_t req_addr2b(std::uint8_t req)
183{
184 return static_cast<std::uint8_t>(((req & DAP_TRANSFER_A2) ? 1u : 0u) |
185 ((req & DAP_TRANSFER_A3) ? 2u : 0u));
186}
187
189static inline constexpr bool req_is_ap(std::uint8_t req)
190{
191 return (req & DAP_TRANSFER_APNDP) != 0u;
192}
193
195static inline constexpr bool req_is_read(std::uint8_t req)
196{
197 return (req & DAP_TRANSFER_RNW) != 0u;
198}
199
201static inline constexpr bool req_need_timestamp(std::uint8_t req)
202{
203 return (req & DAP_TRANSFER_TIMESTAMP) != 0u;
204}
205
206// ==============================
207// State structs / 运行态结构体
208// ==============================
209
211{
212 std::uint8_t idle_cycles = 0u;
213 std::uint16_t retry_count = 100u;
214 std::uint16_t match_retry = 0u;
215 std::uint32_t match_mask = 0u;
216};
217
219{
220 std::uint8_t turnaround = 1u;
221 bool data_phase = false;
222};
223
224struct State
225{
226 DebugPort debug_port = DebugPort::DISABLED;
227 volatile bool transfer_abort = false;
228
231};
232
234{
235 std::uint16_t request_consumed = 0u;
236 std::uint16_t response_generated = 0u;
237};
238
239} // namespace DapLinkV2Def
240
241} // namespace LibXR::USB
@ OK
操作成功 | Operation successful