Skip to content

TIMER - add timer_started flag, fix timerEnd() + timer HW test #8135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion cores/esp32/esp32-hal-timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef struct {
struct timer_struct_t {
gptimer_handle_t timer_handle;
interrupt_config_t interrupt_handle;
bool timer_started;
};

inline uint64_t timerRead(hw_timer_t * timer){
Expand Down Expand Up @@ -62,10 +63,12 @@ uint32_t timerGetFrequency(hw_timer_t * timer){

void timerStart(hw_timer_t * timer){
gptimer_start(timer->timer_handle);
timer->timer_started = true;
}

void timerStop(hw_timer_t * timer){
gptimer_stop(timer->timer_handle);
timer->timer_started = false;
}

void timerRestart(hw_timer_t * timer){
Expand Down Expand Up @@ -111,11 +114,15 @@ hw_timer_t * timerBegin(uint32_t frequency){
}
gptimer_enable(timer->timer_handle);
gptimer_start(timer->timer_handle);
timer->timer_started = true;
return timer;
}

void timerEnd(hw_timer_t * timer){
esp_err_t err = ESP_OK;
if(timer->timer_started == true){
gptimer_stop(timer->timer_handle);
}
gptimer_disable(timer->timer_handle);
err = gptimer_del_timer(timer->timer_handle);
if (err != ESP_OK){
Expand Down Expand Up @@ -147,14 +154,20 @@ void timerAttachInterruptFunctionalArg(hw_timer_t * timer, void (*userFunc)(void
timer->interrupt_handle.fn = (voidFuncPtr)userFunc;
timer->interrupt_handle.arg = arg;

if(timer->timer_started == true){
gptimer_stop(timer->timer_handle);
}
gptimer_disable(timer->timer_handle);
err = gptimer_register_event_callbacks(timer->timer_handle, &cbs, &timer->interrupt_handle);
if (err != ESP_OK){
log_e("Timer Attach Interrupt failed, error num=%d", err);
}
gptimer_enable(timer->timer_handle);
}
if(timer->timer_started == true){
gptimer_start(timer->timer_handle);
}

}

void timerAttachInterruptArg(hw_timer_t * timer, void (*userFunc)(void*), void * arg){
timerAttachInterruptFunctionalArg(timer, userFunc, arg);
Expand Down
31 changes: 15 additions & 16 deletions tests/timer/timer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,29 @@
* S3 - APB + XTAL clk
*/

hw_timer_t * timer = NULL;
hw_timer_t * timer_XTAL = NULL;
static hw_timer_t * timer = NULL;
static volatile bool alarm_flag;

/* These functions are intended to be called before and after each test. */
/* setUp / tearDown functions are intended to be called before / after each test. */
void setUp(void) {
timer = timerBegin(TIMER_FREQUENCY);
if(timer == NULL){
TEST_FAIL_MESSAGE("Timer init failed in setUp()");
}
timerStop(timer);
timerRestart(timer);
}

void tearDown(void){
timerEnd(timer);
timer = NULL;
}



void ARDUINO_ISR_ATTR onTimer(){
alarm_flag = true;
}


void timer_interrupt_test(void){

alarm_flag = false;
timerAttachInterrupt(timer, &onTimer);
timerAlarm(timer, (1.2 * TIMER_FREQUENCY), true, 0);
Expand Down Expand Up @@ -67,9 +65,11 @@ void timer_divider_test(void){

// compare divider 16 and 8, value should be double
timerEnd(timer);
timer = NULL;

timer = timerBegin(2 * TIMER_FREQUENCY);
if(timer == NULL){
TEST_FAIL_MESSAGE("Timer init failed!");
}
timerRestart(timer);
delay(1000);
comp_time_val = timerRead(timer);
Expand All @@ -79,9 +79,11 @@ void timer_divider_test(void){

// divider is 256, value should be 2^4
timerEnd(timer);
timer = NULL;

timer = timerBegin(TIMER_FREQUENCY / 16);
if(timer == NULL){
TEST_FAIL_MESSAGE("Timer init failed!");
}
timerRestart(timer);
delay(1000);
comp_time_val = timerRead(timer);
Expand All @@ -103,13 +105,10 @@ void timer_read_test(void){

void timer_clock_select_test(void){
// Set timer frequency that can be achieved using XTAL clock source (autoselected)
timer_XTAL = timerBegin(TIMER_FREQUENCY_XTAL_CLK);
timer = timerBegin(TIMER_FREQUENCY_XTAL_CLK);

uint32_t resolution = timerGetFrequency(timer_XTAL);
uint32_t resolution = timerGetFrequency(timer);
TEST_ASSERT_EQUAL(TIMER_FREQUENCY_XTAL_CLK,resolution);

timerEnd(timer_XTAL);
timer_XTAL = NULL;
}

void setup(){
Expand All @@ -131,4 +130,4 @@ void setup(){
}

void loop(){
}
}