基类对象,定义在rtdef.h中
struct rt_object
{char name[RT_NAME_MAX];rt_uint8_t type; //设备类Devicert_uint8_t flag;rt_list_t list;
}; //同一类对象挂接在一个链表上
设备基类,定义在rtdef.h中(属于设备接口层)
struct rt_device
{struct rt_object parent;enum rt_device_class_type type;rt_uint16_t flag;rt_uint16_t open_flag;rt_uint8_t ref_count; /**< reference count */rt_uint8_t device_id; /**< 0 - 255 *//* device call back */rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);rt_err_t (*tx_complete)(rt_device_t dev, void *buffer);/* common device interface */rt_err_t (*init) (rt_device_t dev);rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);rt_err_t (*close) (rt_device_t dev);rt_ssize_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);rt_ssize_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);rt_err_t (*control)(rt_device_t dev, int cmd, void *args);void *user_data; /**< device private data */
}
在pin.h中(属于设备驱动框架层)
struct rt_device_pin
{struct rt_device parent;const struct rt_pin_ops *ops;
};
struct rt_pin_ops
{void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_uint8_t mode);void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_uint8_t value);rt_int8_t (*pin_read)(struct rt_device *device, rt_base_t pin);rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_base_t pin,rt_uint8_t mode, void (*hdr)(void *args), void *args);rt_err_t (*pin_detach_irq)(struct rt_device *device, rt_base_t pin);rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint8_t enabled);rt_base_t (*pin_get)(const char *name);
};
在drv_gpio.c中(属于设备驱动层)
实现了设备驱动框架层的操作接口
static const struct rt_pin_ops _stm32_pin_ops =
{stm32_pin_mode,stm32_pin_write,stm32_pin_read,stm32_pin_attach_irq,stm32_pin_dettach_irq,stm32_pin_irq_enable,stm32_pin_get,
};
直接注册
rt_hw_pin_init(void)
{return rt_device_pin_register("pin", &_stm32_pin_ops, RT_NULL);
}
设备驱动框架层pin.c中定义了
static struct rt_device_pin _hw_pin;
struct rt_device_pin_value
{rt_base_t pin;rt_uint8_t value;
}
int rt_device_pin_register(const char*name, const struct rt_pin_ops *ops, void *user_data)
{_hw_pin.parent.type = RT_Device_Class_Pin;_hw_pin.parent.rx_indicate = RT_NULL;_hw_pin.parent.tx_complete = RT_NULL;_hw_pin.parent.init = RT_NULL;_hw_pin.parent.open = RT_NULL;_hw_pin.parent.close = RT_NULL;_hw_pin.parent.read = _pin_read;_hw_pin.parent.write = _pin_write;_hw_pin.parent.control = _pin_control;_hw_pin.ops = ops;_hw_pin.parent.user_data = user_data;rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR);return 0;
}