你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32F103单片机JTAG端口重映射

[复制链接]
STMCU小助手 发布时间:2022-3-22 13:00
      STM32单片机复位后,JTAG口默认是被占用的。如果想将JTAG占用的几个IO口设置为普通IO口,就需要进行端口重映射。

复位后端口默认功能可以在STM32参考手册上看到

BC0N6I`ZY9VHG1D9YL}98GV.png

要将JTAG占用的这些口设置为普通IO口时,需要用复用功能重映射,在程序开始执行时将JTAG口重映射为普通IO口。

G$HUT7PL_3U~9CEATTVFT63.png
EOVLF[$T~GV[5$@CAT`_@R1.png

将JTAG要设置为普通 IO口时,需要设置AFIO_MAPR寄存器的SWJ_CFG位。如果用寄存器操作的话,就直接设置这个寄存器的值就行。如果用库函数操作的话,可以直接调用库函数void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState)。

这个库函数的具体实现可以在stm32f10x_gpio.c这个文件中查看,具体代码如下:

  1. /**
  2.   * @brief  Changes the mapping of the specified pin.
  3.   * @param  GPIO_Remap: selects the pin to remap.
  4.   *   This parameter can be one of the following values:
  5.   *     @arg GPIO_Remap_SPI1             : SPI1 Alternate Function mapping
  6.   *     @arg GPIO_Remap_I2C1             : I2C1 Alternate Function mapping
  7.   *     @arg GPIO_Remap_USART1           : USART1 Alternate Function mapping
  8.   *     @arg GPIO_Remap_USART2           : USART2 Alternate Function mapping
  9.   *     @arg GPIO_PartialRemap_USART3    : USART3 Partial Alternate Function mapping
  10.   *     @arg GPIO_FullRemap_USART3       : USART3 Full Alternate Function mapping
  11.   *     @arg GPIO_PartialRemap_TIM1      : TIM1 Partial Alternate Function mapping
  12.   *     @arg GPIO_FullRemap_TIM1         : TIM1 Full Alternate Function mapping
  13.   *     @arg GPIO_PartialRemap1_TIM2     : TIM2 Partial1 Alternate Function mapping
  14.   *     @arg GPIO_PartialRemap2_TIM2     : TIM2 Partial2 Alternate Function mapping
  15.   *     @arg GPIO_FullRemap_TIM2         : TIM2 Full Alternate Function mapping
  16.   *     @arg GPIO_PartialRemap_TIM3      : TIM3 Partial Alternate Function mapping
  17.   *     @arg GPIO_FullRemap_TIM3         : TIM3 Full Alternate Function mapping
  18.   *     @arg GPIO_Remap_TIM4             : TIM4 Alternate Function mapping
  19.   *     @arg GPIO_Remap1_CAN1            : CAN1 Alternate Function mapping
  20.   *     @arg GPIO_Remap2_CAN1            : CAN1 Alternate Function mapping
  21.   *     @arg GPIO_Remap_PD01             : PD01 Alternate Function mapping
  22.   *     @arg GPIO_Remap_TIM5CH4_LSI      : LSI connected to TIM5 Channel4 input capture for calibration
  23.   *     @arg GPIO_Remap_ADC1_ETRGINJ     : ADC1 External Trigger Injected Conversion remapping
  24.   *     @arg GPIO_Remap_ADC1_ETRGREG     : ADC1 External Trigger Regular Conversion remapping
  25.   *     @arg GPIO_Remap_ADC2_ETRGINJ     : ADC2 External Trigger Injected Conversion remapping
  26.   *     @arg GPIO_Remap_ADC2_ETRGREG     : ADC2 External Trigger Regular Conversion remapping
  27.   *     @arg GPIO_Remap_ETH              : Ethernet remapping (only for Connectivity line devices)
  28.   *     @arg GPIO_Remap_CAN2             : CAN2 remapping (only for Connectivity line devices)
  29.   *     @arg GPIO_Remap_SWJ_NoJTRST      : Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST
  30.   *     @arg GPIO_Remap_SWJ_JTAGDisable  : JTAG-DP Disabled and SW-DP Enabled
  31.   *     @arg GPIO_Remap_SWJ_Disable      : Full SWJ Disabled (JTAG-DP + SW-DP)
  32.   *     @arg GPIO_Remap_SPI3             : SPI3/I2S3 Alternate Function mapping (only for Connectivity line devices)
  33.   *                                        When the SPI3/I2S3 is remapped using this function, the SWJ is configured
  34.   *                                        to Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST.   
  35.   *     @arg GPIO_Remap_TIM2ITR1_PTP_SOF : Ethernet PTP output or USB OTG SOF (Start of Frame) connected
  36.   *                                        to TIM2 Internal Trigger 1 for calibration (only for Connectivity line devices)
  37.   *                                        If the GPIO_Remap_TIM2ITR1_PTP_SOF is enabled the TIM2 ITR1 is connected to
  38.   *                                        Ethernet PTP output. When Reset TIM2 ITR1 is connected to USB OTG SOF output.   
  39.   *     @arg GPIO_Remap_PTP_PPS          : Ethernet MAC PPS_PTS output on PB05 (only for Connectivity line devices)
  40.   *     @arg GPIO_Remap_TIM15            : TIM15 Alternate Function mapping (only for Value line devices)
  41.   *     @arg GPIO_Remap_TIM16            : TIM16 Alternate Function mapping (only for Value line devices)
  42.   *     @arg GPIO_Remap_TIM17            : TIM17 Alternate Function mapping (only for Value line devices)
  43.   *     @arg GPIO_Remap_CEC              : CEC Alternate Function mapping (only for Value line devices)
  44.   *     @arg GPIO_Remap_TIM1_DMA         : TIM1 DMA requests mapping (only for Value line devices)
  45.   *     @arg GPIO_Remap_TIM9             : TIM9 Alternate Function mapping (only for XL-density devices)
  46.   *     @arg GPIO_Remap_TIM10            : TIM10 Alternate Function mapping (only for XL-density devices)
  47.   *     @arg GPIO_Remap_TIM11            : TIM11 Alternate Function mapping (only for XL-density devices)
  48.   *     @arg GPIO_Remap_TIM13            : TIM13 Alternate Function mapping (only for High density Value line and XL-density devices)
  49.   *     @arg GPIO_Remap_TIM14            : TIM14 Alternate Function mapping (only for High density Value line and XL-density devices)
  50.   *     @arg GPIO_Remap_FSMC_NADV        : FSMC_NADV Alternate Function mapping (only for High density Value line and XL-density devices)
  51.   *     @arg GPIO_Remap_TIM67_DAC_DMA    : TIM6/TIM7 and DAC DMA requests remapping (only for High density Value line devices)
  52.   *     @arg GPIO_Remap_TIM12            : TIM12 Alternate Function mapping (only for High density Value line devices)
  53.   *     @arg GPIO_Remap_MISC             : Miscellaneous Remap (DMA2 Channel5 Position and DAC Trigger remapping,
  54.   *                                        only for High density Value line devices)     
  55.   * @param  NewState: new state of the port pin remapping.
  56.   *   This parameter can be: ENABLE or DISABLE.
  57.   * @retval None
  58.   */
  59. void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState)
  60. {
  61.   uint32_t tmp = 0x00, tmp1 = 0x00, tmpreg = 0x00, tmpmask = 0x00;

  62.   /* Check the parameters */
  63.   assert_param(IS_GPIO_REMAP(GPIO_Remap));
  64.   assert_param(IS_FUNCTIONAL_STATE(NewState));  

  65.   if((GPIO_Remap & 0x80000000) == 0x80000000)
  66.   {
  67.     tmpreg = AFIO->MAPR2;
  68.   }
  69.   else
  70.   {
  71.     tmpreg = AFIO->MAPR;
  72.   }

  73.   tmpmask = (GPIO_Remap & DBGAFR_POSITION_MASK) >> 0x10;
  74.   tmp = GPIO_Remap & LSB_MASK;

  75.   if ((GPIO_Remap & (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK)) == (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK))
  76.   {
  77.     tmpreg &= DBGAFR_SWJCFG_MASK;
  78.     AFIO->MAPR &= DBGAFR_SWJCFG_MASK;
  79.   }
  80.   else if ((GPIO_Remap & DBGAFR_NUMBITS_MASK) == DBGAFR_NUMBITS_MASK)
  81.   {
  82.     tmp1 = ((uint32_t)0x03) << tmpmask;
  83.     tmpreg &= ~tmp1;
  84.     tmpreg |= ~DBGAFR_SWJCFG_MASK;
  85.   }
  86.   else
  87.   {
  88.     tmpreg &= ~(tmp << ((GPIO_Remap >> 0x15)*0x10));
  89.     tmpreg |= ~DBGAFR_SWJCFG_MASK;
  90.   }

  91.   if (NewState != DISABLE)
  92.   {
  93.     tmpreg |= (tmp << ((GPIO_Remap >> 0x15)*0x10));
  94.   }

  95.   if((GPIO_Remap & 0x80000000) == 0x80000000)
  96.   {
  97.     AFIO->MAPR2 = tmpreg;
  98.   }
  99.   else
  100.   {
  101.     AFIO->MAPR = tmpreg;
  102.   }  
  103. }
