Skip to content

Commit

Permalink
stlink: fix UART TX and NRST pin conflict in case of SWIM_AS_UART.
Browse files Browse the repository at this point in the history
Also renamed build option from SWIM_AS_UART to SWIM_NRST_AS_UART for better clarity (also for meson configs).

Also expanded documentation about pins remapping and added deprecation warning.
  • Loading branch information
positron96 authored and dragonmux committed Jun 1, 2024
1 parent dd2cacc commit 15b1d4b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cross-file/stlink.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ endian = 'little'
probe = 'stlink'
targets = 'cortexm,hc32,lpc,nrf,nxp,renesas,rp,sam,stm,ti'
rtt_support = false
stlink_swim_as_uart = false
stlink_swim_nrst_as_uart = false
bmd_bootloader = false
4 changes: 2 additions & 2 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ option(
)
option('bmd_bootloader', type: 'boolean', value: true, description: 'Use the BMD bootloader (not always applicable)')
option(
'stlink_swim_as_uart',
'stlink_swim_nrst_as_uart',
type: 'boolean',
value: false,
description: 'Repurpose the SWIM pins as the UART (only applicable to stlink)'
description: 'Repurpose the SWIM and NRST pins as the UART RX and TX (only applicable to stlink)'
)
option('serialno', type: 'string', value: '1', description: 'Serial number to report (only applicable to some probes)')
option(
Expand Down
7 changes: 6 additions & 1 deletion src/platforms/stlink/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ LDFLAGS += --specs=nosys.specs
endif

ifeq ($(SWIM_AS_UART), 1)
CFLAGS += -DSWIM_AS_UART=1
SWIM_NRST_AS_UART=1
$(warning Deprecation: SWIM_AS_UART has been renamed to SWIM_NRST_AS_UART; it might be ignored in the future)
endif

ifeq ($(SWIM_NRST_AS_UART), 1)
CFLAGS += -DSWIM_NRST_AS_UART=1
else
SRC += traceswoasync.c
endif
Expand Down
12 changes: 9 additions & 3 deletions src/platforms/stlink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ Running the BMP firmware on ST-Link v2 and ST-Link v2-1 provides:

For all commands below, unplug all other BMP/ST-Link beside the target(*1)

If your adaptor offers SWIM pins on the connector (many of clones of the official adaptors do this)
then they often don't provide a UART interface. In this case, build the firmware with
`SWIM_AS_UART=1` to repurpose the pins as the UART interface provided to the host over USB.
This build provides a UART interface over USB to the host.
By default, it's USART2 with pins PA2 and PA3 used as TX and RX.
Many clones of the official adaptors provide SWIM pins on the connector and don't provide a UART interface.
In this case, the firmware can be built with `SWIM_NRST_AS_UART=1` flag to repurpose the SWIM pins as the UART.
In particular, USART1 will be provided to the host and pins PB6 and PB7 will be used as TX and RX.

Note: on some clones PB6 is used as nRST pin, so with `SWIM_NRST_AS_UART=1` the UART will take up both SWIM (PB7) and RST (PB6) pins.
In this case nRST function is moved to PB0 pin and, if this pin is not on the connector, reset functionality will be lost.
On other devices where nRST pin is PB0, reset feature will remain functional.

Note: on some clones, SWIM is strongly pulled up by a 680 Ohm resistor.

Expand Down
8 changes: 4 additions & 4 deletions src/platforms/stlink/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ endif

probe_stlink_dependencies = [platform_common, platform_stm32f1]

stlink_swim_as_uart = get_option('stlink_swim_as_uart')
stlink_swim_nrst_as_uart = get_option('stlink_swim_nrst_as_uart')

if probe == 'stlink' and stlink_swim_as_uart
probe_stlink_args += ['-DSWIM_AS_UART=1']
if probe == 'stlink' and stlink_swim_nrst_as_uart
probe_stlink_args += ['-DSWIM_NRST_AS_UART=1']
else
probe_stlink_dependencies += fixme_platform_stm32_traceswoasync
endif
Expand All @@ -97,7 +97,7 @@ summary(
'Platform': 'STM32F1',
'Bootloader': bmd_bootloader ? 'Black Magic Debug Bootloader' : 'OEM ST Bootloader',
'Load Address': probe_stlink_load_address,
'SWIM as UART': probe == 'stlink' and stlink_swim_as_uart,
'SWIM as UART': probe == 'stlink' and stlink_swim_nrst_as_uart,
},
bool_yn: true,
section: 'Probe',
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/stlink/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void platform_init(void)
gpio_set(GPIOA, GPIO15);
blackmagic_usb_init();

#ifdef SWIM_AS_UART
#ifdef SWIM_NRST_AS_UART
gpio_primary_remap(AFIO_MAPR_SWJ_CFG_FULL_SWJ, AFIO_MAPR_USART1_REMAP);
#endif

Expand Down
14 changes: 9 additions & 5 deletions src/platforms/stlink/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ extern bool debug_bmp;
#define SWDIO_IN_PIN GPIO12
#endif

#define NRST_PORT GPIOB
#define NRST_PIN_V1 GPIO1
#define NRST_PIN_V2 GPIO0
#define NRST_PORT GPIOB
#define NRST_PIN_V1 GPIO1
#define NRST_PIN_V2 GPIO0
#ifdef SWIM_NRST_AS_UART
#define NRST_PIN_CLONE GPIO0
#else
#define NRST_PIN_CLONE GPIO6
#endif

#ifdef BLUEPILL
#define LED_PORT GPIOC
Expand All @@ -72,7 +76,7 @@ extern bool debug_bmp;
#define LED_PORT_UART GPIOA
#define LED_UART GPIO9

#ifndef SWIM_AS_UART
#ifndef SWIM_NRST_AS_UART
#define PLATFORM_HAS_TRACESWO 1
#endif

Expand Down Expand Up @@ -131,7 +135,7 @@ extern bool debug_bmp;
#define IRQ_PRI_USB_VBUS (14U << 4U)
#define IRQ_PRI_SWO_DMA (0U << 4U)

#ifdef SWIM_AS_UART
#ifdef SWIM_NRST_AS_UART
#define USBUSART USART1
#define USBUSART_CR1 USART1_CR1
#define USBUSART_DR USART1_DR
Expand Down

0 comments on commit 15b1d4b

Please sign in to comment.