Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1584952
  • 博文数量: 239
  • 博客积分: 1760
  • 博客等级: 上尉
  • 技术积分: 1595
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-08 23:53
文章分类

全部博文(239)

文章存档

2016年(1)

2015年(28)

2014年(53)

2013年(42)

2012年(50)

2011年(65)

分类: LINUX

2015-09-10 16:03:35

Linux2.6.32下SPI驱动的移植如下图所示:

下面需要修改部分内核代码,具体操作如下:

1.  修改arch/arm/mach-s3c2440/mach-mini2440.c文件

在include头文件代码行之后增加如下代码

//spi  add by shiguang 
#include <linux/spi/spi.h> 
#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, 
}; 
//end add spi 

 

然后在函数__initmini2440_machine_init的开头增加下列代码

//spi add by shiguang

s3c_device_spi0.dev.platform_data=&s3c2410_spi0_platdata;

spi_register_board_info(s3c2410_spi0_board,ARRAY_SIZE(s3c2410_spi0_board));

//end spi

 

在mini2440_devices数组的最后中添加

&s3c_device_spi0,// add by shiguang

 

2.修改drivers/spi/spi_s3c24xx.c文件

在文件开头增加下列代码

//add by shiguang

#include <mach/regs-gpio.h>

 

在s3c24xx_spi_initialsetup函数结尾增加下列代码

// add by shiguang

s3c2410_gpio_cfgpin(hw->pdata->pin_cs,S3C2410_GPIO_OUTPUT);

s3c2410_gpio_cfgpin(0x8B, S3C2410_GPIO_SFN2);

s3c2410_gpio_cfgpin(0x8C, S3C2410_GPIO_SFN2);

s3c2410_gpio_cfgpin(0x8D, S3C2410_GPIO_SFN2);

// end add

3. 最后重新编译内核

4. 应用程序测试

测设程序取至Linux源码包下的/home/youshan/linux-2.6.32.2/Documentation/spi/spidev_test.c ,这里我再把它贴一遍。

