Skip to content

delaysystick overflow for large delay value #697

@sysytwl

Description

@sysytwl

line 1577 mod follow
··· c

/**

  • @brief Delays execution using the SysTick timer
  • @param n Number of clock ticks to delay (64-bit for extended range)
  • @note Clock tick duration depends on system clock frequency
    */
    void DelaySysTick( uint64_t n )
    {
    // Return immediately if no delay requested
    if (n == 0) return;

#if defined(CH32V003) || defined(CH32V00x)
// 24-bit SysTick counter, counting up
uint32_t start = SysTick->CNT;
uint32_t target = start + (uint32_t)n; // Truncate to 24-bit range

// Handle counter overflow case
if (target < start) {
    // Wait for counter to wrap around to 0
    while (SysTick->CNT >= start);
}
// Wait until target value is reached
while (SysTick->CNT < target);

#elif defined(CH32V20x) || defined(CH32V30x) || defined(CH32X03x) || defined(CH582_CH583) || defined(CH591_CH592)
// 32-bit SysTick counter, counting up
uint64_t start = SysTick->CNT;
uint64_t target = start + n;
// Wait until target value is reached
while (SysTick->CNT < target);

#elif defined(CH32V10x) || defined(CH570_CH572) || defined(CH584_CH585)
// Using CNTL register (lower 32 bits), counting up
uint32_t start = SysTick->CNTL;
uint32_t target = start + (uint32_t)n; // Truncate to 32-bit range

// Handle counter overflow case
if (target < start) {
    // Wait for counter to wrap around to 0
    while (SysTick->CNTL >= start);
}
// Wait until target value is reached
while (SysTick->CNTL < target);

#elif defined(CH571_CH573)
// Down-counting mode
uint64_t start = SysTick->CNT;
uint64_t target = start - n;

// Handle underflow case
if (target > start) {
    // Wait for counter to wrap around
    while (SysTick->CNT < start);
}
// Wait until target value is reached
while (SysTick->CNT > target);

#else
#error DelaySysTick not defined for this platform.
#endif
}

···

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions