用platform bus driver的方式来写上述程序 ,给出设备树配置

时间: 2024-03-21 20:38:35 浏览: 14
好的,以下是使用platform bus driver的方式编写Winbond SPI Flash驱动程序的示例代码,以及相应的设备树配置。 首先,创建一个名为“winbond-spi-flash”或类似的设备节点,并将其添加到设备树中。以下是一个简单的设备树配置示例: ```dts &spi0 { winbond_spi_flash: winbond-spi-flash@0 { compatible = "winbond,w25q64"; reg = <0>; spi-max-frequency = <100000000>; }; }; ``` 在驱动程序中,您需要实现probe和remove函数,用于初始化和清理设备资源。以下是一个简单的驱动程序示例: ```c #include <linux/init.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/spi/spi.h> #define WINBOND_CMD_READ_ID 0x9f #define WINBOND_CMD_READ_DATA 0x03 static struct spi_device *winbond_spi_device; static int winbond_spi_probe(struct platform_device *pdev) { int ret; struct device *dev = &pdev->dev; u8 id[3]; printk(KERN_INFO "winbond_spi: probe\n"); // get the SPI device pointer winbond_spi_device = dev_get_drvdata(dev); // read the Winbond Flash ID struct spi_transfer transfer = { .tx_buf = &WINBOND_CMD_READ_ID, .rx_buf = id, .len = 3, }; struct spi_message message; spi_message_init(&message); spi_message_add_tail(&transfer, &message); ret = spi_sync(winbond_spi_device, &message); if (ret < 0) { printk(KERN_ERR "winbond_spi: failed to read Winbond Flash ID\n"); return ret; } printk(KERN_INFO "winbond_spi: Winbond Flash ID: %02x %02x %02x\n", id[0], id[1], id[2]); return 0; } static int winbond_spi_remove(struct platform_device *pdev) { printk(KERN_INFO "winbond_spi: remove\n"); return 0; } static struct platform_driver winbond_spi_driver = { .driver = { .name = "winbond-spi-flash", }, .probe = winbond_spi_probe, .remove = winbond_spi_remove, }; static int winbond_spi_probe(struct spi_device *spi) { return 0; } static int winbond_spi_remove(struct spi_device *spi) { return 0; } static struct spi_driver winbond_spi_driver = { .driver = { .name = "winbond-spi-flash", }, .probe = winbond_spi_probe, .remove = winbond_spi_remove, }; static int __init winbond_spi_init(void) { int ret; struct spi_master *master; struct platform_device *pdev; printk(KERN_INFO "winbond_spi: init\n"); // find the SPI master controller master = spi_busnum_to_master(0); if (!master) { printk(KERN_ERR "winbond_spi: failed to find SPI master controller\n"); return -ENODEV; } // register the SPI driver ret = spi_register_driver(&winbond_spi_driver); if (ret < 0) { printk(KERN_ERR "winbond_spi: failed to register SPI driver\n"); return ret; } // create the platform device pdev = platform_device_alloc("winbond-spi-flash", 0); if (!pdev) { printk(KERN_ERR "winbond_spi: failed to allocate platform device\n"); return -ENOMEM; } platform_device_add(pdev); return 0; } static void __exit winbond_spi_exit(void) { printk(KERN_INFO "winbond_spi: exit\n"); // remove the platform device platform_device_unregister(&winbond_spi_device->dev); // unregister the SPI driver spi_unregister_driver(&winbond_spi_driver); } MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Winbond SPI Flash Driver"); module_init(winbond_spi_init); module_exit(winbond_spi_exit); ``` 在该驱动程序中,probe函数用于初始化SPI总线并读取Winbond Flash的ID。在remove函数中,您可以清理和释放设备资源。 需要注意的是,驱动程序中的设备树配置和平台驱动程序名称需要匹配。在本例中,设备树中的节点名称为“winbond-spi-flash”,而平台驱动程序的名称为“winbond-spi-flash”。

