Skip to content

delayMicrosecond sometimes makes much shorter delay than requested. #176

Closed
@fpistm

Description

@fpistm

Issuer reported here by ut2uz:
http://stm32duino.com/viewtopic.php?f=48&t=2960

delayMicrosecond sometimes makes much shorter delay than requested. Say, 1 us instead of 4500.

Original implementation from wiring_time.h:

static inline void delayMicroseconds(uint32_t usec){
  uint32_t start = GetCurrentMicro();

  while((start+usec) > GetCurrentMicro());
}

Proposed solution by ut2uz:

static inline void delayMicroseconds(uint32_t usec){
  uint32_t end = GetCurrentMicro() + usec;

  while(((int32_t)(GetCurrentMicro()-end))<0);
}

Note that delays greater than 2147483647 (2147 sec = 35 min ) are handled incorrectly. I'm not familiar with compiler warnings in this IDE, so left the solution as is.

Metadata

Metadata

Assignees

Labels

bug 🐛Something isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions