美文网首页Zephyr OS 学习笔记
Zephyr OS 学习笔记1-2:延时与流水灯

Zephyr OS 学习笔记1-2:延时与流水灯

作者: 惊起却回首 | 来源:发表于2019-03-15 12:23 被阅读0次

参考文章:http://www.cnblogs.com/arnoldlu/p/7657571.html

#include <zephyr.h>
#include <device.h>
#include <gpio.h>

#define LED_PORT LED0_GPIO_CONTROLLER
#define LED LED0_GPIO_PIN

/*单位是毫秒*/
#define SLEEP_TIME  1000

void main(void)
{
    int cnt = 0;
    struct device *dev;
    dev = device_get_binding(LED_PORT);
    gpio_pin_configure(dev, LED, GPIO_DIR_OUT);
    while (1) {
        gpio_pin_write(dev, LED, cnt % 2);
        cnt++;
        k_sleep(SLEEP_TIME);//将当前线程休眠1秒
    }
}

k_sleep是系统内核(kernel)的一个函数,表示将当前线程休眠,单位是毫秒,精度比较低
相应的用k_busy_wait延时,则不会将CPU移交给系统

Zephyr中的时钟与定时器

参考了Zephyr中文文档

一、时钟系统

1.系统时钟

64位的 系统时钟(system clock) 是一个通过 嘀嗒(ticks) 来测量内核自初始化以来经历了多少时间的计数器,操作系统将CPU运行时间划分为很多时间片,每个时间片就是一个嘀嗒,嘀嗒的时长是可配置的,其典型值为1~100毫秒

如何配置时间片?

打开zephyrproject\zephyr\samples\basic\myblinkled\build\zephyr\misc\generated\configs.c
找到

GEN_ABSOLUTE_SYM(CONFIG_SYS_CLOCK_TICKS_PER_SEC, 100);

可知默认的时间片是1/100秒,即10ms,如果这个值设置太大,进程调度就失去了意义,太小会加重系统开销
使用系统时钟进行延时,实际延时会比理论延时大
系统时钟的测量

int64_t time_stamp;
int64_t milliseconds_spent;

/* 捕获开始时刻*/
time_stamp = k_uptime_get();

/* 所测量的代码*/
...

/* 计算时间增量Δt*/
milliseconds_spent = k_uptime_delta(&time_stamp);

2.硬件时钟

32位的 硬件时钟(hardware clock) 是一个高精度的计数器,该计数器通过一种称为 周期(cycles) 的单位来度量时间。一个周期的长度取决于内核所使用的板卡硬件,其典型的时间长度为纳秒。

uint32_t start_time;
uint32_t stop_time;
uint32_t cycles_spent;
uint32_t nanoseconds_spent;

/* 捕获初始时刻的周期数*/
start_time = k_cycle_get_32();

/* 所测量的代码*/
...

/* 捕获结束时刻的周期数 */
stop_time = k_cycle_get_32();

/* 计算时间增量Δt,单位是周期数*/
cycles_spent = stop_time - start_time;
/*将周期数换算为纳秒*/
nanoseconds_spent = SYS_CLOCK_HW_CYCLES_TO_NS(cycles_spent);

二、定时器

1.系统定时器

void my_work_handler(struct k_work *work)
{
    /* do the processing that needs to be done periodically */
    ...
}

struct k_work my_work = K_WORK_INITIALIZER(my_work_handler);

void my_timer_handler(struct k_timer *dummy)
{
    k_work_submit(&my_work);
}

K_TIMER_DEFINE(my_timer, my_timer_handler, NULL);

...

/* start periodic timer that expires once every second */
k_timer_start(&my_timer, K_SECONDS(1), K_SECONDS(1));

2.硬件定时器

相关文章

网友评论

    本文标题:Zephyr OS 学习笔记1-2:延时与流水灯

    本文链接:https://www.haomeiwen.com/subject/ifrkmqtx.html