?? stm32f10x_gpio.c
字號:
/**
******************************************************************************
* @文件 stm32f10x_gpio.c
* @作者 MCD 應用程序組
* @版本 V3.1.0
* @日期 06/19/2009
* @簡述 這個文件 提供所有 GPIO 固件函數.
* @翻譯 ANSON/sweet1985 Email:airanson110@126.com
* @日期 02/07/2009
******************************************************************************
* @復件
*
* 這個固件僅僅是提供給客戶作為設計產品而編寫程序的參考目的使客戶節約時間。由于
* 客戶使用本固件在開發產品編程上產生的結果意法半導體公司不承擔任何直接的和間接
* 的責任,也不承擔任何損害而引起的賠償。
*
* <h2><center>&復制; 版權所有 2009 意法半導體公司</center></h2>
* 翻譯版本僅供學習,如與英文原版有出入應以英文原版為準。
*/
/* 包含 ------------------------------------------------------------------*/
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
/* 自用類型 -----------------------------------------------------------*/
/* 自用定義 ------------------------------------------------------------*/
/* ------------ RCC registers bit address in the alias region ----------------*/
#define AFIO_OFFSET (AFIO_BASE - PERIPH_BASE)
/* --- EVENTCR Register -----*/
/* Alias word address of EVOE bit */
#define EVCR_OFFSET (AFIO_OFFSET + 0x00)
#define EVOE_BitNumber ((uint8_t)0x07)
#define EVCR_EVOE_BB (PERIPH_BB_BASE + (EVCR_OFFSET * 32) + (EVOE_BitNumber * 4))
/* --- MAPR Register ---*/
/* Alias word address of MII_RMII_SEL bit */
#define MAPR_OFFSET (AFIO_OFFSET + 0x04)
#define MII_RMII_SEL_BitNumber ((u8)0x17)
#define MAPR_MII_RMII_SEL_BB (PERIPH_BB_BASE + (MAPR_OFFSET * 32) + (MII_RMII_SEL_BitNumber * 4))
#define EVCR_PORTPINCONFIG_MASK ((uint16_t)0xFF80)
#define LSB_MASK ((uint16_t)0xFFFF)
#define DBGAFR_POSITION_MASK ((uint32_t)0x000F0000)
#define DBGAFR_SWJCFG_MASK ((uint32_t)0xF0FFFFFF)
#define DBGAFR_LOCATION_MASK ((uint32_t)0x00200000)
#define DBGAFR_NUMBITS_MASK ((uint32_t)0x00100000)
/* 自用宏 -------------------------------------------------------------*/
/* 自用變量 ---------------------------------------------------------*/
/* 自用函數原型 -----------------------------------------------*/
/* 自用函數 ---------------------------------------------------------*/
/**
* @簡述 將GPIO部件的寄存器復位為默認值
* @參數 GPIOx:x可為A到G來選擇特定的GPIO設備
* @返回 沒有
*/
void GPIO_DeInit(GPIO_TypeDef* GPIOx)
{
/* 檢查核對參數*/
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
if (GPIOx == GPIOA)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE);
}
else if (GPIOx == GPIOB)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE);
}
else if (GPIOx == GPIOC)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, DISABLE);
}
else if (GPIOx == GPIOD)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, DISABLE);
}
else if (GPIOx == GPIOE)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, DISABLE);
}
else if (GPIOx == GPIOF)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOF, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOF, DISABLE);
}
else
{
if (GPIOx == GPIOG)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOG, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOG, DISABLE);
}
}
}
/**
* @簡述 將備用功能(重映射,事件控制及EXTI配置)寄存器復位為默認值
* @參數 沒有
* @返回 沒有
*/
void GPIO_AFIODeInit(void)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, DISABLE);
}
/**
* @簡述 按照GPIO_InitStruct(結構體)的特定參數初始化GPIO部件
* @參數 GPIOx:x可為A到G來選擇特定的GPIO設備,
* GPIO_InitStruct: 指向GPIO_InitTypeDef結構的指針,它包含特定GPIO部件的配置信息.
* @返回 沒有
*/
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
uint32_t tmpreg = 0x00, pinmask = 0x00;
/* 檢查參數 */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
/*---------------------------- GPIO 模式配置 --------------------------------*/
currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
{
/* Check the parameters */
assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
/* Output mode */
currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
}
/*-------------------- GPIO CRL(端口控制寄存器低字節) 配置 -------------------*/
/* 配置低8位端口管腳 */
if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)
{
tmpreg = GPIOx->CRL;
for (pinpos = 0x00; pinpos < 0x08; pinpos++)
{
pos = ((uint32_t)0x01) << pinpos;
/* 取得端口引腳的位置 */
currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
if (currentpin == pos)
{
pos = pinpos << 2;
/* 清空相應的低位控制寄存器 */
pinmask = ((uint32_t)0x0F) << pos;
tmpreg &= ~pinmask;
/* 寫相應的模式配置位 */
tmpreg |= (currentmode << pos);
/* 復位相應的輸出數據寄存器 (ODR) 位 */
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
{
GPIOx->BRR = (((uint32_t)0x01) << pinpos);
}
else
{
/* 置位相應的輸出數據寄存器 (ODR) 位 */
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
{
GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
}
}
}
}
GPIOx->CRL = tmpreg;
}
/*-------------------- GPIO CRH(端口控制寄存器高字節) 配置 -------------------*/
/* 配置高8位端口引腳 */
if (GPIO_InitStruct->GPIO_Pin > 0x00FF)
{
tmpreg = GPIOx->CRH;
for (pinpos = 0x00; pinpos < 0x08; pinpos++)
{
pos = (((uint32_t)0x01) << (pinpos + 0x08));
/* 取得端口引腳的位置 */
currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
if (currentpin == pos)
{
pos = pinpos << 2;
/* 清空相應的低位控制寄存器 */
pinmask = ((uint32_t)0x0F) << pos;
tmpreg &= ~pinmask;
/* 寫相應的模式配置位 */
tmpreg |= (currentmode << pos);
/* 復位相應的輸出數據寄存器 (ODR) 位 */
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
{
GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));
}
/* 置位相應的輸出數據寄存器 (ODR) 位 */
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
{
GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));
}
}
}
GPIOx->CRH = tmpreg;
}
}
/**
* @簡述 把GPIO_InitStruct中的每一個參數按缺省值填入.
* @參數 GPIO_InitStruct : 指向GPIO_InitTypeDef結構,該結構將被初始化
* @返回 沒有
*/
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
{
/* 復位GPIO初始化結構參數值 */
GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
}
/**
* @簡述 讀取指定端口管腳的輸入.
* @參數 GPIOx: x可為A到G來選擇特定的GPIO設備.
* GPIO_Pin: 被讀取的引腳位.
* 這個參數GPIO_Pin_x可以選擇0至15.
* @返回 指定端口管腳的輸入值.
*/
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
uint8_t bitstatus = 0x00;
/* 檢查參數 */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
{
bitstatus = (uint8_t)Bit_SET;
}
else
{
bitstatus = (uint8_t)Bit_RESET;
}
return bitstatus;
}
/**
* @簡述 從特定的GPIO輸入數據端口讀取.
* @參數 GPIOx: x可為A到G來選擇特定的GPIO設備.
* @返回 指定的 GPIO 端口管腳的輸入值.
*/
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
{
/* 檢查參數 */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
return ((uint16_t)GPIOx->IDR);
}
/**
* @簡述 讀取指定端口管腳的輸出(位).
* @參數 GPIOx: x可為A到G來選擇特定的GPIO設備.
* GPIO_Pin: 需要讀取的端口位.
* 這個參數GPIO_Pin_x可以選擇0至15.
* @返回 輸出端口的輸出值.
*/
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
uint8_t bitstatus = 0x00;
/* 檢查參數 */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
{
bitstatus = (uint8_t)Bit_SET;
}
else
{
bitstatus = (uint8_t)Bit_RESET;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -