/** * Set a timer. * * This function is used to set a timer for a time sometime in the * future. The function timer_expired() will evaluate to true after * the timer has expired. * * \param t A pointer to the timer * \param interval The interval before the timer expires. * */ void timer_set(struct timer *t, clock_time_t interval) { do_init(t); if (t->started) { timer_stop(t); } switch (sys_execution_context_type_get()) { case NANO_CTX_FIBER: nano_fiber_timer_start(&t->nano_timer, interval); break; case NANO_CTX_TASK: nano_task_timer_start(&t->nano_timer, interval); break; default: return; } PRINTF("%s():%d timer %p started interval %d\n", __FUNCTION__, __LINE__, t, interval); t->started = true; t->interval = interval; t->start = clock_time(); t->triggered = false; }
void main(void) { uint32_t timer_data[2] = {0, 0}; struct device *aon_gpio; struct nano_timer timer; nano_timer_init(&timer, timer_data); aon_gpio = device_get_binding("GPIO_AON_0"); if (!aon_gpio) { printf("aon_gpio device not found.\n"); return; } ipm = device_get_binding("bmi160_ipm"); if (!ipm) { printf("ipm device not found.\n"); return; } gpio_init_callback(&cb, aon_gpio_callback, BIT(BMI160_INTERRUPT_PIN)); gpio_add_callback(aon_gpio, &cb); gpio_pin_configure(aon_gpio, BMI160_INTERRUPT_PIN, GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW | GPIO_INT_DEBOUNCE); gpio_pin_enable_callback(aon_gpio, BMI160_INTERRUPT_PIN); while (1) { nano_task_timer_start(&timer, SLEEPTIME); nano_task_timer_test(&timer, TICKS_UNLIMITED); } }
/** * Restart the timer from the current point in time * * This function restarts a timer with the same interval that was * given to the timer_set() function. The timer will start at the * current time. * * \note A periodic timer will drift if this function is used to reset * it. For preioric timers, use the timer_reset() function instead. * * \param t A pointer to the timer. * * \sa timer_reset() */ void timer_restart(struct timer *t) { do_init(t); if (t->started) { timer_stop(t); } switch (sys_execution_context_type_get()) { case NANO_CTX_FIBER: nano_fiber_timer_start(&t->nano_timer, t->interval); break; case NANO_CTX_TASK: nano_task_timer_start(&t->nano_timer, t->interval); break; default: return; } t->started = true; t->start = clock_time(); t->triggered = false; }
static void test_polling_mode(struct device *bmg160) { uint32_t timer_data[2] = {0, 0}; int32_t remaining_test_time = MAX_TEST_TIME; struct nano_timer timer; nano_timer_init(&timer, timer_data); do { if (sensor_sample_fetch(bmg160) < 0) { printf("Gyro sample update error.\n"); } print_gyro_data(bmg160); print_temp_data(bmg160); /* wait a while */ nano_task_timer_start(&timer, SLEEPTIME); nano_task_timer_test(&timer, TICKS_UNLIMITED); remaining_test_time -= SLEEPTIME; } while (remaining_test_time > 0); }
void main(void) { struct nano_timer timer; uint32_t data[2] = {0, 0}; task_fiber_start(&fiberStack[0], STACKSIZE, (nano_fiber_entry_t) fiberEntry, 0, 0, 7, 0); nano_sem_init(&nanoSemTask); nano_timer_init(&timer, data); while (1) { /* say "hello" */ PRINT("%s: Hello Screen!\n", __FUNCTION__); /* wait a while, then let fiber have a turn */ nano_task_timer_start(&timer, SLEEPTICKS); nano_task_timer_wait(&timer); nano_task_sem_give(&nanoSemFiber); /* now wait for fiber to let us have a turn */ nano_task_sem_take_wait(&nanoSemTask); } }
void main(void) { struct nano_timer timer; uint32_t timer_data[2] = {0, 0}; struct device *glcd; char str[20]; int rgb[] = { 0x0, 0x0, 0x0 }; uint8_t rgb_chg[3]; const uint8_t rgb_step = 16; uint8_t set_config; int i, j, m; int cnt; nano_timer_init(&timer, timer_data); glcd = device_get_binding(GROVE_LCD_NAME); if (!glcd) { printk("Grove LCD: Device not found.\n"); } /* Now configure the LCD the way we want it */ set_config = GLCD_FS_ROWS_2 | GLCD_FS_DOT_SIZE_LITTLE | GLCD_FS_8BIT_MODE; glcd_function_set(glcd, set_config); set_config = GLCD_DS_DISPLAY_ON | GLCD_DS_CURSOR_ON | GLCD_DS_BLINK_ON; glcd_display_state_set(glcd, set_config); /* Setup variables /*/ for (i = 0; i < sizeof(str); i++) { str[i] = '\0'; } /* Starting counting */ cnt = 0; while (1) { glcd_cursor_pos_set(glcd, 0, 0); /* RGB values are from 0 - 511. * First half means incrementally brighter. * Second half is opposite (i.e. goes darker). */ /* Update the RGB values for backlight */ m = (rgb[2] > 255) ? (512 - rgb[2]) : (rgb[2]); rgb_chg[2] = clamp_rgb(m); m = (rgb[1] > 255) ? (512 - rgb[1]) : (rgb[1]); rgb_chg[1] = clamp_rgb(m); m = (rgb[0] > 255) ? (512 - rgb[0]) : (rgb[0]); rgb_chg[0] = clamp_rgb(m); glcd_color_set(glcd, rgb_chg[0], rgb_chg[1], rgb_chg[2]); /* Display the counter * * well... sprintf() might be easier, * but this is more fun. */ m = cnt; i = 1000000000; j = 0; while (i > 0) { str[j] = '0' + (m / i); m = m % i; i = i / 10; j++; } cnt++; glcd_print(glcd, str, j); /* Rotate RGB values */ rgb[2] += rgb_step; if (rgb[2] > 511) { rgb[2] = 0; rgb[1] += rgb_step; } if (rgb[1] > 511) { rgb[1] = 0; rgb[0] += rgb_step; } if (rgb[0] > 511) { rgb[0] = 0; } /* wait a while */ nano_task_timer_start(&timer, SLEEPTICKS); nano_task_timer_test(&timer, TICKS_UNLIMITED); } }
static void test_trigger_mode(struct device *bmg160) { uint32_t timer_data[2] = {0, 0}; int32_t remaining_test_time = MAX_TEST_TIME; struct nano_timer timer; struct sensor_trigger trig; struct sensor_value attr; nano_timer_init(&timer, timer_data); trig.type = SENSOR_TRIG_DELTA; trig.chan = SENSOR_CHAN_GYRO_ANY; printf("Gyro: Testing anymotion trigger.\n"); /* set up the trigger */ /* set slope threshold to 10 dps */ sensor_degrees_to_rad(10, &attr); /* convert to rad/s */ if (sensor_attr_set(bmg160, SENSOR_CHAN_GYRO_ANY, SENSOR_ATTR_SLOPE_TH, &attr) < 0) { printf("Gyro: cannot set slope threshold.\n"); return; } /* set slope duration to 4 samples */ attr.type = SENSOR_VALUE_TYPE_INT; attr.val1 = 4; if (sensor_attr_set(bmg160, SENSOR_CHAN_GYRO_ANY, SENSOR_ATTR_SLOPE_DUR, &attr) < 0) { printf("Gyro: cannot set slope duration.\n"); return; } if (sensor_trigger_set(bmg160, &trig, trigger_handler) < 0) { printf("Gyro: cannot set trigger.\n"); return; } printf("Gyro: rotate the device and wait for events.\n"); do { nano_task_timer_start(&timer, SLEEPTIME); nano_task_timer_test(&timer, TICKS_UNLIMITED); remaining_test_time -= SLEEPTIME; } while (remaining_test_time > 0); if (sensor_trigger_set(bmg160, &trig, NULL) < 0) { printf("Gyro: cannot clear trigger.\n"); return; } printf("Gyro: Anymotion trigger test finished.\n"); printf("Gyro: Testing data ready trigger.\n"); attr.type = SENSOR_VALUE_TYPE_INT; attr.val1 = 100; attr.val2 = 0; if (sensor_attr_set(bmg160, SENSOR_CHAN_GYRO_ANY, SENSOR_ATTR_SAMPLING_FREQUENCY, &attr) < 0) { printf("Gyro: cannot set sampling frequency.\n"); return; } trig.type = SENSOR_TRIG_DATA_READY; trig.chan = SENSOR_CHAN_GYRO_ANY; if (sensor_trigger_set(bmg160, &trig, trigger_handler) < 0) { printf("Gyro: cannot set trigger.\n"); return; } remaining_test_time = MAX_TEST_TIME; do { nano_task_timer_start(&timer, SLEEPTIME); nano_task_timer_test(&timer, TICKS_UNLIMITED); remaining_test_time -= SLEEPTIME; } while (remaining_test_time > 0); if (sensor_trigger_set(bmg160, &trig, NULL) < 0) { printf("Gyro: cannot clear trigger.\n"); return; } printf("Gyro: Data ready trigger test finished.\n"); }