最近項目需要,需要在mini2440上移植SPI驅動,板子需要驅動SPI裝置,上網找了很多資源,但是很多都是有問題,最終在基本理解驅動結構的前提下,將SPI驅動順利移植到mini2440。
,我使用的核心版本是2.6.32.2,這個版本和2.6。29不一樣,網上很多版本都是關於2.6.29,如果完全按照網上步驟,編譯會出現問題,我做的步驟如下:
1,在Linux Source Code中修改arch/arm/mach-s3c2440/mach-mini2440.c檔案,加入標頭檔:
#include <linux/spi/spi.h>#include <../mach-s3c2410/include/mach/spi.h>
然後加入如下代碼:
static struct spi_board_info s3c2410_spi0_board[] ={ [0] = { .modalias = "spidev", .bus_num = 0, .chip_select = 0, .irq = IRQ_EINT9, .max_speed_hz = 500 * 1000, }};static struct s3c2410_spi_info s3c2410_spi0_platdata = { .pin_cs = S3C2410_GPG(2), .num_cs = 1, .bus_num = 0, .gpio_setup = s3c24xx_spi_gpiocfg_bus0_gpe11_12_13,};static struct spi_board_info s3c2410_spi1_board[] ={ [0] = { .modalias = "spidev", .bus_num = 1, .chip_select = 0, .irq = IRQ_EINT2, .max_speed_hz = 500 * 1000, }};static struct s3c2410_spi_info s3c2410_spi1_platdata = { .pin_cs = S3C2410_GPG(3), .num_cs = 1, .bus_num = 1, .gpio_setup = s3c24xx_spi_gpiocfg_bus1_gpg5_6_7,};
這裡需要瞭解驅動架構,其中移植過程中容易出問題的地方時S3C2410_GPG(2)和S3C2410_GPG(3)兩處地方,網上一般給的原始碼是S3C2410_GPG2,這在2.6.29中可行,但是在2.6.32原始碼中沒有定義S3C2410_GPG2宏定義,要使用S3C2410_GPG(2)宏定義。
在mini2440_devices[]平台數組中添加如下代碼:
&s3c_device_spi0,&s3c_device_spi1,
最後在mini2440_machine_init函數中加入如下代碼:
s3c_device_spi0.dev.platform_data= &s3c2410_spi0_platdata;spi_register_board_info(s3c2410_spi0_board, ARRAY_SIZE(s3c2410_spi0_board));s3c_device_spi1.dev.platform_data= &s3c2410_spi1_platdata;spi_register_board_info(s3c2410_spi1_board, ARRAY_SIZE(s3c2410_spi1_board));
最後需要修改arch/arm/plat-s3c24xx/KConfig檔案
找到如下程式碼片段:
config S3C24XX_SPI_BUS0_GPE11_GPE12_GPE13 bool help SPI GPIO configuration code for BUS0 when connected to GPE11, GPE12 and GPE13.config S3C24XX_SPI_BUS1_GPG5_GPG6_GPG7 bool help SPI GPIO configuration code for BUS 1 when connected to GPG5, GPG6 and GPG7.
修改為
config S3C24XX_SPI_BUS0_GPE11_GPE12_GPE13 bool "S3C24XX_SPI_BUS0_GPE11_GPE12_GPE13" help SPI GPIO configuration code for BUS0 when connected to GPE11, GPE12 and GPE13.config S3C24XX_SPI_BUS1_GPG5_GPG6_GPG7 bool "S3C24XX_SPI_BUS1_GPG5_GPG6_GPG7" help SPI GPIO configuration code for BUS 1 when connected to GPG5, GPG6 and GPG7.
最後我們配置編譯檔案:
make menuconfig
圖1
圖2
圖3
圖4
圖5
最後編譯核心
make zImage
將編譯好的核心匯入開發板,並且編譯Linux Source內建的測試程式,在Documentation/spi下,修改spidev_test.c檔案,將device name改為/dev/spidev1.0
交叉編譯:
arm-linux-gcc -I ~/linux-2.6.32.2/include/ spidev_test.c
將編譯好的檔案下載到開發板上,並且將開發板的SPI MOI和MIO短接,也就是讓SPI自己發送自己接收,執行檔案,我們看到如下結果:
FF FF FF FF FF FF40 00 00 00 00 95FF FF FF FF FF FFFF FF FF FF FF FFFF FF FF FF FF FFDE AD BE EF BA ADF0 0D
說明驅動移植成功。
總結:這裡敘述的是驅動移植詳細過程,代碼的具體含義以及開發板的針腳對應圖需要自己去查閱相關資料,這裡不再詳述。