RC 522 module on Linux Platform debug notes

Source: Internet
Author: User

Hardware platform:

1 master control: SMDK Exynos4412 POP s5m8767a

2 RFID module: RC522 module provided by June Shield Group

3 Communication interface: SPI

Software platform: Android ICS & Kernel version 3.0.15

One, enable the main control side SPI

1 hardware enable:

From the SMDK schematic can be seen SPI0 and I²c Common, SPI1 has been connected to other devices, SPI2 unused, so the use of SPI2 here.

2 software enable:

SMDK Exynos4412 The main control side has been configured with the SPI interface, only need to open the macro Config_s3c64xx_dev_spi.

Open mode: Make Menuconfigàdevice driversàspi Supportàsamsung s3c64xx series type SPI.

Build zimage after compiling, burn into development board.

Second, test the main control side SPI

The master side SPI is turned on, and a generic SPI driver can be used to test whether the SPI hardware on the master side is working properly.

Compile as a module: DRIVERS/SPI/SPIDEV.C, generating Spidev.ko, is a generic device-side SPI driver.

Compile the test program: DOCUMENTATION/SPI/SPIDEV_TEST.C, first modify the 32nd line: static const char *device = "/dev/spidev1.1" Device is "/dev/spidev2.0", It then compiles the application to generate Spidev_test, which is the test program that corresponds to the SPI.

Through the serial port, the root authority and the system can read and Write permissions:

[Email protected]:/$ su root

[Email protected]:/# mount-o Remount-rw/system

[391.423930] Ext4-fs (MMCBLK0P2): re-mounted. Opts: (NULL)

[Email protected]:/#

Push the Spidev.ko and spidev_test to the Development Board via ADB:

Load driver:

[Email Protected]:/system # insmod Spidev.ko

Short-circuiting The miso and mosi, i.e. spontaneous self-collection, and then execute the test procedure:

As shown, the data can be sent and received via the SPI, and if all is displayed as 0, the SPI is not working properly. Next, you can safely debug our RC522 module.

Three, RC522 device side Drive debugging

The above experiment has proved that the SPI of the main control can work properly, and then the RC522 can be debugged formally. -Assuming that your rfid_rc522 driver has been written, now only need to debug-if the driver is not written well, see another blog.

1 Open the file associated with the Development Board: ARCH/ARM/MACH-EXYNOS/MACH-SMDK4X12.C

Because of the use of spi2, it is to modify the Board_info Modalias = "rfid_rc522", and the Spi_drviver.name in the driver to match, otherwise probe function is unsuccessful.

Click ( here) to collapse or open

    1. 988 static struct Spi_board_info spi2_board_info[] __initdata = {
    2. 989 {
    3. 990. Modalias = "rfid_rc522",
    4. 991. Platform_data = NULL,
    5. 992. max_speed_hz = 10*1000*1000,
    6. 993. Bus_num = 2,
    7. 994. Chip_select = 0,
    8. 995. Mode = Spi_mode_0,
    9. 996. Controller_data = &spi2_csi[0],
    10. 997}
    11. 998};


2 recompile the kernel and burn it to the Development Board.

3 Compile the rc522 driver and copy the generated Rfid_rc522.ko to the/system directory of the Development Board system via ADB USB, then Insmod Rfid_rc522.ko so that the driver is loaded into the kernel system in the form of a module. After the load is successful, the directory will be rfid_rc522_dev in the/dev directory.

Four, application

The write function in the drive is:

Rc522_write (struct file *filp, const char *BUF, size_t count, loff_t *f_pos);

The application write function for user space is:

Write (rc522_fd, BUFPW1, sizeof (BUFPW1));

How do they relate to each other?

In fact, the write function in the application is implemented by invoking the core function sys_write (unsigned int fd, const char * buf, size_t count) in the operating system, and the Sys_write () function is also in the driver rc522_ Write () is encapsulated.

From the beginning of the forum

The following is a character device driver to specify:

1,insmod driver. The driver requests the secondary device name and the main device number, which can be obtained in/proc/devieces.

2, obtain the main device number from/proc/devices and use the Mknod command to set up the device node file. This is the device node file and device driver that is associated with the main device number. The function pointers implemented by the FoPs method in the driver are indicated in the file property in the device node files.

3, the user program opens the device node file using open, when the operating system kernel knows that the driver is working, and then calls the Open function in the FoPs method to do the corresponding work. The Open method typically returns a file identifier that is not actually manipulated directly, but rather has a system call from the operating system working behind it.

4, when the user uses the Write function to manipulate the device file, the operating system calls the Sys_write function, which first obtains the inode pointer and the flip pointer for the device node file through the file identifier. The inode pointer has the device number information that tells the operating system which device driver to use, and the flip pointer has fops information to tell the operating system that the appropriate FoPs method function can be found there.

5, then Sys_write will call the Write method in the driver to do the writing to the device.

1-3 of them are in user space, and 4-5 are in kernel space. The write function of the user and the write function of the operating system are associated with the system call Sys_write.

Attention:

For block devices, there is also the problem of write mode, which should be solved by the GNU C Library, which is not discussed here, because I have not seen the GNU C Library source code.

From the end of the forum

The application source code is as follows:

