| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #include <rtthread.h>
- #include <rtdevice.h>
- #include <board.h>
- #include <rtthread.h>
- #include <rtdevice.h>
- #define WDT_DEVICE_NAME "ex_wdt" /* 看门狗设备名称 */
- static rt_device_t wdg_dev; /* 看门狗设备句柄 */
- static void idle_hook(void)
- {
- /* 在空闲线程的回调函数里喂狗 */
- rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
- rt_kprintf("feed the dog!\n ");
- }
- static int wdt_sample(int argc, char *argv[])
- {
- rt_err_t ret = RT_EOK;
- rt_uint32_t timeout = 1; /* 溢出时间,单位:秒 */
- char device_name[RT_NAME_MAX];
- /* 判断命令行参数是否给定了设备名称 */
- if (argc == 2)
- {
- rt_strncpy(device_name, argv[1], RT_NAME_MAX);
- }
- else
- {
- rt_strncpy(device_name, WDT_DEVICE_NAME, RT_NAME_MAX);
- }
- /* 根据设备名称查找看门狗设备,获取设备句柄 */
- wdg_dev = rt_device_find(device_name);
- if (!wdg_dev)
- {
- rt_kprintf("find %s failed!\n", device_name);
- return RT_ERROR;
- }
- /* 设置看门狗溢出时间 */
- ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
- if (ret != RT_EOK)
- {
- rt_kprintf("set %s timeout failed!\n", device_name);
- return RT_ERROR;
- }
- /* 启动看门狗 */
- ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
- if (ret != RT_EOK)
- {
- rt_kprintf("start %s failed!\n", device_name);
- return -RT_ERROR;
- }
- /* 设置空闲线程回调函数 */
- rt_thread_idle_sethook(idle_hook);
- return ret;
- }
- /* 导出到 msh 命令列表中 */
- MSH_CMD_EXPORT(wdt_sample, wdt sample);
- //INIT_COMPONENT_EXPORT(wdt_sample);
- /* defined the LED0 pin: PE9 */
- //#define LED0_PIN GET_PIN(E, 14)
- //int main(void)
- //{
- // int count = 1;
- // /* set LED0 pin mode to output */
- // rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
- // while (count++)
- // {
- // rt_pin_write(LED0_PIN, PIN_HIGH);
- // rt_thread_mdelay(500);
- // rt_pin_write(LED0_PIN, PIN_LOW);
- // rt_thread_mdelay(500);
- // }
- // return RT_EOK;
- //}
- //#define EXWATCHDOG_PIN GET_PIN(E, 11)
- ///* 定时器的控制块 */
- //static rt_timer_t watchdogtimer;
- //static int cnt = 0;
- ///* 定时器 1 超时函数 */
- //static void watchdog(void *parameter)
- //{
- // if(cnt == 0) {rt_pin_write(EXWATCHDOG_PIN, PIN_HIGH);cnt = 1;}
- // else if(cnt == 1) {rt_pin_write(EXWATCHDOG_PIN, PIN_LOW);cnt = 0;}
- //
- //}
- //int watchdog_timer(void)
- //{
- // rt_pin_mode(EXWATCHDOG_PIN, PIN_MODE_OUTPUT);
- // /* 创建定时器 1 周期定时器 */
- // watchdogtimer = rt_timer_create("watchdogtimer", watchdog,
- // RT_NULL, 256,
- // RT_TIMER_FLAG_PERIODIC);
- // /* 启动定时器 1 */
- // if (watchdogtimer != RT_NULL) rt_timer_start(watchdogtimer);
- // return 0;
- //}
- //INIT_PREV_EXPORT(watchdog_timer);
- ///* 导出到 msh 命令列表中 */
- ////MSH_CMD_EXPORT(timer_sample, timer sample);
- /*
- * 程序清单:这是一个 hwtimer 设备使用例程
- * 例程导出了 hwtimer_sample 命令到控制终端
- * 命令调用格式:hwtimer_sample
- * 程序功能:硬件定时器超时回调函数周期性的打印当前tick值,2次tick值之差换算为时间等同于定时时间值。
- */
- //#include <rtthread.h>
- //#include <rtdevice.h>
- //#define LOG_TAG "drv.ex_watch"
- //#include <drv_log.h>
- //#define HWTIMER_DEV_NAME "timer11" /* 定时器名称 */
- //#define EXWATCHDOG_PIN GET_PIN(E, 11)
- //static int cnt = 0;
- ///* 定时器超时回调函数 */
- //static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
- //{
- //
- // if(cnt == 0) {rt_pin_write(EXWATCHDOG_PIN, PIN_HIGH);cnt = 1;}
- // else if(cnt == 1) {rt_pin_write(EXWATCHDOG_PIN, PIN_LOW);cnt = 0;}
- // log_i("watch dog is running!");
- // return 0;
- //}
- //static int hwtimer_sample(int argc, char *argv[])
- //{
- // rt_err_t ret = RT_EOK;
- // rt_hwtimerval_t timeout_s; /* 定时器超时值 */
- // rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */
- // rt_hwtimer_mode_t mode; /* 定时器模式 */
- // rt_pin_mode(EXWATCHDOG_PIN, PIN_MODE_OUTPUT);
- //
- // /* 查找定时器设备 */
- // hw_dev = rt_device_find(HWTIMER_DEV_NAME);
- // if (hw_dev == RT_NULL)
- // {
- // rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
- // return RT_ERROR;
- // }
- // /* 以读写方式打开设备 */
- // ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
- // if (ret != RT_EOK)
- // {
- // rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
- // return ret;
- // }
- // /* 设置超时回调函数 */
- // rt_device_set_rx_indicate(hw_dev, timeout_cb);
- // /* 设置模式为周期性定时器 */
- // mode = HWTIMER_MODE_PERIOD;
- // ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
- // if (ret != RT_EOK)
- // {
- // rt_kprintf("set mode failed! ret is :%d\n", ret);
- // return ret;
- // }
- // /* 设置定时器超时值为5s并启动定时器 */
- // timeout_s.sec = 5; /* 秒 */
- // timeout_s.usec = 0; /* 微秒 */
- // if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
- // {
- // rt_kprintf("set timeout value failed\n");
- // return RT_ERROR;
- // }
- // /* 延时3500ms */
- // rt_thread_mdelay(3500);
- // /* 读取定时器当前值 */
- // rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
- // rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
- // return ret;
- //}
- ///* 导出到 msh 命令列表中 */
- //MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
|