1 /* 2 * COPYRIGHT (C) 2018, Real-Thread Information Technology Ltd 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2012-09-12 heyuanjie87 first version. 9 * 2014-03-04 Bernard code cleanup 10 */ 11 12 #include <drivers/watchdog.h> 13 14 /* RT-Thread Device Interface */ 15 16 /* 17 * This function initializes serial 18 */ 19 static rt_err_t rt_watchdog_init(struct rt_device *dev) 20 { 21 rt_watchdog_t *wtd; 22 23 RT_ASSERT(dev != RT_NULL); 24 wtd = (rt_watchdog_t *)dev; 25 if (wtd->ops->init) 26 { 27 return (wtd->ops->init(wtd)); 28 } 29 30 return (-RT_ENOSYS); 31 } 32 33 static rt_err_t rt_watchdog_open(struct rt_device *dev, rt_uint16_t oflag) 34 { 35 return (RT_EOK); 36 } 37 38 static rt_err_t rt_watchdog_close(struct rt_device *dev) 39 { 40 rt_watchdog_t *wtd; 41 42 RT_ASSERT(dev != RT_NULL); 43 wtd = (rt_watchdog_t *)dev; 44 45 if (wtd->ops->control(wtd, RT_DEVICE_CTRL_WDT_STOP, RT_NULL) != RT_EOK) 46 { 47 rt_kprintf(" This watchdog can not be stoped\n"); 48 49 return (-RT_ERROR); 50 } 51 52 return (RT_EOK); 53 } 54 55 static rt_err_t rt_watchdog_control(struct rt_device *dev, 56 int cmd, 57 void *args) 58 { 59 rt_watchdog_t *wtd; 60 61 RT_ASSERT(dev != RT_NULL); 62 wtd = (rt_watchdog_t *)dev; 63 64 return (wtd->ops->control(wtd, cmd, args)); 65 } 66 67 #ifdef RT_USING_DEVICE_OPS 68 const static struct rt_device_ops wdt_ops = 69 { 70 rt_watchdog_init, 71 rt_watchdog_open, 72 rt_watchdog_close, 73 RT_NULL, 74 RT_NULL, 75 rt_watchdog_control, 76 }; 77 #endif 78 79 /** 80 * This function register a watchdog device 81 */ 82 rt_err_t rt_hw_watchdog_register(struct rt_watchdog_device *wtd, 83 const char *name, 84 rt_uint32_t flag, 85 void *data) 86 { 87 struct rt_device *device; 88 RT_ASSERT(wtd != RT_NULL); 89 90 device = &(wtd->parent); 91 92 device->type = RT_Device_Class_Miscellaneous; 93 device->rx_indicate = RT_NULL; 94 device->tx_complete = RT_NULL; 95 96 #ifdef RT_USING_DEVICE_OPS 97 device->ops = &wdt_ops; 98 #else 99 device->init = rt_watchdog_init; 100 device->open = rt_watchdog_open; 101 device->close = rt_watchdog_close; 102 device->read = RT_NULL; 103 device->write = RT_NULL; 104 device->control = rt_watchdog_control; 105 #endif 106 device->user_data = data; 107 108 /* register a character device */ 109 return rt_device_register(device, name, flag); 110 } 111