dap_config.h 6.2 KB

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