mirror of
https://gitee.com/dlmu-cone/tronone-h7-scaffold
synced 2026-07-24 11:37:44 +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, ...);
|
||||
```
|
||||
113
User_Code/bsp/pwm/bsp_pwm.c
Normal file
113
User_Code/bsp/pwm/bsp_pwm.c
Normal file
@@ -0,0 +1,113 @@
|
||||
// #include "bsp_pwm.h"
|
||||
// #include "stdlib.h"
|
||||
// #include "memory.h"
|
||||
//
|
||||
// // 配合中断以及初始化
|
||||
// static uint8_t idx;
|
||||
// static PWMInstance *pwm_instance[PWM_DEVICE_CNT] = {NULL}; // 所有的pwm instance保存于此,用于callback时判断中断来源
|
||||
// static uint32_t PWMSelectTclk(TIM_HandleTypeDef *htim);
|
||||
//
|
||||
// /**
|
||||
// * @brief pwm dma传输完成回调函数
|
||||
// *
|
||||
// * @param htim 发生中断的定时器句柄
|
||||
// */
|
||||
// void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
|
||||
// {
|
||||
// for (uint8_t i = 0; i < idx; i++)
|
||||
// {
|
||||
// // 来自同一个定时器的中断且通道相同
|
||||
// if (pwm_instance[i]->htim == htim && htim->Channel == (1 << (pwm_instance[i]->channel / 4)))
|
||||
// {
|
||||
// if (pwm_instance[i]->callback) // 如果有回调函数
|
||||
// pwm_instance[i]->callback(pwm_instance[i]);
|
||||
// return; // 一次只能有一个通道的中断,所以直接返回
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// PWMInstance *PWMRegister(PWM_Init_Config_s *config)
|
||||
// {
|
||||
// if (idx >= PWM_DEVICE_CNT) // 超过最大实例数,考虑增加或查看是否有内存泄漏
|
||||
// while (1);
|
||||
// PWMInstance *pwm = (PWMInstance *) malloc(sizeof(PWMInstance));
|
||||
// memset(pwm, 0, sizeof(PWMInstance));
|
||||
//
|
||||
// pwm->htim = config->htim;
|
||||
// pwm->channel = config->channel;
|
||||
// pwm->period = config->period;
|
||||
// pwm->dutyratio = config->dutyratio;
|
||||
// pwm->callback = config->callback;
|
||||
// pwm->id = config->id;
|
||||
// pwm->tclk = PWMSelectTclk(pwm->htim);
|
||||
// // 启动PWM
|
||||
// HAL_TIM_PWM_Start(pwm->htim, pwm->channel);
|
||||
// PWMSetPeriod(pwm, pwm->period);
|
||||
// PWMSetDutyRatio(pwm, pwm->dutyratio);
|
||||
// pwm_instance[idx++] = pwm;
|
||||
// return pwm;
|
||||
// }
|
||||
//
|
||||
// /* 只是对HAL的函数进行了形式上的封装 */
|
||||
// void PWMStart(PWMInstance *pwm)
|
||||
// {
|
||||
// HAL_TIM_PWM_Start(pwm->htim, pwm->channel);
|
||||
// }
|
||||
//
|
||||
// /* 只是对HAL的函数进行了形式上的封装 */
|
||||
// void PWMStop(PWMInstance *pwm)
|
||||
// {
|
||||
// HAL_TIM_PWM_Stop(pwm->htim, pwm->channel);
|
||||
// }
|
||||
//
|
||||
// /*
|
||||
// * @brief 设置pwm周期
|
||||
// *
|
||||
// * @param pwm pwm实例
|
||||
// * @param period 周期 单位 s
|
||||
// */
|
||||
// void PWMSetPeriod(PWMInstance *pwm, float period)
|
||||
// {
|
||||
// __HAL_TIM_SetAutoreload(pwm->htim, period * ((pwm->tclk) / (pwm->htim->Init.Prescaler + 1)));
|
||||
// }
|
||||
//
|
||||
// /*
|
||||
// * @brief 设置pwm占空比
|
||||
// *
|
||||
// * @param pwm pwm实例
|
||||
// * @param dutyratio 占空比 0~1
|
||||
// */
|
||||
// void PWMSetDutyRatio(PWMInstance *pwm, float dutyratio)
|
||||
// {
|
||||
// __HAL_TIM_SetCompare(pwm->htim, pwm->channel, dutyratio * (pwm->htim->Instance->ARR));
|
||||
// }
|
||||
//
|
||||
// /* 只是对HAL的函数进行了形式上的封装 */
|
||||
// void PWMStartDMA(PWMInstance *pwm, uint32_t *pData, uint32_t Size)
|
||||
// {
|
||||
// HAL_TIM_PWM_Start_DMA(pwm->htim, pwm->channel, pData, Size);
|
||||
// }
|
||||
//
|
||||
// // 设置pwm对应定时器时钟源频率
|
||||
// //tim2~7,12~14:APB1 tim1,8~11:APB2
|
||||
// static uint32_t PWMSelectTclk(TIM_HandleTypeDef *htim)
|
||||
// {
|
||||
// uintptr_t tclk_temp = ((uintptr_t) ((htim)->Instance));
|
||||
// if (
|
||||
// (tclk_temp <= (APB1PERIPH_BASE + 0x2000UL)) &&
|
||||
// (tclk_temp >= (APB1PERIPH_BASE + 0x0000UL)))
|
||||
// {
|
||||
// return (HAL_RCC_GetPCLK1Freq() * (
|
||||
// APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos] == 0 ? 1 : 2));
|
||||
// }
|
||||
// if (
|
||||
// ((tclk_temp <= (APB2PERIPH_BASE + 0x0400UL)) &&
|
||||
// (tclk_temp >= (APB2PERIPH_BASE + 0x0000UL))) ||
|
||||
// ((tclk_temp <= (APB2PERIPH_BASE + 0x4800UL)) &&
|
||||
// (tclk_temp >= (APB2PERIPH_BASE + 0x4000UL))))
|
||||
// {
|
||||
// return (HAL_RCC_GetPCLK2Freq() * (
|
||||
// APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos] == 0 ? 1 : 2));
|
||||
// }
|
||||
// return 0;
|
||||
// }
|
||||
93
User_Code/bsp/pwm/bsp_pwm.h
Normal file
93
User_Code/bsp/pwm/bsp_pwm.h
Normal file
@@ -0,0 +1,93 @@
|
||||
// /**
|
||||
// * @file bsp_pwm.h
|
||||
// * @author TuxMonkey
|
||||
// * @brief
|
||||
// * @version 0.1
|
||||
// * @date 2023-02-14
|
||||
// *
|
||||
// * @copyright Copyright (c) 2023
|
||||
// *
|
||||
// */
|
||||
//
|
||||
// #ifndef BSP_PWM_H
|
||||
// #define BSP_PWM_H
|
||||
//
|
||||
// #include "tim.h"
|
||||
// #include "stdint.h"
|
||||
// #include "stm32h7xx_hal_rcc.h"
|
||||
// #include "stm32h723xx.h"
|
||||
// #define PWM_DEVICE_CNT 16 // 最大支持的PWM实例数量
|
||||
//
|
||||
// /* pwm实例结构体 */
|
||||
// typedef struct pwm_ins_temp
|
||||
// {
|
||||
// TIM_HandleTypeDef *htim; // TIM句柄
|
||||
// uint32_t channel; // 通道
|
||||
// uint32_t tclk; // 时钟频率
|
||||
// float period; // 周期
|
||||
// float dutyratio; // 占空比
|
||||
// void (*callback)(struct pwm_ins_temp *); // DMA传输完成回调函数
|
||||
// void *id; // 实例ID
|
||||
// } PWMInstance;
|
||||
//
|
||||
// typedef struct
|
||||
// {
|
||||
// TIM_HandleTypeDef *htim; // TIM句柄
|
||||
// uint32_t channel; // 通道
|
||||
// float period; // 周期
|
||||
// float dutyratio; // 占空比
|
||||
// void (*callback)(PWMInstance *); // DMA传输完成回调函数
|
||||
// void *id; // 实例ID
|
||||
// } PWM_Init_Config_s;
|
||||
//
|
||||
// /**
|
||||
// * @brief 注册一个pwm实例
|
||||
// *
|
||||
// * @param config 初始化配置
|
||||
// * @return PWMInstance*
|
||||
// */
|
||||
// PWMInstance *PWMRegister(PWM_Init_Config_s *config);
|
||||
//
|
||||
// /**
|
||||
// * @brief 启动pwm
|
||||
// *
|
||||
// * @param pwm pwm实例
|
||||
// */
|
||||
// void PWMStart(PWMInstance *pwm);
|
||||
//
|
||||
// /**
|
||||
// * @brief 设置pwm占空比
|
||||
// *
|
||||
// * @param pwm pwm实例
|
||||
// * @param dutyratio 占空比 0~1
|
||||
// */
|
||||
//
|
||||
// void PWMSetDutyRatio(PWMInstance *pwm, float dutyratio);
|
||||
//
|
||||
// /**
|
||||
// * @brief 停止pwm
|
||||
// *
|
||||
// * @param pwm pwm实例
|
||||
// */
|
||||
// void PWMStop(PWMInstance *pwm);
|
||||
//
|
||||
// /**
|
||||
// * @brief 设置pwm周期
|
||||
// *
|
||||
// * @param pwm pwm实例
|
||||
// * @param period 周期 单位 s
|
||||
// */
|
||||
// void PWMSetPeriod(PWMInstance *pwm, float period);
|
||||
//
|
||||
// /**
|
||||
// * @brief 启动pwm dma传输
|
||||
// *
|
||||
// * @param pwm pwm实例
|
||||
// * @param pData 数据首地址指针,注意数据的位数必须和CubeMX配置的DMA传输位数(字长)一致
|
||||
// * @param Size 数据长度
|
||||
// * @note 如果使用此函数,则需要在CubeMX中配置DMA传输位数为对应位数
|
||||
// * 例如:使用16位数据,则需要配置DMA传输位数为16位(half word),配置错误会导致指针越界或数据错误
|
||||
// */
|
||||
// void PWMStartDMA(PWMInstance *pwm, uint32_t *pData, uint32_t Size);
|
||||
//
|
||||
// #endif // BSP_PWM_H
|
||||
7
User_Code/bsp/pwm/bsp_pwm.md
Normal file
7
User_Code/bsp/pwm/bsp_pwm.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# bsp pwm
|
||||
|
||||
> 暂未适配H723
|
||||
|
||||
同一定时器下的pwm通道更改周期时请注意该定时器下是否有其他pwm示例,如果有请注意不要影响其他pwm通道的周期。
|
||||
使用pwm dma传输中断时注意占空比的设置,设为0将不会进入中断函数
|
||||
默认tim psc已在cubemx设置好,详情可参考cubemx配置文件
|
||||
Reference in New Issue
Block a user