cancel
Showing results for 
Search instead for 
Did you mean: 

help with spi setup with rc522 module

gecono
Associate
Posted on February 05, 2014 at 17:35

Hi-

I'm quite new to stm32 and emededded development so please bear with me.

I've purchased a Sain Smart RC522 RFID reader/writer (

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CDwQFjAA&url=http://www.nxp.com/documents/data_sheet/MFRC522.pdf&ei=uVfyUqd3i7SxBJ-zgfAI&usg=AFQjCNGIP5OuVPk-sObD-ZpLGioCFvUUMg&sig2=g_vdTzuL3Vd5VZbUoqXFnw&bvm=bv.60799247%2cd.cWc

) which has a MFRC522 chip. The chip supports uart, spi and i2c and auto configures by sensing the logic levels on the control pins after a reset as described in the datasheet section 8.1.1. 

After reading through the datasheet i'm attempting to hook it up to my ST-Discovery F4 board using SPI1 as follows:

RC522   STM32F4

---------------

SDA      PA4

SCK      PA5

MOSI     PA6

MISO     PA7

IRQ      NC

GND      GND

RST      3.3

3.3      3.3

I'm having difficulty figuring out the values to use when initializing SPI1 from the datasheet. Currently I have the following :

GPIO_InitTypeDef GPIO_InitStruct;

SPI_InitTypeDef SPI_InitStruct;

/* configure pins used by SPI1

 * PA5 = SCK

 * PA6 = MISO

 * PA7 = MOSI

 */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

// SPI1 nss

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStruct.GPIO_Mode = GPIO_OType_PP;

GPIO_Init(GPIOA, &GPIO_InitStruct);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

SPI_Cmd(SPI1, DISABLE);

SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStruct.SPI_Mode = SPI_Mode_Master;

SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;

SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;

SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;

SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;

SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStruct.SPI_CRCPolynomial=7;

SPI_Init(SPI1,&SPI_InitStruct);

SPI_SSOutputCmd(SPI1, ENABLE);

SPI_Cmd(SPI1,ENABLE); 

// initialzation of RC522 omitted

I've also have functions that reads and write to rc522 register:

uint8_t rfid_readWriteByte(uint8_t data){

    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

    SPI_I2S_SendData(SPI1,data);

    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

    return SPI_I2S_ReceiveData(SPI1);

}

uint8_t rfid_readFromRegister(uint8_t addr) {

  SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Set);

  rfid_readWriteByte(((addr<<1)&0x7E) | 0x80);

  uint8_t val = rfid_readWriteByte(0x00);

  SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Reset);

  return val;

}

I've put together a simple program that initializes the spi interface and attempts to read the firmware version from RC522 register using the code above. The  register read always comes back as 0XFF. 

I'm trying to narrow done where the problem is by looking at the initialzation of the RC522 first and how it is hooked up. Does anyone see anything wrong with my initialization routine and basic read/write methods?

#metoo-not-quite
15 REPLIES 15
chuanut4
Associate II
Posted on December 24, 2014 at 03:22

This is axactly problem from my spi configuration.

I have fixed my proble but forgot thanking you.

Thanks alot.

djtilava
Associate II
Posted on July 31, 2015 at 15:38

Hello

shiftybits,

Have You got any solutions to your problem??

If you got then plz help me because i have same problem as you have.

I am using STM32F0308Discovery kit..

Posted on July 31, 2015 at 18:29

Two posts from over a year ago, on a different board/processor, kind of grasping at straws there.

I for one wouldn't attach the RST pin to the rail.

Suggest you formulate your question around what you actually have in terms of connectivity, and software configuration. And then what behaviour and testing you have observed and done.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 20, 2017 at 21:13

Hello.

First. I have tested one python script with one raspberry and the MFRC522 works fine. It detects UID, it does SelectTag process as ok, it does Authentication process and It reads sector. All rights

https://github.com/mxgxw/MFRC522-python/blob/master/MFRC522.py

 

I have started to have problem when I have migrated this code to STM32F030CC microcontroller. Migrating code was easy. Only SPI process was a little more complicated. Finally I did two versions: one version by simulating MISO, MOSI, SCK and CS with GPIO and another one using SPI1. MISO, MOSI, SCK with SPI1 and CS manually with GPIO.

With these two versions I can read any register from MFRC522, I can detect card and get UID serial number, problem is that output for SelectTAG process is MI_NOTAGERR instead MI_OK, and process for Authentication process is MI_ERR instead MI_OK. First process for detect card  requires to send to FIFO_DATA_REGISTER into MFRC522 only 1 byte, for getting UID serial requires to send to FIFO_DATA_REGISTER 2 bytes. For rest of process the number of bytes are more than 2 bytes (9 or more). 

Someone has worked with this, and knows the problem?

Best regards!

Francis.

MINT
Associate

I am facing the same issue with the STM32L476RG controller i have configured the SPI but i am still not getting any response from RC522 module can anyone help me out what can be the exact issue,

I have converted the Arduino method into STM32L476 and replicated the steps,

i am getting output on Arduino but not on STM32L476.

Thankyou in advance,

MINT

liaifat85
Senior III

Here is a tutorial on how to use MFRC522 with STM32F446 Nucleo board. How can see it for help.

https://blog.embeddedexpert.io/?p=768