Author Topic: CH32V307VCT6 with Freertos and us delay  (Read 1018 times)

0 Members and 1 Guest are viewing this topic.

Offline ali_asadzadehTopic starter

  • Super Contributor
  • ***
  • Posts: 1905
  • Country: ca
CH32V307VCT6 with Freertos and us delay
« on: June 10, 2023, 08:55:14 am »
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.c

Code: [Select]
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);
}
« Last Edit: June 10, 2023, 08:58:25 am by ali_asadzadeh »
ASiDesigner, Stands for Application specific intelligent devices
I'm a Digital Expert from 8-bits to 64-bits
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4037
  • Country: nz
Re: CH32V307VCT6 with Freertos and us delay
« Reply #1 on: June 10, 2023, 09:33:23 am »
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.
 

Offline ali_asadzadehTopic starter

  • Super Contributor
  • ***
  • Posts: 1905
  • Country: ca
Re: CH32V307VCT6 with Freertos and us delay
« Reply #2 on: June 10, 2023, 09:37:44 am »
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?
ASiDesigner, Stands for Application specific intelligent devices
I'm a Digital Expert from 8-bits to 64-bits
 

Offline Christe4nM

  • Supporter
  • ****
  • Posts: 252
  • Country: nl
Re: CH32V307VCT6 with Freertos and us delay
« Reply #3 on: June 10, 2023, 10:03:14 am »
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
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4037
  • Country: nz
Re: CH32V307VCT6 with Freertos and us delay
« Reply #4 on: June 10, 2023, 10:55:57 am »
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?

I 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.
 
The following users thanked this post: ali_asadzadeh

Offline ali_asadzadehTopic starter

  • Super Contributor
  • ***
  • Posts: 1905
  • Country: ca
Re: CH32V307VCT6 with Freertos and us delay
« Reply #5 on: June 10, 2023, 02:18:14 pm »
Here is a function that I came up with,

Code: [Select]
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 around
        while(SysTick->CNT > 141000);
        // Wait for the remaining ticks after the timer wraps around
        while(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,
Screen-Img-1" border="0

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?
Screen-Img" border="0
ASiDesigner, Stands for Application specific intelligent devices
I'm a Digital Expert from 8-bits to 64-bits
 

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1478
  • Country: gb
Re: CH32V307VCT6 with Freertos and us delay
« Reply #6 on: June 10, 2023, 02:22:12 pm »
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?

If 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
 
The following users thanked this post: ali_asadzadeh

Offline ali_asadzadehTopic starter

  • Super Contributor
  • ***
  • Posts: 1905
  • Country: ca
Re: CH32V307VCT6 with Freertos and us delay
« Reply #7 on: June 11, 2023, 08:09:51 am »
Thanks HwAoRrDk for sharing the link
ASiDesigner, Stands for Application specific intelligent devices
I'm a Digital Expert from 8-bits to 64-bits
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf