Skip to content

Commit

Permalink
Merge branch 'bufgix/esp_timer_set_alarm' into 'master'
Browse files Browse the repository at this point in the history
esp_timer: Fix set_alarm. Case when timestamp < now_time

Closes WIFI-1511

See merge request espressif/esp-idf!6960
  • Loading branch information
projectgus committed Dec 16, 2019
2 parents 1f6bb6e + c6b6818 commit f7b51c1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions components/esp32/esp_timer_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp)
// Note that if by the time we update ALARM_REG, COUNT_REG value is higher,
// interrupt will not happen for another ALARM_OVERFLOW_VAL timer ticks,
// so need to check if alarm value is too close in the future (e.g. <2 us away).
const uint32_t offset = s_timer_ticks_per_us * 2;
const int32_t offset = s_timer_ticks_per_us * 2;
do {
// Adjust current time if overflow has happened
if (timer_overflow_happened()) {
timer_count_reload();
s_time_base_us += s_timer_us_per_overflow;
}
s_mask_overflow = false;
uint64_t cur_count = REG_READ(FRC_TIMER_COUNT_REG(1));
int64_t cur_count = REG_READ(FRC_TIMER_COUNT_REG(1));
// Alarm time relative to the moment when counter was 0
int64_t time_after_timebase_us = (int64_t)timestamp - s_time_base_us;
// Calculate desired timer compare value (may exceed 2^32-1)
Expand Down
19 changes: 19 additions & 0 deletions components/esp32/test/test_esp_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <sys/time.h>
#include <sys/param.h>
#include "unity.h"
#include "soc/frc_timer_reg.h"
#include "esp_timer.h"
#include "esp_heap_caps.h"
#include "freertos/FreeRTOS.h"
Expand Down Expand Up @@ -817,3 +818,21 @@ TEST_CASE("esp_timer_impl_set_alarm and using start_once do not lead that the Sy
}

#endif // !defined(CONFIG_FREERTOS_UNICORE) && defined(CONFIG_ESP32_DPORT_WORKAROUND)

TEST_CASE("Test case when esp_timer_impl_set_alarm needs set timer < now_time", "[esp_timer]")
{
REG_WRITE(FRC_TIMER_LOAD_REG(1), 0);
esp_timer_impl_advance(50331648); // 0xefffffff/80 = 50331647

ets_delay_us(2);

portDISABLE_INTERRUPTS();
esp_timer_impl_set_alarm(50331647);
uint32_t alarm_reg = REG_READ(FRC_TIMER_ALARM_REG(1));
uint32_t count_reg = REG_READ(FRC_TIMER_COUNT_REG(1));
portENABLE_INTERRUPTS();

const uint32_t offset = 80 * 2; // s_timer_ticks_per_us
printf("alarm_reg = 0x%x, count_reg 0x%x\n", alarm_reg, count_reg);
TEST_ASSERT(alarm_reg <= (count_reg + offset));
}
2 changes: 1 addition & 1 deletion tools/ci/config/target-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ example_test_012:

UT_001:
extends: .unit_test_template
parallel: 30
parallel: 31
tags:
- ESP32_IDF
- UT_T1_1
Expand Down

0 comments on commit f7b51c1

Please sign in to comment.