点击(此处)折叠或打开

  1. /*
  2.      * SPI testing utility (using spidev driver)
  3.      *
  4.      * Copyright (c) 2007 MontaVista Software, Inc.
  5.      * Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
  6.      *
  7.      * This program is free software; you can redistribute it and/or modify
  8.      * it under the terms of the GNU General Public License as published by
  9.      * the Free Software Foundation; either version 2 of the License.
  10.      *
  11.      * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
  12.      */
  13.       
  14.     #include <stdint.h>
  15.     #include <unistd.h>
  16.     #include <stdio.h>
  17.     #include <stdlib.h>
  18.     #include <getopt.h>
  19.     #include <fcntl.h>
  20.     #include <sys/ioctl.h>
  21.     #include <linux/types.h>
  22.     #include <linux/spi/spidev.h>
  23.       
  24.     #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  25.       
  26.     static void pabort(const char *s)
  27.     {
  28.             perror(s);
  29.             abort();
  30.     }
  31.       
  32.     static const char *device = "/dev/spidev1.1";
  33.     static uint8_t mode;
  34.     static uint8_t bits = 8;
  35.     static uint32_t speed = 500000;
  36.     static uint16_t delay;
  37.       
  38.     static void transfer(int fd)
  39.     {
  40.             int ret;
  41.             uint8_t tx[] = {
  42.                     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  43.                     0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
  44.                     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  45.                     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  46.                     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  47.                     0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
  48.                     0xF0, 0x0D,
  49.             };
  50.             uint8_t rx[ARRAY_SIZE(tx)] = {0, };
  51.             struct spi_ioc_transfer tr = {
  52.                     .tx_buf = (unsigned long)tx,
  53.                     .rx_buf = (unsigned long)rx,
  54.                     .len = ARRAY_SIZE(tx),
  55.                     .delay_usecs = delay,
  56.                     .speed_hz = speed,
  57.                     .bits_per_word = bits,
  58.             };
  59.       
  60.             ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  61.             if (ret == 1)
  62.                     pabort("can't send spi message");
  63.       
  64.             for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
  65.                     if (!(ret % 6))
  66.                             puts("");
  67.                     printf("%.2X ", rx[ret]);
  68.             }
  69.             puts("");
  70.     }
  71.       
  72.     static void print_usage(const char *prog)
  73.     {
  74.             printf("Usage: %s [-DsbdlHOLC3]\n", prog);
  75.             puts(" -D --device device to use (default /dev/spidev1.1)\n"
  76.                  " -s --speed max speed (Hz)\n"
  77.                  " -d --delay delay (usec)\n"
  78.                  " -b --bpw bits per word \n"
  79.                  " -l --loop loopback\n"
  80.                  " -H --cpha clock phase\n"
  81.                  " -O --cpol clock polarity\n"
  82.                  " -L --lsb least significant bit first\n"
  83.                  " -C --cs-high chip select active high\n"
  84.                  " -3 --3wire SI/SO signals shared\n");
  85.             exit(1);
  86.     }
  87.       
  88.     static void parse_opts(int argc, char *argv[])
  89.     {
  90.             while (1) {
  91.                     static const struct option lopts[] = {
  92.                             { "device", 1, 0, 'D' },
  93.                             { "speed", 1, 0, 's' },
  94.                             { "delay", 1, 0, 'd' },
  95.                             { "bpw", 1, 0, 'b' },
  96.                             { "loop", 0, 0, 'l' },
  97.                             { "cpha", 0, 0, 'H' },
  98.                             { "cpol", 0, 0, 'O' },
  99.                             { "lsb", 0, 0, 'L' },
  100.                             { "cs-high", 0, 0, 'C' },
  101.                             { "3wire", 0, 0, '3' },
  102.                             { "no-cs", 0, 0, 'N' },
  103.                             { "ready", 0, 0, 'R' },
  104.                             { NULL, 0, 0, 0 },
  105.                     };
  106.                     int c;
  107.       
  108.                     c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);
  109.       
  110.                     if (c == -1)
  111.                             break;
  112.       
  113.                     switch (c) {
  114.                     case 'D':
  115.                             device = optarg;
  116.                             break;
  117.                     case 's':
  118.                             speed = atoi(optarg);
  119.                             break;
  120.                     case 'd':
  121.                             delay = atoi(optarg);
  122.                             break;
  123.                     case 'b':
  124.                             bits = atoi(optarg);
  125.                             break;
  126.                     case 'l':
  127.                             mode |= SPI_LOOP;
  128.                             break;
  129.                     case 'H':
  130.                             mode |= SPI_CPHA;
  131.                             break;
  132.                     case 'O':
  133.                             mode |= SPI_CPOL;
  134.                             break;
  135.                     case 'L':
  136.                             mode |= SPI_LSB_FIRST;
  137.                             break;
  138.                     case 'C':
  139.                             mode |= SPI_CS_HIGH;
  140.                             break;
  141.                     case '3':
  142.                             mode |= SPI_3WIRE;
  143.                             break;
  144.                     case 'N':
  145.                             mode |= SPI_NO_CS;
  146.                             break;
  147.                     case 'R':
  148.                             mode |= SPI_READY;
  149.                             break;
  150.                     default:
  151.                             print_usage(argv[0]);
  152.                             break;
  153.                     }
  154.             }
  155.     }
  156.       
  157.     int main(int argc, char *argv[])
  158.     {
  159.             int ret = 0;
  160.             int fd;
  161.       
  162.             parse_opts(argc, argv);
  163.       
  164.             fd = open(device, O_RDWR);
  165.             if (fd < 0)
  166.                     pabort("can't open device");
  167.       
  168.             /*
  169.              * spi mode
  170.              */
  171.             ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
  172.             if (ret == -1)
  173.                     pabort("can't set spi mode");
  174.       
  175.             ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
  176.             if (ret == -1)
  177.                     pabort("can't get spi mode");
  178.       
  179.             /*
  180.              * bits per word
  181.              */
  182.             ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  183.             if (ret == -1)
  184.                     pabort("can't set bits per word");
  185.       
  186.             ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  187.             if (ret == -1)
  188.                     pabort("can't get bits per word");
  189.       
  190.             /*
  191.              * max speed hz
  192.              */
  193.             ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  194.             if (ret == -1)
  195.                     pabort("can't set max speed hz");
  196.       
  197.             ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  198.             if (ret == -1)
  199.                     pabort("can't get max speed hz");
  200.       
  201.             printf("spi mode: %d\n", mode);
  202.             printf("bits per word: %d\n", bits);
  203.             printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
  204.       
  205.             transfer(fd);
  206.       
  207.             close(fd);
  208.       
  209.             return ret;
  210.     }
只需将mini2440的SPI rx和tx短接就可以收到数据。结果如下图所示:
测试完毕!OVER!

转载自:http://blog.csdn.net/stephen_yu/article/details/7205671
阅读(5891) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~