Optimize GPIO and account for micros overflow in delayMicroseconds

This commit is contained in:
me-no-dev
2016-12-02 13:03:48 +02:00
parent 3c81739b33
commit 6db0ee1304
3 changed files with 86 additions and 69 deletions

View File

@ -68,7 +68,6 @@ uint32_t IRAM_ATTR micros()
uint32_t ccount;
__asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
return ccount / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
//return system_get_time();
}
uint32_t IRAM_ATTR millis()
@ -81,12 +80,17 @@ void delay(uint32_t ms)
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delayMicroseconds(uint32_t us)
void IRAM_ATTR delayMicroseconds(uint32_t us)
{
if(us) {
unsigned long endat = micros();
endat += us;
while(micros() < endat) {
uint32_t m = micros();
if(us){
uint32_t e = (m + us) % ((0xFFFFFFFF / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ) + 1);
if(m > e){ //overflow
while(micros() > e){
NOP();
}
}
while(micros() < e){
NOP();
}
}