ex_watchdog.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include <board.h>
  4. #include <rtthread.h>
  5. #include <rtdevice.h>
  6. #define WDT_DEVICE_NAME "ex_wdt" /* 看门狗设备名称 */
  7. static rt_device_t wdg_dev; /* 看门狗设备句柄 */
  8. static void idle_hook(void)
  9. {
  10. /* 在空闲线程的回调函数里喂狗 */
  11. rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
  12. rt_kprintf("feed the dog!\n ");
  13. }
  14. static int wdt_sample(int argc, char *argv[])
  15. {
  16. rt_err_t ret = RT_EOK;
  17. rt_uint32_t timeout = 1; /* 溢出时间,单位:秒 */
  18. char device_name[RT_NAME_MAX];
  19. /* 判断命令行参数是否给定了设备名称 */
  20. if (argc == 2)
  21. {
  22. rt_strncpy(device_name, argv[1], RT_NAME_MAX);
  23. }
  24. else
  25. {
  26. rt_strncpy(device_name, WDT_DEVICE_NAME, RT_NAME_MAX);
  27. }
  28. /* 根据设备名称查找看门狗设备,获取设备句柄 */
  29. wdg_dev = rt_device_find(device_name);
  30. if (!wdg_dev)
  31. {
  32. rt_kprintf("find %s failed!\n", device_name);
  33. return RT_ERROR;
  34. }
  35. /* 设置看门狗溢出时间 */
  36. ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
  37. if (ret != RT_EOK)
  38. {
  39. rt_kprintf("set %s timeout failed!\n", device_name);
  40. return RT_ERROR;
  41. }
  42. /* 启动看门狗 */
  43. ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
  44. if (ret != RT_EOK)
  45. {
  46. rt_kprintf("start %s failed!\n", device_name);
  47. return -RT_ERROR;
  48. }
  49. /* 设置空闲线程回调函数 */
  50. rt_thread_idle_sethook(idle_hook);
  51. return ret;
  52. }
  53. /* 导出到 msh 命令列表中 */
  54. MSH_CMD_EXPORT(wdt_sample, wdt sample);
  55. //INIT_COMPONENT_EXPORT(wdt_sample);
  56. /* defined the LED0 pin: PE9 */
  57. //#define LED0_PIN GET_PIN(E, 14)
  58. //int main(void)
  59. //{
  60. // int count = 1;
  61. // /* set LED0 pin mode to output */
  62. // rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
  63. // while (count++)
  64. // {
  65. // rt_pin_write(LED0_PIN, PIN_HIGH);
  66. // rt_thread_mdelay(500);
  67. // rt_pin_write(LED0_PIN, PIN_LOW);
  68. // rt_thread_mdelay(500);
  69. // }
  70. // return RT_EOK;
  71. //}
  72. //#define EXWATCHDOG_PIN GET_PIN(E, 11)
  73. ///* 定时器的控制块 */
  74. //static rt_timer_t watchdogtimer;
  75. //static int cnt = 0;
  76. ///* 定时器 1 超时函数 */
  77. //static void watchdog(void *parameter)
  78. //{
  79. // if(cnt == 0) {rt_pin_write(EXWATCHDOG_PIN, PIN_HIGH);cnt = 1;}
  80. // else if(cnt == 1) {rt_pin_write(EXWATCHDOG_PIN, PIN_LOW);cnt = 0;}
  81. //
  82. //}
  83. //int watchdog_timer(void)
  84. //{
  85. // rt_pin_mode(EXWATCHDOG_PIN, PIN_MODE_OUTPUT);
  86. // /* 创建定时器 1 周期定时器 */
  87. // watchdogtimer = rt_timer_create("watchdogtimer", watchdog,
  88. // RT_NULL, 256,
  89. // RT_TIMER_FLAG_PERIODIC);
  90. // /* 启动定时器 1 */
  91. // if (watchdogtimer != RT_NULL) rt_timer_start(watchdogtimer);
  92. // return 0;
  93. //}
  94. //INIT_PREV_EXPORT(watchdog_timer);
  95. ///* 导出到 msh 命令列表中 */
  96. ////MSH_CMD_EXPORT(timer_sample, timer sample);
  97. /*
  98. * 程序清单:这是一个 hwtimer 设备使用例程
  99. * 例程导出了 hwtimer_sample 命令到控制终端
  100. * 命令调用格式:hwtimer_sample
  101. * 程序功能:硬件定时器超时回调函数周期性的打印当前tick值,2次tick值之差换算为时间等同于定时时间值。
  102. */
  103. //#include <rtthread.h>
  104. //#include <rtdevice.h>
  105. //#define LOG_TAG "drv.ex_watch"
  106. //#include <drv_log.h>
  107. //#define HWTIMER_DEV_NAME "timer11" /* 定时器名称 */
  108. //#define EXWATCHDOG_PIN GET_PIN(E, 11)
  109. //static int cnt = 0;
  110. ///* 定时器超时回调函数 */
  111. //static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
  112. //{
  113. //
  114. // if(cnt == 0) {rt_pin_write(EXWATCHDOG_PIN, PIN_HIGH);cnt = 1;}
  115. // else if(cnt == 1) {rt_pin_write(EXWATCHDOG_PIN, PIN_LOW);cnt = 0;}
  116. // log_i("watch dog is running!");
  117. // return 0;
  118. //}
  119. //static int hwtimer_sample(int argc, char *argv[])
  120. //{
  121. // rt_err_t ret = RT_EOK;
  122. // rt_hwtimerval_t timeout_s; /* 定时器超时值 */
  123. // rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */
  124. // rt_hwtimer_mode_t mode; /* 定时器模式 */
  125. // rt_pin_mode(EXWATCHDOG_PIN, PIN_MODE_OUTPUT);
  126. //
  127. // /* 查找定时器设备 */
  128. // hw_dev = rt_device_find(HWTIMER_DEV_NAME);
  129. // if (hw_dev == RT_NULL)
  130. // {
  131. // rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
  132. // return RT_ERROR;
  133. // }
  134. // /* 以读写方式打开设备 */
  135. // ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  136. // if (ret != RT_EOK)
  137. // {
  138. // rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
  139. // return ret;
  140. // }
  141. // /* 设置超时回调函数 */
  142. // rt_device_set_rx_indicate(hw_dev, timeout_cb);
  143. // /* 设置模式为周期性定时器 */
  144. // mode = HWTIMER_MODE_PERIOD;
  145. // ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  146. // if (ret != RT_EOK)
  147. // {
  148. // rt_kprintf("set mode failed! ret is :%d\n", ret);
  149. // return ret;
  150. // }
  151. // /* 设置定时器超时值为5s并启动定时器 */
  152. // timeout_s.sec = 5; /* 秒 */
  153. // timeout_s.usec = 0; /* 微秒 */
  154. // if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  155. // {
  156. // rt_kprintf("set timeout value failed\n");
  157. // return RT_ERROR;
  158. // }
  159. // /* 延时3500ms */
  160. // rt_thread_mdelay(3500);
  161. // /* 读取定时器当前值 */
  162. // rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
  163. // rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
  164. // return ret;
  165. //}
  166. ///* 导出到 msh 命令列表中 */
  167. //MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);