Adding SPI Resources on the Freescale Mx6q platform

Source: Internet
Author: User

1: Configure PIN for SPI function

At the end of the board-mx6q_sabresd.h, the copy is redefined

(To add SPI2 as an example)

[CPP]View Plaincopy
    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>


After that, the board-level file BOARD-MX6Q_SABRESD.C will have related functions for the unified initialization of the PIN.

Complete the operation of the Register configuration.

[CPP]View Plaincopy
    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: Add the following code in the board-level file BOARD-MX6Q_SABRESD.C

Will match the driver source in the Driver/spi/spidev.c file

2.1 Completing the SPI Master Registration

SPI2 Blade pin-Select macro definition:

[CPP]View Plaincopy
    1. <span style="FONT-SIZE:14PX;" > #define SABRESD_ECSPI2_CS0 IMX_GPIO_NR (5) </span>


Adding related structures

[CPP]View Plaincopy
    1. <span style="FONT-SIZE:14PX;" >static int mx6q_marsboard_spi1_cs[] = {
    2. Sabresd_ecspi2_cs0,
    3. };
    4. </span>


[CPP]View Plaincopy
    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 Matching SPI2 driver files on the SPI bus

The MX6Q platform has 2 SPI resources and 0/1, of which the Bus_num is the bus selection of the mounted drive.

[CPP]View Plaincopy
  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",//matching principle.
  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, //device Mount number on the SPI bus
  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: Kernel configuration

View Kconfig in the spidev.c file directory and makefile Learn how to add kernel drivers

Select the Blue section option to add the spidev.c file to the kernel.

4: View the Development Board/sys/bus/spi/drivers/spidev directory

Generating device files in/dev/dev/spidev1.0

5: Test the interface using the SPI test program in the Freescale official BSP Package

[CPP]View Plaincopy
  1. <span style="FONT-SIZE:12PX;" >/*
  2. * SPI Testing utility (using Spidev driver)
  3. *
  4. * Copyright (c) MontaVista Software, Inc.
  5. * Copyright (c) Anton Vorontsov <[email protected]>
  6. *
  7. * This program was 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>


Execution of the application, you can see the timing mode (SPI 4 timing mode No. 0), clock frequency and other parameters.

Adding SPI Resources on the Freescale Mx6q platform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.