Просмотр исходного кода

!79 添加虚拟设备 null 的驱动
Merge pull request !79 from qz-lab/add-null-device

bernard 5 лет назад
Родитель
Сommit
3753906bde

+ 8 - 4
components/drivers/Kconfig

@@ -8,7 +8,7 @@ if RT_USING_DEVICE_IPC
     config RT_PIPE_BUFSZ
         int "Set pipe buffer size"
         default 512
-    
+
     config RT_USING_SYSTEM_WORKQUEUE
         bool "Using system default workqueue"
         default n
@@ -118,7 +118,11 @@ config RT_USING_ADC
 config RT_USING_DAC
     bool "Using DAC device drivers"
     default n
-    
+
+config RT_USING_NULL
+    bool "Using NULL device drivers"
+    default n
+
 config RT_USING_PWM
     bool "Using PWM device drivers"
     default n
@@ -202,7 +206,7 @@ config RT_USING_SPI
     bool "Using SPI Bus/Device device drivers"
     default n
 
-    if RT_USING_SPI  
+    if RT_USING_SPI
         config RT_USING_QSPI
             bool "Enable QSPI mode"
             default n
@@ -226,7 +230,7 @@ config RT_USING_SPI
                 config RT_SFUD_USING_FLASH_INFO_TABLE
                 bool "Using defined supported flash chip information table"
                 default y
-                
+
                 config RT_SFUD_USING_QSPI
                 bool "Using QSPI mode support"
                 select RT_USING_QSPI

+ 7 - 4
components/drivers/misc/SConscript

@@ -1,28 +1,31 @@
 from building import *
 
 cwd = GetCurrentDir()
-src = [] 
+src = []
 CPPPATH = [cwd + '/../include']
 group = []
 
 if GetDepend(['RT_USING_PIN']):
     src = src + ['pin.c']
-    
+
 if GetDepend(['RT_USING_ADC']):
     src = src + ['adc.c']
-    
+
 if GetDepend(['RT_USING_DAC']):
     src = src + ['dac.c']
 
 if GetDepend(['RT_USING_PWM']):
     src = src + ['rt_drv_pwm.c']
-    
+
 if GetDepend(['RT_USING_PULSE_ENCODER']):
     src = src + ['pulse_encoder.c']
 
 if GetDepend(['RT_USING_INPUT_CAPTURE']):
     src = src + ['rt_inputcapture.c']
 
+if GetDepend(['RT_USING_NULL']):
+    src = src + ['rt_null.c']
+
 if len(src):
     group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
 

+ 75 - 0
components/drivers/misc/rt_null.c

@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2011-2020, Shanghai Real-Thread Electronic Technology Co.,Ltd
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2020-12-03     quanzhao     the first version
+ */
+
+#include <time.h>
+#include <string.h>
+#include <rtthread.h>
+
+static struct rt_device null_dev;
+
+static rt_size_t null_read    (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
+{
+    rt_memset(buffer, 0, size);
+    return size;
+}
+
+static rt_size_t null_write   (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
+{
+    return size;
+}
+
+static rt_err_t  null_control (rt_device_t dev, int cmd, void *args)
+{
+    return RT_EOK;
+}
+
+#ifdef RT_USING_DEVICE_OPS
+const static struct rt_device_ops null_ops =
+{
+    RT_NULL,
+    RT_NULL,
+    RT_NULL,
+    null_read,
+    null_write,
+    null_control
+};
+#endif
+
+int null_device_init(void)
+{
+    static rt_bool_t init_ok = RT_FALSE;
+
+    if (init_ok)
+    {
+        return 0;
+    }
+    RT_ASSERT(!rt_device_find("null"));
+    null_dev.type    = RT_Device_Class_Miscellaneous;
+
+#ifdef RT_USING_DEVICE_OPS
+    null_dev.ops     = &null_ops;
+#else
+    null_dev.init    = RT_NULL;
+    null_dev.open    = RT_NULL;
+    null_dev.close   = RT_NULL;
+    null_dev.read    = null_read;
+    null_dev.write   = null_write;
+    null_dev.control = null_control;
+#endif
+
+    /* no private */
+    null_dev.user_data = RT_NULL;
+
+    rt_device_register(&null_dev, "null", RT_DEVICE_FLAG_RDWR);
+
+    init_ok = RT_TRUE;
+
+    return 0;
+}
+INIT_DEVICE_EXPORT(null_device_init);
+