实现串口驱动,移植方便
This commit is contained in:
@ -13,7 +13,8 @@ target_include_directories(led PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
# Link with stm32cubemx library
|
||||
# Link with stm32cubemx and hal libraries
|
||||
target_link_libraries(led PRIVATE
|
||||
stm32cubemx
|
||||
hal
|
||||
)
|
||||
|
||||
120
Modules/led/README.md
Normal file
120
Modules/led/README.md
Normal file
@ -0,0 +1,120 @@
|
||||
# LED模块
|
||||
|
||||
## 功能描述
|
||||
LED模块用于控制板载LED的开关和状态切换,提供了简单易用的API接口。
|
||||
|
||||
## 依赖关系
|
||||
- HAL层:hal_gpio
|
||||
|
||||
## 接口说明
|
||||
|
||||
### 数据结构
|
||||
|
||||
#### led_config_t
|
||||
LED配置结构体,用于初始化LED模块
|
||||
```c
|
||||
typedef struct {
|
||||
hal_gpio_port_t gpio_port; /*!< GPIO端口 */
|
||||
hal_gpio_pin_t gpio_pin; /*!< GPIO引脚 */
|
||||
} led_config_t;
|
||||
```
|
||||
|
||||
#### led_t
|
||||
LED实例结构体,用于管理LED状态
|
||||
```c
|
||||
typedef struct {
|
||||
led_config_t config; /*!< LED配置 */
|
||||
uint8_t state; /*!< 当前LED状态(0: 关闭,1: 开启) */
|
||||
} led_t;
|
||||
```
|
||||
|
||||
### 函数接口
|
||||
|
||||
#### led_init
|
||||
```c
|
||||
void led_init(led_t *led, const led_config_t *config);
|
||||
```
|
||||
初始化LED模块
|
||||
|
||||
**参数**:
|
||||
- led: LED实例指针
|
||||
- config: LED配置结构体指针
|
||||
|
||||
**返回值**:无
|
||||
|
||||
#### led_on
|
||||
```c
|
||||
void led_on(led_t *led);
|
||||
```
|
||||
开启LED
|
||||
|
||||
**参数**:
|
||||
- led: LED实例指针
|
||||
|
||||
**返回值**:无
|
||||
|
||||
#### led_off
|
||||
```c
|
||||
void led_off(led_t *led);
|
||||
```
|
||||
关闭LED
|
||||
|
||||
**参数**:
|
||||
- led: LED实例指针
|
||||
|
||||
**返回值**:无
|
||||
|
||||
#### led_toggle
|
||||
```c
|
||||
void led_toggle(led_t *led);
|
||||
```
|
||||
切换LED状态
|
||||
|
||||
**参数**:
|
||||
- led: LED实例指针
|
||||
|
||||
**返回值**:无
|
||||
|
||||
#### led_get_state
|
||||
```c
|
||||
uint8_t led_get_state(const led_t *led);
|
||||
```
|
||||
获取LED当前状态
|
||||
|
||||
**参数**:
|
||||
- led: LED实例指针
|
||||
|
||||
**返回值**:
|
||||
- 0: LED关闭
|
||||
- 1: LED开启
|
||||
|
||||
## 使用示例
|
||||
|
||||
```c
|
||||
/* LED配置 */
|
||||
static led_config_t led_config = {
|
||||
.gpio_port = BSP_LED_PORT,
|
||||
.gpio_pin = BSP_LED_PIN
|
||||
};
|
||||
|
||||
/* LED实例 */
|
||||
static led_t led;
|
||||
|
||||
/* 初始化LED */
|
||||
led_init(&led, &led_config);
|
||||
|
||||
/* 开启LED */
|
||||
led_on(&led);
|
||||
|
||||
/* 延迟500ms */
|
||||
delay_ms(500);
|
||||
|
||||
/* 关闭LED */
|
||||
led_off(&led);
|
||||
|
||||
/* 切换LED状态 */
|
||||
led_toggle(&led);
|
||||
|
||||
/* 获取LED状态 */
|
||||
uint8_t state = led_get_state(&led);
|
||||
```
|
||||
@ -10,14 +10,16 @@
|
||||
#ifndef LED_H
|
||||
#define LED_H
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "hal_gpio.h"
|
||||
|
||||
/**
|
||||
* @brief LED configuration structure
|
||||
*/
|
||||
typedef struct {
|
||||
GPIO_TypeDef *gpio_port; /*!< GPIO port */
|
||||
uint16_t gpio_pin; /*!< GPIO pin */
|
||||
hal_gpio_port_t gpio_port; /*!< GPIO port */
|
||||
hal_gpio_pin_t gpio_pin; /*!< GPIO pin */
|
||||
} led_config_t;
|
||||
|
||||
/**
|
||||
|
||||
@ -23,7 +23,7 @@ void led_init(led_t *led, const led_config_t *config) {
|
||||
led->state = 0;
|
||||
|
||||
/* Ensure LED is initially off */
|
||||
HAL_GPIO_WritePin(led->config.gpio_port, led->config.gpio_pin, GPIO_PIN_RESET);
|
||||
hal_gpio_write_pin(led->config.gpio_port, led->config.gpio_pin, HAL_GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -35,7 +35,7 @@ void led_on(led_t *led) {
|
||||
return;
|
||||
}
|
||||
|
||||
HAL_GPIO_WritePin(led->config.gpio_port, led->config.gpio_pin, GPIO_PIN_SET);
|
||||
hal_gpio_write_pin(led->config.gpio_port, led->config.gpio_pin, HAL_GPIO_PIN_SET);
|
||||
led->state = 1;
|
||||
}
|
||||
|
||||
@ -48,7 +48,7 @@ void led_off(led_t *led) {
|
||||
return;
|
||||
}
|
||||
|
||||
HAL_GPIO_WritePin(led->config.gpio_port, led->config.gpio_pin, GPIO_PIN_RESET);
|
||||
hal_gpio_write_pin(led->config.gpio_port, led->config.gpio_pin, HAL_GPIO_PIN_RESET);
|
||||
led->state = 0;
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ void led_toggle(led_t *led) {
|
||||
return;
|
||||
}
|
||||
|
||||
HAL_GPIO_TogglePin(led->config.gpio_port, led->config.gpio_pin);
|
||||
hal_gpio_toggle_pin(led->config.gpio_port, led->config.gpio_pin);
|
||||
led->state = !led->state;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user