最近调试了型号为hz711的一款压力传感器,调试过程并不算十分艰难,但也需注意此传感器的数据传输方式和获取质量的技巧。
1、查看传感器的相关资料。
查看传感器的硬件连接图:
由此可知SCK与DOUT连接两个gpio口作为数据传输。再看时序图:
2、对传感器工作模式已经了解之后,开始编写驱动程序!首先在DTS中添加节点。文件路径:/kernel/arch/arm64/boot/dts/rockchip/rk3308-firefly.dtsi。在此用的是 gpio1 A0 和 gpio1 A1 。
hz711_test{
compatible = "hz711";
sck-gpio = <&gpio1 RK_PA1 GPIO_ACTIVE_LOW>;
dt-gpio = <&gpio1 RK_PA0 GPIO_ACTIVE_LOW>;
flag-gpio = <&gpio1 RK_PA2 IRQ_TYPE_LEVEL_HIGH>;
status = "okay";
};
3、添加节点后,便在/kernel/driver/ 下创建了名为:hz711 的目录。并在目录中创建 c文件、Kconfig、Makefile ,把目录加入到 /drive 下的Kconfig与Makefile中。并完成 /hz711 下的 Kconfig、Makefile文件的编写。在此不再赘述框架搭建,进入驱动中probe的编写:
static int hz711_probe(struct platform_device *pdev)
{
enum of_gpio_flags dt_flag;
enum of_gpio_flags sck_flag;
hz711 = kmalloc(sizeof(struct HZ711), GFP_KERNEL); //申请内存空间
if(!hz711)
{
printk("hz711 kmalloc memory err!!!\n");
return -ENODEV;
}
hz711->sck_gpio = of_get_named_gpio_flags(pdev->dev.of_node, "sck-gpio", 0, &sck_flag); //获取sck-gpio的信息
if(!gpio_is_valid(hz711->sck_gpio)) //判断对应gpio口是否合法
{
printk("sck-gpio is invalid!\n");
return -ENODEV;
}
gpio_direction_output(hz711->sck_gpio, 0); //设置为输出模式
hz711->dt_gpio = of_get_named_gpio_flags(pdev->dev