SPI Communication with MFRC522

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SPI Communication with MFRC522

3,879 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ccavallieri on Fri May 08 10:51:36 MST 2015
Hello everybody.

I'm working on a RFID kit and trying to make it read cards, using a LPC1768.

I got a code from the internet and did some adaptions. I can initialize the antennas but I can't make it "get the tag".

If anyone could help me, I would be grateful.

Original Attachment has been moved to: rc522.c.zip

Original Attachment has been moved to: main_15.c.zip

Original Attachment has been moved to: rc522.h.zip

Labels (1)
0 Kudos
5 Replies

1,990 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by marcossmart on Thu May 12 21:06:27 MST 2016
Please Help me! How I can set registers below to active interrupt (IRQ) without send command via SPI?

After configured registers active IRQ when card is near, without send  command.

Is possible?


Thank you.




WriteRawRC(DivlEnReg, 0x84);           //
WriteRawRC(DivIrqReg, 0x80);           //
WriteRawRC(ComIEnReg, 0xA0);           //
WriteRawRC(ComIrqReg, 0x80);
ClearBitMask(ComIrqReg, 0x80);           //
WriteRawRC(FIFODataReg, PICC_REQIDL);
    WriteRawRC(CommandReg,PCD_TRANSCEIVE);
WriteRawRC(BitFramingReg, 0x87);
0 Kudos

1,990 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kavita on Sun Aug 23 22:15:30 MST 2015
/**
  ******************************************************************************
  * File Name          : main.c
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2015 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"

/* USER CODE BEGIN Includes */
#include "tm_stm32f4_mfrc522.h"
#include "tm_stm32f4_hd44780.h"
#include "matrix.h"
/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
uint8_t CardID[5]="";
uint8_t page=0;
char change;
uint8_t flag0=0,flag1=0,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,flag7=0,flag8=0;
uint8_t Auth_flag=0,p,s,t,x;
extern char trans[8];
extern char*c;
extern volatile char buffer[16];


  char LCD[]="   PLEASE SHOW      RFID TAG    ";
char *lcd;
char LCD0[]="Tag no:";
  char *lcd0;
char VEHICLE[]="  Vehicle no:   ";
char *vehicle;
char VHNO[]="      0000      ";
char *vhno;
  char LCD1[]="PLEASE ENTER Km:" ;
  char *lcd1;
char LCD2[]="    00000.00km " ;
  char *lcd2;
  char LCD3[]="PLEASE ENTER Hr:" ;
  char *lcd3;
  char LCD4[]="    00000.00hr " ;
char *lcd4;
char LCD5[]=" Authenticate?  " ;
  char *lcd5;
  char LCD6[]="  0=NO   1=YES  " ;
char *lcd6;
char LCD7[]="  PRESS  ENTER  ";
char *lcd7;
char LCD8[]="  Waiting for     Authenticate   " ;
  char *lcd8;
char LCD9[]=" Fueling start  ";
  char *lcd9;
  uint8_t store[30];
void store_data(void);
uint8_t key1[]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
uint8_t rec[50],auth;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_USART2_UART_Init(void);

/* USER CODE BEGIN PFP */


/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI1_Init();
  MX_USART2_UART_Init();

  /* USER CODE BEGIN 2 */
TM_HD44780_Init(16,2); /*initialize lcd*/

/*
   PIN CONFIGURATION
  
PA5  -->  SCK
   PA6  -->  MISO
PA7  -->  MOSI
PC4  -->  CS
PA4  --> TO MFRC RST pin
*/
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4, GPIO_PIN_RESET); /* CS PIN */
HAL_Delay(1);


  TM_MFRC522_Init(); /*Initialization of MFRC522  */

  if(TM_MFRC522_Check(CardID) == MI_OK)
{
//
// char LCD[]="Tag no:";
// char *lcd;
// lcd = &LCD[0];
// c =&CardID[0];
// TM_HD44780_Puts(0,0,lcd) ;
// TM_HD44780_Puts(7,0,lcd);
 
}



//  
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */


page=0;

