CMake构建CH58x项目,脱离eclipse使用Clion或者Vscode编写代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

152 lines
3.4 KiB

#include "api-gpio.h"
static GpioItCallback_t gpioItCallback;
static uint32_t GpioPin(GpioPin_t gpio, uint32_t *pin)
{
uint32_t port = gpio & GPIOX;
*pin = gpio & GPIOPIN;
// *pin = 1 << p;
return port;
}
void Gpio_Init(GpioPin_t gpio, GPIOModeTypeDef mode)
{
uint32_t pin = 0;
uint32_t port = GpioPin(gpio, &pin);
if (port == GPIOA)
{
GPIOA_ModeCfg(pin, mode);
} else if (port == GPIOB)
{
GPIOB_ModeCfg(pin, mode);
}
}
void Gpio_Set(GpioPin_t gpio)
{
uint32_t pin = 0;
uint32_t port = GpioPin(gpio, &pin);
if (port == GPIOA)
{
GPIOA_SetBits(pin);
} else if (port == GPIOB)
{
GPIOB_SetBits(pin);
}
}
bool Gpio_Read(GpioPin_t gpio)
{
uint32_t pin = 0;
uint32_t port = GpioPin(gpio, &pin);
if (port == GPIOA)
{
return GPIOA_ReadPortPin(pin) != 0;
} else if (port == GPIOB)
{
return GPIOB_ReadPortPin(pin) != 0;
}
return false;
}
void Gpio_Reset(GpioPin_t gpio)
{
uint32_t pin = 0;
uint32_t port = GpioPin(gpio, &pin);
if (port == GPIOA)
{
GPIOA_ResetBits(pin);
} else if (port == GPIOB)
{
GPIOB_ResetBits(pin);
}
}
void Gpio_Inverse(GpioPin_t gpio)
{
uint32_t pin = 0;
uint32_t port = GpioPin(gpio, &pin);
if (port == GPIOA)
{
GPIOA_InverseBits(pin);
} else if (port == GPIOB)
{
GPIOB_InverseBits(pin);
}
}
void Gpio_IT_Init(GpioPin_t gpio, GPIOModeTypeDef mode, GPIOITModeTpDef modeIt, void(Callback)(void *), void *context)
{
uint32_t pin = 0;
uint32_t port = GpioPin(gpio, &pin);
if (port == GPIOA)
{
uint8_t i = 0;
GPIOA_ModeCfg(pin, mode);
GPIOA_ITModeCfg(pin, modeIt);
for (i = 0; i < GPIOAITSIZE; ++i)
{
if (pin & (1 << i))
{
gpioItCallback.GPIOA_Callback[i].Callback = Callback;
gpioItCallback.GPIOA_Callback[i].context = context;
break;
}
}
PFIC_EnableIRQ(GPIO_A_IRQn);
} else if (port == GPIOB)
{
GPIOB_ModeCfg(pin, mode);
GPIOB_ITModeCfg(pin, modeIt);
for (int i = 0; i < GPIOBITSIZE; ++i)
{
if (pin & (1 << i))
{
gpioItCallback.GPIOB_Callback[i].Callback = Callback;
gpioItCallback.GPIOB_Callback[i].context = context;
break;
}
}
PFIC_EnableIRQ(GPIO_B_IRQn);
}
}
__HIGH_CODE
void GPIOA_IRQHandler(void)
{
uint16_t flag = R16_PA_INT_IF;
for (int i = 0; i < GPIOAITSIZE; ++i)
{
if (flag & (1 << i))
{
if (gpioItCallback.GPIOA_Callback[i].Callback)
{
gpioItCallback.GPIOA_Callback[i].Callback(gpioItCallback.GPIOA_Callback[i].context);
}
}
}
GPIOA_ClearITFlagBit(flag); /* 清除中断标志 */
}
#include "stdio.h"
__HIGH_CODE
void GPIOB_IRQHandler(void)
{
uint16_t flag = R16_PB_INT_IF;//GPIOB_ReadITFlagPort();//R16_PB_INT_IF;
for (int i = 0; i < GPIOBITSIZE; ++i)
{
if (flag & (1 << i))
{
if (gpioItCallback.GPIOB_Callback[i].Callback)
{
gpioItCallback.GPIOB_Callback[i].Callback(gpioItCallback.GPIOB_Callback[i].context);
}
}
}
GPIOB_ClearITFlagBit(flag); /* 清除中断标志 */
}