相关推荐

[ 4.858794] uwe5621_bt_tty_init [ 4.862131] mtty_probe unisoc soc, continue [ 4.868449] mtty_probe init device addr: 0x000000007db4bee8 [ 4.868608] rfkill_bluetooth_init [ 4.871951] rfkill_bluetooth_init end [ 4.872048] marlin_sdio_init [ 4.873682] mtty_probe unisoc soc, continue [ 4.873724] sysfs: cannot create duplicate filename '/devices/virt[ 4.873829] CPU: 1 PID: 121 Comm: init Not tainted 4.19.193 #34 [ 4.873842] Hardware name: ROC-RK3566-PC HDMI(Android) (DT) [ 4.873849] Call trace: [ 4.873868] dump_backtrace+0x0/0x178 [ 4.873876] show_stack+0x14/0x20 [ 4.873886] dump_stack+0x94/0xb4 [ 4.873895] sysfs_warn_dup+0x64/0x80 [ 4.873902] sysfs_create_dir_ns+0xdc/0xf8 [ 4.873910] kobject_add_internal+0xa0/0x288 [ 4.873916] kobject_add+0x98/0x100 [ 4.873928] device_add+0xec/0x698 [ 4.873934] device_register+0x1c/0x28 [ 4.873945] tty_register_device_attr+0xe4/0x208 [ 4.873951] tty_register_driver+0x138/0x248 [ 4.873970] mtty_probe+0x144/0x33u0 [sprdbt_tty] [ 4.873978] platform_drv_probe+0x50/0xa8 [ a 4.873984] really_probe+0xl228/0x2a0 [ 4.873991] driver_probe_device+0x58/0x100 [ 4.873996] device_driver_attach+0x6c/0x78 [ 4.874001] __driver_attach+0xb0/0xf0 [ 4.874009] bus_for_each_dev+0x68/0xc8 [ 4.874014] driver_attach+0x20/0x28 [ 4.874019] bus_add_driver+0xf8/0x1f0 [ 4.874025] driver_register+0x60/0x110 [ 4.874031] __platform_driver_register+0x40/0x48 [ 4.874044] uwe5621_bt_tty_init+0x44/0x1000 [sprdbt_tty] [ 4.874052] do_one_initcall+0x48/0x240 [ 4.874061] do_init_module+0x5c/0x1c8 [ 4.874069] load_module+0x18f8/0x1f68 [ 4.874074] __se_sys_finit_module+0xc0/0xd8 [ 4.874079] __arm64_sys_finit_module+0x14/0x20 [ 4.874087] el0_svc_common.constprop.0+0x64/0x178 [ 4.874092] el0_svc_handler+0x28/0x78 [ 4.874097] el0_svc+0x8/0xc [ 4.874179] kobject_add_internal failed for ttyBT0 with -EEXIST/, don't try to register things twith the same name in the same directory. [ 4.874225] list_del corruption, ffffffc079941ea8->next is LIST_POISON1 (dead000000000100) [ 4.874270] ------------[ cut here ]------------

最新推荐

recommend-type

linux内核4.7版本设备驱动介绍

linux内核4.7版本设备驱动介绍,包含如下模块:class、pci、platform、pinctrl、kset、kobject、bus、device、device_driver、i2c、tty、regmap、misc、spin_lock、inode、mutex、cdev、gpio_keys、usb
recommend-type

微软内部资料-SQL性能优化2

Contents Module Overview 1 Lesson 1: Memory 3 Lesson 2: I/O 73 Lesson 3: CPU 111 Module 3: Troubleshooting Server Performance ...Troubleshooting server performance-based support calls requires ...
recommend-type

2024华为OD机试D卷 - 最多购买宝石数目 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

华为OD机试D卷 - 小朋友来自多少小区 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

C51单片机的示例-课程实验-LCD1602基础开发库-内含源码和说明书.zip

C51单片机的示例-课程实验-LCD1602基础开发库-内含源码和说明书.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。