找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5622|回复: 0
打印 上一主题 下一主题
收起左侧

stm32f10x_gpio.c

[复制链接]
跳转到指定楼层
楼主
ID:72519 发表于 2015-1-23 03:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 liuda 于 2015-1-23 03:51 编辑
  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f10x_gpio.c
  4.   * @author  MCD Application Team
  5.   * @version V3.5.0
  6.   * @date    11-March-2011
  7.   * @brief   This file provides all the GPIO firmware functions.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17.   *
  18.   * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
  19.   ******************************************************************************
  20.   */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f10x_gpio.h"
  23. #include "stm32f10x_rcc.h"
  24. /** @addtogroup STM32F10x_StdPeriph_Driver
  25.   * @{
  26.   */
  27. /** @defgroup GPIO
  28.   * @brief GPIO driver modules
  29.   * @{
  30.   */
  31. /** @defgroup GPIO_Private_TypesDefinitions
  32.   * @{
  33.   */
  34. /**
  35.   * @}
  36.   */
  37. /** @defgroup GPIO_Private_Defines
  38.   * @{
  39.   */
  40. /* ------------ RCC registers bit address in the alias region ----------------*/
  41. #define AFIO_OFFSET                 (AFIO_BASE - PERIPH_BASE)
  42. /* --- EVENTCR Register -----*/
  43. /* Alias word address of EVOE bit */
  44. #define EVCR_OFFSET                 (AFIO_OFFSET + 0x00)
  45. #define EVOE_BitNumber              ((uint8_t)0x07)
  46. #define EVCR_EVOE_BB                (PERIPH_BB_BASE + (EVCR_OFFSET * 32) + (EVOE_BitNumber * 4))

  47. /* ---  MAPR Register ---*/
  48. /* Alias word address of MII_RMII_SEL bit */
  49. #define MAPR_OFFSET                 (AFIO_OFFSET + 0x04)
  50. #define MII_RMII_SEL_BitNumber      ((u8)0x17)
  51. #define MAPR_MII_RMII_SEL_BB        (PERIPH_BB_BASE + (MAPR_OFFSET * 32) + (MII_RMII_SEL_BitNumber * 4))

  52. #define EVCR_PORTPINCONFIG_MASK     ((uint16_t)0xFF80)
  53. #define LSB_MASK                    ((uint16_t)0xFFFF)
  54. #define DBGAFR_POSITION_MASK        ((uint32_t)0x000F0000)
  55. #define DBGAFR_SWJCFG_MASK          ((uint32_t)0xF0FFFFFF)
  56. #define DBGAFR_LOCATION_MASK        ((uint32_t)0x00200000)
  57. #define DBGAFR_NUMBITS_MASK         ((uint32_t)0x00100000)
  58. /**
  59.   * @}
  60.   */
  61. /** @defgroup GPIO_Private_Macros
  62.   * @{
  63.   */
  64. /**
  65.   * @}
  66.   */
  67. /** @defgroup GPIO_Private_Variables
  68.   * @{
  69.   */
  70. /**
  71.   * @}
  72.   */
  73. /** @defgroup GPIO_Private_FunctionPrototypes
  74.   * @{
  75.   */
  76. /**
  77.   * @}
  78.   */
  79. /** @defgroup GPIO_Private_Functions
  80.   * @{
  81.   */
  82. /**
  83.   * @brief  Deinitializes the GPIOx peripheral registers to their default reset values.
  84.   * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  85.   * @retval None
  86.   */
  87. void GPIO_DeInit(GPIO_TypeDef* GPIOx)//恢复GPIO的默认值;输入:GPIO端口
  88. {
  89.   /* Check the parameters */
  90.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));//参数有效性检测

  91.   if (GPIOx == GPIOA)//GPIOA的寄存器设置恢复到默认值
  92.   {
  93.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE);//先把复位寄存器置1
  94.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE);//再把恢复寄存器置0
  95.   }
  96.   else if (GPIOx == GPIOB)//GPIOB的寄存器设置恢复到默认值
  97.   {
  98.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE);
  99.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE);
  100.   }
  101.   else if (GPIOx == GPIOC)//GPIOC的寄存器设置恢复到默认值
  102.   {
  103.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, ENABLE);
  104.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, DISABLE);
  105.   }
  106.   else if (GPIOx == GPIOD)//GPIOD的寄存器设置恢复到默认值
  107.   {
  108.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, ENABLE);
  109.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, DISABLE);
  110.   }   
  111.   else if (GPIOx == GPIOE)//GPIOE的寄存器设置恢复到默认值
  112.   {
  113.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, ENABLE);
  114.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, DISABLE);
  115.   }
  116.   else if (GPIOx == GPIOF)//GPIOF的寄存器设置恢复到默认值
  117.   {
  118.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOF, ENABLE);
  119.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOF, DISABLE);
  120.   }
  121.   else
  122.   {
  123.     if (GPIOx == GPIOG)//GPIOG的寄存器设置恢复到默认值
  124.     {
  125.       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOG, ENABLE);
  126.       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOG, DISABLE);
  127.     }
  128.   }
  129. }
  130. /**
  131.   * @brief  Deinitializes the Alternate Functions (remap, event control
  132.   *   and EXTI configuration) registers to their default reset values.
  133.   * @param  None
  134.   * @retval None
  135.   */
  136. void GPIO_AFIODeInit(void)//复用功能寄存器恢复到默认值
  137. {
  138.   RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, ENABLE);
  139.   RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, DISABLE);
  140. }
  141. /**
  142.   * @brief  Initializes the GPIOx peripheral according to the specified
  143.   *         parameters in the GPIO_InitStruct.
  144.   * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  145.   * @param  GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that
  146.   *         contains the configuration information for the specified GPIO peripheral.
  147.   * @retval None
  148.   */
  149. /*GPIO初始化函数;输入:需要初始化的GPIO端口,存放端口初始化信息的结构体指针*/
  150. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)//GPIO初始化函数,调用此函数才是真正对GPIO初始化
  151. {
  152.   uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
  153.   uint32_t tmpreg = 0x00, pinmask = 0x00;
  154.   /* Check the parameters */
  155.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));//参数检测,检测参数是否有效
  156.   assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  157.   assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));

  158. /*---------------------------- GPIO Mode Configuration -----------------------*/
  159.   currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
  160.   if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)//如果是0x1n为输出模式,下面对其进行速度设置
  161.   {
  162.     /* Check the parameters */
  163.     assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
  164.     /* Output mode */
  165.     currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed; //速度设置
  166.   }
  167. /*---------------------------- GPIO CRL Configuration ------------------------*/
  168.   /* Configure the eight low port pins */
  169.   if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)//端口的低8位
  170.   {
  171.     tmpreg = GPIOx->CRL;
  172.     for (pinpos = 0x00; pinpos < 0x08; pinpos++)
  173.     {
  174.       pos = ((uint32_t)0x01) << pinpos;
  175.       /* Get the port pins position */
  176.       currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
  177.       if (currentpin == pos)
  178.       {
  179.         pos = pinpos << 2;
  180.         /* Clear the corresponding low control register bits */
  181.         pinmask = ((uint32_t)0x0F) << pos;
  182.         tmpreg &= ~pinmask;
  183.         /* Write the mode configuration in the corresponding bits */
  184.         tmpreg |= (currentmode << pos);
  185.         /* Reset the corresponding ODR bit */
  186.         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)//下拉输入时清零输出数据寄存器(选为下拉)
  187.         {
  188.           GPIOx->BRR = (((uint32_t)0x01) << pinpos);
  189.         }
  190.         else
  191.         {
  192.           /* Set the corresponding ODR bit */
  193.           if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)//上拉输入时置位输出数据寄存器(选为上拉)
  194.           {
  195.             GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
  196.           }
  197.         }
  198.       }
  199.     }
  200.     GPIOx->CRL = tmpreg;
  201.   }
  202. /*---------------------------- GPIO CRH Configuration ------------------------*/
  203.   /* Configure the eight high port pins */
  204.   if (GPIO_InitStruct->GPIO_Pin > 0x00FF) //端口的高8位
  205.   {
  206.     tmpreg = GPIOx->CRH;
  207.     for (pinpos = 0x00; pinpos < 0x08; pinpos++)
  208.     {
  209.       pos = (((uint32_t)0x01) << (pinpos + 0x08));
  210.       /* Get the port pins position */
  211.       currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
  212.       if (currentpin == pos)
  213.       {
  214.         pos = pinpos << 2;
  215.         /* Clear the corresponding high control register bits */
  216.         pinmask = ((uint32_t)0x0F) << pos;
  217.         tmpreg &= ~pinmask;
  218.         /* Write the mode configuration in the corresponding bits */
  219.         tmpreg |= (currentmode << pos);
  220.         /* Reset the corresponding ODR bit */
  221.         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
  222.         {
  223.           GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));
  224.         }
  225.         /* Set the corresponding ODR bit */
  226.         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
  227.         {
  228.           GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));
  229.         }
  230.       }
  231.     }
  232.     GPIOx->CRH = tmpreg;
  233.   }
  234. }
  235. /**
  236.   * @brief  Fills each GPIO_InitStruct member with its default value.
  237.   * @param  GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure which will
  238.   *         be initialized.
  239.   * @retval None
  240.   */
  241. void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)//对存放GPIO端口信息的结构体进行初始化,为所有端口浮空输入
  242. {
  243.   /* Reset GPIO init structure parameters values */
  244.   GPIO_InitStruct->GPIO_Pin  = GPIO_Pin_All;
  245.   GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
  246.   GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
  247. }
  248. /**
  249.   * @brief  Reads the specified input port pin.
  250.   * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  251.   * @param  GPIO_Pin:  specifies the port bit to read.
  252.   *   This parameter can be GPIO_Pin_x where x can be (0..15).
  253.   * @retval The input port pin value.
  254.   */
  255. /*读取GPIO的一位输入值;输入:GPIO端口,端口的某位;返回:此位的读取值*/
  256. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)//读取一个GPIO端口的一位输入值
  257. {
  258.   uint8_t bitstatus = 0x00;

  259.   /* Check the parameters */
  260.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  261.   assert_param(IS_GET_GPIO_PIN(GPIO_Pin));

  262.   if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
  263.   {
  264.     bitstatus = (uint8_t)Bit_SET;
  265.   }
  266.   else
  267.   {
  268.     bitstatus = (uint8_t)Bit_RESET;
  269.   }
  270.   return bitstatus;
  271. }
  272. /**
  273.   * @brief  Reads the specified GPIO input data port.
  274.   * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  275.   * @retval GPIO input data port value.
  276.   */
  277. uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)//读取一个端口的输入数据
  278. {
  279.   /* Check the parameters */
  280.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

  281.   return ((uint16_t)GPIOx->IDR);
  282. }
  283. /**
  284.   * @brief  Reads the specified output data port bit.
  285.   * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  286.   * @param  GPIO_Pin:  specifies the port bit to read.
  287.   *   This parameter can be GPIO_Pin_x where x can be (0..15).
  288.   * @retval The output port pin value.
  289.   */
  290. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)//读取一个端口的一位输出值
  291. {
  292.   uint8_t bitstatus = 0x00;
  293.   /* Check the parameters */
  294.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  295.   assert_param(IS_GET_GPIO_PIN(GPIO_Pin));

  296.   if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
  297.   {
  298.     bitstatus = (uint8_t)Bit_SET;
  299.   }
  300.   else
  301.   {
  302.     bitstatus = (uint8_t)Bit_RESET;
  303.   }
  304.   return bitstatus;
  305. }
  306. /**
  307.   * @brief  Reads the specified GPIO output data port.
  308.   * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  309.   * @retval GPIO output data port value.
  310.   */
  311. uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)//读取一个端口的输出值
  312. {
  313.   /* Check the parameters */
  314.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  315.    
  316.   return ((uint16_t)GPIOx->ODR);
  317. }
  318. /**
  319.   * @brief  Sets the selected data port bits.
  320.   * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  321.   * @param  GPIO_Pin: specifies the port bits to be written.
  322.   *   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  323.   * @retval None
  324.   */
  325. void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)//对一个端口的几位输出置位
  326. {
  327.   /* Check the parameters */
  328.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  329.   assert_param(IS_GPIO_PIN(GPIO_Pin));

  330.   GPIOx->BSRR = GPIO_Pin;
  331. }
  332. /**
  333.   * @brief  Clears the selected data port bits.
  334.   * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  335.   * @param  GPIO_Pin: specifies the port bits to be written.
  336.   *   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  337.   * @retval None
  338.   */
  339. void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)//对一个端口的几位输出复位
  340. {
  341.   /* Check the parameters */
  342.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  343.   assert_param(IS_GPIO_PIN(GPIO_Pin));

  344.   GPIOx->BRR = GPIO_Pin;
  345. }
  346. /**
  347.   * @brief  Sets or clears the selected data port bit.
  348.   * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  349.   * @param  GPIO_Pin: specifies the port bit to be written.
  350.   *   This parameter can be one of GPIO_Pin_x where x can be (0..15).
  351.   * @param  BitVal: specifies the value to be written to the selected bit.
  352.   *   This parameter can be one of the BitAction enum values:
  353.   *     @arg Bit_RESET: to clear the port pin
  354.   *     @arg Bit_SET: to set the port pin
  355.   * @retval None
  356.   */
  357. void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)//对一个端口的一位输出进行操作
  358. {
  359.   /* Check the parameters */
  360.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  361.   assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  362.   assert_param(IS_GPIO_BIT_ACTION(BitVal));

  363.   if (BitVal != Bit_RESET)
  364.   {
  365.     GPIOx->BSRR = GPIO_Pin;
  366.   }
  367.   else
  368.   {
  369.     GPIOx->BRR = GPIO_Pin;
  370.   }
  371. }
  372. /**
  373.   * @brief  Writes data to the specified GPIO data port.
  374.   * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  375.   * @param  PortVal: specifies the value to be written to the port output data register.
  376.   * @retval None
  377.   */
  378. void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)//对一个端口的输出进行操作
  379. {
  380.   /* Check the parameters */
  381.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

  382.   GPIOx->ODR = PortVal;
  383. }
  384. /**
  385.   * @brief  Locks GPIO Pins configuration registers.
  386.   * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  387.   * @param  GPIO_Pin: specifies the port bit to be written.
  388.   *   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  389.   * @retval None
  390.   */
  391. void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)//对一个端口的某些位的配置进行锁定
  392. {
  393.   uint32_t tmp = 0x00010000;

  394.   /* Check the parameters */
  395.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  396.   assert_param(IS_GPIO_PIN(GPIO_Pin));

  397.   tmp |= GPIO_Pin;
  398.   /* Set LCKK bit */
  399.   GPIOx->LCKR = tmp;
  400.   /* Reset LCKK bit */
  401.   GPIOx->LCKR =  GPIO_Pin;
  402.   /* Set LCKK bit */
  403.   GPIOx->LCKR = tmp;
  404.   /* Read LCKK bit*/
  405.   tmp = GPIOx->LCKR;
  406.   /* Read LCKK bit*/
  407.   tmp = GPIOx->LCKR;
  408. }
  409. /**
  410.   * @brief  Selects the GPIO pin used as Event output.
  411.   * @param  GPIO_PortSource: selects the GPIO port to be used as source
  412.   *   for Event output.
  413.   *   This parameter can be GPIO_PortSourceGPIOx where x can be (A..E).
  414.   * @param  GPIO_PinSource: specifies the pin for the Event output.
  415.   *   This parameter can be GPIO_PinSourcex where x can be (0..15).
  416.   * @retval None
  417.   */
  418. void GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)//端口的事件输出配置
  419. {
  420.   uint32_t tmpreg = 0x00;
  421.   /* Check the parameters */
  422.   assert_param(IS_GPIO_EVENTOUT_PORT_SOURCE(GPIO_PortSource));
  423.   assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
  424.    
  425.   tmpreg = AFIO->EVCR;
  426.   /* Clear the PORT[6:4] and PIN[3:0] bits */
  427.   tmpreg &= EVCR_PORTPINCONFIG_MASK;
  428.   tmpreg |= (uint32_t)GPIO_PortSource << 0x04;
  429.   tmpreg |= GPIO_PinSource;
  430.   AFIO->EVCR = tmpreg;
  431. }
  432. /**
  433.   * @brief  Enables or disables the Event Output.
  434.   * @param  NewState: new state of the Event output.
  435.   *   This parameter can be: ENABLE or DISABLE.
  436.   * @retval None
  437.   */
  438. void GPIO_EventOutputCmd(FunctionalState NewState)//使能或禁能事件输出
  439. {
  440.   /* Check the parameters */
  441.   assert_param(IS_FUNCTIONAL_STATE(NewState));

  442.   *(__IO uint32_t *) EVCR_EVOE_BB = (uint32_t)NewState;
  443. }
  444. /**
  445.   * @brief  Changes the mapping of the specified pin.
  446.   * @param  GPIO_Remap: selects the pin to remap.
  447.   *   This parameter can be one of the following values:
  448.   *     @arg GPIO_Remap_SPI1             : SPI1 Alternate Function mapping
  449.   *     @arg GPIO_Remap_I2C1             : I2C1 Alternate Function mapping
  450.   *     @arg GPIO_Remap_USART1           : USART1 Alternate Function mapping
  451.   *     @arg GPIO_Remap_USART2           : USART2 Alternate Function mapping
  452.   *     @arg GPIO_PartialRemap_USART3    : USART3 Partial Alternate Function mapping
  453.   *     @arg GPIO_FullRemap_USART3       : USART3 Full Alternate Function mapping
  454.   *     @arg GPIO_PartialRemap_TIM1      : TIM1 Partial Alternate Function mapping
  455.   *     @arg GPIO_FullRemap_TIM1         : TIM1 Full Alternate Function mapping
  456.   *     @arg GPIO_PartialRemap1_TIM2     : TIM2 Partial1 Alternate Function mapping
  457.   *     @arg GPIO_PartialRemap2_TIM2     : TIM2 Partial2 Alternate Function mapping
  458.   *     @arg GPIO_FullRemap_TIM2         : TIM2 Full Alternate Function mapping
  459.   *     @arg GPIO_PartialRemap_TIM3      : TIM3 Partial Alternate Function mapping
  460.   *     @arg GPIO_FullRemap_TIM3         : TIM3 Full Alternate Function mapping
  461.   *     @arg GPIO_Remap_TIM4             : TIM4 Alternate Function mapping
  462.   *     @arg GPIO_Remap1_CAN1            : CAN1 Alternate Function mapping
  463.   *     @arg GPIO_Remap2_CAN1            : CAN1 Alternate Function mapping
  464.   *     @arg GPIO_Remap_PD01             : PD01 Alternate Function mapping
  465.   *     @arg GPIO_Remap_TIM5CH4_LSI      : LSI connected to TIM5 Channel4 input capture for calibration
  466.   *     @arg GPIO_Remap_ADC1_ETRGINJ     : ADC1 External Trigger Injected Conversion remapping
  467.   *     @arg GPIO_Remap_ADC1_ETRGREG     : ADC1 External Trigger Regular Conversion remapping
  468.   *     @arg GPIO_Remap_ADC2_ETRGINJ     : ADC2 External Trigger Injected Conversion remapping
  469.   *     @arg GPIO_Remap_ADC2_ETRGREG     : ADC2 External Trigger Regular Conversion remapping
  470.   *     @arg GPIO_Remap_ETH              : Ethernet remapping (only for Connectivity line devices)
  471.   *     @arg GPIO_Remap_CAN2             : CAN2 remapping (only for Connectivity line devices)
  472.   *     @arg GPIO_Remap_SWJ_NoJTRST      : Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST
  473.   *     @arg GPIO_Remap_SWJ_JTAGDisable  : JTAG-DP Disabled and SW-DP Enabled
  474.   *     @arg GPIO_Remap_SWJ_Disable      : Full SWJ Disabled (JTAG-DP + SW-DP)
  475.   *     @arg GPIO_Remap_SPI3             : SPI3/I2S3 Alternate Function mapping (only for Connectivity line devices)
  476.   *                                        When the SPI3/I2S3 is remapped using this function, the SWJ is configured
  477.   *                                        to Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST.  
  478.   *     @arg GPIO_Remap_TIM2ITR1_PTP_SOF : Ethernet PTP output or USB OTG SOF (Start of Frame) connected
  479.   *                                        to TIM2 Internal Trigger 1 for calibration (only for Connectivity line devices)
  480.   *                                        If the GPIO_Remap_TIM2ITR1_PTP_SOF is enabled the TIM2 ITR1 is connected to
  481.   *                                        Ethernet PTP output. When Reset TIM2 ITR1 is connected to USB OTG SOF output.   
  482.   *     @arg GPIO_Remap_PTP_PPS          : Ethernet MAC PPS_PTS output on PB05 (only for Connectivity line devices)
  483.   *     @arg GPIO_Remap_TIM15            : TIM15 Alternate Function mapping (only for Value line devices)
  484.   *     @arg GPIO_Remap_TIM16            : TIM16 Alternate Function mapping (only for Value line devices)
  485.   *     @arg GPIO_Remap_TIM17            : TIM17 Alternate Function mapping (only for Value line devices)
  486.   *     @arg GPIO_Remap_CEC              : CEC Alternate Function mapping (only for Value line devices)
  487.   *     @arg GPIO_Remap_TIM1_DMA         : TIM1 DMA requests mapping (only for Value line devices)
  488.   *     @arg GPIO_Remap_TIM9             : TIM9 Alternate Function mapping (only for XL-density devices)
  489.   *     @arg GPIO_Remap_TIM10            : TIM10 Alternate Function mapping (only for XL-density devices)
  490.   *     @arg GPIO_Remap_TIM11            : TIM11 Alternate Function mapping (only for XL-density devices)
  491.   *     @arg GPIO_Remap_TIM13            : TIM13 Alternate Function mapping (only for High density Value line and XL-density devices)
  492.   *     @arg GPIO_Remap_TIM14            : TIM14 Alternate Function mapping (only for High density Value line and XL-density devices)
  493.   *     @arg GPIO_Remap_FSMC_NADV        : FSMC_NADV Alternate Function mapping (only for High density Value line and XL-density devices)
  494.   *     @arg GPIO_Remap_TIM67_DAC_DMA    : TIM6/TIM7 and DAC DMA requests remapping (only for High density Value line devices)
  495.   *     @arg GPIO_Remap_TIM12            : TIM12 Alternate Function mapping (only for High density Value line devices)
  496.   *     @arg GPIO_Remap_MISC             : Miscellaneous Remap (DMA2 Channel5 Position and DAC Trigger remapping,
  497.   *                                        only for High density Value line devices)   
  498.   * @param  NewState: new state of the port pin remapping.
  499.   *   This parameter can be: ENABLE or DISABLE.
  500.   * @retval None
  501.   */
  502. void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState)//端口的重映像配置,输入:某重映像端口,端口映像使能或禁能
  503. {
  504.   uint32_t tmp = 0x00, tmp1 = 0x00, tmpreg = 0x00, tmpmask = 0x00;
  505.   /* Check the parameters */
  506.   assert_param(IS_GPIO_REMAP(GPIO_Remap));
  507.   assert_param(IS_FUNCTIONAL_STATE(NewState));

  508.   if((GPIO_Remap & 0x80000000) == 0x80000000)
  509.   {
  510.     tmpreg = AFIO->MAPR2;
  511.   }
  512.   else
  513.   {
  514.     tmpreg = AFIO->MAPR;
  515.   }
  516.   tmpmask = (GPIO_Remap & DBGAFR_POSITION_MASK) >> 0x10;
  517.   tmp = GPIO_Remap & LSB_MASK;
  518.   if ((GPIO_Remap & (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK)) == (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK))
  519.   {
  520.     tmpreg &= DBGAFR_SWJCFG_MASK;
  521.     AFIO->MAPR &= DBGAFR_SWJCFG_MASK;
  522.   }
  523.   else if ((GPIO_Remap & DBGAFR_NUMBITS_MASK) == DBGAFR_NUMBITS_MASK)
  524.   {
  525.     tmp1 = ((uint32_t)0x03) << tmpmask;
  526.     tmpreg &= ~tmp1;
  527.     tmpreg |= ~DBGAFR_SWJCFG_MASK;
  528.   }
  529.   else
  530.   {
  531.     tmpreg &= ~(tmp << ((GPIO_Remap >> 0x15)*0x10));
  532.     tmpreg |= ~DBGAFR_SWJCFG_MASK;
  533.   }
  534.   if (NewState != DISABLE)
  535.   {
  536.     tmpreg |= (tmp << ((GPIO_Remap >> 0x15)*0x10));
  537.   }
  538.   if((GPIO_Remap & 0x80000000) == 0x80000000)
  539.   {
  540.     AFIO->MAPR2 = tmpreg;
  541.   }
  542.   else
  543.   {
  544.     AFIO->MAPR = tmpreg;
  545.   }
  546. }
  547. /**
  548.   * @brief  Selects the GPIO pin used as EXTI Line.
  549.   * @param  GPIO_PortSource: selects the GPIO port to be used as source for EXTI lines.
  550.   *   This parameter can be GPIO_PortSourceGPIOx where x can be (A..G).
  551.   * @param  GPIO_PinSource: specifies the EXTI line to be configured.
  552.   *   This parameter can be GPIO_PinSourcex where x can be (0..15).
  553.   * @retval None
  554.   */
  555. void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)//端口的外部中断配置;输入:某端口,某引脚
  556. {
  557.   uint32_t tmp = 0x00;
  558.   /* Check the parameters */
  559.   assert_param(IS_GPIO_EXTI_PORT_SOURCE(GPIO_PortSource));
  560.   assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));

  561.   tmp = ((uint32_t)0x0F) << (0x04 * (GPIO_PinSource & (uint8_t)0x03));
  562.   AFIO->EXTICR[GPIO_PinSource >> 0x02] &= ~tmp;
  563.   AFIO->EXTICR[GPIO_PinSource >> 0x02] |= (((uint32_t)GPIO_PortSource) << (0x04 * (GPIO_PinSource & (uint8_t)0x03)));
  564. }
  565. /**
  566.   * @brief  Selects the Ethernet media interface.
  567.   * @note   This function applies only to STM32 Connectivity line devices.
  568.   * @param  GPIO_ETH_MediaInterface: specifies the Media Interface mode.
  569.   *   This parameter can be one of the following values:
  570.   *     @arg GPIO_ETH_MediaInterface_MII: MII mode
  571.   *     @arg GPIO_ETH_MediaInterface_RMII: RMII mode   
  572.   * @retval None
  573.   */
  574. void GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface)//选择用作以太网的接口(模式)
  575. {
  576.   assert_param(IS_GPIO_ETH_MEDIA_INTERFACE(GPIO_ETH_MediaInterface));
  577.   /* Configure MII_RMII selection bit */
  578.   *(__IO uint32_t *) MAPR_MII_RMII_SEL_BB = GPIO_ETH_MediaInterface;
  579. }

  580. /**
  581.   * @}
  582.   */
  583. /**
  584.   * @}
  585.   */
  586. /**
  587.   * @}
  588.   */
  589. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表