实现串口驱动,移植方便
This commit is contained in:
@ -13,7 +13,8 @@ target_include_directories(delay PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
# Link with stm32cubemx library
|
||||
# Link with stm32cubemx and hal libraries
|
||||
target_link_libraries(delay PRIVATE
|
||||
stm32cubemx
|
||||
hal
|
||||
)
|
||||
|
||||
56
Modules/delay/README.md
Normal file
56
Modules/delay/README.md
Normal file
@ -0,0 +1,56 @@
|
||||
# Delay模块
|
||||
|
||||
## 功能描述
|
||||
Delay模块提供了毫秒级和微秒级的延时功能,用于在应用程序中实现精确的时间延迟。
|
||||
|
||||
## 依赖关系
|
||||
- HAL层:hal_delay
|
||||
|
||||
## 接口说明
|
||||
|
||||
### 函数接口
|
||||
|
||||
#### delay_init
|
||||
```c
|
||||
void delay_init(void);
|
||||
```
|
||||
初始化延迟模块
|
||||
|
||||
**参数**:无
|
||||
|
||||
**返回值**:无
|
||||
|
||||
#### delay_ms
|
||||
```c
|
||||
void delay_ms(uint32_t ms);
|
||||
```
|
||||
毫秒级延迟
|
||||
|
||||
**参数**:
|
||||
- ms: 延迟时间(毫秒)
|
||||
|
||||
**返回值**:无
|
||||
|
||||
#### delay_us
|
||||
```c
|
||||
void delay_us(uint32_t us);
|
||||
```
|
||||
微秒级延迟
|
||||
|
||||
**参数**:
|
||||
- us: 延迟时间(微秒)
|
||||
|
||||
**返回值**:无
|
||||
|
||||
## 使用示例
|
||||
|
||||
```c
|
||||
/* 初始化延迟模块 */
|
||||
delay_init();
|
||||
|
||||
/* 延迟1秒 */
|
||||
delay_ms(1000);
|
||||
|
||||
/* 延迟500微秒 */
|
||||
delay_us(500);
|
||||
```
|
||||
@ -10,7 +10,8 @@
|
||||
#ifndef DELAY_H
|
||||
#define DELAY_H
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include <stdint.h>
|
||||
#include "hal_delay.h"
|
||||
|
||||
/**
|
||||
* @brief Initialize delay module
|
||||
|
||||
@ -9,21 +9,11 @@
|
||||
|
||||
#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;
|
||||
hal_delay_init();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -31,7 +21,7 @@ void delay_init(void) {
|
||||
* @param ms: Delay time in milliseconds
|
||||
*/
|
||||
void delay_ms(uint32_t ms) {
|
||||
HAL_Delay(ms);
|
||||
hal_delay_ms(ms);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,11 +29,5 @@ void delay_ms(uint32_t ms) {
|
||||
* @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 */
|
||||
}
|
||||
hal_delay_us(us);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user