linux enc28j60網卡驅動移植(硬體spi和模擬spi)

来源:http://www.cnblogs.com/hackfun/archive/2017/01/07/6260396.html
-Advertisement-
Play Games

本來想移植DM9000網卡的驅動,無奈硬體出了點問題,通過杜邦線鏈接開發板和DM9000網卡模塊,系統上電,還沒載入網卡驅動就直接崩潰了,找不到原因。。。剛好手上有一個enc28j60的網卡模塊,於是就著手移植enc28j60的驅動。 其實移植enc28j60的驅動也十分簡單,網上有現成的,只需要分 ...


本來想移植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個地方好了。

 1 ... ...
 2 
 3 static int enc28j60_set_hw_macaddr(struct net_device *ndev)
 4 {
 5     ... ...
 6     
 7     if (!priv->hw_enable) {
 8         if (netif_msg_drv(priv)) {
 9             /* [cgw]: 屏蔽一下幾行 */
10             //DECLARE_MAC_BUF(mac);
11             //printk(KERN_INFO DRV_NAME
12             //    ": %s: Setting MAC address to %s\n",
13             //    ndev->name, print_mac(mac, ndev->dev_addr));
14         }
15     }
16 
17     ... ...
18 }
19 
20 ... ...
21 
22 static void dump_packet(const char *msg, int len, const char *data)
23 {
24     printk(KERN_DEBUG DRV_NAME ": %s - packet len:%d\n", msg, len);
25     /* [cgw]: 屏蔽一下幾行 */
26     //print_hex_dump(KERN_DEBUG, "pk data: ", DUMP_PREFIX_OFFSET, 16, 1,
27     //        data, len, true);
28 }
29 
30 ... ...
31     
32 static int enc28j60_net_open(struct net_device *dev)
33 {
34     ... ...
35 
36     if (!is_valid_ether_addr(dev->dev_addr)) {
37         if (netif_msg_ifup(priv)) {
38             /* [cgw]: 屏蔽一下幾行 */
39             //DECLARE_MAC_BUF(mac);
40             //dev_err(&dev->dev, "invalid MAC address %s\n",
41             //    print_mac(mac, dev->dev_addr));
42         }
43         return -EADDRNOTAVAIL;
44     }
45 
46     ... ...
47 }
48 
49 ... ...


都是些列印相關的東西,屏蔽掉就好。

spi的框架可以參考這裡:http://www.cnblogs.com/hackfun/p/6082489.html

這裡只列出配置spi硬體資源的代碼,只需要寫一個spi_platform_dev.c文件就行了。模擬spi的模式下,spi_platform_dev.c和http://www.cnblogs.com/hackfun/p/6082489.html這裡的spi_platform_dev.c文件相似,只需要增加一個外部中斷入口給enc28j60用於接收中斷,和更改spi的模式等。

