行业资讯
蚂蚁S9矿板PS led驱动实验-第6课 led设备驱动框架下的led驱动
参考蚂蚁S9矿板引脚定义.csdn蚂蚁S9矿板PS led驱动实验-第1课 字符设备驱动.csdn蚂蚁S9矿板PS led驱动实验-第2课 设备树下的led驱动.csdn蚂蚁S9矿板PS led驱动实验-第3课 gpio子系统下的led驱动.csdn蚂蚁S9矿板PS led驱动实验-第4课 设备驱动分离的驱动.csdn蚂蚁S9矿板PS led驱动实验-第5课 设备驱动框架下用设备树的驱动.csdn蚂蚁S9矿板PS led驱动实验-第6课 led设备驱动框架下的led驱动.csdn蚂蚁S9矿板PS led驱动实验-第7课 linux自带led驱动测试.csdn设备树system-user.dtsi#include dt-bindings/gpio/gpio.h#include dt-bindings/input/input.h#include dt-bindings/media/xilinx-vip.h#include dt-bindings/phy/phy.h#include dt-bindings/interrupt-controller/irq.h/{modelz7 Board ant 789;compatiblexlnx,zynq-zc702,xlnx,zynq-7000;chosen{bootargsconsolettyPS0,115200 earlyconcdns,mmio,0xe0000000,115200n8 keep_bootcon earlyprintk root/dev/mmcblk0p2 rw rootwait;stdout-pathserial0:115200n8;};led{compatibleming,led;statusokay;default-stateon;led-gpiogpio037GPIO_ACTIVE_HIGH;};beeper{compatibleming,beeper;statusokay;default-stateoff;beeper-gpiogpio038GPIO_ACTIVE_HIGH;};key{compatibleming,key;statusokay;key-gpiogpio047GPIO_ACTIVE_LOW;interrupt-parentgpio0;interrupts47IRQ_TYPE_EDGE_BOTH;};};leds-ming.c#includelinux/module.h#includelinux/of_gpio.h#includelinux/platform_device.h#includelinux/leds.h/* LED数据资源结构体 */structmyled_data{structled_classdevcdev;// led设备intgpio;// gpio编号};/* * description : 静态内敛函数该函数通过struct myled_data结构体中cdev变量的地址 * : 得到struct myled_data结构体变量的地址 * param - led_cdev : struct myled_data结构体中cdev变量的地址 * return : 执行成功返回struct myled_data结构体变量的地址 */staticinlinestructmyled_data*cdev_to_led_data(structled_classdev*led_cdev){returncontainer_of(led_cdev,structmyled_data,cdev);}/* * description : LED相关初始化操作 * param – pdev : struct platform_device指针也就是platform设备指针 * return : 成功返回0失败返回负数 */staticintmyled_init(structplatform_device*pdev){structmyled_data*led_dataplatform_get_drvdata(pdev);structdevice*devpdev-dev;intret;/* 从设备树中获取GPIO */led_data-gpioof_get_named_gpio(dev-of_node,led-gpio,0);if(!gpio_is_valid(led_data-gpio)){dev_err(dev,Failed to get gpio);return-EINVAL;}/* 申请使用GPIO */retdevm_gpio_request(dev,led_data-gpio,PS_LED0 Gpio);if(ret){dev_err(dev,Failed to request gpio);returnret;}/* 将GPIO设置为输出模式并将输出低电平 */gpio_direction_output(led_data-gpio,0);return0;}/* * description : 用于设置LED的亮度不可休眠 * param - led_cdev : struct led_classdev类型指针 * param – value : 亮度值 * return : 无 */staticvoidmyled_brightness_set(structled_classdev*led_cdev,enumled_brightnessvalue){structmyled_data*led_datacdev_to_led_data(led_cdev);intlevel;if(valueLED_OFF)level0;elselevel1;gpio_set_value(led_data-gpio,level);}/* * description : 用于设置LED的亮度可以休眠 * param - led_cdev : struct led_classdev类型指针 * param – value : 亮度值 * return : 无 */staticintmyled_brightness_set_blocking(structled_classdev*led_cdev,enumled_brightnessvalue){myled_brightness_set(led_cdev,value);return0;}/* * description : platform驱动的probe函数当驱动与设备 * : 匹配成功以后此函数就会执行 * param – pdev : platform设备指针 * return : 0成功;其他负值,失败 */staticintmyled_probe(structplatform_device*pdev){structmyled_data*led_data;structled_classdev*led_cdev;intret;dev_info(pdev-dev,LED device and driver matched successfully!\n);/* 为led_data指针分配内存 */led_datadevm_kzalloc(pdev-dev,sizeof(structmyled_data),GFP_KERNEL);if(!led_data)return-ENOMEM;platform_set_drvdata(pdev,led_data);/* 初始化LED */retmyled_init(pdev);if(ret)returnret;/* 初始化led_cdev变量 */led_cdevled_data-cdev;led_cdev-namemyled;// 设置设备名字led_cdev-brightnessLED_OFF;// 设置LED初始亮度led_cdev-max_brightnessLED_FULL;// 设置LED最大亮度led_cdev-brightness_setmyled_brightness_set;// 绑定LED亮度设置函数不可休眠led_cdev-brightness_set_blockingmyled_brightness_set_blocking;// 绑定LED亮度设置函数可以休眠/* 注册LED设备 */returnled_classdev_register(pdev-dev,led_cdev);}/* * description : platform驱动模块卸载时此函数会执行 * param – dev : platform设备指针 * return : 0成功;其他负值,失败 */staticintmyled_remove(structplatform_device*pdev){structmyled_data*led_dataplatform_get_drvdata(pdev);led_classdev_unregister(led_data-cdev);dev_info(pdev-dev,LED driver has been removed!\n);return0;}/* 匹配列表 */staticconststructof_device_idled_of_match[]{{.compatibleming,led},{/* Sentinel */}};/* platform驱动结构体 */staticstructplatform_drivermyled_driver{.driver{.namezynq-led,// 驱动名字用于和设备匹配.of_match_tableled_of_match,// 设备树匹配表用于和设备树中定义的设备匹配},.probemyled_probe,// probe函数.removemyled_remove,// remove函数};module_platform_driver(myled_driver);MODULE_AUTHOR(DengTao 773904075qq.com);MODULE_DESCRIPTION(LED Driver Based on LED Driver Framework);MODULE_LICENSE(GPL);测试rootant:~# insmod leds-ming.korootant:~# leds_ming: loading out-of-tree module taints kernel.zynq-led led: LED device and driver matched successfully!# 亮rootant:/sys/bus/platform/devices/led/leds/myled# echo 1 brightness# 灭rootant:/sys/bus/platform/devices/led/leds/myled# echo 0 brightness# 查看triggerrootz8:/sys/bus/platform/devices/led/leds/myled# cat trigger[none]kbd-scrolllock kbd-numlock kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock mmc0 timer oneshot heartbeat backlight gpio cpu cpu0 cpu1 default-on transient flash torch# 写入heartbeat 闪rootant:/sys/bus/platform/devices/led/leds/myled# echo heartbeat trigger# 写入timer 闪rootant:/sys/bus/platform/devices/led/leds/myled# echo timer trigger
郑州网站建设
网页设计
企业官网