DAP.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c) 2013-2017 ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * ----------------------------------------------------------------------
  19. *
  20. * $Date: 1. December 2017
  21. * $Revision: V2.0.0
  22. *
  23. * Project: CMSIS-DAP Include
  24. * Title: DAP.h Definitions
  25. *
  26. *---------------------------------------------------------------------------*/
  27. #ifndef __DAP_H__
  28. #define __DAP_H__
  29. // DAP Firmware Version
  30. #ifdef DAP_FW_V1
  31. #define DAP_FW_VER "1.2.0"
  32. #else
  33. #define DAP_FW_VER "2.0.0"
  34. #endif
  35. // DAP Command IDs
  36. #define ID_DAP_Info 0x00U
  37. #define ID_DAP_HostStatus 0x01U
  38. #define ID_DAP_Connect 0x02U
  39. #define ID_DAP_Disconnect 0x03U
  40. #define ID_DAP_TransferConfigure 0x04U
  41. #define ID_DAP_Transfer 0x05U
  42. #define ID_DAP_TransferBlock 0x06U
  43. #define ID_DAP_TransferAbort 0x07U
  44. #define ID_DAP_WriteABORT 0x08U
  45. #define ID_DAP_Delay 0x09U
  46. #define ID_DAP_ResetTarget 0x0AU
  47. #define ID_DAP_SWJ_Pins 0x10U
  48. #define ID_DAP_SWJ_Clock 0x11U
  49. #define ID_DAP_SWJ_Sequence 0x12U
  50. #define ID_DAP_SWD_Configure 0x13U
  51. #define ID_DAP_SWD_Sequence 0x1DU
  52. #define ID_DAP_JTAG_Sequence 0x14U
  53. #define ID_DAP_JTAG_Configure 0x15U
  54. #define ID_DAP_JTAG_IDCODE 0x16U
  55. #define ID_DAP_SWO_Transport 0x17U
  56. #define ID_DAP_SWO_Mode 0x18U
  57. #define ID_DAP_SWO_Baudrate 0x19U
  58. #define ID_DAP_SWO_Control 0x1AU
  59. #define ID_DAP_SWO_Status 0x1BU
  60. #define ID_DAP_SWO_ExtendedStatus 0x1EU
  61. #define ID_DAP_SWO_Data 0x1CU
  62. #define ID_DAP_QueueCommands 0x7EU
  63. #define ID_DAP_ExecuteCommands 0x7FU
  64. // DAP Vendor Command IDs
  65. #define ID_DAP_Vendor0 0x80U
  66. #define ID_DAP_Vendor1 0x81U
  67. #define ID_DAP_Vendor2 0x82U
  68. #define ID_DAP_Vendor3 0x83U
  69. #define ID_DAP_Vendor4 0x84U
  70. #define ID_DAP_Vendor5 0x85U
  71. #define ID_DAP_Vendor6 0x86U
  72. #define ID_DAP_Vendor7 0x87U
  73. #define ID_DAP_Vendor8 0x88U
  74. #define ID_DAP_Vendor9 0x89U
  75. #define ID_DAP_Vendor10 0x8AU
  76. #define ID_DAP_Vendor11 0x8BU
  77. #define ID_DAP_Vendor12 0x8CU
  78. #define ID_DAP_Vendor13 0x8DU
  79. #define ID_DAP_Vendor14 0x8EU
  80. #define ID_DAP_Vendor15 0x8FU
  81. #define ID_DAP_Vendor16 0x90U
  82. #define ID_DAP_Vendor17 0x91U
  83. #define ID_DAP_Vendor18 0x92U
  84. #define ID_DAP_Vendor19 0x93U
  85. #define ID_DAP_Vendor20 0x94U
  86. #define ID_DAP_Vendor21 0x95U
  87. #define ID_DAP_Vendor22 0x96U
  88. #define ID_DAP_Vendor23 0x97U
  89. #define ID_DAP_Vendor24 0x98U
  90. #define ID_DAP_Vendor25 0x99U
  91. #define ID_DAP_Vendor26 0x9AU
  92. #define ID_DAP_Vendor27 0x9BU
  93. #define ID_DAP_Vendor28 0x9CU
  94. #define ID_DAP_Vendor29 0x9DU
  95. #define ID_DAP_Vendor30 0x9EU
  96. #define ID_DAP_Vendor31 0x9FU
  97. #define ID_DAP_Invalid 0xFFU
  98. // DAP Status Code
  99. #define DAP_OK 0U
  100. #define DAP_ERROR 0xFFU
  101. // DAP ID
  102. #define DAP_ID_VENDOR 1U
  103. #define DAP_ID_PRODUCT 2U
  104. #define DAP_ID_SER_NUM 3U
  105. #define DAP_ID_FW_VER 4U
  106. #define DAP_ID_DEVICE_VENDOR 5U
  107. #define DAP_ID_DEVICE_NAME 6U
  108. #define DAP_ID_CAPABILITIES 0xF0U
  109. #define DAP_ID_TIMESTAMP_CLOCK 0xF1U
  110. #define DAP_ID_SWO_BUFFER_SIZE 0xFDU
  111. #define DAP_ID_PACKET_COUNT 0xFEU
  112. #define DAP_ID_PACKET_SIZE 0xFFU
  113. // DAP Host Status
  114. #define DAP_DEBUGGER_CONNECTED 0U
  115. #define DAP_TARGET_RUNNING 1U
  116. // DAP Port
  117. #define DAP_PORT_AUTODETECT 0U // Autodetect Port
  118. #define DAP_PORT_DISABLED 0U // Port Disabled (I/O pins in High-Z)
  119. #define DAP_PORT_SWD 1U // SWD Port (SWCLK, SWDIO) + nRESET
  120. #define DAP_PORT_JTAG 2U // JTAG Port (TCK, TMS, TDI, TDO, nTRST) + nRESET
  121. // DAP SWJ Pins
  122. #define DAP_SWJ_SWCLK_TCK 0 // SWCLK/TCK
  123. #define DAP_SWJ_SWDIO_TMS 1 // SWDIO/TMS
  124. #define DAP_SWJ_TDI 2 // TDI
  125. #define DAP_SWJ_TDO 3 // TDO
  126. #define DAP_SWJ_nTRST 5 // nTRST
  127. #define DAP_SWJ_nRESET 7 // nRESET
  128. // DAP Transfer Request
  129. #define DAP_TRANSFER_APnDP (1U<<0)
  130. #define DAP_TRANSFER_RnW (1U<<1)
  131. #define DAP_TRANSFER_A2 (1U<<2)
  132. #define DAP_TRANSFER_A3 (1U<<3)
  133. #define DAP_TRANSFER_MATCH_VALUE (1U<<4)
  134. #define DAP_TRANSFER_MATCH_MASK (1U<<5)
  135. #define DAP_TRANSFER_TIMESTAMP (1U<<7)
  136. // DAP Transfer Response
  137. #define DAP_TRANSFER_OK (1U<<0)
  138. #define DAP_TRANSFER_WAIT (1U<<1)
  139. #define DAP_TRANSFER_FAULT (1U<<2)
  140. #define DAP_TRANSFER_ERROR (1U<<3)
  141. #define DAP_TRANSFER_MISMATCH (1U<<4)
  142. // DAP SWO Trace Mode
  143. #define DAP_SWO_OFF 0U
  144. #define DAP_SWO_UART 1U
  145. #define DAP_SWO_MANCHESTER 2U
  146. // DAP SWO Trace Status
  147. #define DAP_SWO_CAPTURE_ACTIVE (1U<<0)
  148. #define DAP_SWO_CAPTURE_PAUSED (1U<<1)
  149. #define DAP_SWO_STREAM_ERROR (1U<<6)
  150. #define DAP_SWO_BUFFER_OVERRUN (1U<<7)
  151. // Debug Port Register Addresses
  152. #define DP_IDCODE 0x00U // IDCODE Register (SW Read only)
  153. #define DP_ABORT 0x00U // Abort Register (SW Write only)
  154. #define DP_CTRL_STAT 0x04U // Control & Status
  155. #define DP_WCR 0x04U // Wire Control Register (SW Only)
  156. #define DP_SELECT 0x08U // Select Register (JTAG R/W & SW W)
  157. #define DP_RESEND 0x08U // Resend (SW Read Only)
  158. #define DP_RDBUFF 0x0CU // Read Buffer (Read Only)
  159. // JTAG IR Codes
  160. #define JTAG_ABORT 0x08U
  161. #define JTAG_DPACC 0x0AU
  162. #define JTAG_APACC 0x0BU
  163. #define JTAG_IDCODE 0x0EU
  164. #define JTAG_BYPASS 0x0FU
  165. // JTAG Sequence Info
  166. #define JTAG_SEQUENCE_TCK 0x3FU // TCK count
  167. #define JTAG_SEQUENCE_TMS 0x40U // TMS value
  168. #define JTAG_SEQUENCE_TDO 0x80U // TDO capture
  169. // SWD Sequence Info
  170. #define SWD_SEQUENCE_CLK 0x3FU // SWCLK count
  171. #define SWD_SEQUENCE_DIN 0x80U // SWDIO capture
  172. #include <stddef.h>
  173. #include <stdint.h>
  174. #include "cmsis_compiler.h"
  175. // DAP Data structure
  176. typedef struct {
  177. uint8_t debug_port; // Debug Port
  178. uint8_t fast_clock; // Fast Clock Flag
  179. uint8_t padding[2];
  180. uint32_t clock_delay; // Clock Delay
  181. uint32_t timestamp; // Last captured Timestamp
  182. struct { // Transfer Configuration
  183. uint8_t idle_cycles; // Idle cycles after transfer
  184. uint8_t padding[3];
  185. uint16_t retry_count; // Number of retries after WAIT response
  186. uint16_t match_retry; // Number of retries if read value does not match
  187. uint32_t match_mask; // Match Mask
  188. } transfer;
  189. #if (DAP_SWD != 0)
  190. struct { // SWD Configuration
  191. uint8_t turnaround; // Turnaround period
  192. uint8_t data_phase; // Always generate Data Phase
  193. } swd_conf;
  194. #endif
  195. #if (DAP_JTAG != 0)
  196. struct { // JTAG Device Chain
  197. uint8_t count; // Number of devices
  198. uint8_t index; // Device index (device at TDO has index 0)
  199. #if (DAP_JTAG_DEV_CNT != 0)
  200. uint8_t ir_length[DAP_JTAG_DEV_CNT]; // IR Length in bits
  201. uint16_t ir_before[DAP_JTAG_DEV_CNT]; // Bits before IR
  202. uint16_t ir_after [DAP_JTAG_DEV_CNT]; // Bits after IR
  203. #endif
  204. } jtag_dev;
  205. #endif
  206. } DAP_Data_t;
  207. extern DAP_Data_t DAP_Data; // DAP Data
  208. extern volatile uint8_t DAP_TransferAbort; // Transfer Abort Flag
  209. // Functions
  210. extern void SWJ_Sequence (uint32_t count, const uint8_t *data);
  211. extern void SWD_Sequence (uint32_t info, const uint8_t *swdo, uint8_t *swdi);
  212. extern void JTAG_Sequence (uint32_t info, const uint8_t *tdi, uint8_t *tdo);
  213. extern void JTAG_IR (uint32_t ir);
  214. extern uint32_t JTAG_ReadIDCode (void);
  215. extern void JTAG_WriteAbort (uint32_t data);
  216. extern uint8_t JTAG_Transfer (uint32_t request, uint32_t *data);
  217. extern uint8_t SWD_Transfer (uint32_t request, uint32_t *data);
  218. extern void Delayms (uint32_t delay);
  219. extern uint32_t SWO_Transport (const uint8_t *request, uint8_t *response);
  220. extern uint32_t SWO_Mode (const uint8_t *request, uint8_t *response);
  221. extern uint32_t SWO_Baudrate (const uint8_t *request, uint8_t *response);
  222. extern uint32_t SWO_Control (const uint8_t *request, uint8_t *response);
  223. extern uint32_t SWO_Status (uint8_t *response);
  224. extern uint32_t SWO_ExtendedStatus (const uint8_t *request, uint8_t *response);
  225. extern uint32_t SWO_Data (const uint8_t *request, uint8_t *response);
  226. extern void SWO_QueueTransfer (uint8_t *buf, uint32_t num);
  227. extern void SWO_AbortTransfer (void);
  228. extern void SWO_TransferComplete (void);
  229. extern uint32_t UART_SWO_Mode (uint32_t enable);
  230. extern uint32_t UART_SWO_Baudrate (uint32_t baudrate);
  231. extern uint32_t UART_SWO_Control (uint32_t active);
  232. extern void UART_SWO_Capture (uint8_t *buf, uint32_t num);
  233. extern uint32_t UART_SWO_GetCount (void);
  234. extern uint32_t Manchester_SWO_Mode (uint32_t enable);
  235. extern uint32_t Manchester_SWO_Baudrate (uint32_t baudrate);
  236. extern uint32_t Manchester_SWO_Control (uint32_t active);
  237. extern void Manchester_SWO_Capture (uint8_t *buf, uint32_t num);
  238. extern uint32_t Manchester_SWO_GetCount (void);
  239. extern uint32_t DAP_ProcessVendorCommand (const uint8_t *request, uint8_t *response);
  240. extern uint32_t DAP_ProcessCommand (const uint8_t *request, uint8_t *response);
  241. extern uint32_t DAP_ExecuteCommand (const uint8_t *request, uint8_t *response);
  242. extern void DAP_Setup (void);
  243. // Configurable delay for clock generation
  244. #ifndef DELAY_SLOW_CYCLES
  245. #define DELAY_SLOW_CYCLES 3U // Number of cycles for one iteration
  246. #endif
  247. __STATIC_FORCEINLINE void PIN_DELAY_SLOW (uint32_t delay) {
  248. uint32_t count;
  249. count = delay;
  250. while (--count);
  251. }
  252. // Fixed delay for fast clock generation
  253. #ifndef DELAY_FAST_CYCLES
  254. #define DELAY_FAST_CYCLES 0U // Number of cycles: 0..3
  255. #endif
  256. __STATIC_FORCEINLINE void PIN_DELAY_FAST (void) {
  257. #if (DELAY_FAST_CYCLES >= 1U)
  258. __nop();
  259. #endif
  260. #if (DELAY_FAST_CYCLES >= 2U)
  261. __nop();
  262. #endif
  263. #if (DELAY_FAST_CYCLES >= 3U)
  264. __nop();
  265. #endif
  266. }
  267. #endif /* __DAP_H__ */