From a95e2e80bcf9e4e3f67cac63d5f7e1fed95fba56 Mon Sep 17 00:00:00 2001 From: Silke Hofstra Date: Sat, 29 Sep 2018 20:23:09 +0200 Subject: [PATCH] stlink: Add option to turn SWIM/RST into UART RX/TX Building for `stlink` with `SWIM_AS_UART=1` now enables the use of the SWIM and RST ports as RX and TX on cheap ST-Link V2 clones. This is done by using USART1 on the alternative port instead of USART2. --- src/platforms/stlink/Makefile.inc | 5 ++++- src/platforms/stlink/platform.c | 6 ++++++ src/platforms/stlink/platform.h | 12 ++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/platforms/stlink/Makefile.inc b/src/platforms/stlink/Makefile.inc index 9fa0ea6..b79b874 100644 --- a/src/platforms/stlink/Makefile.inc +++ b/src/platforms/stlink/Makefile.inc @@ -25,6 +25,10 @@ else LDFLAGS += --specs=nosys.specs endif +ifeq ($(SWIM_AS_UART), 1) +CFLAGS += -DSWIM_AS_UART=1 +endif + VPATH += platforms/stm32 SRC += cdcacm.c \ @@ -47,4 +51,3 @@ blackmagic_dfu.elf: usbdfu.o dfucore.o dfu_f1.o stlink_common.o serialno.o host_clean: -$(Q)$(RM) *.bin *elf *hex - diff --git a/src/platforms/stlink/platform.c b/src/platforms/stlink/platform.c index 021d797..d70f092 100644 --- a/src/platforms/stlink/platform.c +++ b/src/platforms/stlink/platform.c @@ -81,6 +81,12 @@ void platform_init(void) if (rev > 1) /* Reconnect USB */ gpio_set(GPIOA, GPIO15); cdcacm_init(); + +#ifdef SWIM_AS_UART + gpio_primary_remap(AFIO_MAPR_SWJ_CFG_FULL_SWJ, + AFIO_MAPR_USART1_REMAP); +#endif + /* Don't enable UART if we're being debugged. */ if (!(SCS_DEMCR & SCS_DEMCR_TRCENA)) usbuart_init(); diff --git a/src/platforms/stlink/platform.h b/src/platforms/stlink/platform.h index 9e270c4..9e2d210 100644 --- a/src/platforms/stlink/platform.h +++ b/src/platforms/stlink/platform.h @@ -105,15 +105,24 @@ int usbuart_debug_write(const char *buf, size_t len); #define IRQ_PRI_USB_VBUS (14 << 4) #define IRQ_PRI_SWO_DMA (0 << 4) +#ifdef SWIM_AS_UART +#define USBUSART USART1 +#define USBUSART_CR1 USART1_CR1 +#define USBUSART_DR USART1_DR +#define USBUSART_IRQ NVIC_USART1_IRQ +#define USBUSART_CLK RCC_USART1 +#define USBUSART_ISR(x) usart1_isr(x) +#else #define USBUSART USART2 #define USBUSART_CR1 USART2_CR1 #define USBUSART_DR USART2_DR #define USBUSART_IRQ NVIC_USART2_IRQ #define USBUSART_CLK RCC_USART2 +#define USBUSART_ISR(x) usart2_isr(x) +#endif #define USBUSART_PORT GPIOA #define USBUSART_TX_PIN GPIO2 #define USBUSART_RX_PIN GPIO3 -#define USBUSART_ISR(x) usart2_isr(x) #define USBUSART_DMA_BUS DMA1 #define USBUSART_DMA_CLK RCC_DMA1 @@ -179,5 +188,4 @@ extern uint32_t detect_rev(void); #define snprintf sniprintf #endif - #endif