初始化验证编译通过
This commit is contained in:
5
Modules/CMakeLists.txt
Normal file
5
Modules/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
# Add all module subdirectories
|
||||
add_subdirectory(led)
|
||||
add_subdirectory(delay)
|
||||
19
Modules/delay/CMakeLists.txt
Normal file
19
Modules/delay/CMakeLists.txt
Normal file
@ -0,0 +1,19 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
# Create delay library
|
||||
add_library(delay STATIC)
|
||||
|
||||
# Add delay sources
|
||||
target_sources(delay PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/delay.c
|
||||
)
|
||||
|
||||
# Add delay include directories
|
||||
target_include_directories(delay PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
# Link with stm32cubemx library
|
||||
target_link_libraries(delay PRIVATE
|
||||
stm32cubemx
|
||||
)
|
||||
32
Modules/delay/inc/delay.h
Normal file
32
Modules/delay/inc/delay.h
Normal file
@ -0,0 +1,32 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : delay.h
|
||||
* @brief : Delay driver module header file
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
#ifndef DELAY_H
|
||||
#define DELAY_H
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
/**
|
||||
* @brief Initialize delay module
|
||||
*/
|
||||
void delay_init(void);
|
||||
|
||||
/**
|
||||
* @brief Delay in milliseconds
|
||||
* @param ms: Delay time in milliseconds
|
||||
*/
|
||||
void delay_ms(uint32_t ms);
|
||||
|
||||
/**
|
||||
* @brief Delay in microseconds
|
||||
* @param us: Delay time in microseconds
|
||||
*/
|
||||
void delay_us(uint32_t us);
|
||||
|
||||
#endif /* DELAY_H */
|
||||
49
Modules/delay/src/delay.c
Normal file
49
Modules/delay/src/delay.c
Normal file
@ -0,0 +1,49 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : delay.c
|
||||
* @brief : Delay driver module source file
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
#include "delay.h"
|
||||
|
||||
/**
|
||||
* @brief DWT cycle counter frequency in MHz
|
||||
*/
|
||||
static uint32_t delay_tick_freq = 0;
|
||||
|
||||
/**
|
||||
* @brief Initialize delay module
|
||||
*/
|
||||
void delay_init(void) {
|
||||
/* Get the DWT cycle counter frequency */
|
||||
delay_tick_freq = HAL_RCC_GetHCLKFreq() / 1000000U; /* Convert to MHz */
|
||||
|
||||
/* Enable DWT cycle counter */
|
||||
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
||||
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Delay in milliseconds
|
||||
* @param ms: Delay time in milliseconds
|
||||
*/
|
||||
void delay_ms(uint32_t ms) {
|
||||
HAL_Delay(ms);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Delay in microseconds
|
||||
* @param us: Delay time in microseconds
|
||||
*/
|
||||
void delay_us(uint32_t us) {
|
||||
uint32_t start = DWT->CYCCNT;
|
||||
uint32_t cycles = (uint32_t)(us * delay_tick_freq);
|
||||
|
||||
/* Wait until the delay is completed */
|
||||
while ((DWT->CYCCNT - start) < cycles) {
|
||||
/* Do nothing */
|
||||
}
|
||||
}
|
||||
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