模擬spi的模式下的spi_platform_dev.c

  1 #include <linux/module.h>
  2 #include <linux/version.h>
  3 
  4 #include <linux/init.h>
  5 
  6 #include <linux/kernel.h>
  7 #include <linux/types.h>
  8 #include <linux/interrupt.h>
  9 #include <linux/list.h>
 10 #include <linux/timer.h>
 11 #include <linux/init.h>
 12 #include <linux/serial_core.h>
 13 #include <linux/platform_device.h>
 14 #include <linux/irq.h>
 15 
 16 #include <asm/gpio.h>
 17 #include <asm/io.h>
 18 #include <asm/arch/regs-gpio.h>
 19 
 20 #include <linux/spi/spi.h>
 21 #include <linux/spi/spi_bitbang.h>
 22 
 23 #include <asm/arch/regs-spi.h>
 24 #include <asm/arch/spi.h>
 25 #include <asm/arch/spi-gpio.h>
 26 
 27 
 28 static struct spi_board_info board_info[1] = {
 29     {
 30     .modalias = "enc28j60",    /* [cgw]: spi設備名,和設備驅動名對應 */
 31     .bus_num = 0,                 /* [cgw]: spi匯流排號,即spi0 */
 32     .chip_select = 2,             /* [cgw]: spi匯流排上的設備號,即spi0.2 */
 33     .max_speed_hz    = 50000,     /* [cgw]: spi時鐘 */
 34     .mode = SPI_MODE_0,           /* [cgw]: spi數據模式 */
 35     .irq = IRQ_EINT2,
 36     },
 37 };
 38 
 39 
 40 static void enc28j60_chip_select(struct s3c2410_spigpio_info *spi, int cs)
 41 {
 42     /* [cgw]: 選中設備號為2的spi設備 */
 43     if (spi->board_info->chip_select == 2) {
 44         s3c2410_gpio_cfgpin(S3C2410_GPG2, S3C2410_GPIO_OUTPUT);
 45         /* [cgw]: 選中設備 */
 46         if (BITBANG_CS_ACTIVE == cs) {
 47             s3c2410_gpio_setpin(S3C2410_GPG2, 0);
 48         /* [cgw]: 釋放設備 */
 49         } else if (BITBANG_CS_INACTIVE == cs) {
 50             s3c2410_gpio_setpin(S3C2410_GPG2, 1);
 51         }
 52     }
 53 }
 54 
 55 /* [cgw]:  */
 56 static struct s3c2410_spigpio_info spi_dev = {
 57     .pin_clk = S3C2410_GPG7,
 58     .pin_mosi = S3C2410_GPG6,
 59     .pin_miso = S3C2410_GPG5,
 60     .board_size = 1,                    /* [cgw]: 設置板上spi介面數量為1 */
 61     .board_info = &board_info[0],
 62     .chip_select = enc28j60_chip_select
 63 };
 64 
 65 static void spi_dev_release(struct device * dev)
 66 {
 67     printk("spi_dev_release! \n");
 68 }
 69 
 70 /* [cgw]: 分配一個平臺設備 */
 71 static struct platform_device spi_platform_dev = {
 72     .name         = "s3c24xx-spi-gpio",        /* [cgw]: 設置平臺設備名,和平臺驅動名對應 */
 73     .id           = -1,
 74     .dev = { 
 75         .release = spi_dev_release,
 76         .platform_data = (void *)&spi_dev,      /* [cgw]: 通過platform_data傳遞spi_dev給平臺驅動
 77                                                 * 平臺驅動可以訪問spi_dev
 78                                                 */
 79     },
 80 };
 81 
 82 
 83 static int spi_dev_init(void)
 84 {
 85     s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_EINT2);
 86 
 87     /* [cgw]: 註冊spi_platform_dev平臺設備 */
 88     platform_device_register(&spi_platform_dev);
 89     return 0;
 90 }
 91 
 92 static void spi_dev_exit(void)
 93 {
 94     /* [cgw]: 註銷spi_platform_dev平臺設備 */
 95     platform_device_unregister(&spi_platform_dev);
 96 }
 97 
 98 module_init(spi_dev_init);
 99 module_exit(spi_dev_exit);
100 
101 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

  1 #include <linux/module.h>
  2 #include <linux/version.h>
  3 
  4 #include <linux/init.h>
  5 
  6 #include <linux/kernel.h>
  7 #include <linux/types.h>
  8 #include <linux/interrupt.h>
  9 #include <linux/list.h>
 10 #include <linux/timer.h>
 11 #include <linux/init.h>
 12 #include <linux/serial_core.h>
 13 #include <linux/platform_device.h>
 14 #include <linux/irq.h>
 15 
 16 #include <asm/gpio.h>
 17 #include <asm/io.h>
 18 #include <asm/arch/regs-gpio.h>
 19 
 20 #include <linux/spi/spi.h>
 21 #include <linux/spi/spi_bitbang.h>
 22 
 23 #include <asm/arch/regs-spi.h>
 24 #include <asm/arch/spi.h>
 25 #include <asm/arch/spi-gpio.h>
 26 
 27 
 28 /* SPI (1) */
 29 
 30 static struct resource s3c_spi1_resource[] = {
 31     [0] = {
 32         .start = S3C2410_PA_SPI + S3C2410_SPI1,
 33         .end   = S3C2410_PA_SPI + S3C2410_SPI1 + 0x1f,
 34         .flags = IORESOURCE_MEM,
 35     },
 36     [1] = {
 37         .start = IRQ_SPI1,
 38         .end   = IRQ_SPI1,
 39         .flags = IORESOURCE_IRQ,
 40     }
 41 
 42 };
 43 
 44 
 45 static struct spi_board_info board_info[1] = {
 46     {
 47     .modalias = "enc28j60",    /* [cgw]: spi設備名,和設備驅動名對應 */
 48     .bus_num = 0,                 /* [cgw]: spi匯流排號,即spi0 */
 49     .chip_select = 2,             /* [cgw]: spi匯流排上的設備號,即spi0.2 */
 50     .max_speed_hz    = 50000,     /* [cgw]: spi時鐘 */
 51     .mode = SPI_MODE_0,           /* [cgw]: spi數據模式 */
 52     .irq = IRQ_EINT2,
 53     },
 54 };
 55 
 56 static struct s3c2410_spi_info spi_info = {
 57     .pin_cs = S3C2410_GPG2,    /* simple gpio cs */
 58     .board_size = ARRAY_SIZE(board_info),
 59     .board_info = &board_info[0],
 60     .set_cs = NULL
 61 };
 62 
 63 
 64 static void spi_dev_release(struct device * dev)
 65 {
 66     printk("spi_dev_release! \n");
 67 }
 68 
 69 /* [cgw]: 分配一個平臺設備 */
 70 static struct platform_device spi_platform_dev = {
 71     .name         = "s3c2410-spi",        /* [cgw]: 設置平臺設備名,和平臺驅動名對應 */
 72     .id           = 1,
 73     .num_resources      = ARRAY_SIZE(s3c_spi1_resource),
 74     .resource      = s3c_spi1_resource,
 75     .dev = { 
 76         .release = spi_dev_release,
 77         .platform_data = &spi_info,
 78         //.dma_mask = &s3c_device_spi1_dmamask,
 79         //.coherent_dma_mask = 0xffffffffUL
 80     },
 81 };
 82 
 83 
 84 static int spi_dev_init(void)
 85 {
 86     /* [cgw]: 註冊spi_platform_dev平臺設備 */
 87     platform_device_register(&spi_platform_dev);
 88     return 0;
 89 }
 90 
 91 static void spi_dev_exit(void)
 92 {
 93     /* [cgw]: 註銷spi_platform_dev平臺設備 */
 94     platform_device_unregister(&spi_platform_dev);
 95 }
 96 
 97 module_init(spi_dev_init);
 98 module_exit(spi_dev_exit);
 99 
