添加buzzer和led gpio版本

This commit is contained in:
RB 2025-04-27 20:21:47 +08:00
parent 7bf1f90d08
commit 6e16dddd51
4 changed files with 116 additions and 0 deletions

29
User/bsp/buzzer_gpio.c Normal file
View File

@ -0,0 +1,29 @@
/* Includes ----------------------------------------------------------------- */
#include "bsp/buzzer_gpio.h"
#include "bsp/bsp.h"
#include <gpio.h>
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */
/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
int8_t BSP_Buzzer_Set(BSP_Buzzer_Status_t s) {
switch (s) {
case BSP_BUZZER_ON:
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); // 打开蜂鸣器
break;
case BSP_BUZZER_OFF:
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); // 关闭蜂鸣器
break;
case BSP_BUZZER_TAGGLE:
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1); // 切换蜂鸣器状态
break;
default:
return -1; // 无效的状态
}
return 0; // 成功
}

20
User/bsp/buzzer_gpio.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
/* Exported constants ------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */
/* Buzzer状态设置用 */
typedef enum
{
BSP_BUZZER_ON,
BSP_BUZZER_OFF,
BSP_BUZZER_TAGGLE,
} BSP_Buzzer_Status_t;
/* Exported functions prototypes -------------------------------------------- */
int8_t BSP_Buzzer_Set(BSP_Buzzer_Status_t s);

39
User/bsp/led_gpio.c Normal file
View File

@ -0,0 +1,39 @@
/* Includes ----------------------------------------------------------------- */
#include "bsp/led_gpio.h"
#include "bsp/bsp.h"
#include <gpio.h>
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */
static uint32_t led_stats; // 使用位掩码记录每个通道的状态
// 定义 LED 引脚映射表
static const uint16_t LED_PINS[] = {GPIO_PIN_2, GPIO_PIN_3, GPIO_PIN_4};
/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
int8_t BSP_LED_Set(BSP_LED_Channel_t ch, BSP_LED_Status_t s) {
if (ch >= BSP_LED_1 && ch <= BSP_LED_3) {
uint16_t pin = LED_PINS[ch - BSP_LED_1]; // 获取对应的 GPIO 引脚
switch (s) {
case BSP_LED_ON:
led_stats |= (1 << ch); // 设置对应位为1
HAL_GPIO_WritePin(GPIOA, pin, GPIO_PIN_SET); // 点亮LED
break;
case BSP_LED_OFF:
led_stats &= ~(1 << ch); // 清除对应位为0
HAL_GPIO_WritePin(GPIOA, pin, GPIO_PIN_RESET); // 熄灭LED
break;
case BSP_LED_TAGGLE:
led_stats ^= (1 << ch); // 切换对应位
HAL_GPIO_TogglePin(GPIOA, pin); // 切换LED状态
break;
default:
return -1; // 无效的状态
}
return 0; // 成功
}
return -1; // 无效的通道
}

28
User/bsp/led_gpio.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
/* Exported constants ------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */
/* LED灯状态设置用 */
typedef enum
{
BSP_LED_ON,
BSP_LED_OFF,
BSP_LED_TAGGLE,
} BSP_LED_Status_t;
/* LED通道 */
typedef enum
{
BSP_LED_1,
BSP_LED_2,
BSP_LED_3,
} BSP_LED_Channel_t;
/* Exported functions prototypes -------------------------------------------- */
int8_t BSP_LED_Set(BSP_LED_Channel_t ch, BSP_LED_Status_t s);