CH32V307VCT6 with Freertos and us delay - Page 1
Forum section: electronics / general

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 10, 2023, 08:55:14 am
content: Hi,I'm starting to do some projects in CH32V307VCT6, I'm using MounRiver Studio and FreeRTOS, there is Delay_Us function in debug.c file, But it seems this function is modifying the systick and when I use it, it would ruin the FreeRTOS,How can I reimplement the Delay_Us function without modifying the systick, Also I could not find any info about the systick regs or any other system peripherals like NVIC in CH32V parts, the peripherals window in debug mode also would not show them, so I wonder where the info is hidden?this is the original Delay_Us function in debug.cCode:void Delay_Us(uint32_t n){uint32_t i;SysTick->SR &= ~(1 << 0);i = (uint32_t)n * p_us;SysTick->CMP = i;SysTick->CTLR |= (1 << 4) | (1 << 5) | (1 << 0);while((SysTick->SR & (1 << 0)) != (1 << 0));SysTick->CTLR &= ~(1 << 0);}

author: brucehoult
author_rank: Super Contributor
timestamp: June 10, 2023, 09:33:23 am
content: Well, just get the current time, add the delay you want, and then loop while (endTime - currentTime) > 0. Using signed integers, obviously. No need to modify the counter.

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 10, 2023, 09:37:44 am
content: Thanks brucehoult, do you know the register defs? Also How can I see the systick registers when I'm debugging, I can not find them in the peripherals dialog, is it some where else that I can not find?

author: Christe4nM
author_rank: Unknown
timestamp: June 10, 2023, 10:03:14 am
content: SysTick and NVIC are ARM Cortex-M specific, wheras your part is RISC-V based. With ARM the SysTick and NVIC registers are part of the ARM core, and usually don't show up in any peripheral register views from the vendor. Perhaps the same applies to your RISC-V part?With ARM you'd need the ARM Cortex-Mx Reference Manual to know which registers are at play. The SysTick is also implemented by the vendor, and IIRC you'd need the register map for your part to see where it sits in the address space.I am unfamiliar with RISC-V, but seeing as that the vendor of the CH32V parts uses both cores within their product family - could it be that the code was written with ARM in mind and later modified for use with the RISC-V parts? So when used with a RISC-V part it stills uses the ARM names SysTick and NVIC, but it maps to the RISC-V specific implementations of those? Perhaps if you find out what actual (core) peripherals they are in RISC-V, you could find them in your part's RM

author: brucehoult
author_rank: Super Contributor
timestamp: June 10, 2023, 10:55:57 am
content: Quote from: ali_asadzadeh on June 10, 2023, 09:37:44 amI don't know what custom registers WCH might have, but every RISC-V core is supposed to include mtime and mcycles CSRs which can be read using the normal CSRR instruction e.g. CSRR dstReg,mtime or its alias RDTIME dstReg. That's on a 64 bit CPU. On a 32 bit CPU you use RDTIMEH; RDTIMEL; RDTIMEH and check that both RDTIMEH returned the same value, or else loop and try again. Or similarly for clock cycles. For delays of less than 2^31 units you can just read the L value.

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 10, 2023, 02:18:14 pm
content: Here is a function that I came up with,Code:void delay_Us(uint64_t us){uint64_t start = SysTick->CNT;uint64_t end = start + us;if (end > SysTick->CMP) {// we need to account for an overflow of the counter.end -= SysTick->CMP;// Wait until the timer wraps around, we choose 141000 as a high enough value so it has time to wraps aroundwhile(SysTick->CNT > 141000);// Wait for the remaining ticks after the timer wraps aroundwhile(SysTick->CNT < end);}else{while(SysTick->CNT < end);}}SysTick->CMP is 144000, since the MCU clock is 144MHz, and FreeRTOS tick is set to 1Khz, when ever the SysTick->CNT reaches the SysTick->CMP, it would be rested to 0 and start again,My proposed code works almost perfectly, as can be seen in the scope image for a delay of 10us,But if I capture more waves in a longer time, I can see some inconsistency , it is visible in the second image with the blank bars! so what am I doing wrong?Certainly there is small jitter and I do not know where it comes, any suggestions?

author: HwAoRrDk
author_rank: Super Contributor
timestamp: June 10, 2023, 02:22:12 pm
content: Quote from: ali_asadzadeh on June 10, 2023, 08:55:14 amIf I remember correctly, the SysTick registers are described in the CPU manual, not the Reference manual. For the CH32V307, this would be the QingKeV4 Processor Manual. You can download it from the WCH site:http://www.wch-ic.com/downloads/QingKeV4_Processor_Manual_PDF.html

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 11, 2023, 08:09:51 am
content: Thanks HwAoRrDk for sharing the link