热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

enc28j60linux驱动_Linuxenc28j60网卡驱动移植(硬件spi和模拟spi)

本来想移植DM9000网卡的驱动,无奈硬件出了点问题,通过杜邦线链接开发板和DM9000网卡模块,系统上电,还没加载网卡驱动

本来想移植DM9000网卡的驱动,无奈硬件出了点问题,通过杜邦线链接开发板和DM9000网卡模块,系统上电,还没加载网卡驱动就直接崩溃了,找不到原因。刚好手上有一个enc28j60的网卡模块,于是就着手移植enc28j60的驱动。

其实移植enc28j60的驱动也十分简单,网上有现成的,只需要分配一些硬件资源即可。

由于我的内核版本老到掉牙,没有自带enc28j60的驱动,只能在网上找一个:

enc28j60.c

http://git.ti.com/ti-linux-kernel/ti-linux-kernel/blobs/7dac6f8df607929e51f4fd598d80bd009c45a9f8/drivers/net/enc28j60.c

enc28j60_hw.h

http://git.ti.com/ti-linux-kernel/ti-linux-kernel/blobs/7dac6f8df607929e51f4fd598d80bd009c45a9f8/drivers/net/enc28j60_hw.h

由于这个驱动是支持较新的内核,移植到2.6.22.6,只要改动3个地方好了。

... ...

static int enc28j60_set_hw_macaddr(struct net_device *ndev)

{

... ...

if (!priv->hw_enable) {

if (netif_msg_drv(priv)) {

/* [cgw]: 屏蔽一下几行 */

//DECLARE_MAC_BUF(mac);

//printk(KERN_INFO DRV_NAME

//    ": %s: Setting MAC address to %s\n",

//    ndev->name, print_mac(mac, ndev->dev_addr));

}

}

... ...

}

... ...

static void dump_packet(const char *msg, int len, const char *data)

{

printk(KERN_DEBUG DRV_NAME ": %s - packet len:%d\n", msg, len);

/* [cgw]: 屏蔽一下几行 */

//print_hex_dump(KERN_DEBUG, "pk data: ", DUMP_PREFIX_OFFSET, 16, 1,

//        data, len, true);

}

... ...

static int enc28j60_net_open(struct net_device *dev)

{

... ...

if (!is_valid_ether_addr(dev->dev_addr)) {

if (netif_msg_ifup(priv)) {

/* [cgw]: 屏蔽一下几行 */

//DECLARE_MAC_BUF(mac);

//dev_err(&dev->dev, "invalid MAC address %s\n",

//    print_mac(mac, dev->dev_addr));

}

return -EADDRNOTAVAIL;

}

... ...

}

... ...

都是些打印相关的东西,屏蔽掉就好。

这里只列出配置spi硬件资源的代码,只需要写一个spi_platform_dev.c文件就行了。模拟spi的模式下,spi_platform_dev.c和 http://www.linuxidc.com/Linux/2016-12/138512.htm 这里的spi_platform_dev.c文件相似,只需要增加一个外部中断入口给enc28j60用于接收中断,和更改spi的模式等。

模拟spi的模式下的spi_platform_dev.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

static struct spi_board_info board_info[1] = {

{

.modalias = "enc28j60",    /* [cgw]: spi设备名,和设备驱动名对应 */

.bus_num = 0,                /* [cgw]: spi总线号,即spi0 */

.chip_select = 2,            /* [cgw]: spi总线上的设备号,即spi0.2 */

.max_speed_hz    = 50000,    /* [cgw]: spi时钟 */

.mode = SPI_MODE_0,          /* [cgw]: spi数据模式 */

.irq = IRQ_EINT2,

},

};

static void enc28j60_chip_select(struct s3c2410_spigpio_info *spi, int cs)

{

/* [cgw]: 选中设备号为2的spi设备 */

if (spi->board_info->chip_select == 2) {

s3c2410_gpio_cfgpin(S3C2410_GPG2, S3C2410_GPIO_OUTPUT);

/* [cgw]: 选中设备 */

if (BITBANG_CS_ACTIVE == cs) {

s3c2410_gpio_setpin(S3C2410_GPG2, 0);

/* [cgw]: 释放设备 */

} else if (BITBANG_CS_INACTIVE == cs) {

s3c2410_gpio_setpin(S3C2410_GPG2, 1);

}

}

}

/* [cgw]:  */

static struct s3c2410_spigpio_info spi_dev = {

.pin_clk = S3C2410_GPG7,

.pin_mosi = S3C2410_GPG6,

.pin_miso = S3C2410_GPG5,

.board_size = 1,                    /* [cgw]: 设置板上spi接口数量为1 */

.board_info = &board_info[0],

.chip_select = enc28j60_chip_select

};

static void spi_dev_release(struct device * dev)

{

printk("spi_dev_release! \n");

}

/* [cgw]: 分配一个平台设备 */

static struct platform_device spi_platform_dev = {

.name        = "s3c24xx-spi-gpio",        /* [cgw]: 设置平台设备名,和平台驱动名对应 */

.id          = -1,

.dev = {

.release = spi_dev_release,

.platform_data = (void *)&spi_dev,      /* [cgw]: 通过platform_data传递spi_dev给平台驱动

* 平台驱动可以访问spi_dev

*/

},

};

static int spi_dev_init(void)

{

s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_EINT2);

/* [cgw]: 注册spi_platform_dev平台设备 */

platform_device_register(&spi_platform_dev);

return 0;

}

static void spi_dev_exit(void)

{

/* [cgw]: 注销spi_platform_dev平台设备 */

platform_device_unregister(&spi_platform_dev);

}

module_init(spi_dev_init);

