用系统自带的函数:
void usleep(int us); //微秒级
void ets_delay_us(uint32_t us);//微秒级
用系统寄存器计算:[可实现纳秒级延时]
static __inline void delay_clock(int ts)
{
uint32_t start, curr;
__asm__ __volatile__("rsr %0, ccount" : "=r"(start));
do
{
__asm__ __volatile__("rsr %0, ccount" : "=r"(curr));
}while (curr - start <= ts);
}
void udelay(int us)
{
while (us--)
{
delay_clock(160);//CPU_Freq=160MHz
}
}
void mdelay(int ms)
{
while (ms--)
{
delay_clock(160*1000);//CPU_Freq=160MHz
}
}
获取当前系统运行时间函数:
1. esp_timer_get_time() //精度1微秒 "esp_timer.h"
2. esp_log_timestamp() //精度1毫秒 "esp_log.h" 使用FreeRTOS滴答计数器
3. esp_log_early_timestamp() //精度1毫秒 "esp_log.h" 使用硬件周期计数器
PS: 对于32位RISC-V芯片获取运行周期数
// 定义一个读取mcycle的宏(32位架构,读取低32位)
uint64_t get_cpu_cycles()
{
//测试用printf("cycles= %" PRIu64 "\n", t1);//RISC-V内核
uint64_t __tmp;
asm volatile("csrr %0, mcycle" : "=r"(__tmp));
return __tmp;
}
1257

被折叠的 条评论
为什么被折叠?



