void stop_step_motor(void)
{
ESP_LOGI(GATTS_TABLE_TAG, "stop_step_motor");
//停止定时器
gptimer_stop(gptimer);
}
static bool IRAM_ATTR step_timer_inr(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *args)
{
BaseType_t high_task_awoken = pdFALSE;
if(step_flag==0)
{//上升沿有效
step_flag=1;
gpio_set_level(GPIO_NUM_10,1);//step
bjdj_step_target--;//目标步数
if(bjdj_step_target==0)
{
//停止定时器
stop_step_motor();
}
}
else
{
step_flag=0;
gpio_set_level(GPIO_NUM_10,0);
}
// return whether we need to yield at the end of ISR
return (high_task_awoken == pdTRUE);
}
void step_timer_init()
{
ESP_LOGI(GATTS_TABLE_TAG, "Create timer handle");
gptimer_config_t timer_config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1000000, // 1MHz, 1 tick=1us
};
ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer));
gptimer_event_callbacks_t cbs = {
.on_alarm = step_timer_inr,
};
ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, NULL));
ESP_ERROR_CHECK(gptimer_enable(gptimer));
ESP_LOGI(GATTS_TABLE_TAG, "Start timer, stop it at alarm event");
gptimer_alarm_config_t alarm_config1 = {
.reload_count = 0,
// .alarm_count = 10000, // period = 10ms
.alarm_count = 10000, // period = 0.1ms
.flags.auto_reload_on_alarm = true,
};
ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config1));
// ESP_ERROR_CHECK(gptimer_start(gptimer));
}
一运行到函数stop_step_motor(),esp32C3就重启,并报如下的错:
abort() was called at PC 0x40381aa5 on core 0
0x40381aa5: lock_acquire_generic at D:/esp/esp-idf/components/newlib/locks.c:130
Core 0 register dump:
MEPC : 0x40380926 RA : 0x4038b2da SP : 0x3fc943f0 GP : 0x3fc92c00
0x40380926: panic_abort at D:/esp/esp-idf/components/esp_system/panic.c:423
0x4038b2da: __ubsan_include at D:/esp/esp-idf/components/esp_system/ubsan.c:313
TP : 0x3fc78e74 T0 : 0x37363534 T1 : 0x7271706f T2 : 0x33323130
S0/FP : 0x00000004 S1 : 0x3fc94454 A0 : 0x3fc9441c A1 : 0x3fc94452
最后发现是打LOG引起的,注释掉就可以了
void stop_step_motor(void)
{
//ESP_LOGI(GATTS_TABLE_TAG, "stop_step_motor");
//停止定时器
gptimer_stop(gptimer);
}
得到的教训就是一定不能在定时器中断函数中占用太长时间。