You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/10/25 04:38:10 UTC

[1/2] incubator-mynewt-core git commit: native; add hal_timer()

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 74d828cb7 -> 145b20f78


native; add hal_timer()


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/561c23d8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/561c23d8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/561c23d8

Branch: refs/heads/develop
Commit: 561c23d89ff0e5a1025a82c9d5c0231cb92e2655
Parents: 74d828c
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Oct 24 21:37:27 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Oct 24 21:37:27 2016 -0700

----------------------------------------------------------------------
 hw/mcu/native/src/hal_timer.c | 380 +++++++++++++++++++++++++++++++++++++
 1 file changed, 380 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/561c23d8/hw/mcu/native/src/hal_timer.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_timer.c b/hw/mcu/native/src/hal_timer.c
new file mode 100644
index 0000000..8d4a8ca
--- /dev/null
+++ b/hw/mcu/native/src/hal_timer.c
@@ -0,0 +1,380 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+#include <stdint.h>
+#include <assert.h>
+#include <os/os.h>
+
+#include "hal/hal_timer.h"
+
+/*
+ * For native cpu implementation.
+ */
+static uint8_t native_timer_task_started;
+#define NATIVE_TIMER_STACK_SIZE   (1024)
+static os_stack_t native_timer_stack[NATIVE_TIMER_STACK_SIZE];
+static struct os_task native_timer_task_struct;
+static struct os_eventq native_timer_evq;
+
+struct native_timer {
+    struct os_callout_func callout;
+    uint32_t ticks_per_ostick;
+    uint32_t cnt;
+    uint32_t last_ostime;
+    int num;
+    TAILQ_HEAD(hal_timer_qhead, hal_timer) timers;
+} native_timers[1];
+
+/**
+ * This is the function called when the timer fires.
+ *
+ * @param arg
+ */
+void
+native_timer_cb(void *arg)
+{
+    struct native_timer *nt = (struct native_timer *)arg;
+    uint32_t cnt;
+    struct hal_timer *ht;
+    os_sr_t sr;
+
+    OS_ENTER_CRITICAL(sr);
+    cnt = hal_timer_read(nt->num);
+    while ((ht = TAILQ_FIRST(&nt->timers)) != NULL) {
+        if (((int32_t)(cnt - ht->expiry)) >= 0) {
+            TAILQ_REMOVE(&nt->timers, ht, link);
+            ht->link.tqe_prev = NULL;
+            ht->cb_func(ht->cb_arg);
+        } else {
+            break;
+        }
+    }
+    ht = TAILQ_FIRST(&nt->timers);
+    if (ht) {
+        os_callout_reset(&nt->callout.cf_c,
+          (ht->expiry - hal_timer_read(nt->num)) / nt->ticks_per_ostick);
+    }
+    OS_EXIT_CRITICAL(sr);
+}
+
+void
+native_timer_task(void *arg)
+{
+    struct os_event *ev;
+    struct os_callout_func *cf;
+
+    while (1) {
+        ev = os_eventq_get(&native_timer_evq);
+        switch (ev->ev_type) {
+        case OS_EVENT_T_TIMER:
+            cf = (struct os_callout_func *)ev;
+            assert(cf->cf_func);
+            cf->cf_func(CF_ARG(cf));
+            break;
+        default:
+            assert(0);
+            break;
+        }
+    }
+}
+
+/**
+ * hal_timer_init
+ *
+ * Initialize the cputime module to run at the desired frequency.
+ *
+ * @param timer_num
+ * @param clock_freq The desired cputime frequency, in hertz (Hz).
+ *
+ * @return int 0 on success; -1 on error.
+ */
+int
+hal_timer_init(int num, uint32_t clock_freq)
+{
+    struct native_timer *nt;
+
+    if (num != 0) {
+        return -1;
+    }
+    nt = &native_timers[num];
+
+    /* Set the clock frequency */
+    nt->ticks_per_ostick = clock_freq / OS_TICKS_PER_SEC;
+    if (!nt->ticks_per_ostick) {
+        nt->ticks_per_ostick = 1;
+    }
+    nt->num = num;
+    nt->cnt = 0;
+    nt->last_ostime = os_time_get();
+    if (!native_timer_task_started) {
+        os_task_init(&native_timer_task_struct, "native_timer",
+          native_timer_task, NULL, OS_TASK_PRI_HIGHEST, OS_WAIT_FOREVER,
+          native_timer_stack, NATIVE_TIMER_STACK_SIZE);
+
+        /* Initialize the eventq and task */
+        os_eventq_init(&native_timer_evq);
+        native_timer_task_started = 1;
+    }
+
+    /* Initialize the callout function */
+    os_callout_func_init(&nt->callout, &native_timer_evq, native_timer_cb, nt);
+
+    return 0;
+}
+
+int
+hal_timer_deinit(int num)
+{
+    struct native_timer *nt;
+
+    if (num != 0) {
+        return -1;
+    }
+    nt = &native_timers[num];
+
+    os_callout_stop(&nt->callout.cf_c);
+    return 0;
+}
+
+/**
+ * hal timer get resolution
+ *
+ * Get the resolution of the timer. This is the timer period, in nanoseconds
+ *
+ * @param timer_num
+ *
+ * @return uint32_t
+ */
+uint32_t
+hal_timer_get_resolution(int num)
+{
+    struct native_timer *nt;
+
+    if (num >= 0) {
+        return 0;
+    }
+    nt = &native_timers[num];
+    return 1000000000 / (nt->ticks_per_ostick * OS_TICKS_PER_SEC);
+}
+
+/**
+ * hal_timer_read
+ *
+ * Returns the low 32 bits of timer counter.
+ *
+ * @return uint32_t The timer counter register.
+ */
+uint32_t
+hal_timer_read(int num)
+{
+    struct native_timer *nt;
+    os_sr_t sr;
+    uint32_t ostime;
+    uint32_t delta_osticks;
+
+    if (num != 0) {
+        return -1;
+    }
+    nt = &native_timers[num];
+    OS_ENTER_CRITICAL(sr);
+    ostime = os_time_get();
+    delta_osticks = (uint32_t)(ostime - nt->last_ostime);
+    if (delta_osticks) {
+        nt->last_ostime = ostime;
+        nt->cnt += nt->ticks_per_ostick * delta_osticks;
+
+    }
+    OS_EXIT_CRITICAL(sr);
+
+    return (uint32_t)nt->cnt;
+}
+
+/**
+ * hal timer delay
+ *
+ * Blocking delay for n ticks
+ *
+ * @param timer_num
+ * @param ticks
+ *
+ * @return int 0 on success; error code otherwise.
+ */
+int
+hal_timer_delay(int num, uint32_t ticks)
+{
+    uint32_t until;
+
+    if (num != 0) {
+        return -1;
+    }
+
+    until = hal_timer_read(0) + ticks;
+    while ((int32_t)(hal_timer_read(0) - until) <= 0) {
+        ;
+    }
+    return 0;
+}
+
+/**
+ *
+ * Initialize the HAL timer structure with the callback and the callback
+ * argument. Also initializes the HW specific timer pointer.
+ *
+ * @param cb_func
+ *
+ * @return int
+ */
+int
+hal_timer_set_cb(int num, struct hal_timer *timer, hal_timer_cb cb_func,
+                 void *arg)
+{
+    struct native_timer *nt;
+
+    if (num != 0) {
+        return -1;
+    }
+    nt = &native_timers[num];
+    timer->cb_func = cb_func;
+    timer->cb_arg = arg;
+    timer->bsp_timer = nt;
+    timer->link.tqe_prev = NULL;
+
+    return 0;
+}
+
+/**
+ * hal_timer_start()
+ *
+ * Start a timer. Timer fires 'ticks' ticks from now.
+ *
+ * @param timer
+ * @param ticks
+ *
+ * @return int
+ */
+int
+hal_timer_start(struct hal_timer *timer, uint32_t ticks)
+{
+    struct native_timer *nt;
+    uint32_t tick;
+
+    nt = (struct native_timer *)timer->bsp_timer;
+
+    tick = ticks + hal_timer_read(nt->num);
+    return hal_timer_start_at(timer, tick);
+}
+
+/**
+ * hal_timer_start_at()
+ *
+ * Start a timer. Timer fires at tick 'tick'.
+ *
+ * @param timer
+ * @param tick
+ *
+ * @return int
+ */
+int
+hal_timer_start_at(struct hal_timer *timer, uint32_t tick)
+{
+    struct native_timer *nt;
+    struct hal_timer *ht;
+    uint32_t curtime;
+    uint32_t osticks;
+    os_sr_t sr;
+
+    nt = (struct native_timer *)timer->bsp_timer;
+
+    timer->expiry = tick;
+
+    OS_ENTER_CRITICAL(sr);
+
+    if (TAILQ_EMPTY(&nt->timers)) {
+        TAILQ_INSERT_HEAD(&nt->timers, timer, link);
+    } else {
+        TAILQ_FOREACH(ht, &nt->timers, link) {
+            if ((int32_t)(timer->expiry - ht->expiry) < 0) {
+                TAILQ_INSERT_BEFORE(ht, timer, link);
+                break;
+            }
+        }
+        if (!ht) {
+            TAILQ_INSERT_TAIL(&nt->timers, timer, link);
+        }
+    }
+
+    curtime = hal_timer_read(nt->num);
+    if ((int32_t)(tick - curtime) <= 0) {
+        /*
+         * Event in the past (should be the case if it was just inserted).
+         */
+        os_callout_reset(&nt->callout.cf_c, 0);
+    } else {
+        if (timer == TAILQ_FIRST(&nt->timers)) {
+            osticks = (tick - curtime) / nt->ticks_per_ostick;
+            os_callout_reset(&nt->callout.cf_c, osticks);
+        }
+    }
+    OS_EXIT_CRITICAL(sr);
+
+    return 0;
+}
+
+/**
+ * hal_timer_stop()
+ *
+ * Cancels the timer.
+ *
+ * @param timer
+ *
+ * @return int
+ */
+int
+hal_timer_stop(struct hal_timer *timer)
+{
+    struct native_timer *nt;
+    struct hal_timer *ht;
+    int reset_ocmp;
+    os_sr_t sr;
+
+    OS_ENTER_CRITICAL(sr);
+
+    nt = (struct native_timer *)timer->bsp_timer;
+    if (timer->link.tqe_prev != NULL) {
+        reset_ocmp = 0;
+        if (timer == TAILQ_FIRST(&nt->timers)) {
+            /* If first on queue, we will need to reset OCMP */
+            ht = TAILQ_NEXT(timer, link);
+            reset_ocmp = 1;
+        }
+        TAILQ_REMOVE(&nt->timers, timer, link);
+        timer->link.tqe_prev = NULL;
+        if (reset_ocmp) {
+            if (ht) {
+                os_callout_reset(&nt->callout.cf_c,
+                  (ht->expiry - hal_timer_read(nt->num) /
+                    nt->ticks_per_ostick));
+            } else {
+                os_callout_stop(&nt->callout.cf_c);
+            }
+        }
+    }
+    OS_EXIT_CRITICAL(sr);
+
+    return 0;
+}


[2/2] incubator-mynewt-core git commit: apps/ocf_sample; does not need to include os_cputime.h anymore.

Posted by ma...@apache.org.
apps/ocf_sample; does not need to include os_cputime.h anymore.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/145b20f7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/145b20f7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/145b20f7

Branch: refs/heads/develop
Commit: 145b20f7831be231e9e84c8b466afb44a1b1ab86
Parents: 561c23d
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Oct 24 21:37:45 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Oct 24 21:37:45 2016 -0700

----------------------------------------------------------------------
 apps/ocf_sample/src/main.c | 1 -
 1 file changed, 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/145b20f7/apps/ocf_sample/src/main.c
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/src/main.c b/apps/ocf_sample/src/main.c
index e3e1fc1..0f5a559 100644
--- a/apps/ocf_sample/src/main.c
+++ b/apps/ocf_sample/src/main.c
@@ -19,7 +19,6 @@
 #include <assert.h>
 #include "sysinit/sysinit.h"
 #include <os/os.h>
-#include <os/os_cputime.h>
 #include <sysinit/sysinit.h>
 #include <bsp/bsp.h>
 #include <log/log.h>