Skip to content

Commit

Permalink
Merge pull request #124 from niklaut/fix/stm32_gpio_config
Browse files Browse the repository at this point in the history
Fix STM32 GPIO configuration for Trace pins
  • Loading branch information
mubes authored Oct 2, 2023
2 parents 523421a + 70dc2c6 commit 6f01150
Showing 1 changed file with 59 additions and 45 deletions.
104 changes: 59 additions & 45 deletions Support/gdbtrace.init
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ set $TPIUBASE=0xE0040000
set $ETMBASE=0xE0041000

define _setAddressesSTM32

# Locations in the memory map for interesting things on STM32
# Locations in the memory map for interesting things on STM32
set $RCCBASE = 0x40023800
set $GPIOBASE = 0x40020000
end

define _setAddressesIMXRT
Expand Down Expand Up @@ -809,20 +810,11 @@ define enableSTM32SWO
end

set $CPU=$CPU_STM32
_setAddressesSTM32
_setAddressesSTM32

if (($tgt==4) || ($tgt==7))
# STM32F4/7 variant.
# Enable AHB1ENR
set *0x40023830 |= 0x02
# Set MODER for PB3
set *0x40020400 &= ~(0x000000C0)
set *0x40020400 |= 0x00000080
# Set max (100MHz) speed in OSPEEDR
set *0x40020408 |= 0x000000C0
# No pull up or down in PUPDR
set *0x4002040C &= ~(0x000000C0)
# Set AF0 (==TRACESWO) in AFRL
set *0x40020420 &= ~(0x0000F000)
# STM32F4/7 variant: SWO on PB3.
enableSTM32Pin 1 3 3
else
# STM32F1 variant.
# RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
Expand Down Expand Up @@ -937,10 +929,55 @@ define _doTRACE

set *($TPIUBASE+0x304) = 0x102
end

# ====================================================================
define enableSTM32TRACE
define enableSTM32Pin
#set language c
_setAddressesSTM32
set $_GPIOPORT = $GPIOBASE + 0x400 * $arg0

# Enable GPIO port in RCC
set *($RCCBASE + 0x30) |= (0x1<<$arg0)

# MODER: Alternate Function
set *($_GPIOPORT+0x00) &= ~(0x3<<2*$arg1)
set *($_GPIOPORT+0x00) |= 0x2<<2*$arg1

# OTYPER: Push-Pull
set *($_GPIOPORT+0x04) &= ~(0x1<<$arg1)

# OSPEEDR: Drive speed
set *($_GPIOPORT+0x08) &= ~(0x3<<2*$arg1)
set *($_GPIOPORT+0x08) |= $arg2<<2*$arg1

# PUPDR: No pullup or pulldown
set *($_GPIOPORT+0x0C) &= ~(0x3<<2*$arg1)

# AFRL: AF0
if ($arg1 < 8)
set *($_GPIOPORT+0x20) &= ~(0xF<<4*$arg1)
else
set *($_GPIOPORT+0x24) &= ~(0xF<<4*($arg1 - 8))
end

# LOCKR: lock pin until the next reset
set *($_GPIOPORT+0x1C) = 0x10000 | (0x1<<$arg1)
set *($_GPIOPORT+0x1C) = 0x00000 | (0x1<<$arg1)
set *($_GPIOPORT+0x1C) = 0x10000 | (0x1<<$arg1)
set $_null = *($_GPIOPORT+0x1C)

#set language auto
end
document enableSTM32Pin
enableSTM32Pin <Port> <Pin> <Drive>: Enable TRACE function AF0 on a single STM32 pin
<Port> : Number of the port (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, 6=G, ...)
<Pin> : Pin number in [0, 15]
<Drive> : Drive strength (0=lowest, 3=highest)
end

#set language c
# ====================================================================
define enableSTM32TRACE
#set language c

set $bits=4
set $drive=1
Expand All @@ -967,47 +1004,24 @@ define enableSTM32TRACE

# Enable Trace TRCENA (DCB DEMCR) needed for clocks
set *($CDBBASE+0xC)=(1<<24)

# Enable AHB1ENR
set *0x40023830 |= 0x10

# Enable compensation cell
set *0x40023844 |= (1<<14)
set *0x40013820 |=1

# Setup PE2 & PE3
# Port Mode
set *0x40021000 &= ~(0x000000F0)
set *0x40021000 |= 0xA0

# Drive speed
set *0x40021008 &= ~0xf0
set *0x40021008 |= ($drive<<4)|($drive<<6)

# No Pull up or down
set *0x4002100C &= ~0xF0
# AF0
set *0x40021020 &= ~0xF0
enableSTM32Pin 4 2 $drive
enableSTM32Pin 4 3 $drive

if ($bits>0)
# Setup PE4
set *0x40021000 &= ~(0x00000300)
set *0x40021000 |= 0x200
set *0x40021008 &= ~0x300
set *0x40021008 |= ($drive<<8)
set *0x4002100C &= ~0x300
set *0x40021020 &= ~0x300
enableSTM32Pin 4 4 $drive
end

if ($bits>1)
# Setup PE5 & PE6

set *0x40021000 &= ~(0x00003C00)
set *0x40021000 |= 0x2800
set *0x40021008 &= ~0x3C00
set *0x40021008 |= ($drive<<10)|($drive<<12)
set *0x4002100C &= ~0x3C00
set *0x40021020 &= ~0x3C00
enableSTM32Pin 4 5 $drive
enableSTM32Pin 4 6 $drive
end

# Set number of bits in DBGMCU_CR
Expand Down

0 comments on commit 6f01150

Please sign in to comment.