1 /*
2  * Copyright (c) 2011-2023, Shanghai Real-Thread Electronic Technology Co.,Ltd
3  *
4  * Change Logs:
5  * Date           Author       Notes
6  * 2020-12-03     quanzhao     the first version
7  */
8 
9 #include <time.h>
10 #include <string.h>
11 #include <rtthread.h>
12 
13 static struct rt_device zero_dev;
14 
zero_read(rt_device_t dev,rt_off_t pos,void * buffer,rt_size_t size)15 static rt_ssize_t zero_read    (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
16 {
17     rt_memset(buffer, 0, size);
18     return size;
19 }
20 
zero_write(rt_device_t dev,rt_off_t pos,const void * buffer,rt_size_t size)21 static rt_ssize_t zero_write   (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
22 {
23     return size;
24 }
25 
zero_control(rt_device_t dev,int cmd,void * args)26 static rt_err_t  zero_control (rt_device_t dev, int cmd, void *args)
27 {
28     return RT_EOK;
29 }
30 
31 #ifdef RT_USING_DEVICE_OPS
32 const static struct rt_device_ops zero_ops =
33 {
34     RT_NULL,
35     RT_NULL,
36     RT_NULL,
37     zero_read,
38     zero_write,
39     zero_control
40 };
41 #endif
42 
zero_device_init(void)43 int zero_device_init(void)
44 {
45     static rt_bool_t init_ok = RT_FALSE;
46 
47     if (init_ok)
48     {
49         return 0;
50     }
51     RT_ASSERT(!rt_device_find("zero"));
52     zero_dev.type    = RT_Device_Class_Miscellaneous;
53 
54 #ifdef RT_USING_DEVICE_OPS
55     zero_dev.ops     = &zero_ops;
56 #else
57     zero_dev.init    = RT_NULL;
58     zero_dev.open    = RT_NULL;
59     zero_dev.close   = RT_NULL;
60     zero_dev.read    = zero_read;
61     zero_dev.write   = zero_write;
62     zero_dev.control = zero_control;
63 #endif
64 
65     /* no private */
66     zero_dev.user_data = RT_NULL;
67 
68     rt_device_register(&zero_dev, "zero", RT_DEVICE_FLAG_RDWR);
69 
70     init_ok = RT_TRUE;
71 
72     return 0;
73 }
74 INIT_DEVICE_EXPORT(zero_device_init);
75 
76