Merge branch 'feature/add_esp_get_time_int64' into 'master'

Add API to get microseconds

See merge request sdk/ESP8266_RTOS_SDK!826
This commit is contained in:
Dong Heng
2019-03-12 14:25:05 +08:00
7 changed files with 139 additions and 70 deletions

View File

@ -0,0 +1,68 @@
// Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define ESP_TICKS_MAX UINT32_MAX
typedef uint32_t esp_tick_t;
typedef uint32_t esp_irqflag_t;
static inline esp_tick_t soc_get_ticks(void)
{
esp_tick_t ticks;
__asm__ __volatile__(
"rsr %0, ccount\n"
: "=a"(ticks)
:
: "memory"
);
return ticks;
}
static inline esp_irqflag_t soc_save_local_irq(void)
{
esp_irqflag_t flag;
__asm__ __volatile__(
"rsil %0, 1\n"
: "=a"(flag)
:
: "memory"
);
return flag;
}
static inline void soc_restore_local_irq(esp_irqflag_t flag)
{
__asm__ __volatile__(
"wsr %0, ps\n"
:
: "a"(flag)
: "memory"
);
}
#ifdef __cplusplus
}
#endif

View File

@ -187,6 +187,13 @@ esp_err_t esp_timer_stop(esp_timer_handle_t timer);
*/
esp_err_t esp_timer_delete(esp_timer_handle_t timer);
/**
* @brief Get time in microseconds since RTOS starts
* @return number of microseconds since RTOS starts starts (this normally
* happens much early during application startup).
*/
int64_t esp_timer_get_time();
#ifdef __cplusplus
}
#endif

View File

@ -18,6 +18,7 @@
#include "FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "driver/soc.h"
#define ESP_TIMER_HZ CONFIG_FREERTOS_HZ
@ -198,3 +199,29 @@ esp_err_t esp_timer_delete(esp_timer_handle_t timer)
return os_ret == pdPASS ? ESP_OK : ESP_ERR_INVALID_STATE;
}
int64_t esp_timer_get_time()
{
extern esp_tick_t g_cpu_ticks;
extern uint64_t g_os_ticks;
esp_irqflag_t flag;
esp_tick_t diff_ticks, ticks;
uint64_t time;
flag = soc_save_local_irq();
time = g_os_ticks * portTICK_PERIOD_MS * 1000;
ticks = soc_get_ticks();
if (ticks >= g_cpu_ticks) {
diff_ticks = ticks - g_cpu_ticks;
} else {
diff_ticks = ESP_TICKS_MAX - g_cpu_ticks + ticks;
}
time += diff_ticks / (_xt_tick_divisor * configTICK_RATE_HZ / (1000 * 1000));
soc_restore_local_irq(flag);
return (int64_t)time;
}