libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
swd_protocol.hpp
1// swd_protocol.hpp
2#pragma once
3
4#include <cstdint>
5
6namespace LibXR::Debug::SwdProtocol
7{
11enum class Port : uint8_t
12{
13 DP = 0,
14 AP = 1,
15};
16
20enum class Pin : uint8_t
21{
22 SWCLK = 0,
23 SWDIO = 1,
24};
25
32enum class Ack : uint8_t
33{
34 NO_ACK = 0x0,
35 OK = 0x1,
36 WAIT = 0x2,
37 FAULT = 0x4,
38 PROTOCOL = 0x7,
39};
40
44struct Request
45{
46 Port port = Port::DP;
47 bool rnw =
48 true;
49 uint8_t addr2b = 0;
50 uint32_t wdata = 0;
51};
52
57{
58 Ack ack = Ack::PROTOCOL;
59 uint32_t rdata = 0;
60 bool parity_ok = true;
61};
62
66enum class DpReadReg : uint8_t
67{
68 IDCODE = 0,
69 CTRL_STAT = 1,
70 SELECT = 2,
71 RDBUFF = 3,
72};
73
77enum class DpWriteReg : uint8_t
78{
79 ABORT = 0,
80 CTRL_STAT = 1,
81 SELECT = 2,
82};
83
90inline constexpr uint32_t DP_ABORT_DAPABORT = (1u << 0);
91inline constexpr uint32_t DP_ABORT_STKCMPCLR = (1u << 1);
92inline constexpr uint32_t DP_ABORT_STKERRCLR = (1u << 2);
93inline constexpr uint32_t DP_ABORT_WDERRCLR = (1u << 3);
94inline constexpr uint32_t DP_ABORT_ORUNERRCLR = (1u << 4);
95
99inline constexpr uint32_t DP_CTRLSTAT_CDBGPWRUPREQ =
100 (1u << 28);
101inline constexpr uint32_t DP_CTRLSTAT_CDBGPWRUPACK =
102 (1u << 29);
103inline constexpr uint32_t DP_CTRLSTAT_CSYSPWRUPREQ =
104 (1u << 30);
105inline constexpr uint32_t DP_CTRLSTAT_CSYSPWRUPACK =
106 (1u << 31);
107
117constexpr uint32_t make_select(uint8_t apsel, uint8_t apbanksel, uint8_t dpbanksel = 0)
118{
119 return (static_cast<uint32_t>(apsel) << 24) |
120 ((static_cast<uint32_t>(apbanksel) & 0x0Fu) << 4) |
121 (static_cast<uint32_t>(dpbanksel) & 0x0Fu);
122}
123
130constexpr Request make_dp_read_req(DpReadReg reg)
131{
132 return Request{Port::DP, true, static_cast<uint8_t>(reg), 0u};
133}
134
142constexpr Request make_dp_write_req(DpWriteReg reg, uint32_t wdata)
143{
144 return Request{Port::DP, false, static_cast<uint8_t>(reg), wdata};
145}
146
153constexpr Request make_ap_read_req(uint8_t addr2b)
154{
155 return Request{Port::AP, true, static_cast<uint8_t>(addr2b & 0x03u), 0u};
156}
157
165constexpr Request make_ap_write_req(uint8_t addr2b, uint32_t wdata)
166{
167 return Request{Port::AP, false, static_cast<uint8_t>(addr2b & 0x03u), wdata};
168}
169
170} // namespace LibXR::Debug::SwdProtocol
SWD 传输请求 / SWD transfer request.
bool rnw
读写标志:true=读,false=写 / Read-not-write: true=read, false=write
uint32_t wdata
写数据(仅写请求有效)/ Write data (valid for write requests)
uint8_t addr2b
A[3:2] 两位地址编码(0..3)/ A[3:2] encoded as 0..3.
Port port
目标端口(DP/AP)/ Target port (DP/AP)
SWD 传输响应 / SWD transfer response.
bool parity_ok
奇偶校验是否正确 / Whether parity is OK
uint32_t rdata
读数据(仅读响应有效)/ Read data (valid for read responses)