switch(page)

{

case 0: //display please show RFID card

      p=0;
if(flag0==0)
{
    lcd = &LCD[0];
        TM_HD44780_Puts(0,0,lcd) ;

flag0=1;
    }


    if(TM_MFRC522_Check(CardID) == MI_OK) //checks whether the card is there...
        {

if(TM_MFRC522_SelectTag(CardID))
{
//auth=TM_MFRC522_ReadRegister(MFRC522_REG_STATUS2);

    for(auth=4;auth<11;auth++)
     {  

   
     //auth=TM_MFRC522_SelectTag(CardID);

     if(TM_MFRC522_Auth(PICC_AUTHENT1A,4,key1,CardID)==MI_OK)
     {
  page++;
       TM_MFRC522_Read(4,rec);
    // }
auth=TM_MFRC522_ReadRegister(MFRC522_REG_STATUS2);

if(TM_MFRC522_Auth(PICC_AUTHENT1B,4,key1,CardID)==MI_OK)
     {
       TM_MFRC522_Read(4,rec);
     }

//}
      
// }

}
break;


  /* USER CODE END 3 */



/** System Clock Configuration
*/
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

}

/* SPI1 init function */
void MX_SPI1_Init(void)
{

  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  hspi1.Init.CRCPolynomial = 10;
  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLED;
  HAL_SPI_Init(&hspi1);

}

/* USART2 init function */
void MX_USART2_UART_Init(void)
{

  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  HAL_UART_Init(&huart2);

}

/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
*/
void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __GPIOA_CLK_ENABLE();
  __GPIOF_CLK_ENABLE();
  __GPIOC_CLK_ENABLE();
  __GPIOB_CLK_ENABLE();
  __GPIOD_CLK_ENABLE();

  /*Configure GPIO pin : PA0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : PF5 */
  GPIO_InitStruct.Pin = GPIO_PIN_5;
  GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

  /*Configure GPIO pin : PA4 */
  GPIO_InitStruct.Pin = GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : PC4 PC8 PC9 PC12 */
  GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_12;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pins : PB12 PB3 PB4 PB5
                           PB6 PB7 */
  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
                          |GPIO_PIN_6|GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pins : PB13 PB14 PB15 */
  GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pins : PC6 PC7 */
  GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pins : PA8 PA9 PA10 */
  GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : PA15 */
  GPIO_InitStruct.Pin = GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : PD2 */
  GPIO_InitStruct.Pin = GPIO_PIN_2;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

}

/* USER CODE BEGIN 4 */
void store_data()
{
if(p==1)  //stores tag id
{
for(s=0,t=0;s<8;s++,t++)
{
store[s]=trans[t];
}

}
if(p==2)  //stores vehicle no
{
for(s=8,t=6;s<12;s++,t++)
{
store[s]=buffer[t];
}

}
if(p==3)
{
for(s=12,t=4;s<20;s++,t++)
{
store[s]=buffer[t];
}

}
if(p==4)
{
for(s=20,t=4;s<28;s++,t++)
{
store[s]=buffer[t];
}

}

}


/* USER CODE END 4 */

#ifdef USE_FULL_ASSERT

/**
   * @brief Reports the name of the source file and the source line number
   * where the assert_param error has occurred.
   * @param file: pointer to the source file name
   * @param line: assert_param error line source number
   * @retval None
   */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */

}

#endif

/**
  * @}
  */

/**
  * @}
*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
0 Kudos

1,990 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mysepp on Sat Aug 22 06:00:51 MST 2015
Can you show/upload the code for STM, which seems to be at least working more than the code of the original author of the thread?
0 Kudos

1,990 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kavita on Sat Aug 22 05:33:38 MST 2015
hii
I am using MFRC522.
I have read the card id but i am not able to read blocks of RFID card...
Authentication command gives error....
I am using stm controller
can anyone help me...
0 Kudos

1,482 Views
jonathan1
Contributor I

Hello, tell me, please, how did you do the sequence of reading / writing the registers to be able to read the ID? Cheers

0 Kudos