static void threads_suspend_resume(int prio) { int old_prio = k_thread_priority_get(k_current_get()); /* set current thread */ last_prio = prio; k_thread_priority_set(k_current_get(), last_prio); /* spawn thread with lower priority */ int spawn_prio = last_prio + 1; k_tid_t tid = k_thread_spawn(tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, spawn_prio, 0, 0); /* checkpoint: suspend current thread */ k_thread_suspend(tid); k_sleep(100); /* checkpoint: spawned thread shouldn't be executed after suspend */ assert_false(last_prio == spawn_prio, NULL); k_thread_resume(tid); k_sleep(100); /* checkpoint: spawned thread should be executed after resume */ assert_true(last_prio == spawn_prio, NULL); k_thread_abort(tid); /* restore environment */ k_thread_priority_set(k_current_get(), old_prio); }
void main(void) { struct device *ipm; int rc; uint32_t value32; rc = bt_enable(bt_ready); if (rc) { printk("Bluetooth init failed (err %d)\n", rc); return; } bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&auth_cb_display); ipm = device_get_binding("power_ipm"); ipm_register_callback(ipm, sensor_ipm_callback, NULL); ipm_set_enabled(ipm, 1); while (1) { if (default_conn) { value32 = sys_cpu_to_le32(consumption_value); bt_gatt_notify(default_conn, &attrs[2], &value32, sizeof(value32)); k_sleep(INTERVAL); value32 = sys_cpu_to_le32(solar_value); bt_gatt_notify(default_conn, &attrs[6], &value32, sizeof(value32)); } k_sleep(INTERVAL); } }
static void test_polling_mode(struct device *bmi160) { s32_t remaining_test_time = MAX_TEST_TIME; struct sensor_value attr; #if defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME) /* set sampling frequency to 100Hz for accel */ attr.val1 = 100; attr.val2 = 0; if (sensor_attr_set(bmi160, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY, &attr) < 0) { printk("Cannot set sampling frequency for accelerometer.\n"); return; } #endif #if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME) /* set sampling frequency to 3200Hz for gyro */ attr.val1 = 3200; attr.val2 = 0; if (sensor_attr_set(bmi160, SENSOR_CHAN_GYRO_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY, &attr) < 0) { printk("Cannot set sampling frequency for gyroscope.\n"); return; } #endif /* wait for the change to take effect */ k_sleep(SLEEPTIME); /* poll the data and print it */ do { if (sensor_sample_fetch(bmi160) < 0) { printk("Sample update error.\n"); return; } #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND) print_gyro_data(bmi160); #endif #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND) print_accel_data(bmi160); #endif print_temp_data(bmi160); /* wait a while */ k_sleep(SLEEPTIME); remaining_test_time -= SLEEPTIME; } while (remaining_test_time > 0); }
void main(void) { struct device *gpiob; printk("Press the user defined button on the board\n"); gpiob = device_get_binding(PORT); if (!gpiob) { printk("error\n"); return; } gpio_pin_configure(gpiob, PIN, GPIO_DIR_IN | GPIO_INT | PULL_UP | EDGE); gpio_init_callback(&gpio_cb, button_pressed, BIT(PIN)); gpio_add_callback(gpiob, &gpio_cb); gpio_pin_enable_callback(gpiob, PIN); while (1) { u32_t val = 0; gpio_pin_read(gpiob, PIN, &val); k_sleep(SLEEP_TIME); } }
void main(void) { printk("Quark SE: Power Management sample application\n"); #if (CONFIG_RTC) setup_rtc(); #elif (CONFIG_COUNTER) setup_counter(); #elif (CONFIG_GPIO_QMSI_1) setup_aon_gpio(); #endif build_suspend_device_list(); #ifdef CONFIG_SOC_WATCH /* Start the event monitoring thread */ soc_watch_logger_thread_start(); #endif /* All our application does is putting the task to sleep so the kernel * triggers the suspend operation. */ while (1) { k_sleep(TIMEOUT * 1000); printk("Back to the application\n"); } }
static void test_data_ready_trigger(struct device *bmi160) { s32_t remaining_test_time = MAX_TEST_TIME; struct sensor_trigger trig; /* enable data ready trigger */ trig.type = SENSOR_TRIG_DATA_READY; trig.chan = SENSOR_CHAN_ACCEL_XYZ; if (sensor_trigger_set(bmi160, &trig, trigger_hdlr) < 0) { printk("Cannot enable data ready trigger.\n"); return; } printk("Data ready test:\n"); do { /* wait a while */ k_sleep(SLEEPTIME); remaining_test_time -= SLEEPTIME; } while (remaining_test_time > 0); printk("Data ready test: finished, removing data ready trigger...\n"); if (sensor_trigger_set(bmi160, &trig, NULL) < 0) { printk("Cannot remove data ready trigger.\n"); return; } }
static int network_setup(void) { #if defined(CONFIG_NET_L2_BT) const char *progress_mark = "/-\\|"; int i = 0; int rc; rc = bt_enable(NULL); if (rc) { printk("bluetooth init failed\n"); return rc; } bt_conn_cb_register(&bt_conn_cb); printk("\nwaiting for bt connection: "); while (bt_connected == false) { k_sleep(250); printk("%c\b", progress_mark[i]); i = (i + 1) % (sizeof(progress_mark) - 1); } printk("\n"); #endif return 0; }
void test_priority_preemptible(void) { int old_prio = k_thread_priority_get(k_current_get()); /* set current thread to a non-negative priority */ last_prio = 2; k_thread_priority_set(k_current_get(), last_prio); int spawn_prio = last_prio - 1; k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, spawn_prio, 0, 0); /* checkpoint: thread is preempted by higher thread */ zassert_true(last_prio == spawn_prio, NULL); k_sleep(100); k_thread_abort(tid); spawn_prio = last_prio + 1; tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, spawn_prio, 0, 0); /* checkpoint: thread is not preempted by lower thread */ zassert_false(last_prio == spawn_prio, NULL); k_thread_abort(tid); /* restore environment */ k_thread_priority_set(k_current_get(), old_prio); }
void main(void) { int err; err = bt_enable(bt_ready); if (err) { printk("Bluetooth init failed (err %d)\n", err); return; } bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&auth_cb_display); /* Implement notification. At the moment there is no suitable way * of starting delayed work so we do it here */ while (1) { k_sleep(MSEC_PER_SEC); /* Heartrate measurements simulation */ hrs_notify(); /* Battery level simulation */ bas_notify(); } }
static void trigger_callback(struct device *dev, int enable_cb) { gpio_pin_write(dev, PIN_OUT, 0); k_sleep(100); cb_cnt[0] = 0; cb_cnt[1] = 0; if (enable_cb == 1) { gpio_pin_enable_callback(dev, PIN_IN); } else { gpio_pin_disable_callback(dev, PIN_IN); } k_sleep(100); gpio_pin_write(dev, PIN_OUT, 1); k_sleep(1000); }
static int apds9960_init(struct device *dev) { struct apds9960_data *data = dev->driver_data; /* Initialize time 5.7ms */ k_sleep(6); data->i2c = device_get_binding(DT_AVAGO_APDS9960_0_BUS_NAME); if (data->i2c == NULL) { LOG_ERR("Failed to get pointer to %s device!", DT_AVAGO_APDS9960_0_BUS_NAME); return -EINVAL; } (void)memset(data->sample_crgb, 0, sizeof(data->sample_crgb)); data->pdata = 0U; apds9960_sensor_setup(dev); if (apds9960_init_interrupt(dev) < 0) { LOG_ERR("Failed to initialize interrupt!"); return -EIO; } return 0; }
static void test_anymotion_trigger(struct device *bmi160) { s32_t remaining_test_time = MAX_TEST_TIME; struct sensor_value attr; struct sensor_trigger trig; /* set up anymotion trigger */ /* * Set slope threshold to 0.1G (0.1 * 9.80665 = 4.903325 m/s^2). * This depends on the chosen range. One cannot choose a threshold * bigger than half the range. For example, for a 16G range, the * threshold must not exceed 8G. */ attr.val1 = 0; attr.val2 = 980665; if (sensor_attr_set(bmi160, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_SLOPE_TH, &attr) < 0) { printk("Cannot set anymotion slope threshold.\n"); return; } /* * Set slope duration to 2 consecutive samples (after which the * anymotion interrupt will trigger. * * Allowed values are from 1 to 4. */ attr.val1 = 2; attr.val2 = 0; if (sensor_attr_set(bmi160, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_SLOPE_DUR, &attr) < 0) { printk("Cannot set anymotion slope duration.\n"); return; } /* enable anymotion trigger */ trig.type = SENSOR_TRIG_DELTA; trig.chan = SENSOR_CHAN_ACCEL_XYZ; if (sensor_trigger_set(bmi160, &trig, trigger_hdlr) < 0) { printk("Cannot enable anymotion trigger.\n"); return; } printk("Anymotion test: shake the device to get anymotion events.\n"); do { /* wait a while */ k_sleep(SLEEPTIME); remaining_test_time -= SLEEPTIME; } while (remaining_test_time > 0); printk("Anymotion test: finished, removing anymotion trigger...\n"); if (sensor_trigger_set(bmi160, &trig, NULL) < 0) { printk("Cannot remove anymotion trigger.\n"); return; } }
static int dma_stm32_disable_stream(struct dma_stm32_device *ddata, u32_t id) { u32_t config; int count = 0; int ret = 0; for (;;) { config = dma_stm32_read(ddata, DMA_STM32_SCR(id)); /* Stream already disabled */ if (!(config & DMA_STM32_SCR_EN)) { return 0; } /* Try to disable stream */ dma_stm32_write(ddata, DMA_STM32_SCR(id), config &= ~DMA_STM32_SCR_EN); /* After trying for 5 seconds, give up */ k_sleep(K_SECONDS(5)); if (count++ > (5 * 1000) / 50) { SYS_LOG_ERR("DMA error: Stream in use\n"); return -EBUSY; } } return ret; }
static int test_task(uint32_t chan_id, uint32_t blen) { enum dma_burst_length burst_len = BURST_TRANS_LENGTH_1; struct dma_channel_config dma_chan_cfg = {0}; struct dma_transfer_config dma_trans = {0}; struct device *dma = device_get_binding(DMA_DEVICE_NAME); if (!dma) { TC_PRINT("Cannot get dma controller\n"); return TC_FAIL; } switch (blen) { case 8: burst_len = BURST_TRANS_LENGTH_8; break; case 16: burst_len = BURST_TRANS_LENGTH_16; break; default: burst_len = BURST_TRANS_LENGTH_1; } dma_chan_cfg.channel_direction = MEMORY_TO_MEMORY; dma_chan_cfg.source_transfer_width = TRANS_WIDTH_8; dma_chan_cfg.destination_transfer_width = TRANS_WIDTH_8; dma_chan_cfg.source_burst_length = burst_len; dma_chan_cfg.destination_burst_length = burst_len; dma_chan_cfg.dma_transfer = test_done; dma_chan_cfg.dma_error = test_error; dma_chan_cfg.callback_data = (void *)&chan_id; TC_PRINT("Preparing DMA Controller: Chan_ID=%u, BURST_LEN=%u\n", chan_id, burst_len); if (dma_channel_config(dma, chan_id, &dma_chan_cfg)) { TC_PRINT("Error: configuration\n"); return TC_FAIL; } TC_PRINT("Starting the transfer\n"); dma_trans.block_size = strlen(tx_data); dma_trans.source_address = (uint32_t *)tx_data; dma_trans.destination_address = (uint32_t *)rx_data; if (dma_transfer_config(dma, chan_id, &dma_trans)) { TC_PRINT("ERROR: transfer\n"); return TC_FAIL; } if (dma_transfer_start(dma, chan_id)) { TC_PRINT("ERROR: transfer\n"); return TC_FAIL; } k_sleep(2000); TC_PRINT("%s\n", rx_data); if (strcmp(tx_data, rx_data) != 0) return TC_FAIL; return TC_PASS; }
static int i2s_stm32_set_clock(struct device *dev, u32_t bit_clk_freq) { const struct i2s_stm32_cfg *cfg = DEV_CFG(dev); u32_t pll_src = LL_RCC_PLL_GetMainSource(); int freq_in; u8_t i2s_div, i2s_odd; freq_in = (pll_src == LL_RCC_PLLSOURCE_HSI) ? HSI_VALUE : CONFIG_CLOCK_STM32_HSE_CLOCK; #ifdef CONFIG_I2S_STM32_USE_PLLI2S_ENABLE /* Set PLLI2S */ LL_RCC_PLLI2S_Disable(); LL_RCC_PLLI2S_ConfigDomain_I2S(pll_src, CONFIG_I2S_STM32_PLLI2S_PLLM, CONFIG_I2S_STM32_PLLI2S_PLLN, pllr(CONFIG_I2S_STM32_PLLI2S_PLLR)); LL_RCC_PLLI2S_Enable(); /* wait until PLLI2S gets locked */ while (!LL_RCC_PLLI2S_IsReady()) { if (plli2s_ms_count++ > PLLI2S_MAX_MS_TIME) { return -EIO; } /* wait 1 ms */ k_sleep(1); } LOG_DBG("PLLI2S is locked"); /* Adjust freq_in according to PLLM, PLLN, PLLR */ float freq_tmp; freq_tmp = freq_in / CONFIG_I2S_STM32_PLLI2S_PLLM; freq_tmp *= CONFIG_I2S_STM32_PLLI2S_PLLN; freq_tmp /= CONFIG_I2S_STM32_PLLI2S_PLLR; freq_in = (int) freq_tmp; #endif /* CONFIG_I2S_STM32_USE_PLLI2S_ENABLE */ /* Select clock source */ LL_RCC_SetI2SClockSource(cfg->i2s_clk_sel); /* * The ratio between input clock (I2SxClk) and output * clock on the pad (I2S_CK) is obtained using the * following formula: * (i2s_div * 2) + i2s_odd */ i2s_div = div_round_closest(freq_in, bit_clk_freq); i2s_odd = (i2s_div & 0x1) ? 1 : 0; i2s_div >>= 1; LOG_DBG("i2s_div: %d - i2s_odd: %d", i2s_div, i2s_odd); LL_I2S_SetPrescalerLinear(cfg->i2s, i2s_div); LL_I2S_SetPrescalerParity(cfg->i2s, i2s_odd); return 0; }
static void isr_alert(void) { handler_executed = 0; /**TESTPOINT: thread-isr sync via alert*/ irq_offload(tIsr_entry, NULL); k_sleep(TIMEOUT); alert_recv(); }
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 eswifi_spi_poll_thread(void *p1) { struct eswifi_dev *eswifi = p1; while (1) { k_sleep(K_MSEC(1000)); eswifi_spi_read_msg(eswifi); } }
void panic(const char *msg) { if (msg) { NET_ERR("%s", msg); } for (;;) { k_sleep(K_FOREVER); } }
static void tmutex_test_lock_timeout(struct k_mutex *pmutex, void (*entry_fn)(void *, void *, void *)) { /**TESTPOINT: test k_mutex_init mutex*/ k_mutex_init(pmutex); k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, entry_fn, pmutex, NULL, NULL, K_PRIO_PREEMPT(0), 0, 0); k_mutex_lock(pmutex, K_FOREVER); TC_PRINT("access resource from main thread\n"); /* wait for spawn thread to take action */ k_sleep(TIMEOUT); k_mutex_unlock(pmutex); k_sleep(TIMEOUT); /* teardown */ k_thread_abort(tid); }
void test_threads_abort_self(void) { execute_flag = 0; k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry_abort, NULL, NULL, NULL, 0, 0, 0); k_sleep(100); /**TESTPOINT: spawned thread executed but abort itself*/ zassert_true(execute_flag == 1, NULL); k_thread_abort(tid); }
static void thread_alert(void) { handler_executed = 0; /**TESTPOINT: thread-thread sync via alert*/ k_tid_t tid = k_thread_spawn(tstack, STACK_SIZE, tThread_entry, NULL, NULL, NULL, K_PRIO_PREEMPT(0), 0, 0); alert_send(); k_sleep(TIMEOUT); k_thread_abort(tid); }
static int eswifi_spi_wait_cmddata_ready(struct eswifi_spi_data *spi) { unsigned int max_retries = 60 * 1000; /* 1 minute */ do { /* allow other threads to be scheduled */ k_sleep(1); } while (!eswifi_spi_cmddata_ready(spi) && --max_retries); return max_retries ? 0 : -ETIMEDOUT; }
static void tx_thread(void) { BT_DBG(""); /* FIXME: make periodic sending */ h5_send(sync_req, HCI_3WIRE_LINK_PKT, sizeof(sync_req)); while (true) { struct net_buf *buf; u8_t type; BT_DBG("link_state %u", h5.link_state); switch (h5.link_state) { case UNINIT: /* FIXME: send sync */ k_sleep(100); break; case INIT: /* FIXME: send conf */ k_sleep(100); break; case ACTIVE: buf = net_buf_get(&h5.tx_queue, K_FOREVER); type = h5_get_type(buf); h5_send(buf->data, type, buf->len); /* buf is dequeued from tx_queue and queued to unack * queue. */ net_buf_put(&h5.unack_queue, buf); unack_queue_len++; k_delayed_work_submit(&retx_work, H5_TX_ACK_TIMEOUT); break; } } }
void main(void) { struct device *ipm; int rc; uint16_t value16; uint32_t value32; rc = bt_enable(bt_ready); if (rc) { printk("Bluetooth init failed (err %d)\n", rc); return; } bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&auth_cb_display); ipm = device_get_binding("ess_ipm"); ipm_register_callback(ipm, sensor_ipm_callback, NULL); ipm_set_enabled(ipm, 1); while (1) { /* Notify value changes via BLE here so that the notification intervals can be controlled. */ if (default_conn) { value16 = sys_cpu_to_le16(temp_value); bt_gatt_notify(default_conn, &attrs[2], &value16, sizeof(value16)); k_sleep(INTERVAL); value16 = sys_cpu_to_le16(humidity_value); bt_gatt_notify(default_conn, &attrs[6], &value16, sizeof(value16)); k_sleep(INTERVAL); value32 = sys_cpu_to_le32(pressure_value); bt_gatt_notify(default_conn, &attrs[10], &value32, sizeof(value32)); k_sleep(INTERVAL); value16 = sys_cpu_to_le16(uv_index_value); bt_gatt_notify(default_conn, &attrs[14], &uv_index_value, sizeof(uv_index_value)); } k_sleep(INTERVAL); } }
void main(void) { struct device *dev = device_get_binding("MCP9808"); if (dev == NULL) { printf("device not found. aborting test.\n"); return; } #ifdef DEBUG printf("dev %p\n", dev); printf("dev %p name %s\n", dev, dev->config->name); #endif #ifdef CONFIG_MCP9808_TRIGGER struct sensor_value val; struct sensor_trigger trig; val.type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO; val.val1 = 26; val.val2 = 0; sensor_attr_set(dev, SENSOR_CHAN_TEMP, SENSOR_ATTR_UPPER_THRESH, &val); trig.type = SENSOR_TRIG_THRESHOLD; trig.chan = SENSOR_CHAN_TEMP; sensor_trigger_set(dev, &trig, trigger_handler); #endif while (1) { struct sensor_value temp; int rc; rc = sensor_sample_fetch(dev); if (rc != 0) { printf("sensor_sample_fetch error: %d\n", rc); break; } rc = sensor_channel_get(dev, SENSOR_CHAN_TEMP, &temp); if (rc != 0) { printf("sensor_channel_get error: %d\n", rc); break; } printf("temp: %d.%06d\n", temp.val1, temp.val2); k_sleep(2000); } }
void test_threads_abort_others(void) { execute_flag = 0; k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, 0, 0, 0); k_thread_abort(tid); k_sleep(100); /**TESTPOINT: check not-started thread is aborted*/ zassert_true(execute_flag == 0, NULL); tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, 0, 0, 0); k_sleep(50); k_thread_abort(tid); /**TESTPOINT: check running thread is aborted*/ zassert_true(execute_flag == 1, NULL); k_sleep(1000); zassert_true(execute_flag == 1, NULL); }
void test_pipe_block_put(void) { /**TESTPOINT: test k_pipe_block_put without semaphore*/ k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, tThread_block_put, &kpipe, NULL, NULL, K_PRIO_PREEMPT(0), 0, 0); k_sleep(10); tpipe_get(&kpipe); k_sem_take(&end_sema, K_FOREVER); k_thread_abort(tid); }
void main(void) { printk("Power Management Demo on %s\n", CONFIG_ARCH); setup_rtc(); create_device_list(); while (1) { printk("\nApplication main thread\n"); k_sleep(SECONDS_TO_SLEEP * 1000); } }
void test_pipe_block_put_sema(void) { struct k_sem sync_sema; k_sem_init(&sync_sema, 0, 1); /**TESTPOINT: test k_pipe_block_put with semaphore*/ k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, tThread_block_put, &pipe, &sync_sema, NULL, K_PRIO_PREEMPT(0), 0, 0); k_sleep(10); tpipe_get(&pipe); k_sem_take(&end_sema, K_FOREVER); k_thread_abort(tid); }