{"id":3182,"date":"2025-01-21T14:36:49","date_gmt":"2025-01-21T14:36:49","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=3182"},"modified":"2025-01-21T15:48:09","modified_gmt":"2025-01-21T15:48:09","slug":"stm32-spi-conference-entry-using-rfid-card","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=3182","title":{"rendered":"STM32 SPI: Conference Entry using RFID Card"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-904860705401d715315fcf852441174f wp-block-paragraph\" style=\"color:#5c5c5c\">Some time we need a square door lock or square entry for security reasons. RFID card has a unique ID with 1Kb ram. We can write or read from the memory block. The MFRC522 is a highly integrated reader\/writer IC for contactless communication at 13.56MHz. Since on board we have 27.12MHz frequency, we have to divide the clock. The MFRC522 can operate in SPI, I<sup>2<\/sup>C or UART protocol. In this article we will develop a conference entry with RFID card. In this tutorial we use ST microcontroller with its internal RTC (Real Time Clock) function. You can introduce GSM technology for soft remainder since it has internal calendar with alarm feature. To know more about RFID card reader please goes through my RFID Card Interference section. In Atmel section we learn about SPI communication, the same function works on STM32. Here some useful HAL library for SPI interface as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nHAL_SPI_Transmit\/ HAL_SPI_Transmit_IT\/ HAL_SPI_Transmit_DMA (SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);\nHAL_SPI_Receive\/ HAL_SPI_Receive_IT \/ HAL_SPI_Receive_DMA (SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);\nHAL_SPI_TransmitReceive\/ HAL_SPI_TransmitReceive_IT \/ HAL_SPI_TransmitReceive_DMA (SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">if we use IT no Timeout parameter is needed.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6fedf5b82ab8c8e829674ce0173d4e15 wp-block-paragraph\" style=\"color:#5c5c5c\">It has also Interrupt function like i.e. HAL_SPI_TxCpltCallback(&amp;hspi), HAL_SPI_RxCpltCallback(&amp;hspi) etc.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-1599e972722fbd1a6c7954cec0cfd257 wp-block-paragraph\" style=\"color:#5c5c5c\">In my ATmel section we learn the basic interfacing with RFID. Let\u2019s start your journey with the basic of RFID interface, before start first goes through the datasheet of MFRC522 IC. For SPI communication the 7<sup>th<\/sup> bit represent read\/write &amp; 0<sup>th<\/sup> bit is always 0. So whenever we want to read keep the 7<sup>th<\/sup> bit 1, reaming 6 bit will be the address of the register and 0<sup>th<\/sup> bit will 0. Similarly for writing purpose, changes in the 7<sup>th<\/sup> bit only. The MFRC522 card reader has 7bit unique address to operate as follow-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"100\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/function-15-1024x100.jpg\" alt=\"\" class=\"wp-image-3189\" style=\"width:778px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/function-15-1024x100.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/function-15-300x29.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/function-15-768x75.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/function-15.jpg 1436w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-7ceb380358e5675093c5a504bdf6bc08 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s build our read &amp; write function using STM32F103RCT6 IC with spi2 &amp; GPIOA pin 4 for chip selection<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define CS_SELECT                 HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET)\n#define CS_DESELECT               HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET)\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t readFromRegister(uint8_t addr) \n{\n  uint8_t value&#x5B;2]={((addr&lt;&lt;1)&amp;0x7E)|0x80,0};\n  uint8_t Rx_buffer&#x5B;2];\n  CS_SELECT;\n  \/\/Read Address format: 1XXXXXX0\n  HAL_SPI_TransmitReceive(&amp;hspi2,value,Rx_buffer,2,100);\n  CS_DESELECT;\n  return Rx_buffer&#x5B;1];\n}\n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid writeToRegister(uint8_t addr, uint8_t val) \n{\n  CS_SELECT;\n  uint8_t value&#x5B;2]={(addr&lt;&lt;1)&amp;0x7E,val};\n  uint8_t Rx_buffer&#x5B;2];\n  \/\/Write Address format: 0XXXXXX0\n  HAL_SPI_TransmitReceive(&amp;hspi2,value,Rx_buffer,2,100);\n  CS_DESELECT;\n}\n<\/pre><\/div><\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-c5e3ff5df51194dceffe151b220ee121 wp-block-paragraph\" style=\"color:#5c5c5c\">For example to reset the MFRC522 we have to send command 0x0F to register Command register: 0x01<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid reset(void) \n{\n  writeToRegister(CommandReg, MFRC522_SOFTRESET); \/\/ MFRC522_SOFTRESET: 0x0F\n}  \n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-9c9fb3e666268acbad849f4c7e8f02e0 wp-block-paragraph\" style=\"color:#5c5c5c\">To know all the register please goes though the datasheet of MFRC522. Let\u2019s make another 2 function to set &amp; clear the bit of a register.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid setBitMask(uint8_t addr, uint8_t mask) \n{\n  uint8_t current;\n  current = readFromRegister(addr);\n  writeToRegister(addr, current | mask);\n}\n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid clearBitMask(uint8_t addr, uint8_t mask) \n{\n  uint8_t current;\n  current = readFromRegister(addr);\n  writeToRegister(addr, current &amp; (~mask));\n}\n<\/pre><\/div><\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-c711ed7d2f6eb6649cc91ac1fa4ee767 wp-block-paragraph\" style=\"color:#5c5c5c\">Some of our basic functions are done to communicate with MFRC522 card reader. Now our first step is to initialize the MFRC522 card reader. Let\u2019s initialize the MFRC522 with 24ms timer, 100% ASK, CRC value 0x6363 &amp; turn on antenna.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid begin(void) \n{\n  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,GPIO_PIN_SET);\n  reset();\n  \/\/Timer: TPrescaler*TreloadVal\/6.78MHz = 24ms\n  writeToRegister(TModeReg, 0x8D);   \/\/ Tauto=1; f(Timer) = 6.78MHz\/TPreScaler\n  writeToRegister(TPrescalerReg, 0x3E);  \/\/ TModeReg&#x5B;3..0] + TPrescalerReg\n  writeToRegister(TReloadRegL, 30);  writeToRegister(TReloadRegH, 0);\n  writeToRegister(TxAutoReg, 0x40);      \/\/ 100%ASK\n  writeToRegister(ModeReg, 0x3D);        \/\/ CRC initial value 0x6363\n  setBitMask(TxControlReg, 0x03);        \/\/ Turn antenna on.\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-93b5b11536dccaa2464c3ed942f0c8c1 wp-block-paragraph\" style=\"color:#5c5c5c\">For reference use <a href=\"https:\/\/github.com\/Hamid-R-Tanhaei\/RFID-MIFARE-RC522-ARM-STM32\">https:\/\/github.com\/Hamid-R-Tanhaei\/RFID-MIFARE-RC522-ARM-STM32<\/a>. When we place a RF card it will return 64bits of data pattern. We just use first 4bits, since the first 4bits are the unique address of the RFID Card. We can also read &amp; write some data to the RF ID card since it has 1kb memory. Let\u2019s start our project with some RFID card reading &amp; note the resultant value to a public name. You can use OLED for display the card number. Here I use debug feature &amp; also I<sup>2<\/sup>C OLED for data readout.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t FoundTag,ReadTag,TagData&#x5B;16]; \/\/ Variable used to store Full Tag Data\nFoundTag = requestTag(MF1_REQIDL, TagData); \nif (FoundTag == 0x00) ReadTag = antiCollision(TagData);\nsprintf(display_tag,&quot; %2X,%2X,%2X,%2X &quot;,TagData&#x5B;0],TagData&#x5B;1],TagData&#x5B;2],TagData&#x5B;3]);\nOLCD_write_string(6,0,display_tag);\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-ae061357ec197c84719bd9b38d2e17f1 wp-block-paragraph\" style=\"color:#5c5c5c\">Here TagData returns 4bits unique address. Assign some name to store the data i.e.<\/p>\n\n\n\n<ul style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color wp-elements-22a062ae8bb4cee64a15dbeaab3568a2\">\n<li>0xB3: 0x4A: 0x63: 0xA9 \u2013 Subeer Sarkar -> name1<\/li>\n\n\n\n<li>0x13: 0xF9: 0xFF: 0xFA \u2013 Swarnolata\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ->\u00a0 name2<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-bb8ddc1e52fd491cc8e55005defba26c wp-block-paragraph\" style=\"color:#5c5c5c\">Above is the reading data of 2 RFID card. Using those card we just use condition statement to turn ON\/OFF a GPIO pin, here I used GPIOA -> Pin8. The basic code as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t Card1&#x5B;4]={0xB3,0x4A,0x63,0xA9}; \/\/ Subeer Sarkar\nuint8_t Card2&#x5B;4]={0x13,0xF9,0xFF,0xFA}; \/\/ Swarnolata\nHAL_RTC_GetTime(&amp;hrtc,&amp;sTime,RTC_FORMAT_BCD);\nHAL_RTC_GetDate(&amp;hrtc,&amp;sDate,RTC_FORMAT_BCD);\nsprintf(display_time,&quot; %02X:%02X:%02X &quot;,sTime.Hours,sTime.Minutes,sTime.Seconds);\nOLCD_write_string(3,0,display_time);\nsprintf(display_time,&quot; %02X\/%02X\/%02X %s&quot;,sDate.Date,sDate.Month,sDate.Year,day_name&#x5B;sDate.WeekDay+1]);\n\t  OLCD_write_string(4,0,display_time);\nif((Card1&#x5B;0]==TagData&#x5B;0])&amp;&amp;(Card1&#x5B;1]==TagData&#x5B;1])&amp;&amp;(Card1&#x5B;2]==TagData&#x5B;2])&amp;&amp;(Card1&#x5B;3]==TagData&#x5B;3]))\n\t {\n\t\t OLCD_write_string(5,0,&quot;Subeer Sarkar&quot;);            \/\/ person name here\n         OLCD_write_string(6,0,display_time);\n\t\t HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);  \/\/ open for 10s\n\t\t HAL_Delay(10000);\n\t\t HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);\n\t\t OLCD_write_string(5,0,&quot;               &quot;);\n         OLCD_write_string(6,0,&quot;               &quot;);\n\t     }\nelse if((Card2&#x5B;0]==TagData&#x5B;0])&amp;&amp;(Card2&#x5B;1]==TagData&#x5B;1])&amp;&amp;(Card2&#x5B;2]==TagData&#x5B;2])&amp;&amp;(Card2&#x5B;3]==TagData&#x5B;3]))\n     {\n\t\t OLCD_write_string(5,0,&quot;Swarnolata   &quot;);            \/\/ person name here\n         OLCD_write_string(6,0,display_time);\n\t\t HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);  \/\/ open for 10s\n\t\t HAL_Delay(10000);\n\t\t HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);\n\t\t OLCD_write_string(5,0,&quot;               &quot;);\n         OLCD_write_string(6,0,&quot;               &quot;);\n\t }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-7b3e6902d1ef024b27acac310bf3a665 wp-block-paragraph\" style=\"color:#5c5c5c\">You can introduce as many cards as you wanted &amp; add more else if loop. Here I use internal RTC clock. For most squared entry you can introduce GSM SMS system to send the date &amp; time to the selected person for conference. Assume the conference time 6<sup>th<\/sup> January 2025 at 10am to 11am &amp; the control logic works according to that time and card only works on that interval,\u00a0 the logic will be \u2013<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nFoundTag = requestTag(MF1_REQIDL, TagData); \nif(sTime.Hours == 0x10 &amp;&amp; sDate.Date == 0x06 &amp;&amp;  sDate.Month == 0x01)\nif (FoundTag == 0x00) ReadTag = antiCollision(TagData); \/\/ Read tag only at 10am, 06\/01\/25\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"STM32: RFID Card  \u25b6 Conference Entry\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/eKQyQwRUT0o?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"926\" height=\"477\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/RFID-Interface-STM32.jpg\" alt=\"\" class=\"wp-image-3221\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/RFID-Interface-STM32.jpg 926w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/RFID-Interface-STM32-300x155.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/RFID-Interface-STM32-768x396.jpg 768w\" sizes=\"(max-width: 926px) 100vw, 926px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"336\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/RFID-Card-1024x336.jpg\" alt=\"\" class=\"wp-image-2107\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/RFID-Card-1024x336.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/RFID-Card-300x98.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/RFID-Card-768x252.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/RFID-Card.jpg 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">MFRC522 \ud83d\udcf1 SPI Interface \u25b6 RFID Card Reader<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Some time we need a square door lock or square entry for security reasons. RFID card has a unique ID with 1Kb ram. We can write or read from the memory block. The MFRC522 is a highly integrated reader\/writer IC for contactless communication at 13.56MHz. Since on board we have 27.12MHz frequency, we have to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-3182","post","type-post","status-publish","format-standard","hentry","category-stm-arm-tutorials"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3182"}],"version-history":[{"count":24,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3182\/revisions"}],"predecessor-version":[{"id":3222,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3182\/revisions\/3222"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}