STM32单片机复位后,JTAG口默认是被占用的。如果想将JTAG占用的几个IO口设置为普通IO口,就需要进行端口重映射。
) G+ m6 I: [6 I, m" b, {2 n( i: n8 D% e& m7 g
复位后端口默认功能可以在STM32参考手册上看到
# [% b* \% _9 \5 _0 V. o
6 `& | I3 J0 \
4 @2 U' g6 q z2 E& s Y! o% u! L
要将JTAG占用的这些口设置为普通IO口时,需要用复用功能重映射,在程序开始执行时将JTAG口重映射为普通IO口。
+ f* ~- D( \2 G3 l0 ~ B# {$ |! q# B3 w( L) I
4 T% g2 H/ P3 B
; x0 K: \4 Q q5 C# L7 S' t- s) Q
将JTAG要设置为普通 IO口时,需要设置AFIO_MAPR寄存器的SWJ_CFG位。如果用寄存器操作的话,就直接设置这个寄存器的值就行。如果用库函数操作的话,可以直接调用库函数void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState)。
6 C. d' g% F/ S: b) Y! r
5 _$ q) T( u3 @+ h$ S( D c这个库函数的具体实现可以在stm32f10x_gpio.c这个文件中查看,具体代码如下:
1 \* Q$ I9 v/ G: x( q7 o) u! L# E0 W5 u4 E# L3 @
- /**
8 A% }7 f5 ~/ R; \ - * @brief Changes the mapping of the specified pin.2 a1 k$ k8 O! V5 V) G# J: r
- * @param GPIO_Remap: selects the pin to remap.
K+ s! O: I0 L* s - * This parameter can be one of the following values:: ]+ O7 C* @# g& \
- * @arg GPIO_Remap_SPI1 : SPI1 Alternate Function mapping
$ M9 t. q! I' x' ]* K- P - * @arg GPIO_Remap_I2C1 : I2C1 Alternate Function mapping, x d4 m0 j u
- * @arg GPIO_Remap_USART1 : USART1 Alternate Function mapping9 H; X0 j% ]9 [) A6 W
- * @arg GPIO_Remap_USART2 : USART2 Alternate Function mapping
' X" Y( `3 S7 b0 E" m - * @arg GPIO_PartialRemap_USART3 : USART3 Partial Alternate Function mapping+ i( t6 H" b1 y7 f. w- e
- * @arg GPIO_FullRemap_USART3 : USART3 Full Alternate Function mapping, ~* P2 x1 J+ ?1 r
- * @arg GPIO_PartialRemap_TIM1 : TIM1 Partial Alternate Function mapping. s6 F3 N7 \ H
- * @arg GPIO_FullRemap_TIM1 : TIM1 Full Alternate Function mapping
$ F+ h; J w+ s1 h0 U9 d- c - * @arg GPIO_PartialRemap1_TIM2 : TIM2 Partial1 Alternate Function mapping
! [; ?( `5 I: i/ m' ~" I - * @arg GPIO_PartialRemap2_TIM2 : TIM2 Partial2 Alternate Function mapping
) {2 ?8 I" { }: } - * @arg GPIO_FullRemap_TIM2 : TIM2 Full Alternate Function mapping
% v" c* W5 b6 o7 g* k - * @arg GPIO_PartialRemap_TIM3 : TIM3 Partial Alternate Function mapping
& r* T4 V) c2 g5 {7 d - * @arg GPIO_FullRemap_TIM3 : TIM3 Full Alternate Function mapping8 s; J; A0 H4 Y7 r" q1 [3 ^6 V
- * @arg GPIO_Remap_TIM4 : TIM4 Alternate Function mapping% s4 g5 z; m6 |
- * @arg GPIO_Remap1_CAN1 : CAN1 Alternate Function mapping
_) c1 C7 n3 ^" f9 Y - * @arg GPIO_Remap2_CAN1 : CAN1 Alternate Function mapping
. E% o% l ]5 v6 a( T - * @arg GPIO_Remap_PD01 : PD01 Alternate Function mapping9 z4 l2 ], `2 k5 Z4 L w/ ?
- * @arg GPIO_Remap_TIM5CH4_LSI : LSI connected to TIM5 Channel4 input capture for calibration* e, I6 F. U+ }! n
- * @arg GPIO_Remap_ADC1_ETRGINJ : ADC1 External Trigger Injected Conversion remapping0 c6 R' ~: |6 ]3 T8 h$ d0 ^% O
- * @arg GPIO_Remap_ADC1_ETRGREG : ADC1 External Trigger Regular Conversion remapping
0 E. q- w( o& j% W3 G - * @arg GPIO_Remap_ADC2_ETRGINJ : ADC2 External Trigger Injected Conversion remapping
4 c# f E! p/ q7 d/ g |% Y* G - * @arg GPIO_Remap_ADC2_ETRGREG : ADC2 External Trigger Regular Conversion remapping0 K3 a+ o8 _( @! Q8 R1 T" y
- * @arg GPIO_Remap_ETH : Ethernet remapping (only for Connectivity line devices)
' Y4 c4 c H7 r9 O' D' } - * @arg GPIO_Remap_CAN2 : CAN2 remapping (only for Connectivity line devices)( q, N. c. j9 M) G- L0 y6 C7 d
- * @arg GPIO_Remap_SWJ_NoJTRST : Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST
& @+ `# p4 [0 W - * @arg GPIO_Remap_SWJ_JTAGDisable : JTAG-DP Disabled and SW-DP Enabled
3 ^. z4 e% [7 k3 U - * @arg GPIO_Remap_SWJ_Disable : Full SWJ Disabled (JTAG-DP + SW-DP)# l( Y0 `* ?/ ^$ m
- * @arg GPIO_Remap_SPI3 : SPI3/I2S3 Alternate Function mapping (only for Connectivity line devices)3 ~; |9 ~. ]) ~' f* C O
- * When the SPI3/I2S3 is remapped using this function, the SWJ is configured
9 ^, Z2 O6 q4 x9 {# W: W8 ~- ? - * to Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST. " m3 p4 W! q4 y6 n _$ y; j
- * @arg GPIO_Remap_TIM2ITR1_PTP_SOF : Ethernet PTP output or USB OTG SOF (Start of Frame) connected
- ` b6 `9 Q' |# L7 t( {" m - * to TIM2 Internal Trigger 1 for calibration (only for Connectivity line devices), q3 {# i. d2 e: q; g
- * If the GPIO_Remap_TIM2ITR1_PTP_SOF is enabled the TIM2 ITR1 is connected to
$ `1 |/ ?2 c6 l' z ]+ j - * Ethernet PTP output. When Reset TIM2 ITR1 is connected to USB OTG SOF output.
1 u$ a" A# X: W' `7 b - * @arg GPIO_Remap_PTP_PPS : Ethernet MAC PPS_PTS output on PB05 (only for Connectivity line devices)$ R& r1 b' y+ j2 W1 @' Y
- * @arg GPIO_Remap_TIM15 : TIM15 Alternate Function mapping (only for Value line devices)1 c* `& T* K0 H$ c9 ]9 ^
- * @arg GPIO_Remap_TIM16 : TIM16 Alternate Function mapping (only for Value line devices)' ]7 T3 x; U. `- _
- * @arg GPIO_Remap_TIM17 : TIM17 Alternate Function mapping (only for Value line devices)
0 c; K+ g- V: {( u( W- l - * @arg GPIO_Remap_CEC : CEC Alternate Function mapping (only for Value line devices)
+ u. M5 X+ _) K. r - * @arg GPIO_Remap_TIM1_DMA : TIM1 DMA requests mapping (only for Value line devices)
5 Y1 b, |% a& p6 A - * @arg GPIO_Remap_TIM9 : TIM9 Alternate Function mapping (only for XL-density devices): ^: Z E3 Y/ V/ S) t4 r, {8 q
- * @arg GPIO_Remap_TIM10 : TIM10 Alternate Function mapping (only for XL-density devices)
; K5 l& }" w6 j( D - * @arg GPIO_Remap_TIM11 : TIM11 Alternate Function mapping (only for XL-density devices)# T! c& M1 {3 | i3 V- n# I) ?" q3 ^7 `9 O
- * @arg GPIO_Remap_TIM13 : TIM13 Alternate Function mapping (only for High density Value line and XL-density devices)
S8 K" m2 b5 b" H7 [ - * @arg GPIO_Remap_TIM14 : TIM14 Alternate Function mapping (only for High density Value line and XL-density devices)6 E& _2 A6 d) ^* B2 o8 V' J* L
- * @arg GPIO_Remap_FSMC_NADV : FSMC_NADV Alternate Function mapping (only for High density Value line and XL-density devices)
) `6 X; ~. P4 d* G% l - * @arg GPIO_Remap_TIM67_DAC_DMA : TIM6/TIM7 and DAC DMA requests remapping (only for High density Value line devices) [- R/ P% u) w$ s% M
- * @arg GPIO_Remap_TIM12 : TIM12 Alternate Function mapping (only for High density Value line devices)
q4 l. g9 e1 V9 @- I - * @arg GPIO_Remap_MISC : Miscellaneous Remap (DMA2 Channel5 Position and DAC Trigger remapping,
& Q6 I8 h h3 p5 O' |9 G - * only for High density Value line devices)
}; k- J+ g) Q% c! G+ U, O - * @param NewState: new state of the port pin remapping.
- f3 P& m! @0 ]4 d6 t; N2 ? - * This parameter can be: ENABLE or DISABLE.) F" A3 ?8 E N2 Z
- * @retval None
" ]$ v I9 Y% |: R& ? - */- w9 H( t; L* m8 o; J7 n/ D' V
- void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState)
0 \6 O4 l/ A- z* ~0 {1 m& F @ - {
0 w) R' o* S2 z6 B3 k. e. t - uint32_t tmp = 0x00, tmp1 = 0x00, tmpreg = 0x00, tmpmask = 0x00;
9 a! w4 b5 P6 r - 5 b* t, i& z# x. U9 ~
- /* Check the parameters */
6 @% E* F5 @1 Q5 |, _3 X - assert_param(IS_GPIO_REMAP(GPIO_Remap));% p9 _- h; d8 t- H
- assert_param(IS_FUNCTIONAL_STATE(NewState));
; u( \+ @0 l% S6 T - - v, K2 H# u: ^: [2 F Q. X0 |. B
- if((GPIO_Remap & 0x80000000) == 0x80000000)
: O, t2 D7 `4 w) E/ Z - {5 d' B8 `9 }! y& N5 Y
- tmpreg = AFIO->MAPR2;
2 j# K% b# G( v/ V2 s4 ^ - }
/ r6 f. T% t& H# @, R" W/ d - else4 E: s1 Y& n# Q# {
- {3 ~4 D8 e$ f4 m3 j8 M2 x4 C( q
- tmpreg = AFIO->MAPR;
! i3 n: g& W- c4 j" | - }
* i4 M# _9 y9 H0 s2 y4 h3 o G - . |9 Y6 p2 F* R
- tmpmask = (GPIO_Remap & DBGAFR_POSITION_MASK) >> 0x10;* a' m4 T& b! `, Y
- tmp = GPIO_Remap & LSB_MASK;
% Q- ]. @" V/ L% e2 O
, [4 R I" w( F7 p6 }- if ((GPIO_Remap & (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK)) == (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK))" X, q2 a, g& y5 O
- {
6 c5 ]7 d: T5 ] - tmpreg &= DBGAFR_SWJCFG_MASK;
2 g+ N3 l% o( M. i4 ]0 P - AFIO->MAPR &= DBGAFR_SWJCFG_MASK;# I1 |$ h' R8 {6 V5 s
- }% o2 _' r- J2 K
- else if ((GPIO_Remap & DBGAFR_NUMBITS_MASK) == DBGAFR_NUMBITS_MASK): Q9 Y/ {7 ]/ ^* j) j _
- {+ a( `( p( T2 a
- tmp1 = ((uint32_t)0x03) << tmpmask;6 w2 g( Q" R' ?$ {$ C
- tmpreg &= ~tmp1;
; E1 d' p" }! _) F; `0 N - tmpreg |= ~DBGAFR_SWJCFG_MASK;
) ^# x# a9 k5 n b% Q - }- {4 N* D1 e8 {. U
- else8 c. F+ Y2 y; m9 v+ a, P8 Z
- {2 K3 N, V: F8 W% o$ K9 z( t' e- \
- tmpreg &= ~(tmp << ((GPIO_Remap >> 0x15)*0x10));! R( h( u4 P$ z& U$ m1 L* a
- tmpreg |= ~DBGAFR_SWJCFG_MASK;
# A. @. X8 k4 X/ B; \2 F1 p - }
0 Z, G/ p' a: i
: I' y2 k$ b" e& |* a$ u q- T- if (NewState != DISABLE)& k3 m E0 _& c! V5 T6 c
- {
$ s0 n. p6 W4 Q; V% e! d; f" h - tmpreg |= (tmp << ((GPIO_Remap >> 0x15)*0x10));
% n7 X4 G1 p8 {- G! t - }' x5 @; m3 u, g: d# E
- 3 x) H1 o+ V, l/ O" ]$ N- S/ N+ K
- if((GPIO_Remap & 0x80000000) == 0x80000000)
7 n3 O' R- `, }# b/ ^; ^ - {, l$ m+ j! f6 ~
- AFIO->MAPR2 = tmpreg;
% C8 ]2 @" y. Y- p. t0 t* G - }
, Q+ J2 m/ |2 ?( ~7 B1 y - else% J! L6 a7 c2 |6 \ s
- {. g- g/ L8 d% L# Q: F$ ?$ V6 d
- AFIO->MAPR = tmpreg;0 ]8 |3 X3 {& K8 X/ l$ r
- }
+ P" p2 z/ s# w5 l0 e - }
复制代码 * Z) j. }. n7 }* k$ f: N4 b( t! b
重映射JTAG口的相关设置只有三个。" i, I) M( P. N R, K6 V
' j& r. \ U& ^8 F$ o/ c
' U. r7 G; z7 ^7 Z4 t4 u$ q. w' z# T K- l B( I
通过后面的注释可以知道
3 c% P# e$ A2 p5 ?# ^9 K7 g' j# J6 Q8 f
GPIO_Remap_SWJ_NoJTRST 是使能JTAG和SW功能,但是不包括JTRST引脚。9 j. h/ f: u. f' A
2 a( S8 V8 O- E. K* J/ I
GPIO_Remap_SWJ_JTAGDisable 是禁止JTAG功能,使能SW功能。
m% T+ M' H) ~$ ?5 `2 {6 H
1 V6 ]) \" m9 r. \4 F4 o. oGPIO_Remap_SWJ_Disable 是禁止JTAG功能、禁止SW功能。
. C: J0 O# T/ m8 A3 @" Q @9 W. g- p( |/ a' m/ M8 k
在设置的时候要注意,如果选中了GPIO_Remap_SWJ_Disable ,那么单片机就不能通过J-link烧写程序了。烧写程序只能通过串口用 ISP 方式进行下载。/ A: v4 d9 u7 d! ~! x' X; U" X
: I' g8 L; _. |9 j% K% M通过库函数将JTAG口重映射为普通IO口代码如下:
: Y8 F& W7 f3 I+ P; T2 Y4 W! d$ f) S* p' Y' \. X+ N+ l6 K
- GPIO_InitTypeDef GPIO_InitStructure; U3 d6 m! R2 |+ ?9 m' a
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE ); //使能PA、PB端口时钟
: X9 P- i2 z, M% U" R* _ - RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE ); //使能复用功能时钟# h4 e& w. y$ w, x% `) O* @4 O
- ; h. n) l3 A& D( Q6 ?% S1 ]+ {: ]
- //将JTAG端口设置为普通IO口2 W, y# V( s7 A( `9 _% F& }+ o
- GPIO_PinRemapConfig( GPIO_Remap_SWJ_JTAGDisable, ENABLE ); // JTAG-DP Disabled and SW-DP Enabled
5 u6 E1 B( M8 o$ h2 R
$ s# z% n0 i) M7 ` P" P% R1 V8 w$ C- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_14 | GPIO_Pin_15;# ^! B' I; b0 g/ M, @- L. A
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
1 p: u# _( D+ P4 p/ ] - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
8 m( a q$ G; a" V' `. t1 I - GPIO_Init( GPIOB, &GPIO_InitStructure );
, u4 E" y7 f; d9 |- ~# N
/ r% F, t N- k7 S- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_15 | GPIO_Pin_11 | GPIO_Pin_8;; k8 a" O4 S9 h: g& ]
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;) g( n9 T$ D* t( P* j
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
& Z8 a' L& m2 U! i& H - GPIO_Init( GPIOA, &GPIO_InitStructure );
复制代码
/ a# I% F9 N$ O4 h+ f在使用复用功能重映射时,要开启AFIO时钟,否则端口重映射功能开启就会失败。8 R( y J( B% g! C- l
# C i7 O1 x: x( L7 i# I! T3 L( h0 K1 _1 h2 j* _
/ }6 ~4 y3 b X6 Y8 M1 x5 l, {! l |