89 lines
3.4 KiB
C
Executable File
89 lines
3.4 KiB
C
Executable File
#include "../main/SystemInclude.h"
|
||
|
||
/**------------------------------------------------------------------------
|
||
* @brief 系统 GPIO 初始化,将所有引脚配置为模拟模式以降低功耗
|
||
* @note GPIO0 和 GPIO1 所有引脚设为模拟模式,GPIO2 避开 XIN/XOUT/SWDIO/SWCLK 引脚,
|
||
* 即 GPIO_PIN_1/2/3/4 保持默认状态,其余设为模拟模式。
|
||
* @param 无
|
||
* @example SystemGPIOInit();
|
||
**/
|
||
void SystemGPIOInit(void)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStruct;
|
||
|
||
GPIO_InitStruct.Pin = GPIO_PIN_All; //引脚选择
|
||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; //模式设置 GPIO_MODE_INPUT GPIO_MODE_OUTPUT_PP GPIO_MODE_OUTPUT_OD
|
||
GPIO_InitStruct.Current = GPIO_CURRENT_2mA; //驱动能力
|
||
GPIO_InitStruct.Pull = GPIO_PULLUP; //默认拉高 GPIO_PULLDOWN GPIO_NOPULL
|
||
GPIO_InitStruct.SchmittTrigger = ENABLE; //启用施密特触发器
|
||
GPIO_InitStruct.Alternate = GPIOx_AF_GPIO; //复用为普通IO
|
||
|
||
LHL_GPIO_Init(GPIO0, &GPIO_InitStruct);
|
||
LHL_GPIO_Init(GPIO1, &GPIO_InitStruct);
|
||
|
||
GPIO_InitStruct.Pin &= ~(GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 | GPIO_PIN_4); //避开XIN XOUT SWDIO SWCLK
|
||
LHL_GPIO_Init(GPIO2, &GPIO_InitStruct);
|
||
}
|
||
|
||
/**------------------------------------------------------------------------
|
||
* @brief 使能指定引脚为推挽输出模式
|
||
* @param GPIOx: GPIO 端口 (pGPIO0/pGPIO1/pGPIO2)
|
||
* @param GPIO_PinPos: 引脚位置 (0~15)
|
||
* @note 函数使能输出,禁用输入,并将输出类型设为推挽(通过清除 OD 位)。
|
||
* 如需开漏输出,可手动修改 OD 寄存器。
|
||
* @example EnableOutput(pGPIO0, 3); // 使能 P0.3 为推挽输出
|
||
**/
|
||
void EnableOutput(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinPos)
|
||
{
|
||
GPIOx->OE |= 1<<GPIO_PinPos; //使能输出
|
||
GPIOx->IE &= ~(1<<GPIO_PinPos); //禁用输入
|
||
|
||
// 设置端口电流驱动能力
|
||
GPIOx->DR &= ~(3u << (GPIO_PinPos * 2));
|
||
GPIOx->DR |= ((uint32_t)GPIO_CURRENT_4mA<< (GPIO_PinPos * 2));
|
||
|
||
// 推挽输出设定(普通IO)
|
||
GPIOx->OD &= ~(1u << GPIO_PinPos);
|
||
// 开漏输出设定
|
||
// GPIOx->OD |= (1u << GPIO_PinPos);
|
||
}
|
||
|
||
/**------------------------------------------------------------------------
|
||
* @brief 使能指定引脚为输入模式
|
||
* @param GPIOx: GPIO 端口 (pGPIO0/pGPIO1/pGPIO2)
|
||
* @param GPIO_PinPos: 引脚位置 (0~15)
|
||
* @note 函数禁用输出,使能输入。内部上下拉需单独配置。
|
||
* @example EnableInput(pGPIO0, 3); // 使能 P0.3 为输入
|
||
**/
|
||
void EnableInput(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinPos)
|
||
{
|
||
GPIOx->OE &= ~(1u << GPIO_PinPos);//禁用输出
|
||
GPIOx->IE |= (1u << GPIO_PinPos); //使能输入
|
||
}
|
||
|
||
/**------------------------------------------------------------------------
|
||
* @brief 启用指定引脚内部上拉电阻
|
||
* @param GPIOx: GPIO 端口 (pGPIO0/pGPIO1/pGPIO2)
|
||
* @param GPIO_PinPos: 引脚位置 (0~15)
|
||
* @note 函数禁用下拉,使能上拉。需确保引脚已配置为输入模式。
|
||
* @example EnablePullUp(pGPIO0, 3); // 使能 P0.3 内部上拉
|
||
**/
|
||
void EnablePullUp(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinPos)
|
||
{
|
||
GPIOx->PDE &= ~(1u << GPIO_PinPos);
|
||
GPIOx->PUE |= (1u << GPIO_PinPos);
|
||
}
|
||
|
||
/**------------------------------------------------------------------------
|
||
* @brief 启用指定引脚内部下拉电阻
|
||
* @param GPIOx: GPIO 端口 (pGPIO0/pGPIO1/pGPIO2)
|
||
* @param GPIO_PinPos: 引脚位置 (0~15)
|
||
* @note 函数禁用上拉,使能下拉。需确保引脚已配置为输入模式。
|
||
* @example EnablePullDown(pGPIO0, 3); // 使能 P0.3 内部下拉
|
||
**/
|
||
void EnablePullDown(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinPos)
|
||
{
|
||
GPIOx->PUE &= ~(1u << GPIO_PinPos);
|
||
GPIOx->PDE |= (1u << GPIO_PinPos);
|
||
}
|