/* pin.h */
struct rt_pin_ops
{
void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_base_t mode);
void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_base_t value);
int (*pin_read)(struct rt_device *device, rt_base_t pin);
/* TODO: add GPIO interrupt */
rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_int32_t pin,
rt_uint32_t mode, void (*hdr)(void *args), void *args);
rt_err_t (*pin_detach_irq)(struct rt_device *device, rt_int32_t pin);
rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled);
};
API | 描述 |
---|---|
pin_mode | 设置引脚模式 |
pin_write | 设置引脚电平 |
pin_read | 读取引脚电平 |
pin_attach_irq | 绑定引脚中断回调函数 |
pin_irq_enable | 使能引脚中断 |
pin_detach_irq | 脱离引脚中断回调函数 |
const static 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,
};
int rt_hw_pin_init(void)
{
......
return rt_device_pin_register("pin", &_stm32_pin_ops, RT_NULL);
}
int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data)
{
_hw_pin.parent.type = RT_Device_Class_Miscellaneous;
_hw_pin.parent.rx_indicate = RT_NULL;
_hw_pin.parent.tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
_hw_pin.parent.ops = &pin_ops;
#else
_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;
#endif
_hw_pin.ops = ops;
_hw_pin.parent.user_data = user_data;
/* register a character device */
rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR);
return 0;
}
API | 描述 |
---|---|
rt_pin_mode() | 设置引脚模式 |
rt_pin_write() | 设置引脚电平 |
rt_pin_read() | 读取引脚电平 |
rt_pin_attach_irq() | 绑定引脚中断回调函数 |
rt_pin_irq_enable() | 使能引脚中断 |
rt_pin_detach_irq() | 脱离引脚中断回调函数 |
#define LED0_PIN GET_PIN(I, 13)
int main(void)
{
int count = 1;
/* set LED0 pin mode to output */
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
while (count++)
{
rt_pin_write(LED0_PIN, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(LED0_PIN, PIN_LOW);
rt_thread_mdelay(500);
}
return RT_EOK;
}
API | 说明 |
---|---|
rt_err_t rt_device_init (rt_device_t dev) | 设备初始化 |
rt_err_t rt_device_open (rt_device_t dev, rt_uint16_t oflag) | 打开设备 |
rt_err_t rt_device_close(rt_device_t dev) | 关闭设备 |
rt_size_t rt_device_read (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) | 读设备 |
rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size) | 写设备 |
rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg) | 控制设备 |
static rt_err_t _pin_control(rt_device_t dev, int cmd, void *args)
{
struct rt_device_pin_mode *mode;
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
/* check parameters */
RT_ASSERT(pin != RT_NULL);
mode = (struct rt_device_pin_mode *) args;
if (mode == RT_NULL) return -RT_ERROR;
pin->ops->pin_mode(dev, (rt_base_t)mode->pin, (rt_base_t)mode->mode);
return 0;
}
#define LED_PIN GET_PIN(I, 13)
int main(void)
{
int count = 1;
struct rt_device_pin *pin_dev = RT_NULL;
pin_dev = (struct rt_device_pin *)rt_device_find("pin");
rt_device_open((rt_device_t)pin_dev, RT_DEVICE_OFLAG_RDWR);
pin_dev->ops->pin_mode(&pin_dev->parent, LED_PIN, PIN_MODE_OUTPUT);
while (count++)
{
pin_dev->ops->pin_write(&pin_dev->parent, LED_PIN, PIN_HIGH);
rt_thread_mdelay(1000);
pin_dev->ops->pin_write(&pin_dev->parent, LED_PIN, PIN_LOW);
rt_thread_mdelay(1000);
}
return RT_EOK;
}
本文分享自 Rice 嵌入式开发技术分享 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!