Browse Source

add: 实现mcu库的tick64相关的API,为socket库做基础

Wendal Chen 3 years ago
parent
commit
e8efcd0d50
1 changed files with 29 additions and 3 deletions
  1. 29 3
      luatos/components/luat/port/luat_mcu_idf5.c

+ 29 - 3
luatos/components/luat/port/luat_mcu_idf5.c

@@ -4,6 +4,10 @@
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "esp_flash.h"
+#include "driver/gptimer.h"
+
+#define LUAT_LOG_TAG "mcu"
+#include "luat_log.h"
 
 int luat_mcu_set_clk(size_t mhz) {
     return 0;
@@ -34,17 +38,39 @@ uint32_t luat_mcu_hz(void) {
     return configTICK_RATE_HZ;
 }
 
+static gptimer_handle_t us_timer;
 uint64_t luat_mcu_tick64(void) {
-    return 0;
+    uint64_t ret = 0;
+    gptimer_get_raw_count(us_timer, &ret);
+    // LLOGD("tick64 %lld", ret);
+    return ret;
 }
 
 int luat_mcu_us_period(void) {
-    return 0;
+    return 1;
 }
 
 uint64_t luat_mcu_tick64_ms(void) {
-    return 0;
+    return luat_mcu_tick64() / 1000;
 }
 void luat_mcu_set_clk_source(uint8_t source_main, uint8_t source_32k, uint32_t delay) {
     // nop
 }
+
+#define TIMER_DIVIDER         (16)  //  Hardware timer clock divider
+#define TIMER_SCALE           (TIMER_BASE_CLK / TIMER_DIVIDER)  // convert counter value to seconds
+
+void luat_mcu_us_timer_init() {
+        /* Select and initialize basic parameters of the timer */
+    const gptimer_config_t config = {
+        .clk_src = GPTIMER_CLK_SRC_XTAL,
+        .direction = GPTIMER_COUNT_UP,
+        .resolution_hz = 1000000,
+    }; // default clock source is APB
+    int ret = gptimer_new_timer(&config, &us_timer);
+    // LLOGD("gptimer_new_timer %d", ret);
+    if (ret == 0) {
+        if (0 == gptimer_enable(us_timer))
+            gptimer_start(us_timer);
+    }
+}