swd.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @file swd.h
  3. * @brief Serial Wire Debug (SWD) bus functions.
  4. *
  5. * This file is responsible for:
  6. *
  7. * - Debug hardware initialisation
  8. * - Target selection in a multidrop bus
  9. * - Debug and Access port access
  10. *
  11. * For more information, see ARM IHI0031G
  12. * https://documentation-service.arm.com/static/622222b2e6f58973271ebc21
  13. */
  14. #pragma once
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. // Only bits [3:2] are used to access DP registers
  19. #define SWD_DP_REG_ADDR_SHIFT (2U)
  20. // Debug port registers - write
  21. #define SWD_DP_REG_WO_ABORT (0x0U >> SWD_DP_REG_ADDR_SHIFT)
  22. #define SWD_DP_REG_WO_SELECT (0x8U >> SWD_DP_REG_ADDR_SHIFT)
  23. #define SWD_DP_REG_WO_TASRGETSEL (0xCU >> SWD_DP_REG_ADDR_SHIFT)
  24. // Debug port registers - read
  25. #define SWD_DP_REG_RO_DPIDR (0x0U >> SWD_DP_REG_ADDR_SHIFT)
  26. #define SWD_DP_REG_RO_RESEND (0x8U >> SWD_DP_REG_ADDR_SHIFT)
  27. #define SWD_DP_REG_RO_RDBUFF (0xCU >> SWD_DP_REG_ADDR_SHIFT)
  28. // Debug port registers - read/write
  29. #define SWD_DP_REG_RW_BANK (0x4U >> SWD_DP_REG_ADDR_SHIFT)
  30. #define SWD_DP_REG_RW_CTRL_STAT (SWD_DP_REG_RW_BANK)
  31. // Access port registers
  32. #define SWD_AP_REG_RW_CSW (0x00U)
  33. #define SWD_AP_REG_RW_TAR (0x04U)
  34. #define SWD_AP_REG_RW_DRW (0x0CU)
  35. #define SWD_AP_REG_RO_IDR (0xFCU)
  36. // CTRL/STAT bits
  37. #define SWD_DP_REG_CTRL_STAT_CDBGPWRUPREQ (1UL << 28U)
  38. #define SWD_DP_REG_CTRL_STAT_CDBGPWRUPACK (1UL << 29U)
  39. #define SWD_DP_REG_CTRL_STAT_CSYSPWRUPREQ (1UL << 30U)
  40. #define SWD_DP_REG_CTRL_STAT_CSYSPWRUPACK (1UL << 31U)
  41. // CSW bits (PROT bits are for AHB3)
  42. #define SWD_AP_REG_CSW_SIZE_WORD (2UL << 0U)
  43. #define SWD_AP_REG_CSW_HPROT_DATA (1UL << 24U)
  44. #define SWD_AP_REG_CSW_HPROT_PRIVILIGED (1UL << 25U)
  45. #define SWD_AP_REG_CSW_HPROT_BUFFERABLE (1UL << 26U)
  46. #define SWD_AP_REG_CSW_HPROT_CACHEABLE (1UL << 27U)
  47. #define SWD_AP_REG_CSW_HNONSEC (1UL << 30U)
  48. /**
  49. * @brief Initialise SWD bus.
  50. *
  51. * Configures SWCLK and SWDIO pins, wakes up the target from
  52. * dormant state and resets the SWD bus.
  53. */
  54. void swd_init(void);
  55. /**
  56. * @brief Disable SWD bus.
  57. *
  58. * Sets the target to dormant state and returns
  59. * SWCLK and SWDIO pins to analog mode.
  60. */
  61. void swd_deinit(void);
  62. /**
  63. * @brief Select one target on a multidrop (SWD v2) bus.
  64. *
  65. * @param[in] target_id target address or id (specified in device datasheet)
  66. */
  67. void swd_select_target(uint32_t target_id);
  68. /**
  69. * @brief Perform a Debug Port (DP) read.
  70. *
  71. * Reads a 32-bit word from the designated DP register.
  72. *
  73. * @param[in] address DP register address.
  74. * @param[out] data pointer to the value to contain the read data.
  75. * @returns true on success, false otherwise.
  76. */
  77. bool swd_dp_read(uint8_t address, uint32_t* data);
  78. /**
  79. * @brief Perform a Debug Port (DP) write.
  80. *
  81. * Writes a 32-bit word to the designated DP register.
  82. *
  83. * @param[in] address DP register address.
  84. * @param[in] data value to be written as data.
  85. * @returns true on success, false otherwise.
  86. */
  87. bool swd_dp_write(uint8_t address, uint32_t data);
  88. /**
  89. * @brief Perform an Access Port (AP) read.
  90. *
  91. * Reads a 32-bit word from the designated AP register.
  92. *
  93. * @param[in] address AP register address.
  94. * @param[out] data pointer to the value to contain the read data.
  95. * @returns true on success, false otherwise.
  96. */
  97. bool swd_ap_read(uint8_t address, uint32_t* data);
  98. /**
  99. * @brief Perform an Access Port (AP) write.
  100. *
  101. * Writes a 32-bit word to the designated AP register.
  102. *
  103. * @param[in] address AP register address.
  104. * @param[in] data value to be written as data.
  105. * @returns true on success, false otherwise.
  106. */
  107. bool swd_ap_write(uint8_t address, uint32_t data);