1:配置管脚为SPI功能

在board-mx6q_sabresd.h的最后添加,复制被重定义

(以添加SPI2为例)

  1. <span style="font-size:18px;">        MX6Q_PAD_EIM_CS0__ECSPI2_SCLK,
  2. MX6Q_PAD_EIM_CS1__ECSPI2_MOSI,
  3. MX6Q_PAD_EIM_OE__ECSPI2_MISO,
  4. MX6Q_PAD_CSI0_DAT11__ECSPI2_SS0 ,
  5. </span>

之后再板级文件board-mx6q_sabresd.c中将有相关函数对管脚进行统一初始化。

完成寄存器配置的工作。

  1. <span style="font-size:18px;">  mxc_iomux_v3_setup_multiple_pads(mx6q_sabresd_pads,
  2. ARRAY_SIZE(mx6q_sabresd_pads));
  3. mxc_iomux_v3_setup_multiple_pads(mx6q_sabresd_cstm_tq_pads,\
  4. ARRAY_SIZE(mx6q_sabresd_cstm_tq_pads));</span>

2:在板级文件board-mx6q_sabresd.c中添加以下代码

将匹配到driver/spi/spidev.c文件中的驱动源码

2.1完成SPI master的注册

SPI2片选管脚宏定义:

  1. <span style="font-size:14px;">#define SABRESD_ECSPI2_CS0 IMX_GPIO_NR(5, 29)</span>

添加相关结构体

  1. <span style="font-size:14px;">static int mx6q_marsboard_spi1_cs[] = {
  2. SABRESD_ECSPI2_CS0,
  3. };
  4. </span>
  1. <span style="font-size:14px;">static const struct spi_imx_master mx6q_sabresd_spi2_data __initconst = {
  2. .chipselect = mx6q_marsboard_spi2_cs,
  3. .num_chipselect = ARRAY_SIZE(mx6q_marsboard_spi1_cs),
  4. };
  5. </span>

2.2在spi 总线上匹配spi2的驱动文件

mx6q平台有2路spi资源,0/1,其中的bus_num则为挂载驱动的总线选择。

  1. <span style="font-size:18px;"><span style="font-size:14px;">static struct mtd_partition imx6_sabrelite_spi_nor_partitions[] = {
  2. {
  3. .name = "bootloader",
  4. .offset = 0,
  5. .size = 0x00100000,
  6. },
  7. {
  8. .name = "kernel",
  9. .offset = MTDPART_OFS_APPEND,
  10. .size = MTDPART_SIZ_FULL,
  11. },
  12. };
  13. static struct flash_platform_data imx6_sabrelite__spi_flash_data = {
  14. .name = "spidev",  / /匹配原则。
  15. .parts = imx6_sabrelite_spi_nor_partitions,
  16. .nr_parts = ARRAY_SIZE(imx6_sabrelite_spi_nor_partitions),
  17. .type = "sst25vf016b",
  18. };
  19. static struct spi_board_info imx6_sabrelite_spi_nor_device[] __initdata = {
  20. {
  21. .modalias = "spidev",
  22. .max_speed_hz = 20000000, /* max spi clock (SCK) speed in HZ */
  23. .bus_num = 1,    //设备挂载第几号spi总线上
  24. .chip_select = 0,
  25. .platform_data = &imx6_sabrelite__spi_flash_data,
  26. },
  27. };
  28. spi_register_board_info(imx6_sabrelite_spi_nor_device,
  29. ARRAY_SIZE(imx6_sabrelite_spi_nor_device));
  30. imx6q_add_ecspi(0, &mx6q_sabrelite_spi2_data);
  31. </span>
  32. </span>

3:内核配置

查看spidev.c文件目录下的Kconfig以及Makefile得知内核驱动的添加方法

选择蓝色部分选项,将spidev.c文件添加到内核中。

4:查看开发板/sys/bus/spi/drivers/spidev目录

在/dev下生成设备文件/dev/spidev1.0

5:使用freescale官方的bsp包中的spi测试程序对接口进行测试

  1. <span style="font-size:12px;">/*
  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.0";
  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. int main(int argc, char *argv[])
  65. {
  66. int ret = 0;
  67. int fd;
  68. //parse_opts(argc, argv); /* for what ,unknow*/
  69. fd = open(device, O_RDWR);
  70. if (fd < 0)
  71. pabort("can't open device");
  72. /*
  73. * spi mode
  74. */
  75. ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
  76. if (ret == -1)
  77. pabort("can't set spi mode");
  78. ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
  79. if (ret == -1)
  80. pabort("can't get spi mode");
  81. /*
  82. * bits per word
  83. */
  84. ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  85. if (ret == -1)
  86. pabort("can't set bits per word");
  87. ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  88. if (ret == -1)
  89. pabort("can't get bits per word");
  90. /*
  91. * max speed hz
  92. */
  93. ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  94. if (ret == -1)
  95. pabort("can't set max speed hz");
  96. ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  97. if (ret == -1)
  98. pabort("can't get max speed hz");
  99. printf("spi mode: %d\n", mode);
  100. printf("bits per word: %d\n", bits);
  101. printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
  102. transfer(fd);
  103. close(fd);
  104. return ret;
  105. }
  106. </span>

