AnsweredAssumed Answered

help with spi setup with rc522 module

Question asked by shiftybits on Feb 5, 2014
Latest reply on Sep 20, 2017 by Francisco SinapseEnergia
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 (datasheet) 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?

Outcomes