linux下的spi驱动及测试程序移植开发

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

 

modalias的名字一定要是spidev,这一点要注意.

然后在函数__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. 最后重新编译内核


重启mini2440,查看/dev下的设备文件

[root@ShiGuang /]# ls /dev/spidev0.0 -l
crw-rw---- 1 root root 153, 0 Jan 1 08:00 /dev/spidev0.0
[root@ShiGuang /]#

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


大笑 测试完毕!OVER!
注意:.speed_hz = speed, 我的测试程序必须要屏蔽这条语句才能正常工作,原因不明.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值