5050#include <libopencm3/stm32/gpio.h>
5151#include "DAP/CMSIS_DAP_config.h"
5252
53+ //#define SBDAP 1
54+
5355/*
5456SWD functionality
57+ see: https://arm-software.github.io/CMSIS_5/latest/DAP/html/group__DAP__Config__PortIO__gr.html
5558*/
5659
60+ /*
61+ Setup SWD I/O pins: SWCLK, SWDIO, and nRESET.
62+ Configures the DAP Hardware I/O pins for Serial Wire Debug (SWD) mode:
63+
64+ SWCLK, SWDIO, nRESET to output mode and set to default high level.
65+ */
5766static __inline void PORT_SWD_SETUP (void )
5867{
59- GPIO_BSRR (SWDIO_GPIO_PORT ) = SWDIO_GPIO_PIN ;
6068 GPIO_BSRR (SWCLK_GPIO_PORT ) = SWCLK_GPIO_PIN ;
69+ GPIO_BSRR (SWDIO_GPIO_PORT ) = SWDIO_GPIO_PIN ;
70+ GPIO_BSRR (nRESET_GPIO_PORT ) = nRESET_GPIO_PIN ;
71+
72+ #if defined(SBDAP )
73+ // set SWDIO buffer to output mode
74+ GPIO_BSRR (SWDIR_GPIO_PORT ) = SWDIR_GPIO_PIN ;
75+ // do not invoke NXP LPC boot loader
76+ GPIO_BSRR (CTL_GPIO_PORT ) = CTL_GPIO_PIN ;
77+ #endif
6178
62- gpio_set_output_options (SWDIO_GPIO_PORT , GPIO_OTYPE_PP , GPIO_OSPEED_HIGH , SWDIO_GPIO_PIN );
6379 gpio_set_output_options (SWCLK_GPIO_PORT , GPIO_OTYPE_PP , GPIO_OSPEED_HIGH , SWCLK_GPIO_PIN );
80+ gpio_set_output_options (SWDIO_GPIO_PORT , GPIO_OTYPE_PP , GPIO_OSPEED_HIGH , SWDIO_GPIO_PIN );
81+ // gpio_set_output_options(nRESET_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_HIGH, nRESET_GPIO_PIN);
82+ // gpio_set_output_options(nRESET_GPIO_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_LOW, nRESET_GPIO_PIN);
83+
84+ #if defined(SBDAP )
85+ gpio_set_output_options (SWDIR_GPIO_PORT , GPIO_OTYPE_PP , GPIO_OSPEED_HIGH , SWDIR_GPIO_PIN );
86+ //gpio_set_output_options(CTL_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_HIGH, CTL_GPIO_PIN);
87+ #endif
88+
6489
65- gpio_mode_setup (SWDIO_GPIO_PORT , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , SWDIO_GPIO_PIN );
6690 gpio_mode_setup (SWCLK_GPIO_PORT , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , SWCLK_GPIO_PIN );
91+ gpio_mode_setup (SWDIO_GPIO_PORT , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , SWDIO_GPIO_PIN );
92+ // gpio_mode_setup(nRESET_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, nRESET_GPIO_PIN);
93+ // gpio_mode_setup(nRESET_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, nRESET_GPIO_PIN);
94+
95+ #if defined(SBDAP )
96+ gpio_mode_setup (SWDIR_GPIO_PORT , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , SWDIR_GPIO_PIN );
97+ //gpio_mode_setup(CTL_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, CTL_GPIO_PIN);
98+ #endif
6799}
68100
101+ /*
102+ Disable SWD I/O Pins.
103+ Disables the DAP Hardware I/O pins which configures:
104+
105+ SWCLK, SWDIO, (SWO), nRESET to High-Z mode.
106+ */
69107static __inline void PORT_OFF (void )
70108{
109+ #if defined(SBDAP )
110+ GPIO_BRR (SWDIO_GPIO_PORT ) = SWDIO_GPIO_PIN ;
111+ GPIO_BRR (SWCLK_GPIO_PORT ) = SWCLK_GPIO_PIN ;
112+ GPIO_BSRR (nRESET_GPIO_PORT ) = nRESET_GPIO_PIN ;
113+ gpio_mode_setup (SWDIO_GPIO_PORT , GPIO_MODE_INPUT , GPIO_PUPD_NONE , SWDIO_GPIO_PIN );
114+
115+ // set SWDIO buffer to input mode
116+ GPIO_BRR (SWDIR_GPIO_PORT ) = SWDIR_GPIO_PIN ;
117+
118+ #else
71119 GPIO_BRR (SWDIO_GPIO_PORT ) = SWDIO_GPIO_PIN ;
72120 GPIO_BRR (SWCLK_GPIO_PORT ) = SWCLK_GPIO_PIN ;
73121 gpio_mode_setup (SWDIO_GPIO_PORT , GPIO_MODE_INPUT , GPIO_PUPD_NONE , SWDIO_GPIO_PIN );
74122 gpio_mode_setup (SWCLK_GPIO_PORT , GPIO_MODE_INPUT , GPIO_PUPD_NONE , SWCLK_GPIO_PIN );
123+ #endif
75124}
76125
126+ /*
127+ SWCLK/TCK I/O pin: Set Output to High.
128+ Set the SWCLK/TCK DAP hardware I/O pin to high level.
129+ */
77130static __inline void PIN_SWCLK_TCK_SET (void )
78131{
79132 GPIO_BSRR (SWCLK_GPIO_PORT ) = SWCLK_GPIO_PIN ;
80133}
81134
135+ /*
136+ SWCLK/TCK I/O pin: Set Output to Low.
137+ Set the SWCLK/TCK DAP hardware I/O pin to low level.
138+ */
82139static __inline void PIN_SWCLK_TCK_CLR (void )
83140{
84141 GPIO_BRR (SWCLK_GPIO_PORT ) = SWCLK_GPIO_PIN ;
85142}
86143
144+ /*
145+ SWDIO/TMS I/O pin: Get Input.
146+ Returns
147+ Current status of the SWDIO/TMS DAP hardware I/O pin.
148+ */
87149static __inline uint32_t PIN_SWDIO_TMS_IN (void )
88150{
89151 return (GPIO_IDR (SWDIO_GPIO_PORT ) & SWDIO_GPIO_PIN ) ? 0x1 : 0x0 ;
90152}
91153
154+ /*
155+ SWDIO/TMS I/O pin: Set Output to High.
156+ Set the SWDIO/TMS DAP hardware I/O pin to high level.
157+ */
92158static __inline void PIN_SWDIO_TMS_SET (void )
93159{
94160 GPIO_BSRR (SWDIO_GPIO_PORT ) = SWDIO_GPIO_PIN ;
95161}
96162
163+ /*
164+ SWDIO/TMS I/O pin: Set Output to Low.
165+ Set the SWDIO/TMS DAP hardware I/O pin to low level.
166+ */
97167static __inline void PIN_SWDIO_TMS_CLR (void )
98168{
99169 GPIO_BRR (SWDIO_GPIO_PORT ) = SWDIO_GPIO_PIN ;
100170}
101171
172+ /*
173+ SWDIO I/O pin: Get Input (used in SWD mode only).
174+ Returns
175+ Current status of the SWDIO DAP hardware I/O pin.
176+ */
102177static __inline uint32_t PIN_SWDIO_IN (void )
103178{
104179 return (GPIO_IDR (SWDIO_GPIO_PORT ) & SWDIO_GPIO_PIN ) ? 0x1 : 0x0 ;
105180}
106181
182+ /*
183+ SWDIO I/O pin: Set Output (used in SWD mode only).
184+ Parameters
185+ bit Output value for the SWDIO DAP hardware I/O pin.
186+ */
107187static __inline void PIN_SWDIO_OUT (uint32_t bit )
108188{
109189 if (bit & 1 ) {
@@ -113,41 +193,49 @@ static __inline void PIN_SWDIO_OUT (uint32_t bit)
113193 }
114194}
115195
116- static __inline void PIN_SWDIO_OUT_ENABLE (void )
117- {
118-
119- GPIO_MODER (SWDIO_GPIO_PORT ) &= ~( (0x3 << (SWDIO_GPIO_PIN_NUM * 2 )) );
120- GPIO_MODER (SWDIO_GPIO_PORT ) |= ( (0x1 << (SWDIO_GPIO_PIN_NUM * 2 )) );
121- }
196+ /*
197+ SWDIO I/O pin: Switch to Output mode (used in SWD mode only).
198+ Configure the SWDIO DAP hardware I/O pin to output mode. This function is called prior PIN_SWDIO_OUT function calls.
199+ */
200+ static __inline void PIN_SWDIO_OUT_ENABLE (void )
201+ {
202+ #if defined(SBDAP )
203+ // set SWDIO buffer to output mode
204+ GPIO_BSRR (SWDIR_GPIO_PORT ) = SWDIR_GPIO_PIN ;
205+ #endif
122206
123- static __inline void PIN_SWDIO_OUT_DISABLE (void )
124- {
125207 GPIO_MODER (SWDIO_GPIO_PORT ) &= ~( (0x3 << (SWDIO_GPIO_PIN_NUM * 2 )) );
208+ GPIO_MODER (SWDIO_GPIO_PORT ) |= ( (0x1 << (SWDIO_GPIO_PIN_NUM * 2 )) );
126209}
127210
128211/*
129- JTAG-only functionality (not used in this application)
212+ SWDIO I/O pin: Switch to Input mode (used in SWD mode only).
213+ Configure the SWDIO DAP hardware I/O pin to input mode. This function is called prior PIN_SWDIO_IN function calls.
130214*/
215+ static __inline void PIN_SWDIO_OUT_DISABLE (void )
216+ {
217+ GPIO_MODER (SWDIO_GPIO_PORT ) &= ~( (0x3 << (SWDIO_GPIO_PIN_NUM * 2 )) );
131218
132- static __inline void PORT_JTAG_SETUP (void ) {}
133-
134- static __inline uint32_t PIN_TDI_IN (void ) { return 0 ; }
135-
136- static __inline void PIN_TDI_OUT (uint32_t bit ) { (void )bit ; }
137-
138- static __inline uint32_t PIN_TDO_IN (void ) { return 0 ; }
139-
140- static __inline uint32_t PIN_nTRST_IN (void ) { return 0 ; }
141-
142- static __inline void PIN_nTRST_OUT (uint32_t bit ) { (void )bit ; }
219+ #if defined(SBDAP )
220+ // set SWDIO buffer to input mode
221+ GPIO_BRR (SWDIR_GPIO_PORT ) = SWDIR_GPIO_PIN ;
222+ #endif
223+ }
143224
144225/*
145- other functionality not applicable to this application
226+ SWCLK/TCK I/O pin: Get Input.
227+ Returns
228+ Current status of the SWCLK/TCK DAP hardware I/O pin.
146229*/
147230static __inline uint32_t PIN_SWCLK_TCK_IN (void ) {
148231 return (GPIO_IDR (SWCLK_GPIO_PORT ) & SWCLK_GPIO_PIN ) ? 0x1 : 0x0 ;
149232}
150233
234+ /*
235+ nRESET I/O pin: Get Input.
236+ Returns
237+ Current status of the nRESET DAP hardware I/O pin.
238+ */
151239static __inline uint32_t PIN_nRESET_IN (void ) {
152240#if defined(nRESET_GPIO_PORT ) && defined(nRESET_GPIO_PIN )
153241 return (GPIO_IDR (nRESET_GPIO_PORT ) & nRESET_GPIO_PIN ) ? 0x1 : 0x0 ;
@@ -156,6 +244,13 @@ static __inline uint32_t PIN_nRESET_IN (void) {
156244#endif
157245}
158246
247+ /*
248+ nRESET I/O pin: Set Output.
249+ Parameters
250+ bit target device hardware reset pin status:
251+ 0: issue a device hardware reset.
252+ 1: release device hardware reset.
253+ */
159254static __inline void PIN_nRESET_OUT (uint32_t bit ) {
160255#if defined(nRESET_GPIO_PORT ) && defined(nRESET_GPIO_PIN )
161256 if (bit & 0x1 ) {
@@ -166,6 +261,13 @@ static __inline void PIN_nRESET_OUT (uint32_t bit) {
166261#endif
167262}
168263
264+ /*
265+ Debug Unit: Set status of Connected LED.
266+ Parameters
267+ bit status of the Connect LED.
268+ 1: Connect LED ON: debugger is connected to CMSIS-DAP Debug Unit.
269+ 0: Connect LED OFF: debugger is not connected to CMSIS-DAP Debug Unit.
270+ */
169271static __inline void LED_CONNECTED_OUT (uint32_t bit ) {
170272#if defined(LED_CON_GPIO_PORT ) && defined(LED_CON_GPIO_PIN )
171273 if ((bit & 0x1 ) ^ LED_OPEN_DRAIN ) {
@@ -176,6 +278,13 @@ static __inline void LED_CONNECTED_OUT (uint32_t bit) {
176278#endif
177279}
178280
281+ /*
282+ Debug Unit: Set status Target Running LED.
283+ Parameters
284+ bit status of the Target Running LED.
285+ 1: Target Running LED ON: program execution in target started.
286+ 0: Target Running LED OFF: program execution in target stopped.
287+ */
179288static __inline void LED_RUNNING_OUT (uint32_t bit ) {
180289#if defined(LED_RUN_GPIO_PORT ) && defined(LED_RUN_GPIO_PIN )
181290 if ((bit & 0x1 ) ^ LED_OPEN_DRAIN ) {
@@ -186,6 +295,7 @@ static __inline void LED_RUNNING_OUT (uint32_t bit) {
186295#endif
187296}
188297
298+ // show USB activity
189299static __inline void LED_ACTIVITY_OUT (uint32_t bit ) {
190300#if defined(LED_ACT_GPIO_PORT ) && defined(LED_ACT_GPIO_PIN )
191301 if ((bit & 0x1 ) ^ LED_OPEN_DRAIN ) {
@@ -196,21 +306,50 @@ static __inline void LED_ACTIVITY_OUT (uint32_t bit) {
196306#endif
197307}
198308
309+ /*
310+ Setup of the Debug Unit I/O pins and LEDs (called when Debug Unit is initialized).
311+ This function performs the initialization of the CMSIS-DAP Hardware I/O Pins and the Status LEDs.
312+ In detail the operation of Hardware I/O and LED pins are enabled and set:
313+ I/O clock system enabled.
314+ all I/O pins: input buffer enabled, output pins are set to HighZ mode.
315+ for nTRST, nRESET a weak pull-up (if available) is enabled.
316+ LED output pins are enabled and LEDs are turned off.
317+ */
199318static __inline void DAP_SETUP (void ) {
200319 LED_ACTIVITY_OUT (0 );
201320 LED_RUNNING_OUT (0 );
202321 LED_CONNECTED_OUT (0 );
203322
204323#if defined(nRESET_GPIO_PORT ) && defined(nRESET_GPIO_PIN )
205- // Configure nRESET as an open-drain output
324+ // Configure nRESET as an open-drain output with pull-up resistor
206325 GPIO_BSRR (nRESET_GPIO_PORT ) = nRESET_GPIO_PIN ;
207-
208326 gpio_set_output_options (nRESET_GPIO_PORT , GPIO_OTYPE_OD , GPIO_OSPEED_LOW , nRESET_GPIO_PIN );
327+ gpio_mode_setup (nRESET_GPIO_PORT , GPIO_MODE_OUTPUT , GPIO_PUPD_PULLUP , nRESET_GPIO_PIN );
328+ #endif
209329
210- gpio_mode_setup (nRESET_GPIO_PORT , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , nRESET_GPIO_PIN );
330+ #if defined(SBDAP )
331+ // do not invoke NXP LPC boot loader
332+ GPIO_BSRR (CTL_GPIO_PORT ) = CTL_GPIO_PIN ;
333+ gpio_set_output_options (CTL_GPIO_PORT , GPIO_OTYPE_PP , GPIO_OSPEED_HIGH , CTL_GPIO_PIN );
334+ gpio_mode_setup (CTL_GPIO_PORT , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , CTL_GPIO_PIN );
211335#endif
212336}
213337
338+ /*
339+ JTAG-only functionality (not used in this application)
340+ */
341+
342+ static __inline void PORT_JTAG_SETUP (void ) {}
343+ static __inline uint32_t PIN_TDI_IN (void ) { return 0 ; }
344+ static __inline void PIN_TDI_OUT (uint32_t bit ) { (void )bit ; }
345+ static __inline uint32_t PIN_TDO_IN (void ) { return 0 ; }
346+ static __inline uint32_t PIN_nTRST_IN (void ) { return 0 ; }
347+ static __inline void PIN_nTRST_OUT (uint32_t bit ) { (void )bit ; }
348+
349+ /*
350+ other functionality (not used in this application)
351+ */
352+
214353static __inline uint32_t RESET_TARGET (void ) { return 0 ; }
215354
216355#endif
0 commit comments