dap_config.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. // Copyright (c) 2022, Alex Taradov <alex@taradov.com>. All rights reserved.
  3. #ifndef _DAP_CONFIG_H_
  4. #define _DAP_CONFIG_H_
  5. /*- Includes ----------------------------------------------------------------*/
  6. #include "rp2040.h"
  7. #include "hal_config.h"
  8. /*- Definitions -------------------------------------------------------------*/
  9. #define DAP_CONFIG_ENABLE_JTAG
  10. #define DAP_CONFIG_DEFAULT_PORT DAP_PORT_SWD
  11. #define DAP_CONFIG_DEFAULT_CLOCK 1000000 // Hz
  12. #define DAP_CONFIG_PACKET_SIZE 64
  13. #define DAP_CONFIG_PACKET_COUNT 1
  14. #define DAP_CONFIG_JTAG_DEV_COUNT 8
  15. // DAP_CONFIG_PRODUCT_STR must contain "CMSIS-DAP" to be compatible with the standard
  16. #define DAP_CONFIG_VENDOR_STR "Alex Taradov"
  17. #define DAP_CONFIG_PRODUCT_STR "Generic CMSIS-DAP Adapter"
  18. #define DAP_CONFIG_SER_NUM_STR usb_serial_number
  19. #define DAP_CONFIG_CMSIS_DAP_VER_STR "2.0.0"
  20. //#define DAP_CONFIG_RESET_TARGET_FN target_specific_reset_function
  21. //#define DAP_CONFIG_VENDOR_FN vendor_command_handler_function
  22. // Attribute to use for performance-critical functions
  23. #define DAP_CONFIG_PERFORMANCE_ATTR
  24. // A value at which dap_clock_test() produces 1 kHz output on the SWCLK pin
  25. #define DAP_CONFIG_DELAY_CONSTANT 20000
  26. // A threshold for switching to fast clock (no added delays)
  27. // This is the frequency produced by dap_clock_test(1) on the SWCLK pin
  28. #define DAP_CONFIG_FAST_CLOCK 8550000 // Hz
  29. /*- Prototypes --------------------------------------------------------------*/
  30. extern char usb_serial_number[16];
  31. /*- Implementations ---------------------------------------------------------*/
  32. //-----------------------------------------------------------------------------
  33. static inline void DAP_CONFIG_SWCLK_TCK_write(int value)
  34. {
  35. HAL_GPIO_SWCLK_TCK_write(value);
  36. }
  37. //-----------------------------------------------------------------------------
  38. static inline void DAP_CONFIG_SWDIO_TMS_write(int value)
  39. {
  40. HAL_GPIO_SWDIO_TMS_write(value);
  41. }
  42. //-----------------------------------------------------------------------------
  43. static inline void DAP_CONFIG_TDI_write(int value)
  44. {
  45. #ifdef DAP_CONFIG_ENABLE_JTAG
  46. HAL_GPIO_TDI_write(value);
  47. #else
  48. (void)value;
  49. #endif
  50. }
  51. //-----------------------------------------------------------------------------
  52. static inline void DAP_CONFIG_TDO_write(int value)
  53. {
  54. #ifdef DAP_CONFIG_ENABLE_JTAG
  55. HAL_GPIO_TDO_write(value);
  56. #else
  57. (void)value;
  58. #endif
  59. }
  60. //-----------------------------------------------------------------------------
  61. static inline void DAP_CONFIG_nTRST_write(int value)
  62. {
  63. (void)value;
  64. }
  65. //-----------------------------------------------------------------------------
  66. static inline void DAP_CONFIG_nRESET_write(int value)
  67. {
  68. HAL_GPIO_nRESET_write(value);
  69. }
  70. //-----------------------------------------------------------------------------
  71. static inline int DAP_CONFIG_SWCLK_TCK_read(void)
  72. {
  73. return HAL_GPIO_SWCLK_TCK_read();
  74. }
  75. //-----------------------------------------------------------------------------
  76. static inline int DAP_CONFIG_SWDIO_TMS_read(void)
  77. {
  78. return HAL_GPIO_SWDIO_TMS_read();
  79. }
  80. //-----------------------------------------------------------------------------
  81. static inline int DAP_CONFIG_TDO_read(void)
  82. {
  83. #ifdef DAP_CONFIG_ENABLE_JTAG
  84. return HAL_GPIO_TDO_read();
  85. #else
  86. return 0;
  87. #endif
  88. }
  89. //-----------------------------------------------------------------------------
  90. static inline int DAP_CONFIG_TDI_read(void)
  91. {
  92. #ifdef DAP_CONFIG_ENABLE_JTAG
  93. return HAL_GPIO_TDI_read();
  94. #else
  95. return 0;
  96. #endif
  97. }
  98. //-----------------------------------------------------------------------------
  99. static inline int DAP_CONFIG_nTRST_read(void)
  100. {
  101. return 0;
  102. }
  103. //-----------------------------------------------------------------------------
  104. static inline int DAP_CONFIG_nRESET_read(void)
  105. {
  106. return HAL_GPIO_nRESET_read();
  107. }
  108. //-----------------------------------------------------------------------------
  109. static inline void DAP_CONFIG_SWCLK_TCK_set(void)
  110. {
  111. HAL_GPIO_SWCLK_TCK_set();
  112. }
  113. //-----------------------------------------------------------------------------
  114. static inline void DAP_CONFIG_SWCLK_TCK_clr(void)
  115. {
  116. HAL_GPIO_SWCLK_TCK_clr();
  117. }
  118. //-----------------------------------------------------------------------------
  119. static inline void DAP_CONFIG_SWDIO_TMS_in(void)
  120. {
  121. HAL_GPIO_SWDIO_TMS_in();
  122. }
  123. //-----------------------------------------------------------------------------
  124. static inline void DAP_CONFIG_SWDIO_TMS_out(void)
  125. {
  126. HAL_GPIO_SWDIO_TMS_out();
  127. }
  128. //-----------------------------------------------------------------------------
  129. static inline void DAP_CONFIG_SETUP(void)
  130. {
  131. HAL_GPIO_SWCLK_TCK_in();
  132. HAL_GPIO_SWDIO_TMS_in();
  133. HAL_GPIO_nRESET_in();
  134. #ifdef DAP_CONFIG_ENABLE_JTAG
  135. HAL_GPIO_TDO_in();
  136. HAL_GPIO_TDI_in();
  137. #endif
  138. }
  139. //-----------------------------------------------------------------------------
  140. static inline void DAP_CONFIG_DISCONNECT(void)
  141. {
  142. HAL_GPIO_SWCLK_TCK_in();
  143. HAL_GPIO_SWDIO_TMS_in();
  144. HAL_GPIO_nRESET_in();
  145. #ifdef DAP_CONFIG_ENABLE_JTAG
  146. HAL_GPIO_TDO_in();
  147. HAL_GPIO_TDI_in();
  148. #endif
  149. }
  150. //-----------------------------------------------------------------------------
  151. static inline void DAP_CONFIG_CONNECT_SWD(void)
  152. {
  153. HAL_GPIO_SWDIO_TMS_out();
  154. HAL_GPIO_SWDIO_TMS_set();
  155. HAL_GPIO_SWCLK_TCK_out();
  156. HAL_GPIO_SWCLK_TCK_set();
  157. HAL_GPIO_nRESET_out();
  158. HAL_GPIO_nRESET_set();
  159. #ifdef DAP_CONFIG_ENABLE_JTAG
  160. HAL_GPIO_TDO_in();
  161. HAL_GPIO_TDI_in();
  162. #endif
  163. }
  164. //-----------------------------------------------------------------------------
  165. static inline void DAP_CONFIG_CONNECT_JTAG(void)
  166. {
  167. HAL_GPIO_SWDIO_TMS_out();
  168. HAL_GPIO_SWDIO_TMS_set();
  169. HAL_GPIO_SWCLK_TCK_out();
  170. HAL_GPIO_SWCLK_TCK_set();
  171. HAL_GPIO_nRESET_out();
  172. HAL_GPIO_nRESET_set();
  173. #ifdef DAP_CONFIG_ENABLE_JTAG
  174. HAL_GPIO_TDO_in();
  175. HAL_GPIO_TDI_out();
  176. HAL_GPIO_TDI_set();
  177. #endif
  178. }
  179. //-----------------------------------------------------------------------------
  180. static inline void DAP_CONFIG_LED(int index, int state)
  181. {
  182. (void)index;
  183. (void)state;
  184. }
  185. //-----------------------------------------------------------------------------
  186. __attribute__((always_inline))
  187. static inline void DAP_CONFIG_DELAY(uint32_t cycles)
  188. {
  189. asm volatile (
  190. "1: sub %[cycles], %[cycles], #1 \n"
  191. " bne 1b \n"
  192. : [cycles] "+l"(cycles)
  193. );
  194. }
  195. #endif // _DAP_CONFIG_H_