100 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時,載入驅動的實例:

 

謝謝!

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近在學習linux,在某個用戶(xxx)下使用sudo的時候,提示以下錯誤:xxx is not in the sudoers file. This incident will be reported。 百度了下,究其原因是用戶沒有加入到sudo的配置文件里。 解決方法如下: 1、切換到root用 ...
  • 第八節 Linux 文件的屬性(下半部分) 標簽(空格分隔): Linux教學筆記 [更多相關資料請點我查看][1] 第1章 鏈接的概念 在linux系統中,鏈接可分為兩種:一種為硬鏈接(Hard Link),另一種為軟連接或符號鏈接(Symbolic Link or Soft link)。我們在前 ...
  • 一.以查詢方式實現 1.寫出驅動框架 1.1 仿照其他程式加一些必要的頭文件 1.2 構造一個結構體file_operations 1.3 根據file_operations的所選項寫出所需的函數,並構建出來 1.4 入口函數、出口函數的註冊和卸載 1.5 修飾入口函數和出口函數 1.6 給sysf ...
  • 本人接觸的優化主要分為三大類 黑體的為本模塊下的重點 安全優化 安全在生產場景中是第一位的 1.1 站點目錄許可權的優化 (修改許可權755 644 所屬用戶root,需要上傳的目錄給予nginx許可權) 1.2 防盜連接的優化(通過$http_referer實現判斷用戶來源,對非法的referer 返回 ...
  • SecureCRT下的文件傳輸協議有以下幾種:ASCII、Xmodem、Ymodem、Zmodem ASCII:這是最快的傳輸協議,但只能傳送文本文件。 Xmodem:這種古老的傳輸協議速度較慢,但由於使用了CRC錯誤偵測方法,傳輸的準確率可高達99.6%。 Ymodem:這是Xmodem的改良版, ...
  • keepalived管理的的ipvs功能支持對後端節點真實伺服器的健康檢查 一般常用的方式包括tcp_check 和http_get(更準確) tcp_check 原理就是對真實伺服器進行ip+埠的檢測實現的(我們可以用wget nmap telnet實現) 如果相應的埠開啟,則加入lvs的轉發 ...
  • 相關文章: ...
  • OpenSSH伺服器: ssh協議:提供安全的ssh環境用於遠程管理,預設埠:22; 服務名稱:sshd openssh-server包的安裝文件: 使用ssh客戶端程式: 遠程複製 : 從遠程主機複製過來:scp 用戶名@主機名:/目標文件夾/文件名 目的文件夾 使用ssh客戶端的常用程式:pu ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...