CY8C6xx7_CM4.JLinkScript 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*********************************************************************
  2. *
  3. * Constants (similar to defines)
  4. *
  5. **********************************************************************
  6. */
  7. /*----------------- Pin mapping for the TRACE signals ----------------
  8. Copy 0
  9. P6_4 DS #5 cpuss.swj_swo_tdo (SWO DATA)
  10. P7_0 ACT #14 cpuss.trace_clock (ETM/SWO TRACECLK)
  11. P9_3 ACT #15 cpuss.trace_data[0]:0 (ETM TRACEDATA[0])
  12. P9_2 ACT #15 cpuss.trace_data[1]:0 (ETM TRACEDATA[1])
  13. P9_1 ACT #15 cpuss.trace_data[2]:0 (ETM TRACEDATA[2])
  14. P9_0 ACT #15 cpuss.trace_data[3]:0 (ETM TRACEDATA[3])
  15. Copy 1
  16. P10_3 ACT #15 cpuss.trace_data[0]:1 (ETM TRACEDATA[0])
  17. P10_2 ACT #15 cpuss.trace_data[1]:1 (ETM TRACEDATA[1])
  18. P10_1 ACT #15 cpuss.trace_data[2]:1 (ETM TRACEDATA[2])
  19. P10_0 ACT #15 cpuss.trace_data[3]:1 (ETM TRACEDATA[3])
  20. Copy 2
  21. P7_7 ACT #15 cpuss.trace_data[0]:2 (ETM TRACEDATA[0])
  22. P7_6 ACT #15 cpuss.trace_data[1]:2 (ETM TRACEDATA[1])
  23. P7_5 ACT #15 cpuss.trace_data[2]:2 (ETM TRACEDATA[2])
  24. P7_4 ACT #15 cpuss.trace_data[3]:2 (ETM TRACEDATA[3])
  25. See 'Multiple Alternate Functions' table in device datasheet.
  26. Examples:
  27. 1) CY8CKIT-062-WIFI-BT Pioneer Kit, J12:
  28. TCLK => P7_0, TD0 => P9_3, TD1 => P7_6, TD2 => P7_5, TD3 => P7_4
  29. */
  30. /* Global variables */
  31. U32 _IS_TRACE_CONFIGURED = 0x0;
  32. /* Trace clock setup registers */
  33. __constant U32 _PERI_CLOCK_CTL54_ADDR = 0x40010CD8; // Clock control register for cpuss.clock_trace_in
  34. __constant U32 _PERI_CLOCK_CTL_DIV_SEL_MASK = 0x0000000F; // PERI_CLOCK_CTL.DIV_SEL
  35. __constant U32 _PERI_DIV_8_CTL0_ADDR = 0x40010800; // Divider control (for 8.0 divider)
  36. __constant U32 _PERI_DIV_CMD_ADDR = 0x40010400; // Divider command
  37. __constant U32 _PERI_DIV_CMD_ENABLE_MASK = 0x80000000; // ENABLE field in PERI_DIV_CMD
  38. __constant U32 _PERI_DIV_CMD_DISABLE_MASK = 0x40000000; // DISABLE field in PERI_DIV_CMD
  39. __constant U32 _PERI_DIV_CMD_PA_SEL_ROL = 0x00000008; // PA_TYPE_SEL + PA_DIV_SEL fields offset in PERI_DIV_CMD
  40. __constant U32 _PERI_DIV_PA_SEL_MASK = 0x000000FF; // PA_TYPE_SEL + PA_DIV_SEL fields mask (size)
  41. /* Trace pins setup registers */
  42. __constant U32 _HSIOM_PRT7_PORT_SEL0 = 0x40310070; // Port 7 selection 0
  43. __constant U32 _HSIOM_PRT9_PORT_SEL0 = 0x40310090; // Port 9 selection 0
  44. __constant U32 _HSIOM_PRT10_PORT_SEL0 = 0x403100A0; // Port 10 selection 0
  45. __constant U32 _GPIO_PRT7_CFG = 0x403203A8; // Port 7 configuration
  46. __constant U32 _GPIO_PRT9_CFG = 0x403204A8; // Port 9 configuration
  47. __constant U32 _GPIO_PRT10_CFG = 0x40320528; // Port 10 configuration
  48. __constant U32 _GPIO_PRT7_CFG_OUT = 0x403203B0; // Port 7 output buffer configuration
  49. __constant U32 _GPIO_PRT9_CFG_OUT = 0x403204B0; // Port 9 output buffer configuration
  50. __constant U32 _GPIO_PRT10_CFG_OUT = 0x40320530; // Port 10 output buffer configuration
  51. __constant U32 _PRT_IO_SEL_MASK = 0x1F; // Mask for IO[pin]_SEL field in HSIOM_PRT[port]_PORT_SEL[0/1] register
  52. __constant U32 _PRT_DRIVE_MODE_MASK = 0xF; // Mask for IN_EN[pin] & DRIVE_MODE[pin] fields in GPIO_PRT[port]_CFG register
  53. __constant U32 _PRT_SLOW_MASK = 0x1; // Mask for SLOW[pin] field in GPIO_PRT[port]_CFG_OUT register
  54. __constant U32 _PRT_DRIVE_SEL_MASK = 0x3; // Mask for DRIVE_SEL[pin] field in GPIO_PRT[port]_CFG_OUT register
  55. /*********************************************************************
  56. *
  57. * Local functions
  58. *
  59. **********************************************************************
  60. */
  61. /*********************************************************************
  62. *
  63. * _SetupTraceClock() - Selects TPIU Clock divider for ETM Trace.
  64. */
  65. int _SetupTraceClock(void) {
  66. U32 ClockCtlVal;
  67. U32 ClockDivCtlVal;
  68. U32 ClockDivSel;
  69. U32 ClockDivVal;
  70. U32 ClockDivCmd;
  71. U32 TRACE_CLOCK_CTL_ADDR;
  72. U32 TRACE_CLOCK_DIV_CTL_ADDR;
  73. U32 TRACE_CLOCK_DIV_CMD_ADDR;
  74. TRACE_CLOCK_CTL_ADDR = _PERI_CLOCK_CTL54_ADDR;
  75. TRACE_CLOCK_DIV_CTL_ADDR = _PERI_DIV_8_CTL0_ADDR;
  76. TRACE_CLOCK_DIV_CMD_ADDR = _PERI_DIV_CMD_ADDR;
  77. ClockDivSel = (7 & _PERI_CLOCK_CTL_DIV_SEL_MASK); // Peripheral clock divider index to use for trace clock
  78. ClockDivVal = (0 & _PERI_DIV_PA_SEL_MASK); // Peripheral clock divider value for trace clock
  79. // Actual divider is (1+ClockDivVal)
  80. ClockCtlVal = JLINK_MEM_ReadU32(TRACE_CLOCK_CTL_ADDR);
  81. ClockDivCtlVal = JLINK_MEM_ReadU32(TRACE_CLOCK_DIV_CTL_ADDR + (ClockDivSel*4));
  82. if((ClockCtlVal != ClockDivSel) || (ClockDivCtlVal != ((ClockDivVal << _PERI_DIV_CMD_PA_SEL_ROL) | 0x1))){
  83. JLINK_SYS_Report("JLinkScript/Trace: Setup TPIU clock");
  84. //
  85. // Select TPIU Clock divider
  86. //
  87. // DISABLE 8.0 DIV in PERI_DIV_CMD:
  88. ClockDivCmd = _PERI_DIV_CMD_DISABLE_MASK | (_PERI_DIV_PA_SEL_MASK << _PERI_DIV_CMD_PA_SEL_ROL) | ClockDivSel;
  89. JLINK_MEM_WriteU32(TRACE_CLOCK_DIV_CMD_ADDR, ClockDivCmd);
  90. // Use selected divider (8.0) for cpuss.clock_trace_in
  91. JLINK_MEM_WriteU32(TRACE_CLOCK_CTL_ADDR, ClockDivSel);
  92. // Set 8.0 DIV = ClockDivVal
  93. JLINK_MEM_WriteU32((TRACE_CLOCK_DIV_CTL_ADDR+(ClockDivSel*4)), (ClockDivVal << 8));
  94. // ENABLE 8.0 DIV
  95. ClockDivCmd = _PERI_DIV_CMD_ENABLE_MASK | (_PERI_DIV_PA_SEL_MASK << _PERI_DIV_CMD_PA_SEL_ROL) | ClockDivSel;
  96. JLINK_MEM_WriteU32(TRACE_CLOCK_DIV_CMD_ADDR, ClockDivCmd);
  97. }
  98. return 0;
  99. }
  100. /*********************************************************************
  101. *
  102. * _SetupTracePin() - Configures Trace Pin.
  103. * Parameters:
  104. * pin: Pin number
  105. * hsiomPrtPortSel0Addr: HSIOM_PRT[port]_PORT_SEL0 register address
  106. * ioSelVal: IO[pin]_SEL field value (connection) for HSIOM_PRT[port]_PORT_SEL register
  107. * gpioPrtCfgAddr: GPIO_PRT[port]_CFG register address
  108. * gpioPrtCfgOutAddr: GPIO_PRT[port]_CFG_OUT register address
  109. */
  110. int _SetupTracePin(U32 pin,
  111. U32 hsiomPrtPortSel0Addr, U32 ioSelVal,
  112. U32 gpioPrtCfgAddr,
  113. U32 gpioPrtCfgOutAddr) {
  114. U32 reg0;
  115. U32 reg1;
  116. U32 offset;
  117. U32 hsiomRegAddr; // Address of HSIOM_PRT[port]_PORT_SEL0 or HSIOM_PRT[port]_PORT_SEL1
  118. U32 pMode; // pin drive mode
  119. U32 pSlew; // pin slew rate
  120. U32 pStrange; // pin drive strange
  121. //
  122. // Select pin route connection in HSIOM_PRT[port]_PORT_SEL[0/1] register
  123. // See HSIOM_PRT0_PORT_SEL0 in registers TRM for the bit-field map of HSIOM_PRT[port]_PORT_SEL0 registers
  124. // See HSIOM_PRT2_PORT_SEL1 in registers TRM for the bit-field map of HSIOM_PRT[port]_PORT_SEL1 registers
  125. if (pin < 4) { /* Pin[0-3] selection is in HSIOM_PRT[port]_PORT_SEL0 register */
  126. hsiomRegAddr = hsiomPrtPortSel0Addr; // Use HSIOM_PRT[port]_PORT_SEL0
  127. offset = pin * 8; // Offset of the IO[pin]_SEL field for required pin number,
  128. // where 8 = 5 bits for IO[pin]_SEL field + 3 not used bits
  129. }
  130. else { /* Pin[4-7] selection is in HSIOM_PRT[port]_PORT_SEL1 register */
  131. hsiomRegAddr = hsiomPrtPortSel0Addr + 4; // Use HSIOM_PRT[port]_PORT_SEL1
  132. offset = (pin - 4) * 8; // Offset of the IO[pin]_SEL field for required pin number,
  133. // where 8 = 5 bits for IO[pin]_SEL field + 3 not used bits
  134. }
  135. reg0 = JLINK_MEM_ReadU32(hsiomRegAddr);
  136. reg1 = reg0;
  137. reg1 &= ~(_PRT_IO_SEL_MASK << offset); // Clear IO[pin]_SEL field
  138. reg1 |= (ioSelVal << offset); // Set field value
  139. if (reg0 |= reg1) {
  140. JLINK_MEM_WriteU32(hsiomRegAddr, reg1);
  141. }
  142. //
  143. // Disable input buffer and set drive mode in GPIO_PRT[port]_CFG register
  144. // See GPIO_PRT2_CFG in registers TRM for the bit-field map:
  145. pMode = 6; // DRIVE_MODE[pin]:
  146. // 0: HIGHZ: Output buffer is off creating a high impedance input (default)
  147. // 1: RESERVED: This mode is reserved and should not be used
  148. // 2: PULLUP: Resistive pull up
  149. // 3: PULLDOWN: Resistive pull down
  150. // 4: OD_DRIVESLOW: Open drain, drives low
  151. // 5: OD_DRIVESHIGH: Open drain, drives high
  152. // 6: STRONG: Strong D_OUTput buffer
  153. // 7: PULLUP_DOWN: Pull up or pull down
  154. reg0 = JLINK_MEM_ReadU32(gpioPrtCfgAddr);
  155. reg1 = reg0;
  156. offset = pin * 4; // Offset of the DRIVE_MODE[pin] field for required pin number,
  157. // where 4 = 3 bits for DRIVE_MODE[pin] + 1 bit for IN_EN fields
  158. reg1 &= ~(_PRT_DRIVE_MODE_MASK << offset); // Clear IN_EN[pin] and DRIVE_MODE[pin] fields
  159. reg1 |= (pMode << offset); // Set DRIVE_MODE[pin] field value
  160. if (reg0 |= reg1) {
  161. JLINK_MEM_WriteU32(gpioPrtCfgAddr, reg1);
  162. }
  163. //
  164. // Set slew rate and drive strength in GPIO_PRT[port]_CFG_OUT register
  165. // See GPIO_PRT2_CFG_OUT in registers TRM for the bit-field map:
  166. pSlew = 0x0; // SLOW[pin]:
  167. // 0 - Fast slew rate (default)
  168. // 1 - Slow slew rate
  169. pStrange = 0x3; // DRIVE_SEL[pin]:
  170. // 0 - FULL_DRIVE: Full drive strength: GPIO drives current at its max rated spec.
  171. // 1 - ONE_HALF_DRIVE: 1/2 drive strength: GPIO drives current at 1/2 of its max rated spec (default)
  172. // 2 - ONE_QUARTER_DRIVE: 1/4 drive strength: GPIO drives current at 1/4 of its max rated spec.
  173. // 3 - ONE_EIGHTH_DRIVE: 1/8 drive strength: GPIO drives current at 1/8 of its max rated spec.
  174. reg0 = JLINK_MEM_ReadU32(gpioPrtCfgOutAddr);
  175. reg1 = reg0;
  176. offset = pin;
  177. reg1 &= ~(_PRT_SLOW_MASK << offset); // Clear SLOW[pin] field
  178. reg1 |= (pSlew << offset); // Set field value
  179. offset = 16 + pin * 2; // Offset of the DRIVE_SEL[pin] field for required pin number,
  180. // where '16' is the offset of DRIVE_SEL[pin] for pin 0 and '2' is the size of DRIVE_SEL[pin]
  181. reg1 &= ~(_PRT_DRIVE_SEL_MASK << offset); // Clear DRIVE_SEL[pin] field
  182. reg1 |= (pStrange << offset); // Set field value
  183. if (reg0 |= reg1) {
  184. JLINK_MEM_WriteU32(gpioPrtCfgOutAddr, reg1);
  185. }
  186. return 0;
  187. }
  188. /*********************************************************************
  189. *
  190. * Global functions
  191. *
  192. **********************************************************************
  193. */
  194. int ConfigTargetSettings(void) {
  195. //
  196. // Mark a specific memory region as memory type illegal
  197. // in order to make sure that the software is not allowed to access these regions
  198. //
  199. // Note: This does not work for J-Flash tool
  200. //
  201. // Exclude SFLASH regions
  202. JLINK_ExecCommand("map region 0x16000000-0x160007FF XI"); // [SFLASH Start - User Data Start]
  203. JLINK_ExecCommand("map region 0x16001000-0x160019FF XI"); // [User Data End - NAR Start]
  204. JLINK_ExecCommand("map region 0x16001C00-0x160059FF XI"); // [NAR End - Public Key Start]
  205. JLINK_ExecCommand("map region 0x16006600-0x16007BFF XI"); // [Public Key End - TOC2 Start]
  206. // Exclude Cy Metadata
  207. JLINK_ExecCommand("map region 0x90300000-0x903FFFFF XI"); // Cy Checksum
  208. JLINK_ExecCommand("map region 0x90500000-0x905FFFFF XI"); // Cy Metadata
  209. }
  210. void InitTarget(void) {
  211. Report("JLinkScript/InitTarget: CORESIGHT setup");
  212. CORESIGHT_AddAP(0, CORESIGHT_AHB_AP); // SYSAP
  213. CORESIGHT_AddAP(1, CORESIGHT_AHB_AP); // AHB-AP used to connect to M0+ core
  214. CORESIGHT_AddAP(2, CORESIGHT_AHB_AP); // AHB-AP used to connect to M4 core
  215. CORESIGHT_IndexAHBAPToUse = 2;
  216. CPU=CORTEX_M4;
  217. }
  218. /*********************************************************************
  219. *
  220. * OnTraceStart()
  221. *
  222. * Function description
  223. * If present, called right before trace is started.
  224. * Used to initialize MCU specific trace related things like configuring the trace pins for alternate function.
  225. *
  226. * Return value
  227. * >= 0: O.K.
  228. * < 0: Error
  229. *
  230. * Notes
  231. * (1) May use high-level API functions like JLINK_MEM_ etc.
  232. * (2) Should not call JLINK_TARGET_Halt(). Can rely on target being halted when entering this function
  233. */
  234. int OnTraceStart(void) {
  235. U32 PortWidth;
  236. U32 IO_SEL_ACT14;
  237. U32 IO_SEL_ACT15;
  238. //U32 IO_SEL_DS5;
  239. if (_IS_TRACE_CONFIGURED) {
  240. return 0;
  241. }
  242. // Adjust sampling point of trace pin (Optional: not needed for this cpu)
  243. // JLINK_ExecCommand("TraceSampleAdjust TD=2000");
  244. // Setup peripheral clocks for tracing
  245. _SetupTraceClock();
  246. // Setup pins for tracing: TCLK > P7_0, TD0 > P9_3, TD1 > P7_6, TD2 > P7_5, TD3 > P7_4
  247. PortWidth = JLINK_TRACE_PortWidth;
  248. JLINK_SYS_Report("JLinkScript/Trace: Setup clock and data pins");
  249. IO_SEL_ACT14 = 0x1A; // Connection route for 'cpuss.trace_clock' signal (P7_0)
  250. IO_SEL_ACT15 = 0x1B; // Connection route for 'cpuss.trace_data[0-3]' signals (P7, P9 and P10)
  251. //IO_SEL_DS5 = 0x1D; // Connection route for 'cpuss.swj_swo_tdo' signal (P6_4)
  252. _SetupTracePin( /*P7_0*/ 0, _HSIOM_PRT7_PORT_SEL0, IO_SEL_ACT14, _GPIO_PRT7_CFG, _GPIO_PRT7_CFG_OUT);
  253. _SetupTracePin( /*P9_3*/ 3, _HSIOM_PRT9_PORT_SEL0, IO_SEL_ACT15, _GPIO_PRT9_CFG, _GPIO_PRT9_CFG_OUT);
  254. _SetupTracePin( /*P7_6*/ 6, _HSIOM_PRT7_PORT_SEL0, IO_SEL_ACT15, _GPIO_PRT7_CFG, _GPIO_PRT7_CFG_OUT);
  255. if (PortWidth > 2) {
  256. _SetupTracePin( /*P7_5*/ 5, _HSIOM_PRT7_PORT_SEL0, IO_SEL_ACT15, _GPIO_PRT7_CFG, _GPIO_PRT7_CFG_OUT);
  257. _SetupTracePin( /*P7_4*/ 4, _HSIOM_PRT7_PORT_SEL0, IO_SEL_ACT15, _GPIO_PRT7_CFG, _GPIO_PRT7_CFG_OUT);
  258. }
  259. _IS_TRACE_CONFIGURED = 1;
  260. return 0;
  261. }