module_exit(spi_dev_exit);

MODULE_LICENSE("GPL");

makefile:

KERN_DIR = /work/system/linux-2.6.22.6

all:

make -C $(KERN_DIR) M=`pwd` modules

clean:

make -C $(KERN_DIR) M=`pwd` modules clean

rm -rf modules.order

obj-m    += spi_platform_dev.o

obj-m    += spi_s3c24xx_gpio.o

obj-m    += spi_bitbang.o

obj-m    += enc28j60.o

硬件spi的模式下的spi_platform_dev.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

/* SPI (1) */

static struct resource s3c_spi1_resource[] = {

[0] = {

.start = S3C2410_PA_SPI + S3C2410_SPI1,

.end  = S3C2410_PA_SPI + S3C2410_SPI1 + 0x1f,

.flags = IORESOURCE_MEM,

},

[1] = {

.start = IRQ_SPI1,

.end  = IRQ_SPI1,

.flags = IORESOURCE_IRQ,

}

};

static struct spi_board_info board_info[1] = {

{

.modalias = "enc28j60",    /* [cgw]: spi设备名,和设备驱动名对应 */

.bus_num = 0,                /* [cgw]: spi总线号,即spi0 */

.chip_select = 2,            /* [cgw]: spi总线上的设备号,即spi0.2 */

.max_speed_hz    = 50000,    /* [cgw]: spi时钟 */

.mode = SPI_MODE_0,          /* [cgw]: spi数据模式 */

.irq = IRQ_EINT2,

},

};

static struct s3c2410_spi_info spi_info = {

.pin_cs = S3C2410_GPG2,    /* simple gpio cs */

.board_size = ARRAY_SIZE(board_info),

.board_info = &board_info[0],

.set_cs = NULL

};

static void spi_dev_release(struct device * dev)

{

printk("spi_dev_release! \n");

}

/* [cgw]: 分配一个平台设备 */

static struct platform_device spi_platform_dev = {

.name        = "s3c2410-spi",        /* [cgw]: 设置平台设备名,和平台驱动名对应 */

.id          = 1,

.num_resources      = ARRAY_SIZE(s3c_spi1_resource),

.resource      = s3c_spi1_resource,

.dev = {

.release = spi_dev_release,

.platform_data = &spi_info,

//.dma_mask = &s3c_device_spi1_dmamask,

//.coherent_dma_mask = 0xffffffffUL

},

};

static int spi_dev_init(void)

{

/* [cgw]: 注册spi_platform_dev平台设备 */

platform_device_register(&spi_platform_dev);

return 0;

}

static void spi_dev_exit(void)

{

/* [cgw]: 注销spi_platform_dev平台设备 */

platform_device_unregister(&spi_platform_dev);

}

module_init(spi_dev_init);

module_exit(spi_dev_exit);

MODULE_LICENSE("GPL");

makefile:

KERN_DIR = /work/system/linux-2.6.22.6

all:

make -C $(KERN_DIR) M=`pwd` modules

clean:

make -C $(KERN_DIR) M=`pwd` modules clean

rm -rf modules.order

obj-m    += spi_platform_dev.o

obj-m    += spi_s3c24xx.o

obj-m    += spi_bitbang.o

obj-m    += enc28j60.o

加载spi平台设备时(platform_device),应注意模拟spi时应加载spi_s3c24xx_gpio.c,硬件spi时应加载spi_s3c24xx.c

如:

模拟spi:

1 # insmod spi_bitbang.ko

2 # insmod spi_platform_dev.ko

3 # insmod spi_s3c24xx_gpio.ko

4 # insmod enc28j60.ko

硬件spi:

1 # insmod spi_bitbang.ko

2 # insmod spi_platform_dev.ko

3 # insmod spi_s3c24xx.ko

4 # insmod enc28j60.ko

其中spi_bitbang.c , spi_s3c24xx_gpio.c , spi_s3c24xx.c为内核原生源文件,不需要改动。

硬件spi时,加载驱动的实例:

谢谢!



推荐阅读
  • 在多线程编程环境中,线程之间共享全局变量可能导致数据竞争和不一致性。为了解决这一问题,Linux提供了线程局部存储(TLS),使每个线程可以拥有独立的变量副本,确保线程间的数据隔离与安全。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 作者:守望者1028链接:https:www.nowcoder.comdiscuss55353来源:牛客网面试高频题:校招过程中参考过牛客诸位大佬的面经,但是具体哪一块是参考谁的我 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 本文深入探讨了Linux系统中网卡绑定(bonding)的七种工作模式。网卡绑定技术通过将多个物理网卡组合成一个逻辑网卡,实现网络冗余、带宽聚合和负载均衡,在生产环境中广泛应用。文章详细介绍了每种模式的特点、适用场景及配置方法。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • C++构造函数与初始化列表详解
    本文深入探讨了C++中构造函数的初始化列表,包括赋值与初始化的区别、初始化列表的使用规则、静态成员初始化等内容。通过实例和调试证明,详细解释了初始化列表在对象创建时的重要性。 ... [详细]
  • Linux设备驱动程序:异步时间操作与调度机制
    本文介绍了Linux内核中的几种异步延迟操作方法,包括内核定时器、tasklet机制和工作队列。这些机制允许在未来的某个时间点执行任务,而无需阻塞当前线程,从而提高系统的响应性和效率。 ... [详细]
  • 本文探讨了《魔兽世界》中红蓝两方阵营在备战阶段的策略与实现方法,通过代码展示了双方如何根据资源和兵种特性进行战士生产。 ... [详细]
  • 实体映射最强工具类:MapStruct真香 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
author-avatar
吴国伟60942
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有