复制代码

重映射JTAG口的相关设置只有三个。



通过后面的注释可以知道

GPIO_Remap_SWJ_NoJTRST  是使能JTAG和SW功能,但是不包括JTRST引脚。

GPIO_Remap_SWJ_JTAGDisable 是禁止JTAG功能,使能SW功能。

GPIO_Remap_SWJ_Disable  是禁止JTAG功能、禁止SW功能。

在设置的时候要注意,如果选中了GPIO_Remap_SWJ_Disable ,那么单片机就不能通过J-link烧写程序了。烧写程序只能通过串口用 ISP 方式进行下载。

通过库函数将JTAG口重映射为普通IO口代码如下:

  1. GPIO_InitTypeDef  GPIO_InitStructure;
  2.     RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE );                 //使能PA、PB端口时钟
  3.     RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE );                //使能复用功能时钟

  4.     //将JTAG端口设置为普通IO口
  5.     GPIO_PinRemapConfig( GPIO_Remap_SWJ_JTAGDisable, ENABLE );                                                                // JTAG-DP Disabled and SW-DP Enabled

  6.     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;
  7.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  8.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  9.     GPIO_Init( GPIOB, &GPIO_InitStructure );

  10.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_15 | GPIO_Pin_11 | GPIO_Pin_8;
  11.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  12.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13.     GPIO_Init( GPIOA, &GPIO_InitStructure );
复制代码

在使用复用功能重映射时,要开启AFIO时钟,否则端口重映射功能开启就会失败。



收藏 评论0 发布时间:2022-3-22 13:00

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版