初始化验证编译通过
This commit is contained in:
19
Modules/led/CMakeLists.txt
Normal file
19
Modules/led/CMakeLists.txt
Normal file
@ -0,0 +1,19 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
# Create led library
|
||||
add_library(led STATIC)
|
||||
|
||||
# Add led sources
|
||||
target_sources(led PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/led.c
|
||||
)
|
||||
|
||||
# Add led include directories
|
||||
target_include_directories(led PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
# Link with stm32cubemx library
|
||||
target_link_libraries(led PRIVATE
|
||||
stm32cubemx
|
||||
)
|
||||
63
Modules/led/inc/led.h
Normal file
63
Modules/led/inc/led.h
Normal file
@ -0,0 +1,63 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : led.h
|
||||
* @brief : LED driver module header file
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
#ifndef LED_H
|
||||
#define LED_H
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
/**
|
||||
* @brief LED configuration structure
|
||||
*/
|
||||
typedef struct {
|
||||
GPIO_TypeDef *gpio_port; /*!< GPIO port */
|
||||
uint16_t gpio_pin; /*!< GPIO pin */
|
||||
} led_config_t;
|
||||
|
||||
/**
|
||||
* @brief LED instance structure
|
||||
*/
|
||||
typedef struct {
|
||||
led_config_t config; /*!< LED configuration */
|
||||
uint8_t state; /*!< Current LED state (0: off, 1: on) */
|
||||
} led_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize LED module
|
||||
* @param led: Pointer to LED instance
|
||||
* @param config: Pointer to LED configuration
|
||||
*/
|
||||
void led_init(led_t *led, const led_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief Turn on LED
|
||||
* @param led: Pointer to LED instance
|
||||
*/
|
||||
void led_on(led_t *led);
|
||||
|
||||
/**
|
||||
* @brief Turn off LED
|
||||
* @param led: Pointer to LED instance
|
||||
*/
|
||||
void led_off(led_t *led);
|
||||
|
||||
/**
|
||||
* @brief Toggle LED state
|
||||
* @param led: Pointer to LED instance
|
||||
*/
|
||||
void led_toggle(led_t *led);
|
||||
|
||||
/**
|
||||
* @brief Get LED current state
|
||||
* @param led: Pointer to LED instance
|
||||
* @retval 0: LED is off, 1: LED is on
|
||||
*/
|
||||
uint8_t led_get_state(const led_t *led);
|
||||
|
||||
#endif /* LED_H */
|
||||
79
Modules/led/src/led.c
Normal file
79
Modules/led/src/led.c
Normal file
@ -0,0 +1,79 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : led.c
|
||||
* @brief : LED driver module source file
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
#include "led.h"
|
||||
|
||||
/**
|
||||
* @brief Initialize LED module
|
||||
* @param led: Pointer to LED instance
|
||||
* @param config: Pointer to LED configuration
|
||||
*/
|
||||
void led_init(led_t *led, const led_config_t *config) {
|
||||
if (led == NULL || config == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
led->config = *config;
|
||||
led->state = 0;
|
||||
|
||||
/* Ensure LED is initially off */
|
||||
HAL_GPIO_WritePin(led->config.gpio_port, led->config.gpio_pin, GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Turn on LED
|
||||
* @param led: Pointer to LED instance
|
||||
*/
|
||||
void led_on(led_t *led) {
|
||||
if (led == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
HAL_GPIO_WritePin(led->config.gpio_port, led->config.gpio_pin, GPIO_PIN_SET);
|
||||
led->state = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Turn off LED
|
||||
* @param led: Pointer to LED instance
|
||||
*/
|
||||
void led_off(led_t *led) {
|
||||
if (led == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
HAL_GPIO_WritePin(led->config.gpio_port, led->config.gpio_pin, GPIO_PIN_RESET);
|
||||
led->state = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Toggle LED state
|
||||
* @param led: Pointer to LED instance
|
||||
*/
|
||||
void led_toggle(led_t *led) {
|
||||
if (led == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
HAL_GPIO_TogglePin(led->config.gpio_port, led->config.gpio_pin);
|
||||
led->state = !led->state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get LED current state
|
||||
* @param led: Pointer to LED instance
|
||||
* @retval 0: LED is off, 1: LED is on
|
||||
*/
|
||||
uint8_t led_get_state(const led_t *led) {
|
||||
if (led == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return led->state;
|
||||
}
|
||||
Reference in New Issue
Block a user