详细介绍:Zephyr RTOS的设备模型
资源链接:基于Zephyr RTOS开发STM32的例程包 ( 压缩-part1 )
资源链接:基于Zephyr RTOS开发STM32的例程包 ( 压缩-part2 )
语法基础
我们知道如果对一个数据类型进行重命名,可以使用 typedef 关键字,如:typedef unsigned char uint8_t; 这里的unsigned char为原基础类型名,uint8_t为类型别名。现在我们要定义一个无符号字符变量,就可以写为:uint8_t val;同理,如果我们现在要定义一个函数指针,该怎么做呢?比如,在zephyr设备模型定义中出现:
typedef int (*subsystem_do_this_t)(const struct device *dev, int foo, int bar);
该怎么理解呢?按照我们上面所说,原类型名和类型别名又分别是谁呢?
这里,原类型名: int (*)(const struct device *dev, int foo, int bar);类型别名:subsystem_do_this_t。
按照上面所述,如果我们现在要定义一个指向返回值为 int 类型、参数为 const struct device*、 int、 int 的函数的指针变量,就可以写成:subsystem_do_this_t my_func 。为什么是这样呢?这是因为 C 语言中 函数指针类型表达式是匿名的类型描述。
返回值类型 (*)(参数列表)
那么,如果理解了上面的定义,下面这段代码也就很好理解了。
typedef int (*subsystem_do_this_t)(const struct device *dev, int foo, int bar);
typedef void (*subsystem_do_that_t)(const struct device *dev, void *baz);
__subsystem struct subsystem_driver_api {
subsystem_do_this_t do_this;
subsystem_do_that_t do_that;
};
static int my_driver_do_this(const struct device *dev, int foo, int bar)
{
...
}
static void my_driver_do_that(const struct device *dev, void *baz)
{
...
}
static DEVICE_API(subsystem, my_driver_api_funcs) = {
.do_this = my_driver_do_this,
.do_that = my_driver_do_that,
};
上面的语言现象是理解Zephyr RTOS设备模型的基础。
API扩展
虽然Zephyr官方已经提供了主流器件开发的标准化API,但Zephyr依然给开发者提供了自行定义扩展API的方法,根据这种范式写
浙公网安备 33010602011771号