执行应用程序,可以看到时序模式(spi 4种时序模式第0种),时钟频率等参数。

    /*
8 * SPI testing utility (using spidev driver)
9 *
10 * Copyright (c) 2007 MontaVista Software, Inc.
11 * Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License.
16 *
17 * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
18 */ #include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h> #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) static void pabort(const char *s)
{
perror(s);
abort();
} static const char *device = "/dev/spidev1.0";
static uint8_t mode;
static uint8_t bits = ;
static uint32_t speed = ;
static uint16_t delay=; static void transfer(int fd)
{
int ret;
uint8_t tx[] = {
0x11, 0x12, 0x18, 0x78, 0x24, 0xa2,
0x40, 0x00, 0x13, 0x00, 0x00, 0x95,
0x23, 0x56, 0x54, 0x34, 0x45, 0x65,
0x69, 0x78, 0x98, 0xFF, 0xFF, 0x36,
};
uint8_t rx[ARRAY_SIZE(tx)] = {, };
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
.cs_change=
}; ret = ioctl(fd, SPI_IOC_MESSAGE(), &tr);
if (ret < )
pabort("can't send spi message"); for (ret = ; ret < ARRAY_SIZE(tx); ret++) {
if (!(ret % ))
puts("");
printf("%.2X ", rx[ret]);
}
puts("");
} static void print_usage(const char *prog)
{
printf("Usage: %s [-DsbdlHOLC3]\n", prog);
puts(" -D --device device to use (default /dev/spidev1.0)\n"
" -s --speed max speed (Hz)\n"
" -d --delay delay (usec)\n"
" -b --bpw bits per word \n"
" -l --loop loopback\n"
" -H --cpha clock phase\n"
" -O --cpol clock polarity\n"
" -L --lsb least significant bit first\n"
" -C --cs-high chip select active high\n"
" -3 --3wire SI/SO signals shared\n");
exit();
} static void parse_opts(int argc, char *argv[])
{
while () {
static const struct option lopts[] = {
{ "device", , , 'D' },
{ "speed", , , 's' },
{ "delay", , , 'd' },
{ "bpw", , , 'b' },
{ "loop", , , 'l' },
{ "cpha", , , 'H' },
{ "cpol", , , 'O' },
{ "lsb", , , 'L' },
{ "cs-high", , , 'C' },
{ "3wire", , , '' },
{ "no-cs", , , 'N' },
{ "ready", , , 'R' },
{ NULL, , , },
};
int c; c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL); if (c == -)
break; switch (c) {
case 'D':
device = optarg;
break;
case 's':
speed = atoi(optarg);
break;
case 'd':
delay = atoi(optarg);
break;
case 'b':
bits = atoi(optarg);
break;
case 'l':
mode |= SPI_LOOP;
break;
case 'H':
mode |= SPI_CPHA;
break;
case 'O':
mode |= SPI_CPOL;
break;
case 'L':
mode |= SPI_LSB_FIRST;
break;
case 'C':
mode |= SPI_CS_HIGH;
break;
case '':
mode |= SPI_3WIRE;
break;
case 'N':
mode |= SPI_NO_CS;
break;
case 'R':
mode |= SPI_READY;
break;
default:
print_usage(argv[]);
break;
}
}
} int main(int argc, char *argv[])
{
int ret = ;
int fd; parse_opts(argc, argv); fd = open(device, O_RDWR);
if (fd < )
pabort("can't open device"); /*
173 * spi mode
174 */
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -)
pabort("can't set spi mode"); ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -)
pabort("can't get spi mode"); /*
184 * bits per word
185 */
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -)
pabort("can't set bits per word"); ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -)
pabort("can't get bits per word"); /*
195 * max speed hz
196 */
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -)
pabort("can't set max speed hz"); ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -)
pabort("can't get max speed hz"); printf("spi mode: %d\n", mode);
printf("bits per word: %d\n", bits);
printf("max speed: %d Hz (%d KHz)\n", speed, speed/); transfer(fd); close(fd); return ret;
}

最新文章

  1. nodejs-基本语法
  2. shell test -n -z
  3. JAAS LOGIN IN WEBLOGIC SERVER--reference
  4. Delphi2010新发现-类的构造和析构函数功能
  5. ios 6 横竖屏转换
  6. 可供VC调用的QT编写的界面DLL方法
  7. WinSpy涉及的windows api
  8. Python学习笔记2
  9. C++ 脑筋急转弯
  10. [SCOI2016]美味
  11. Java学习笔记之——集合
  12. 用crontab部署定时任务
  13. [matlab] 19.matlab 基础几何学
  14. [转] HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换
  15. 【最大公约数&amp;链表】权值 @upcexam5921
  16. 使用POI导出Excel文件
  17. Android--自定义半圆环型进度(带动画)
  18. CLR回收非托管资源
  19. Arduino和C51开发DS1302时钟
  20. Elasticsearch在centos6中的安装

热门文章

  1. 阿里云服务器 端口开放问题 浏览器钟输入ip 访问服务器
  2. 算法之动态规划(最长递增子序列——LIS)
  3. 对象中prototype与__proto__与从cinstructor的作用和区别
  4. MySQL的order by子句
  5. python 中NumPy和Pandas工具包中的函数使用笔记(方便自己查找)
  6. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.3——用Eclipse ADT导出App
  7. Java中Solr集群的测试
  8. 自定义表单验证--jquery validator addMethod的使用
  9. p:nth-last-child(2)
  10. linux 服务器所支持的最大句柄数调高数倍(与服务器的内存数量相关)