/** * * @brief Test the k_cpu_idle() routine * * This tests the k_cpu_idle() routine. The first thing it does is align to * a tick boundary. The only source of interrupts while the test is running is * expected to be the tick clock timer which should wake the CPU. Thus after * each call to k_cpu_idle(), the tick count should be one higher. * * @return TC_PASS on success * @return TC_FAIL on failure */ static int test_kernel_cpu_idle(int atomic) { int tms; /* current time in millisecond */ int i; /* loop variable */ /* Align to a "ms boundary". */ tms = k_uptime_get_32(); while (tms == k_uptime_get_32()) { } tms = k_uptime_get_32(); for (i = 0; i < 5; i++) { /* Repeat the test five times */ if (atomic) { unsigned int key = irq_lock(); k_cpu_atomic_idle(key); } else { k_cpu_idle(); } /* calculating milliseconds per tick*/ tms += sys_clock_us_per_tick / USEC_PER_MSEC; if (k_uptime_get_32() < tms) { return TC_FAIL; } } return TC_PASS; }
/** * * @brief Test the k_cpu_idle() routine * * This tests the k_cpu_idle() routine. The first thing it does is align to * a tick boundary. The only source of interrupts while the test is running is * expected to be the tick clock timer which should wake the CPU. Thus after * each call to k_cpu_idle(), the tick count should be one higher. * * @return TC_PASS on success * @return TC_FAIL on failure */ static int test_kernel_cpu_idle(int atomic) { int tms, tms2;; /* current time in millisecond */ int i; /* loop variable */ /* Align to a "ms boundary". */ tms = k_uptime_get_32(); while (tms == k_uptime_get_32()) { } tms = k_uptime_get_32(); for (i = 0; i < 5; i++) { /* Repeat the test five times */ if (atomic) { unsigned int key = irq_lock(); k_cpu_atomic_idle(key); } else { k_cpu_idle(); } /* calculating milliseconds per tick*/ tms += sys_clock_us_per_tick / USEC_PER_MSEC; tms2 = k_uptime_get_32(); if (tms2 < tms) { TC_ERROR("Bad ms per tick value computed, got %d which is less than %d\n", tms2, tms); return TC_FAIL; } } return TC_PASS; }
void main(void) { int status = TC_FAIL; u32_t start_tick; u32_t end_tick; TC_START("Test kernel Sleep and Wakeup APIs\n"); test_objects_init(); test_thread_id = k_thread_create(&test_thread_data, test_thread_stack, THREAD_STACK, (k_thread_entry_t) test_thread, 0, 0, NULL, TEST_THREAD_PRIORITY, 0, 0); TC_PRINT("Test thread started: id = %p\n", test_thread_id); helper_thread_id = k_thread_create(&helper_thread_data, helper_thread_stack, THREAD_STACK, (k_thread_entry_t) helper_thread, 0, 0, NULL, HELPER_THREAD_PRIORITY, 0, 0); TC_PRINT("Helper thread started: id = %p\n", helper_thread_id); /* Activate test_thread */ k_sem_give(&test_thread_sem); /* Wait for test_thread to activate us */ k_sem_take(&task_sem, K_FOREVER); /* Wake the test fiber */ k_wakeup(test_thread_id); if (test_failure) { goto done_tests; } TC_PRINT("Testing kernel k_sleep()\n"); align_to_tick_boundary(); start_tick = k_uptime_get_32(); /* FIXME: one tick less to account for * one extra tick for _TICK_ALIGN in k_sleep*/ k_sleep(ONE_SECOND - TICKS_PER_MS); end_tick = k_uptime_get_32(); if (!sleep_time_valid(start_tick, end_tick, ONE_SECOND)) { TC_ERROR("k_sleep() slept for %d ticks, not %d\n", end_tick - start_tick, ONE_SECOND); goto done_tests; } status = TC_PASS; done_tests: TC_END_REPORT(status); }
static void align_to_tick_boundary(void) { u32_t tick; tick = k_uptime_get_32(); while (k_uptime_get_32() == tick) { /* Busy wait to align to tick boundary */ } }
static void beacon_complete(int err, void *user_data) { struct bt_mesh_subnet *sub = user_data; BT_DBG("err %d", err); sub->beacon_sent = k_uptime_get_32(); }
static int shell_cmd_uptime(int argc, char *argv[]) { ARG_UNUSED(argc); ARG_UNUSED(argv); printk("uptime: %u ms\n", k_uptime_get_32()); return 0; }
void dtls_timing_set_delay(void *data, u32_t int_ms, u32_t fin_ms) { struct dtls_timing_context *ctx = (struct dtls_timing_context *)data; ctx->int_ms = int_ms; ctx->fin_ms = fin_ms; if (fin_ms != 0) { ctx->snapshot = k_uptime_get_32(); } }
static int iwdg_stm32_install_timeout(struct device *dev, const struct wdt_timeout_cfg *config) { IWDG_TypeDef *iwdg = IWDG_STM32_STRUCT(dev); u32_t timeout = config->window.max * USEC_PER_MSEC; u32_t prescaler = 0U; u32_t reload = 0U; u32_t tickstart; if (config->callback != NULL) { return -ENOTSUP; } iwdg_stm32_convert_timeout(timeout, &prescaler, &reload); if (IS_IWDG_TIMEOUT(timeout) || IS_IWDG_PRESCALER(prescaler) || IS_IWDG_RELOAD(reload)) { /* One of the parameters provided is invalid */ return -EINVAL; } tickstart = k_uptime_get_32(); while (LL_IWDG_IsReady(iwdg) == 0) { /* Wait untill WVU, RVU, PVU are reset before updating */ if ((k_uptime_get_32() - tickstart) > IWDG_DEFAULT_TIMEOUT) { return -ENODEV; } } LL_IWDG_EnableWriteAccess(iwdg); LL_IWDG_SetPrescaler(iwdg, prescaler); LL_IWDG_SetReloadCounter(iwdg, reload); return 0; }
int dtls_timing_get_delay(void *data) { struct dtls_timing_context *ctx = (struct dtls_timing_context *)data; unsigned long elapsed_ms; if (ctx->fin_ms == 0) { return -1; } elapsed_ms = k_uptime_get_32() - ctx->snapshot; if (elapsed_ms >= ctx->fin_ms) { return 2; } if (elapsed_ms >= ctx->int_ms) { return 1; } return 0; }
static int secure_beacon_send(void) { static const struct bt_mesh_send_cb send_cb = { .end = beacon_complete, }; u32_t now = k_uptime_get_32(); int i; BT_DBG(""); for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; struct net_buf *buf; u32_t time_diff; if (sub->net_idx == BT_MESH_KEY_UNUSED) { continue; } time_diff = now - sub->beacon_sent; if (time_diff < K_SECONDS(600) && time_diff < BEACON_THRESHOLD(sub)) { continue; } buf = bt_mesh_adv_create(BT_MESH_ADV_BEACON, PROV_XMIT_COUNT, PROV_XMIT_INT, K_NO_WAIT); if (!buf) { BT_ERR("Unable to allocate beacon buffer"); return -ENOBUFS; } bt_mesh_beacon_create(sub, &buf->b); bt_mesh_adv_send(buf, &send_cb, sub); net_buf_unref(buf); } return 0; }
/** * @brief This function configures the source of stm32cube time base. * Cube HAL expects a 1ms tick which matches with k_uptime_get_32. * Tick interrupt priority is not used * @return HAL status */ uint32_t HAL_GetTick(void) { return k_uptime_get_32(); }
static void test_thread(int arg1, int arg2) { u32_t start_tick; u32_t end_tick; k_sem_take(&test_thread_sem, K_FOREVER); TC_PRINT("Testing normal expiration of k_sleep()\n"); align_to_tick_boundary(); start_tick = k_uptime_get_32(); /* FIXME: one tick less to account for * one extra tick for _TICK_ALIGN in k_sleep*/ k_sleep(ONE_SECOND - TICKS_PER_MS); end_tick = k_uptime_get_32(); if (!sleep_time_valid(start_tick, end_tick, ONE_SECOND)) { TC_ERROR(" *** k_sleep() slept for %d ticks not %d.", end_tick - start_tick, ONE_SECOND); return; } TC_PRINT("Testing: test thread sleep + helper thread wakeup test\n"); k_sem_give(&helper_thread_sem); /* Activate helper fiber */ align_to_tick_boundary(); start_tick = k_uptime_get_32(); /* FIXME: one tick less to account for * one extra tick for _TICK_ALIGN in k_sleep*/ k_sleep(ONE_SECOND - TICKS_PER_MS); end_tick = k_uptime_get_32(); if (end_tick - start_tick > 1) { TC_ERROR(" *** k_wakeup() took too long (%d ticks) \n", end_tick - start_tick); return; } TC_PRINT("Testing: test thread sleep + isr offload wakeup test\n"); k_sem_give(&helper_thread_sem); /* Activate helper fiber */ align_to_tick_boundary(); start_tick = k_uptime_get_32(); /* FIXME: one tick less to account for * one extra tick for _TICK_ALIGN in k_sleep*/ k_sleep(ONE_SECOND - TICKS_PER_MS); end_tick = k_uptime_get_32(); if (end_tick - start_tick > 1) { TC_ERROR(" *** k_wakeup() took too long (%d ticks)\n", end_tick - start_tick); return; } TC_PRINT("Testing: test thread sleep + main wakeup test thread\n"); k_sem_give(&task_sem); /* Activate task */ align_to_tick_boundary(); start_tick = k_uptime_get_32(); /* FIXME: one tick less to account for * one extra tick for _TICK_ALIGN in k_sleep*/ k_sleep(ONE_SECOND - TICKS_PER_MS); /* Task will execute */ end_tick = k_uptime_get_32(); if (end_tick - start_tick > 1) { TC_ERROR(" *** k_wakeup() took too long (%d ticks) at LAST\n", end_tick - start_tick); return; } test_failure = false; }