mirror of
https://gitee.com/dlmu-cone/tronone-h7-scaffold
synced 2026-07-24 03:27:45 +08:00
add new drv but not benefit stm32h723vg
This commit is contained in:
66
User_Code/bsp/gpio/bsp_gpio.c
Normal file
66
User_Code/bsp/gpio/bsp_gpio.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "bsp_gpio.h"
|
||||
#include "memory.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
static uint8_t idx;
|
||||
static GPIOInstance *gpio_instance[GPIO_MX_DEVICE_NUM] = {NULL};
|
||||
|
||||
/**
|
||||
* @brief EXTI中断回调函数,根据GPIO_Pin找到对应的GPIOInstance,并调用模块回调函数(如果有)
|
||||
* @note 如何判断具体是哪一个GPIO的引脚连接到这个EXTI中断线上?
|
||||
* 一个EXTI中断线只能连接一个GPIO引脚,因此可以通过GPIO_Pin来判断,PinX对应EXTIX
|
||||
* 一个Pin号只会对应一个EXTI,详情见gpio.md
|
||||
* @param GPIO_Pin 发生中断的GPIO_Pin
|
||||
*/
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||
{
|
||||
// 如有必要,可以根据pinstate和HAL_GPIO_ReadPin来判断是上升沿还是下降沿/rise&fall等
|
||||
GPIOInstance *gpio;
|
||||
for (size_t i = 0; i < idx; i++)
|
||||
{
|
||||
gpio = gpio_instance[i];
|
||||
if (gpio->GPIO_Pin == GPIO_Pin && gpio->gpio_model_callback != NULL)
|
||||
{
|
||||
gpio->gpio_model_callback(gpio);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GPIOInstance *GPIORegister(GPIO_Init_Config_s *GPIO_config)
|
||||
{
|
||||
GPIOInstance *ins = (GPIOInstance *) malloc(sizeof(GPIOInstance));
|
||||
memset(ins, 0, sizeof(GPIOInstance));
|
||||
|
||||
ins->GPIOx = GPIO_config->GPIOx;
|
||||
ins->GPIO_Pin = GPIO_config->GPIO_Pin;
|
||||
ins->pin_state = GPIO_config->pin_state;
|
||||
ins->exti_mode = GPIO_config->exti_mode;
|
||||
ins->id = GPIO_config->id;
|
||||
ins->gpio_model_callback = GPIO_config->gpio_model_callback;
|
||||
gpio_instance[idx++] = ins;
|
||||
return ins;
|
||||
}
|
||||
|
||||
// ----------------- GPIO API -----------------
|
||||
// 都是对HAL的形式上的封装,后续考虑增加GPIO state变量,可以直接读取state
|
||||
|
||||
void GPIOToggel(GPIOInstance *_instance)
|
||||
{
|
||||
HAL_GPIO_TogglePin(_instance->GPIOx, _instance->GPIO_Pin);
|
||||
}
|
||||
|
||||
void GPIOSet(GPIOInstance *_instance)
|
||||
{
|
||||
HAL_GPIO_WritePin(_instance->GPIOx, _instance->GPIO_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
void GPIOReset(GPIOInstance *_instance)
|
||||
{
|
||||
HAL_GPIO_WritePin(_instance->GPIOx, _instance->GPIO_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
GPIO_PinState GPIORead(GPIOInstance *_instance)
|
||||
{
|
||||
return HAL_GPIO_ReadPin(_instance->GPIOx, _instance->GPIO_Pin);
|
||||
}
|
||||
87
User_Code/bsp/gpio/bsp_gpio.h
Normal file
87
User_Code/bsp/gpio/bsp_gpio.h
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "gpio.h"
|
||||
#include "stdint.h"
|
||||
|
||||
#define GPIO_MX_DEVICE_NUM 10
|
||||
|
||||
/**
|
||||
* @brief 用于判断中断来源,注意和CUBEMX中配置一致
|
||||
*
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
GPIO_EXTI_MODE_RISING,
|
||||
GPIO_EXTI_MODE_FALLING,
|
||||
GPIO_EXTI_MODE_RISING_FALLING,
|
||||
GPIO_EXTI_MODE_NONE,
|
||||
} GPIO_EXTI_MODE_e;
|
||||
|
||||
/**
|
||||
* @brief GPIO实例结构体定义
|
||||
*
|
||||
*/
|
||||
typedef struct tmpgpio
|
||||
{
|
||||
GPIO_TypeDef *GPIOx; // GPIOA,GPIOB,GPIOC...
|
||||
GPIO_PinState pin_state; // 引脚状态,Set,Reset;not frequently used
|
||||
GPIO_EXTI_MODE_e exti_mode; // 外部中断模式 not frequently used
|
||||
uint16_t GPIO_Pin; // 引脚号,
|
||||
// 这些引脚是stm32f4xx_hal_gpio.h中定义的宏!!! 一定要注意
|
||||
// 随便取个名字当临时声明
|
||||
void (*gpio_model_callback)(struct tmpgpio *); // exti中断回调函数
|
||||
void *id; // 区分不同的GPIO实例
|
||||
|
||||
} GPIOInstance;
|
||||
|
||||
/**
|
||||
* @brief GPIO初始化配置结构体定义
|
||||
*
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
GPIO_TypeDef *GPIOx; // GPIOA,GPIOB,GPIOC...
|
||||
GPIO_PinState pin_state; // 引脚状态,Set,Reset not frequently used
|
||||
GPIO_EXTI_MODE_e exti_mode; // 外部中断模式 not frequently used
|
||||
uint16_t GPIO_Pin; // 引脚号,@note 这里的引脚号是GPIO_PIN_0,GPIO_PIN_1...
|
||||
// 这些引脚是stm32f4xx_hal_gpio.h中定义的宏!!! 一定要注意
|
||||
|
||||
void (*gpio_model_callback)(GPIOInstance *); // exti中断回调函数
|
||||
void *id; // 区分不同的GPIO实例
|
||||
|
||||
} GPIO_Init_Config_s;
|
||||
|
||||
/**
|
||||
* @brief 注册GPIO实例
|
||||
*
|
||||
* @param GPIO_config
|
||||
* @return GPIOInstance*
|
||||
*/
|
||||
GPIOInstance *GPIORegister(GPIO_Init_Config_s *GPIO_config);
|
||||
|
||||
/**
|
||||
* @brief GPIO API,切换GPIO电平
|
||||
*
|
||||
* @param _instance
|
||||
*/
|
||||
void GPIOToggel(GPIOInstance *_instance);
|
||||
|
||||
/**
|
||||
* @brief 设置GPIO电平
|
||||
*
|
||||
* @param _instance
|
||||
*/
|
||||
void GPIOSet(GPIOInstance *_instance);
|
||||
|
||||
/**
|
||||
* @brief 复位GPIO电平
|
||||
*
|
||||
* @param _instance
|
||||
*/
|
||||
void GPIOReset(GPIOInstance *_instance);
|
||||
|
||||
/**
|
||||
* @brief 读取GPIO电平
|
||||
*
|
||||
* @param _instance
|
||||
* @return GPIO_PinState
|
||||
*/
|
||||
GPIO_PinState GPIORead(GPIOInstance *_instance);
|
||||
18
User_Code/bsp/gpio/bsp_gpio.md
Normal file
18
User_Code/bsp/gpio/bsp_gpio.md
Normal file
@@ -0,0 +1,18 @@
|
||||
可以作为io接口,也可以处理外部中断.
|
||||
|
||||
使用示例
|
||||
|
||||
```c
|
||||
//在app层只需要设置前三个,callback由module自动设置
|
||||
|
||||
GPIO_Init_Config_s gpio_init = {
|
||||
.exti_mode = GPIO_EXTI_MODE_FALLING, // 注意和CUBEMX的配置一致
|
||||
.GPIO_Pin = GPIO_PIN_6, // GPIO引脚
|
||||
.GPIOx = GPIOG, // GPIO外设
|
||||
.gpio_model_callback = NULL, // EXTI回调函数
|
||||
},
|
||||
|
||||
GPIOInstance* test_example = GPIORegister(&gpio_init);
|
||||
GPIOSet(test_example);
|
||||
// GPIOxxx(test_exmaple, ...);
|
||||
```
|
||||
Reference in New Issue
Block a user