Click ( here) to collapse or open

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <arpa/inet.h>
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <sys/ioctl.h>
  13. #include <math.h>
  14. static enum Io_cmd {
  15. Read_card = 0,
  16. CHANGE_PASSWD = 1,
  17. Change_block = 3,
  18. Set_rw_time = 4,
  19. Write_card = 5,
  20. };
  21. int main (int argc, char** argv)
  22. {
  23. int rc522_fd;
  24. int I, read_num;
  25. Char r[256];
  26. printf ("test:rc522%s%s\n", __date__, __time__);
  27. printf ("Test:before open rc522_fd\n");
  28. RC522_FD = open ("/dev/rfid_rc522_dev", O_RDWR);
  29. printf ("test:rc522_fd=%d\n", RC522_FD);
  30. if (rc522_fd = =-1)
  31. {
  32. printf ("Test:error Opening rc522\n");
  33. Return (-1);
  34. }
  35. printf ("test:wait 01\n");
  36. Sleep (1); Wait
  37. printf ("test:wait 02\n");
  38. /******* never to open ********/
  39. #if 0
  40. Change Password as:020202020202
  41. IOCTL (RC522_FD, Change_block, 0);//Parameter 3: Select NO. 0 Block
  42. IOCTL (RC522_FD, CHANGE_PASSWD, 0);
  43. Char Bufpw1[6] = {0XFF,0XFF,0XFF,0XFF,0XFF,0XFF};
  44. Write (rc522_fd, BUFPW1, sizeof (BUFPW1));
  45. #endif
  46. IOCTL (RC522_FD, Change_block, 0);//Parameter 3: Select NO. 0 Block
  47. IOCTL (RC522_FD, Read_card, 0);//Parameter 3 useless
  48. for (i = 0; i < 3; i++)//read three times card number
  49. {
  50. Read_num = Read (RC522_FD, r, 0);
  51. printf ("test:i=%d read_num=%d", I, read_num);
  52. if (Read_num > 0) {
  53. printf ("r=[0x%.2x]", r[0]);
  54. }
  55. printf ("\ n");
  56. Sleep (1);
  57. }
  58. Write something to the card
  59. IOCTL (RC522_FD, Change_block, 1);//Parameter 3: SELECT 2nd block
  60. IOCTL (RC522_FD, Write_card, 1);
  61. printf ("Before write card!\n");
  62. Char buf[11] = "186653803xx";
  63. if (Write (RC522_FD, buf,sizeof (BUF)))
  64. {
  65. printf ("Write error\n");
  66. }
  67. Read Block[1], just writed with
  68. IOCTL (RC522_FD, Change_block, 1);//Parameter 3: SELECT 1th block
  69. IOCTL (RC522_FD, Read_card, 0);//Parameter 3 useless
  70. Read_num = Read (RC522_FD, r, 0);
  71. printf ("read block[1]\n\n\n the number just writed is:%s\n\n\n", R);
  72. printf ("Test:close rc522_fd\n");
  73. Close (RC522_FD);
  74. printf ("Test:exit rc522_fd\n");
  75. return 0;
  76. }


The application compiles makefile as follows:

Click ( here) to collapse or open

  1. # comment/uncomment The following line to Disable/enable debugging
  2. #DEBUG = y
  3. Dest_bin_dir= drivers/
  4. Extra_cflags + =-d_v3
  5. #TESTFLAGS =-d_v3
  6. # ADD your debugging flag (or not) to CFLAGS
  7. Ifeq ($ (DEBUG), y)
  8. Debflags =-o-g-dscull_debug # "-O" is needed to expand inlines
  9. Else
  10. Debflags =-o2
  11. endif
  12. Extra_cflags + = $ (debflags)
  13. Extra_cflags + =-i$ (lddinc)
  14. Extra_cflags + =-drev_version=$ (rev_version)
  15. Ldflags + =--static
  16. All:test
  17. Clean
  18. RM-RF test_rc522
  19. Cp:
  20. Cp-f test_rc522 $ (dest_bin_dir)
  21. Mv:
  22. Mv-f test_rc522 $ (dest_bin_dir)
  23. Test
  24. ARM-LINUX-GCC $ (CFLAGS) $ (ldflags)-o2 Test_rc522.c-o test_rc522
  25. Depend. Depend DEP:
  26. $ (CC) $ (CFLAGS)-M *.c >. Depend
  27. Ifeq (. depend,$ (wildcard. Depend))
  28. Include. Depend
  29. endif


When the test, the card close to the RC522 antenna area, you can normally read the card ID.

Five, summary

This debugging is quite smooth, encountered several relatively big problems as follows:

1 SMDK Development Board SPI0 Communication problems, began to always think that the driver of the problem, do not know how to test the Development Board SPI interface is OK, after finding some information on the Internet, the SPI driver can be tested through the kernel's own driver modules and applications.

2 The Application (test program) cannot run on the board system because the link library is not set to static.

The 3 RC522 VCC power supply requires a TTL high of 3.3v,mosi,clk and so on is also 3.3V. However, the high level of the 4412 Master Gpio output is all 1.8V, so the module does not work properly. Because it is debugging, it is impossible to add a TTL level conversion IC. Later, we tried to lower VCC to 2.6V, and the result module worked.

Reproduced

RC 522 module on Linux Platform debug notes

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.