mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Remove "delay_microseconds_accurate()" and improve systemwide delayMicroseconds() (#2497)
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							6e5cfac927
						
					
				
				
					commit
					875b803483
				
			| @@ -73,7 +73,7 @@ void AHT10Component::update() { | |||||||
|   bool success = false; |   bool success = false; | ||||||
|   for (int i = 0; i < AHT10_ATTEMPTS; ++i) { |   for (int i = 0; i < AHT10_ATTEMPTS; ++i) { | ||||||
|     ESP_LOGVV(TAG, "Attempt %d at %6u", i, millis()); |     ESP_LOGVV(TAG, "Attempt %d at %6u", i, millis()); | ||||||
|     delay_microseconds_accurate(4); |     delayMicroseconds(4); | ||||||
|  |  | ||||||
|     uint8_t reg = 0; |     uint8_t reg = 0; | ||||||
|     if (this->write(®, 1) != i2c::ERROR_OK) { |     if (this->write(®, 1) != i2c::ERROR_OK) { | ||||||
|   | |||||||
| @@ -21,11 +21,7 @@ void IRAM_ATTR HOT yield() { vPortYield(); } | |||||||
| uint32_t IRAM_ATTR HOT millis() { return (uint32_t)(esp_timer_get_time() / 1000ULL); } | uint32_t IRAM_ATTR HOT millis() { return (uint32_t)(esp_timer_get_time() / 1000ULL); } | ||||||
| void IRAM_ATTR HOT delay(uint32_t ms) { vTaskDelay(ms / portTICK_PERIOD_MS); } | void IRAM_ATTR HOT delay(uint32_t ms) { vTaskDelay(ms / portTICK_PERIOD_MS); } | ||||||
| uint32_t IRAM_ATTR HOT micros() { return (uint32_t) esp_timer_get_time(); } | uint32_t IRAM_ATTR HOT micros() { return (uint32_t) esp_timer_get_time(); } | ||||||
| void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { | void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); } | ||||||
|   auto start = (uint64_t) esp_timer_get_time(); |  | ||||||
|   while (((uint64_t) esp_timer_get_time()) - start < us) |  | ||||||
|     ; |  | ||||||
| } |  | ||||||
| void arch_restart() { | void arch_restart() { | ||||||
|   esp_restart(); |   esp_restart(); | ||||||
|   // restart() doesn't always end execution |   // restart() doesn't always end execution | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ void IRAM_ATTR HOT yield() { ::yield(); } | |||||||
| uint32_t IRAM_ATTR HOT millis() { return ::millis(); } | uint32_t IRAM_ATTR HOT millis() { return ::millis(); } | ||||||
| void IRAM_ATTR HOT delay(uint32_t ms) { ::delay(ms); } | void IRAM_ATTR HOT delay(uint32_t ms) { ::delay(ms); } | ||||||
| uint32_t IRAM_ATTR HOT micros() { return ::micros(); } | uint32_t IRAM_ATTR HOT micros() { return ::micros(); } | ||||||
| void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { ::delayMicroseconds(us); } | void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); } | ||||||
| void arch_restart() { | void arch_restart() { | ||||||
|   ESP.restart();  // NOLINT(readability-static-accessed-through-instance) |   ESP.restart();  // NOLINT(readability-static-accessed-through-instance) | ||||||
|   // restart() doesn't always end execution |   // restart() doesn't always end execution | ||||||
|   | |||||||
| @@ -121,10 +121,8 @@ void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t sen | |||||||
|     } else { |     } else { | ||||||
|       this->status_clear_warning(); |       this->status_clear_warning(); | ||||||
|     } |     } | ||||||
|     if (i + 1 < send_times) { |     if (i + 1 < send_times) | ||||||
|       delay(send_wait / 1000UL); |       delayMicroseconds(send_wait); | ||||||
|       delayMicroseconds(send_wait % 1000UL); |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -36,7 +36,7 @@ void RemoteTransmitterComponent::calculate_on_off_time_(uint32_t carrier_frequen | |||||||
| void RemoteTransmitterComponent::mark_(uint32_t on_time, uint32_t off_time, uint32_t usec) { | void RemoteTransmitterComponent::mark_(uint32_t on_time, uint32_t off_time, uint32_t usec) { | ||||||
|   if (this->carrier_duty_percent_ == 100 || (on_time == 0 && off_time == 0)) { |   if (this->carrier_duty_percent_ == 100 || (on_time == 0 && off_time == 0)) { | ||||||
|     this->pin_->digital_write(true); |     this->pin_->digital_write(true); | ||||||
|     delay_microseconds_accurate(usec); |     delayMicroseconds(usec); | ||||||
|     this->pin_->digital_write(false); |     this->pin_->digital_write(false); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
| @@ -48,19 +48,19 @@ void RemoteTransmitterComponent::mark_(uint32_t on_time, uint32_t off_time, uint | |||||||
|     const uint32_t elapsed = current_time - start_time; |     const uint32_t elapsed = current_time - start_time; | ||||||
|     this->pin_->digital_write(true); |     this->pin_->digital_write(true); | ||||||
|  |  | ||||||
|     delay_microseconds_accurate(std::min(on_time, usec - elapsed)); |     delayMicroseconds(std::min(on_time, usec - elapsed)); | ||||||
|     this->pin_->digital_write(false); |     this->pin_->digital_write(false); | ||||||
|     if (elapsed + on_time >= usec) |     if (elapsed + on_time >= usec) | ||||||
|       return; |       return; | ||||||
|  |  | ||||||
|     delay_microseconds_accurate(std::min(usec - elapsed - on_time, off_time)); |     delayMicroseconds(std::min(usec - elapsed - on_time, off_time)); | ||||||
|  |  | ||||||
|     current_time = micros(); |     current_time = micros(); | ||||||
|   } |   } | ||||||
| } | } | ||||||
| void RemoteTransmitterComponent::space_(uint32_t usec) { | void RemoteTransmitterComponent::space_(uint32_t usec) { | ||||||
|   this->pin_->digital_write(false); |   this->pin_->digital_write(false); | ||||||
|   delay_microseconds_accurate(usec); |   delayMicroseconds(usec); | ||||||
| } | } | ||||||
| void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) { | void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) { | ||||||
|   ESP_LOGD(TAG, "Sending remote code..."); |   ESP_LOGD(TAG, "Sending remote code..."); | ||||||
| @@ -81,9 +81,8 @@ void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t sen | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (i + 1 < send_times) { |     if (i + 1 < send_times) | ||||||
|       delay_microseconds_accurate(send_wait); |       delayMicroseconds(send_wait); | ||||||
|     } |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| #include "sdp3x.h" | #include "sdp3x.h" | ||||||
| #include "esphome/core/log.h" | #include "esphome/core/log.h" | ||||||
|  | #include "esphome/core/hal.h" | ||||||
| #include "esphome/core/helpers.h" | #include "esphome/core/helpers.h" | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| @@ -25,7 +26,7 @@ void SDP3XComponent::setup() { | |||||||
|     ESP_LOGW(TAG, "Soft Reset SDP3X failed!");  // This sometimes fails for no good reason |     ESP_LOGW(TAG, "Soft Reset SDP3X failed!");  // This sometimes fails for no good reason | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   delay_microseconds_accurate(20000); |   delayMicroseconds(20000); | ||||||
|  |  | ||||||
|   if (this->write(SDP3X_READ_ID1, 2) != i2c::ERROR_OK) { |   if (this->write(SDP3X_READ_ID1, 2) != i2c::ERROR_OK) { | ||||||
|     ESP_LOGE(TAG, "Read ID1 SDP3X failed!"); |     ESP_LOGE(TAG, "Read ID1 SDP3X failed!"); | ||||||
|   | |||||||
| @@ -209,17 +209,18 @@ uint8_t crc8(uint8_t *data, uint8_t len) { | |||||||
|   return crc; |   return crc; | ||||||
| } | } | ||||||
|  |  | ||||||
| void delay_microseconds_accurate(uint32_t usec) { | void delay_microseconds_safe(uint32_t us) {  // avoids CPU locks that could trigger WDT or affect WiFi/BT stability | ||||||
|   if (usec == 0) |   auto start = micros(); | ||||||
|     return; |   const uint32_t lag = 5000;  // microseconds, specifies the maximum time for a CPU busy-loop. | ||||||
|   if (usec < 5000UL) { |                               // it must be larger than the worst-case duration of a delay(1) call (hardware tasks) | ||||||
|     delayMicroseconds(usec); |                               // 5ms is conservative, it could be reduced when exact BT/WiFi stack delays are known | ||||||
|     return; |   if (us > lag) { | ||||||
|   } |     delay((us - lag) / 1000UL);  // note: in disabled-interrupt contexts delay() won't actually sleep | ||||||
|   uint32_t start = micros(); |     while (micros() - start < us - lag) | ||||||
|   while (micros() - start < usec) { |       delay(1);  // in those cases, this loop allows to yield for BT/WiFi stack tasks | ||||||
|     delay(0); |  | ||||||
|   } |   } | ||||||
|  |   while (micros() - start < us)  // fine delay the remaining usecs | ||||||
|  |     ; | ||||||
| } | } | ||||||
|  |  | ||||||
| uint8_t reverse_bits_8(uint8_t x) { | uint8_t reverse_bits_8(uint8_t x) { | ||||||
|   | |||||||
| @@ -255,7 +255,7 @@ struct is_callable  // NOLINT | |||||||
|   static constexpr auto value = decltype(test<T>(nullptr))::value;  // NOLINT |   static constexpr auto value = decltype(test<T>(nullptr))::value;  // NOLINT | ||||||
| }; | }; | ||||||
|  |  | ||||||
| void delay_microseconds_accurate(uint32_t usec); | void delay_microseconds_safe(uint32_t us); | ||||||
|  |  | ||||||
| template<typename T> class Deduplicator { | template<typename T> class Deduplicator { | ||||||
|  public: |  public: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user