summaryrefslogtreecommitdiff
path: root/bsp/wch/risc-v/libraries/ch32_drivers/drv_common.c
blob: 1d735ede1ceaf5366c39b2d2c3906758c9c9cf36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-06-14     muaxiaohei   first version
 */

#include <rtthread.h>
#include "drv_common.h"
#include <board.h> /* for rt_hw_us_delay */

#define DBG_TAG    "drv.common"
#define DBG_LVL    DBG_INFO
#include <rtdbg.h>

void rt_hw_us_delay(rt_uint32_t us)
{
    rt_uint64_t total_delay_ticks, us_ticks, start, now, delta, reload;

    start = SysTick->CNT;
    reload = SysTick->CMP;
    us_ticks = SystemCoreClock / 8000000UL;
    total_delay_ticks = us * us_ticks;
    if (total_delay_ticks >= reload)
    {
        LOG_E("rt_hw_us_delay: the us parameter exceeds the maximum limit!");
    }

    do {
        now = SysTick->CNT;
        delta = start > now ? start - now : reload + start - now;
    } while(delta < total_delay_ticks);
}