初始化验证编译通过
This commit is contained in:
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 */
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user