DAP.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) 2013-2016 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. * http://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: 20. May 2015
  21. * $Revision: V1.10
  22. *
  23. * Project: CMSIS-DAP Include
  24. * Title: DAP.h Definitions
  25. *
  26. *---------------------------------------------------------------------------*/
  27. #ifndef __DAP_H__
  28. #define __DAP_H__
  29. // DAP Command IDs
  30. #define ID_DAP_Info 0x00U
  31. #define ID_DAP_HostStatus 0x01U
  32. #define ID_DAP_Connect 0x02U
  33. #define ID_DAP_Disconnect 0x03U
  34. #define ID_DAP_TransferConfigure 0x04U
  35. #define ID_DAP_Transfer 0x05U
  36. #define ID_DAP_TransferBlock 0x06U
  37. #define ID_DAP_TransferAbort 0x07U
  38. #define ID_DAP_WriteABORT 0x08U
  39. #define ID_DAP_Delay 0x09U
  40. #define ID_DAP_ResetTarget 0x0AU
  41. #define ID_DAP_SWJ_Pins 0x10U
  42. #define ID_DAP_SWJ_Clock 0x11U
  43. #define ID_DAP_SWJ_Sequence 0x12U
  44. #define ID_DAP_SWD_Configure 0x13U
  45. #define ID_DAP_JTAG_Sequence 0x14U
  46. #define ID_DAP_JTAG_Configure 0x15U
  47. #define ID_DAP_JTAG_IDCODE 0x16U
  48. #define ID_DAP_SWO_Transport 0x17U
  49. #define ID_DAP_SWO_Mode 0x18U
  50. #define ID_DAP_SWO_Baudrate 0x19U
  51. #define ID_DAP_SWO_Control 0x1AU
  52. #define ID_DAP_SWO_Status 0x1BU
  53. #define ID_DAP_SWO_Data 0x1CU
  54. #define ID_DAP_QueueCommands 0x7EU
  55. #define ID_DAP_ExecuteCommands 0x7FU
  56. // DAP Vendor Command IDs
  57. #define ID_DAP_Vendor0 0x80U
  58. #define ID_DAP_Vendor1 0x81U
  59. #define ID_DAP_Vendor2 0x82U
  60. #define ID_DAP_Vendor3 0x83U
  61. #define ID_DAP_Vendor4 0x84U
  62. #define ID_DAP_Vendor5 0x85U
  63. #define ID_DAP_Vendor6 0x86U
  64. #define ID_DAP_Vendor7 0x87U
  65. #define ID_DAP_Vendor8 0x88U
  66. #define ID_DAP_Vendor9 0x89U
  67. #define ID_DAP_Vendor10 0x8AU
  68. #define ID_DAP_Vendor11 0x8BU
  69. #define ID_DAP_Vendor12 0x8CU
  70. #define ID_DAP_Vendor13 0x8DU
  71. #define ID_DAP_Vendor14 0x8EU
  72. #define ID_DAP_Vendor15 0x8FU
  73. #define ID_DAP_Vendor16 0x90U
  74. #define ID_DAP_Vendor17 0x91U
  75. #define ID_DAP_Vendor18 0x92U
  76. #define ID_DAP_Vendor19 0x93U
  77. #define ID_DAP_Vendor20 0x94U
  78. #define ID_DAP_Vendor21 0x95U
  79. #define ID_DAP_Vendor22 0x96U
  80. #define ID_DAP_Vendor23 0x97U
  81. #define ID_DAP_Vendor24 0x98U
  82. #define ID_DAP_Vendor25 0x99U
  83. #define ID_DAP_Vendor26 0x9AU
  84. #define ID_DAP_Vendor27 0x9BU
  85. #define ID_DAP_Vendor28 0x9CU
  86. #define ID_DAP_Vendor29 0x9DU
  87. #define ID_DAP_Vendor30 0x9EU
  88. #define ID_DAP_Vendor31 0x9FU
  89. #define ID_DAP_Invalid 0xFFU
  90. // DAP Status Code
  91. #define DAP_OK 0U
  92. #define DAP_ERROR 0xFFU
  93. // DAP ID
  94. #define DAP_ID_VENDOR 1U
  95. #define DAP_ID_PRODUCT 2U
  96. #define DAP_ID_SER_NUM 3U
  97. #define DAP_ID_FW_VER 4U
  98. #define DAP_ID_DEVICE_VENDOR 5U
  99. #define DAP_ID_DEVICE_NAME 6U
  100. #define DAP_ID_CAPABILITIES 0xF0U
  101. #define DAP_ID_SWO_BUFFER_SIZE 0xFDU
  102. #define DAP_ID_PACKET_COUNT 0xFEU
  103. #define DAP_ID_PACKET_SIZE 0xFFU
  104. // DAP Host Status
  105. #define DAP_DEBUGGER_CONNECTED 0U
  106. #define DAP_TARGET_RUNNING 1U
  107. // DAP Port
  108. #define DAP_PORT_AUTODETECT 0U // Autodetect Port
  109. #define DAP_PORT_DISABLED 0U // Port Disabled (I/O pins in High-Z)
  110. #define DAP_PORT_SWD 1U // SWD Port (SWCLK, SWDIO) + nRESET
  111. #define DAP_PORT_JTAG 2U // JTAG Port (TCK, TMS, TDI, TDO, nTRST) + nRESET
  112. // DAP SWJ Pins
  113. #define DAP_SWJ_SWCLK_TCK 0 // SWCLK/TCK
  114. #define DAP_SWJ_SWDIO_TMS 1 // SWDIO/TMS
  115. #define DAP_SWJ_TDI 2 // TDI
  116. #define DAP_SWJ_TDO 3 // TDO
  117. #define DAP_SWJ_nTRST 5 // nTRST
  118. #define DAP_SWJ_nRESET 7 // nRESET
  119. // DAP Transfer Request
  120. #define DAP_TRANSFER_APnDP (1U<<0)
  121. #define DAP_TRANSFER_RnW (1U<<1)
  122. #define DAP_TRANSFER_A2 (1U<<2)
  123. #define DAP_TRANSFER_A3 (1U<<3)
  124. #define DAP_TRANSFER_MATCH_VALUE (1U<<4)
  125. #define DAP_TRANSFER_MATCH_MASK (1U<<5)
  126. // DAP Transfer Response
  127. #define DAP_TRANSFER_OK (1U<<0)
  128. #define DAP_TRANSFER_WAIT (1U<<1)
  129. #define DAP_TRANSFER_FAULT (1U<<2)
  130. #define DAP_TRANSFER_ERROR (1U<<3)
  131. #define DAP_TRANSFER_MISMATCH (1U<<4)
  132. // DAP SWO Trace Mode
  133. #define DAP_SWO_OFF 0U
  134. #define DAP_SWO_UART 1U
  135. #define DAP_SWO_MANCHESTER 2U
  136. // DAP SWO Trace Status
  137. #define DAP_SWO_CAPTURE_ACTIVE (1U<<0)
  138. #define DAP_SWO_CAPTURE_PAUSED (1U<<1)
  139. #define DAP_SWO_STREAM_ERROR (1U<<6)
  140. #define DAP_SWO_BUFFER_OVERRUN (1U<<7)
  141. // Debug Port Register Addresses
  142. #define DP_IDCODE 0x00U // IDCODE Register (SW Read only)
  143. #define DP_ABORT 0x00U // Abort Register (SW Write only)
  144. #define DP_CTRL_STAT 0x04U // Control & Status
  145. #define DP_WCR 0x04U // Wire Control Register (SW Only)
  146. #define DP_SELECT 0x08U // Select Register (JTAG R/W & SW W)
  147. #define DP_RESEND 0x08U // Resend (SW Read Only)
  148. #define DP_RDBUFF 0x0CU // Read Buffer (Read Only)
  149. // JTAG IR Codes
  150. #define JTAG_ABORT 0x08U
  151. #define JTAG_DPACC 0x0AU
  152. #define JTAG_APACC 0x0BU
  153. #define JTAG_IDCODE 0x0EU
  154. #define JTAG_BYPASS 0x0FU
  155. // JTAG Sequence Info
  156. #define JTAG_SEQUENCE_TCK 0x3FU // TCK count
  157. #define JTAG_SEQUENCE_TMS 0x40U // TMS value
  158. #define JTAG_SEQUENCE_TDO 0x80U // TDO capture
  159. #include <stddef.h>
  160. #include <stdint.h>
  161. // DAP Data structure
  162. typedef struct {
  163. uint8_t debug_port; // Debug Port
  164. uint8_t fast_clock; // Fast Clock Flag
  165. uint32_t clock_delay; // Clock Delay
  166. struct { // Transfer Configuration
  167. uint8_t idle_cycles; // Idle cycles after transfer
  168. uint16_t retry_count; // Number of retries after WAIT response
  169. uint16_t match_retry; // Number of retries if read value does not match
  170. uint32_t match_mask; // Match Mask
  171. } transfer;
  172. #if (DAP_SWD != 0)
  173. struct { // SWD Configuration
  174. uint8_t turnaround; // Turnaround period
  175. uint8_t data_phase; // Always generate Data Phase
  176. } swd_conf;
  177. #endif
  178. #if (DAP_JTAG != 0)
  179. struct { // JTAG Device Chain
  180. uint8_t count; // Number of devices
  181. uint8_t index; // Device index (device at TDO has index 0)
  182. #if (DAP_JTAG_DEV_CNT != 0)
  183. uint8_t ir_length[DAP_JTAG_DEV_CNT]; // IR Length in bits
  184. uint16_t ir_before[DAP_JTAG_DEV_CNT]; // Bits before IR
  185. uint16_t ir_after [DAP_JTAG_DEV_CNT]; // Bits after IR
  186. #endif
  187. } jtag_dev;
  188. #endif
  189. } DAP_Data_t;
  190. extern DAP_Data_t DAP_Data; // DAP Data
  191. extern volatile uint8_t DAP_TransferAbort; // Transfer Abort Flag
  192. // Functions
  193. extern void SWJ_Sequence (uint32_t count, const uint8_t *data);
  194. extern void JTAG_Sequence (uint32_t info, const uint8_t *tdi, uint8_t *tdo);
  195. extern void JTAG_IR (uint32_t ir);
  196. extern uint32_t JTAG_ReadIDCode (void);
  197. extern void JTAG_WriteAbort (uint32_t data);
  198. extern uint8_t JTAG_Transfer (uint32_t request, uint32_t *data);
  199. extern uint8_t SWD_Transfer (uint32_t request, uint32_t *data);
  200. extern void Delayms (uint32_t delay);
  201. extern uint32_t SWO_Transport (const uint8_t *request, uint8_t *response);
  202. extern uint32_t SWO_Mode (const uint8_t *request, uint8_t *response);
  203. extern uint32_t SWO_Baudrate (const uint8_t *request, uint8_t *response);
  204. extern uint32_t SWO_Control (const uint8_t *request, uint8_t *response);
  205. extern uint32_t SWO_Status (uint8_t *response);
  206. extern uint32_t SWO_Data (const uint8_t *request, uint8_t *response);
  207. extern uint32_t DAP_ProcessVendorCommand (const uint8_t *request, uint8_t *response);
  208. extern uint32_t DAP_ProcessCommand (const uint8_t *request, uint8_t *response);
  209. extern uint32_t DAP_ExecuteCommand (const uint8_t *request, uint8_t *response);
  210. extern void DAP_Setup (void);
  211. // Configurable delay for clock generation
  212. #ifndef DELAY_SLOW_CYCLES
  213. #define DELAY_SLOW_CYCLES 3U // Number of cycles for one iteration
  214. #endif
  215. static __forceinline void PIN_DELAY_SLOW (uint32_t delay) {
  216. uint32_t count;
  217. count = delay;
  218. while (--count);
  219. }
  220. // Fixed delay for fast clock generation
  221. #ifndef DELAY_FAST_CYCLES
  222. #define DELAY_FAST_CYCLES 0U // Number of cycles: 0..3
  223. #endif
  224. static __forceinline void PIN_DELAY_FAST (void) {
  225. #if (DELAY_FAST_CYCLES >= 1U)
  226. __nop();
  227. #endif
  228. #if (DELAY_FAST_CYCLES >= 2U)
  229. __nop();
  230. #endif
  231. #if (DELAY_FAST_CYCLES >= 3U)
  232. __nop();
  233. #endif
  234. }
  235. #endif /* __DAP_H__ */