swd.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "swd.h"
  2. #include <furi.h>
  3. #include <furi_hal_resources.h>
  4. #define TAG "VgmSwd"
  5. #define SWD_REQUEST_LEN (8U)
  6. #define SWD_RESPONSE_LEN (3U)
  7. #define SWD_DATA_LEN (32U)
  8. #define SWD_ALERT_SEQUENCE_0 (0x6209F392UL)
  9. #define SWD_ALERT_SEQUENCE_1 (0x86852D95UL)
  10. #define SWD_ALERT_SEQUENCE_2 (0xE3DDAFE9UL)
  11. #define SWD_ALERT_SEQUENCE_3 (0x19BC0EA2UL)
  12. #define SWD_ACTIVATION_CODE (0x1AU)
  13. #define SWD_SLEEP_SEQUENCE (0xE3BCU)
  14. #define SWD_READ_REQUEST_INIT (0x85U)
  15. #define SWD_WRITE_REQUEST_INIT (0x81U)
  16. #define SWD_REQUEST_INIT (0x81U)
  17. typedef enum {
  18. SwdioDirectionIn,
  19. SwdioDirectionOut,
  20. } SwdioDirection;
  21. typedef enum {
  22. SwdResponseOk = 1U,
  23. SwdResponseWait = 2U,
  24. SwdResponseFault = 4U,
  25. SwdResponseNone = 7U,
  26. } SwdResponse;
  27. typedef enum {
  28. SwdAccessTypeDp = 0U << 1,
  29. SwdAccessTypeAp = 1U << 1,
  30. } SwdAccessType;
  31. typedef enum {
  32. SwdAccessDirectionWrite = 0U << 2,
  33. SwdAccessDirectionRead = 1U << 2,
  34. } SwdAccessDirection;
  35. #ifdef SWD_ENABLE_CYCLE_DELAY
  36. // Slows SWCLK down, useful for debugging via logic analyzer
  37. __attribute__((always_inline)) static inline void swd_delay_half_cycle(void) {
  38. asm volatile("nop \n"
  39. "nop \n"
  40. "nop \n"
  41. "nop \n"
  42. "nop \n"
  43. "nop \n"
  44. "nop \n"
  45. "nop \n"
  46. "nop \n"
  47. "nop \n"
  48. "nop \n"
  49. "nop \n"
  50. "nop \n"
  51. "nop \n"
  52. "nop \n"
  53. "nop \n"
  54. "nop \n"
  55. "nop \n"
  56. "nop \n"
  57. "nop \n"
  58. "nop \n"
  59. "nop \n"
  60. "nop \n"
  61. "nop \n");
  62. }
  63. #else
  64. #define swd_delay_half_cycle()
  65. #endif
  66. static void __attribute__((optimize("-O3"))) swd_turnaround(SwdioDirection mode) {
  67. static SwdioDirection prev_dir = SwdioDirectionIn;
  68. if(prev_dir == mode) {
  69. return;
  70. } else {
  71. prev_dir = mode;
  72. }
  73. if(mode == SwdioDirectionIn) {
  74. // Using LL functions for performance reasons
  75. LL_GPIO_SetPinMode(gpio_swdio.port, gpio_swdio.pin, LL_GPIO_MODE_INPUT);
  76. } else {
  77. furi_hal_gpio_write(&gpio_swclk, false);
  78. }
  79. swd_delay_half_cycle();
  80. furi_hal_gpio_write(&gpio_swclk, true);
  81. swd_delay_half_cycle();
  82. if(mode == SwdioDirectionOut) {
  83. furi_hal_gpio_write(&gpio_swclk, false);
  84. // Using LL functions for performance reasons
  85. LL_GPIO_SetPinMode(gpio_swdio.port, gpio_swdio.pin, LL_GPIO_MODE_OUTPUT);
  86. }
  87. }
  88. static void __attribute__((optimize("-O3"))) swd_tx(uint32_t data, uint32_t n_cycles) {
  89. swd_turnaround(SwdioDirectionOut);
  90. for(uint32_t i = 0; i < n_cycles; ++i) {
  91. furi_hal_gpio_write(&gpio_swclk, false);
  92. furi_hal_gpio_write(&gpio_swdio, data & (1UL << i));
  93. swd_delay_half_cycle();
  94. furi_hal_gpio_write(&gpio_swclk, true);
  95. swd_delay_half_cycle();
  96. }
  97. furi_hal_gpio_write(&gpio_swclk, false);
  98. }
  99. static void __attribute__((optimize("-O3"))) swd_tx_parity(uint32_t data, uint32_t n_cycles) {
  100. const int parity = __builtin_parity(data);
  101. swd_tx(data, n_cycles);
  102. furi_hal_gpio_write(&gpio_swdio, parity);
  103. swd_delay_half_cycle();
  104. furi_hal_gpio_write(&gpio_swclk, true);
  105. swd_delay_half_cycle();
  106. furi_hal_gpio_write(&gpio_swclk, false);
  107. }
  108. static uint32_t __attribute__((optimize("-O3"))) swd_rx(uint32_t n_cycles) {
  109. uint32_t ret = 0;
  110. swd_turnaround(SwdioDirectionIn);
  111. for(uint32_t i = 0; i < n_cycles; ++i) {
  112. furi_hal_gpio_write(&gpio_swclk, false);
  113. ret |= furi_hal_gpio_read(&gpio_swdio) ? (1UL << i) : 0;
  114. swd_delay_half_cycle();
  115. furi_hal_gpio_write(&gpio_swclk, true);
  116. swd_delay_half_cycle();
  117. }
  118. furi_hal_gpio_write(&gpio_swclk, false);
  119. return ret;
  120. }
  121. static bool __attribute__((optimize("-O3"))) swd_rx_parity(uint32_t* data, uint32_t n_cycles) {
  122. const uint32_t rx_value = swd_rx(n_cycles);
  123. swd_delay_half_cycle();
  124. const bool parity_calc = __builtin_parity(rx_value);
  125. const bool parity_rx = furi_hal_gpio_read(&gpio_swdio);
  126. furi_hal_gpio_write(&gpio_swclk, true);
  127. swd_delay_half_cycle();
  128. furi_hal_gpio_write(&gpio_swclk, false);
  129. if(data) {
  130. *data = rx_value;
  131. }
  132. return parity_calc == parity_rx;
  133. }
  134. static void swd_line_reset(bool idle_cycles) {
  135. swd_tx(0xFFFFFFFFUL, 32U);
  136. swd_tx(0x0FFFFFFFUL, idle_cycles ? 32U : 24U);
  137. }
  138. static void swd_leave_dormant_state(void) {
  139. swd_line_reset(false);
  140. swd_tx(SWD_ALERT_SEQUENCE_0, 32U);
  141. swd_tx(SWD_ALERT_SEQUENCE_1, 32U);
  142. swd_tx(SWD_ALERT_SEQUENCE_2, 32U);
  143. swd_tx(SWD_ALERT_SEQUENCE_3, 32U);
  144. swd_tx(SWD_ACTIVATION_CODE << 4U, 12U);
  145. }
  146. static void swd_enter_dormant_state(void) {
  147. swd_line_reset(false);
  148. swd_tx(SWD_SLEEP_SEQUENCE, 16U);
  149. }
  150. void swd_init(void) {
  151. furi_hal_gpio_init_ex(
  152. &gpio_swclk, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFnUnused);
  153. furi_hal_gpio_init_ex(
  154. &gpio_swdio, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFnUnused);
  155. swd_leave_dormant_state();
  156. swd_line_reset(true);
  157. }
  158. void swd_deinit(void) {
  159. swd_enter_dormant_state();
  160. furi_hal_gpio_init_simple(&gpio_swclk, GpioModeAnalog);
  161. furi_hal_gpio_init_simple(&gpio_swdio, GpioModeAnalog);
  162. }
  163. static inline uint8_t swd_prepare_request(
  164. SwdAccessDirection access_direction,
  165. SwdAccessType access_type,
  166. uint8_t address) {
  167. uint8_t ret = SWD_REQUEST_INIT | access_type | access_direction | (address << 3);
  168. ret |= __builtin_parity(ret) << 5;
  169. return ret;
  170. }
  171. static bool swd_read_request(SwdAccessType access_type, uint8_t address, uint32_t* data) {
  172. const uint8_t request = swd_prepare_request(SwdAccessDirectionRead, access_type, address);
  173. swd_tx(request, SWD_REQUEST_LEN);
  174. const uint32_t response = swd_rx(SWD_RESPONSE_LEN);
  175. if(response == SwdResponseOk) {
  176. return swd_rx_parity(data, SWD_DATA_LEN);
  177. } else {
  178. return false;
  179. }
  180. }
  181. static bool swd_write_request(SwdAccessType access_type, uint8_t address, uint32_t data) {
  182. const uint8_t request = swd_prepare_request(SwdAccessDirectionWrite, access_type, address);
  183. swd_tx(request, SWD_REQUEST_LEN);
  184. const uint32_t response = swd_rx(SWD_RESPONSE_LEN);
  185. if(response == SwdResponseOk) {
  186. swd_tx_parity(data, SWD_DATA_LEN);
  187. swd_tx(0UL, 8);
  188. return true;
  189. } else {
  190. return false;
  191. }
  192. }
  193. void swd_select_target(uint32_t target_id) {
  194. swd_tx(SWD_WRITE_REQUEST_INIT | (SWD_DP_REG_WO_TASRGETSEL << 3), SWD_REQUEST_LEN);
  195. swd_rx(SWD_RESPONSE_LEN);
  196. swd_tx_parity(target_id, SWD_DATA_LEN);
  197. swd_tx(0UL, 8);
  198. }
  199. bool swd_dp_read(uint8_t address, uint32_t* data) {
  200. return swd_read_request(SwdAccessTypeDp, address, data);
  201. }
  202. bool swd_dp_write(uint8_t address, uint32_t data) {
  203. return swd_write_request(SwdAccessTypeDp, address, data);
  204. }
  205. bool swd_ap_read(uint8_t address, uint32_t* data) {
  206. bool success = false;
  207. do {
  208. // Using hardcoded AP 0
  209. const uint32_t select_val = address & 0xF0U;
  210. if(!swd_write_request(SwdAccessTypeDp, SWD_DP_REG_WO_SELECT, select_val)) break;
  211. if(!swd_read_request(SwdAccessTypeAp, (address & 0x0FU) >> 2, NULL)) break;
  212. if(!swd_read_request(SwdAccessTypeDp, SWD_DP_REG_RO_RDBUFF, data)) break;
  213. success = true;
  214. } while(false);
  215. return success;
  216. }
  217. bool swd_ap_write(uint8_t address, uint32_t data) {
  218. bool success = false;
  219. do {
  220. // Using hardcoded AP 0
  221. const uint32_t select_val = address & 0xF0U;
  222. if(!swd_write_request(SwdAccessTypeDp, SWD_DP_REG_WO_SELECT, select_val)) break;
  223. if(!swd_write_request(SwdAccessTypeAp, (address & 0x0FU) >> 2, data)) break;
  224. success = true;
  225. } while(false);
  226. return success;
  227. }