初始化验证编译通过
This commit is contained in:
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