Notes for debugging the RC 522 module on the LINUX platform

Source: Internet
Author: User

Hardware Platform:

1 master: SMDK Exynos4412 POP S5M8767A

2 RFID module: RC522 module provided by jundun Group

3. Communication Interface: SPI

Software Platform: Android ICS & kernel version 3.0.15

1. enable the master SPI

1 hardware enabling:

From the SMDK schematic, we can see that SPI0 is shared with I2C. SPI1 has been connected to other devices, and SPI2 is not used. Therefore, SPI2 is used here.

2 software enabling:

The SPI interface has been configured on the SMDK Exynos4412 master. You only need to enable macro config_jx64xx_dev_spi for use.

Open Method: make menuconfig à Device Drivers à SPI support à Samsung s364xx series type SPI.

After compilation, zImage is generated and burned into the Development Board.

Ii. Test the SPI on the master side

The master SPI has been enabled. Next, you can use a general SPI driver to test whether the master SPI hardware works properly.

Compile the program as a module: drivers/spi/spidev. c to generate spidev. ko, which is a common device-side SPI driver.

Compile the test program: Documentation/spi/spidev_test.c. First, modify the device "/dev/spidev2.0" for line 1: static const char * device = "/dev/spidev1.1 ", then compile the program as an application to generate the spidev_test, which is the test program corresponding to the SPI.

Grant root and system read/write permissions through the serial port:

Shell @ android:/$ su root

Shell @ android: // # mount-o remount-rw/system

[391.423930] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null)

Shell @ android :/#

Push spidev. ko and spidev_test to the Development Board through adb:

Load DRIVER:

Shell @ android:/system # insmod spidev. ko

Short-circuited MISO and MOSI, that is, self-collect, and then run the test program:

As shown in, it indicates that data can be sent and received through SPI. If all data is displayed as 0, it means that SPI is not working properly. Next we can debug our RC522 module with confidence.

3. RC522 Device Driver debugging

The above experiment has proved that the SPI of the master can work normally, and then you can debug RC522. -Assume that your rfid_rc522 driver has been written, and now you only need to debug it-if the driver is not written, read another Blog.

1 open the Development Board related files: arch/arm/mach-exynos/mach-smdk4x12.c

Because spi2 is used, modify modalias = "rfid_rc522" in board_info to match spi_drviver.name in the driver. Otherwise, the probe function fails.


  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 install it on the Development Board.

3. Compile the rc522 driver and copy the generated rfid_rc522.ko to the/system directory of the Development Board system through adb usb. Then, insmod rfid_rc522.ko, in this way, the driver is loaded into the kernel system in the form of modules. After the file is loaded successfully, the directory rfid_rc522_dev is displayed in the/dev directory.

4. Applications

The write function in the driver is:

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

The user space application write function is:

Write (rc522_fd, bufpw1, sizeof (bufpw1 ));

How do they connect?

In fact, the write function in the application is implemented by calling the core function sys_write (unsigned int fd, const char * buf, size_t count) in the operating system, while sys_write () the function encapsulates rc522_write () in the driver.

// Start from Forum

The driver of the character device is described as follows:

1. insmod driver. The device name and master device number applied by the driver can be obtained in/proc/devieces.

2. Obtain the master device number from/proc/devices and run the mknod command to create a device node file. This is to associate the device node file with the device driver by the master device number. The file attribute in the device node file specifies the function pointer implemented by the fops method in the driver.

3. the user program uses open to open the device node file. When the operating system kernel knows that the driver is working, it calls the open function in the fops Method for relevant work. The open method generally returns a File Identifier. In fact, it does not directly operate on it, but the system calls of the operating system work behind the scenes.

4. When you use the write function to operate the device file, the operating system calls the sys_write function. This function first obtains the inode pointer and flip pointer corresponding to the device node file through the file identifier. The inode pointer contains the device number, which tells the operating system which device driver should be used, the flip pointer contains the fops information, and the corresponding fops method function of the operating system can be found there.

5. Then, sys_write will call the write method in the driver to write the device.

Among them, 1-3 is performed in the user space, and 4-5 is performed in the kernel space. The user's write function is associated with the OS's write function by calling sys_write.

Note:

For Block devices, the writing mode still exists. This should be solved by the gnu c library. I will not discuss it here because I have not read the source code of the GNU C library.

// End of the Forum

The application source code is as follows:


  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 \ 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 0th

  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 0th

  47. Ioctl (rc522_fd, READ_CARD, 0); // parameter 3 is useless

  48. For (I = 0; I <3; I ++) // read the Three Card Numbers

  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

  60. Ioctl (rc522_fd, WRITE_CARD, 1 );

  61. Printf ("before write card! \ N ");



  62. Char buf [11] = "186652133xx ";

  63. If (write (rc522_fd, buf, sizeof (buf )))

  64. {

  65. Printf ("write error \ n ");

  66. }

  67. // Read block [1], just writed

  68. Ioctl (rc522_fd, CHANGE_BLOCK, 1); // parameter 3: Select 1st

  69. Ioctl (rc522_fd, READ_CARD, 0); // parameter 3 is useless

  70. Read_num = read (rc522_fd, r, 0 );

  71. Printf ("read block [1] \ n The number you just writed is: % s \ 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 Makefile compiled by the application is as follows:


  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

During the test, close the card to the antenna area of RC522 to read the card ID normally.

V. Summary

This debugging was successful and encountered several major problems as follows:

1 SMDK Development Board SPI0 communication problems, started to think that driver problems, do not know how to test the Development Board SPI interface OK, after finding some information on the Internet, I found that the SPI driver can be tested by the driver modules and applications that come with the kernel.

2. Applications (test programs) cannot run on the development board system because the Linked Library is not set to static.

3 The VCC in RC522 needs 3.3 V, MOSI, CLK, and other TTL high levels, which are also 3.3 V. However, the high levels of GPIO output by the 4412 controller are all 1.8 V, so the module cannot work normally. Due to debugging, it is impossible to add an IC for TTL level conversion. Later, I tried to lower the VCC to 2.6 V, and the result module was working properly.

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.