Skip to content

Commit 740717c

Browse files
committed
cpu/gd32v: Allow configuration of SWJ_CFG
Expose the compile time configuration knob `CONFIG_AFIO_PCF0_SWJ_CFG` to allow freeing some/all JTAG pins and use them as GPIOs. As default, PB4 is remapped from NJTRST to be usable as regular GPIO. This still allows using the JTAG interface for debugging/flashing, but makes an GPIO exposed by some boards available.
1 parent 2fbdeb8 commit 740717c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

cpu/gd32v/cpu.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ void cpu_init(void)
3333
periph_clk_en(APB1, RCU_APB1EN_PMUEN_Msk);
3434
/* Common RISC-V initialization */
3535
riscv_init();
36+
37+
/* Apply configured SWJ_CFG, unless it is configured to the reset value */
38+
if (CONFIG_AFIO_PCF0_SWJ_CFG != SWJ_CFG_FULL_JTAG) {
39+
/* The remapping periph clock must first be enabled */
40+
RCU->APB2EN |= RCU_APB2EN_AFEN_Msk;
41+
/* Then the remap can occur */
42+
AFIO->PCF0 |= CONFIG_AFIO_PCF0_SWJ_CFG;
43+
}
44+
3645
early_init();
3746
periph_init();
3847
}

cpu/gd32v/include/cpu_conf.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,34 @@ extern "C" {
4646
#define CPU_FLASH_BASE 0x08000000
4747
/** @} */
4848

49+
/**
50+
* @brief Possible values of the `SWJ_CFG` field in the AFIO->PCF0 register
51+
*/
52+
typedef enum {
53+
/**
54+
* @brief Full JTAG interface (reset value)
55+
*/
56+
SWJ_CFG_FULL_JTAG = 0,
57+
/**
58+
* @brief JTAG enabled, but NJTRST disabled and pin PB4 usable as GPIO
59+
*/
60+
SWJ_CFG_NO_NJTRST = 1U << AFIO_PCF0_SWJ_CFG_Pos,
61+
/**
62+
* @brief JTAG disabled, all debug pins usable as GPIOs
63+
*/
64+
SWJ_CFG_NO_JTAG = 4U << AFIO_PCF0_SWJ_CFG_Pos,
65+
} afio_pcf0_swj_cfg_t;
66+
67+
#ifndef CONFIG_AFIO_PCF0_SWJ_CFG
68+
/**
69+
* @brief By default, enable JTAG but disable NJTRST
70+
*
71+
* This default makes PB4 usable as GPIO while still being able to debug and
72+
* flash via JTAG.
73+
*/
74+
#define CONFIG_AFIO_PCF0_SWJ_CFG SWJ_CFG_NO_NJTRST
75+
#endif
76+
4977
#ifdef __cplusplus
5078
}
5179
#endif

0 commit comments

Comments
 (0)