2019-12-18 03:31 AM
here's my initialization code for spi and rc522:
/* USER CODE BEGIN SPI1_Init 1 */
LL_SPI_SetRxFIFOThreshold(SPI1,LL_SPI_RX_FIFO_TH_QUARTER);
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV32;
SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
SPI_InitStruct.CRCPoly = 7;
LL_SPI_Init(SPI1, &SPI_InitStruct);
LL_SPI_SetStandard(SPI1, LL_SPI_PROTOCOL_MOTOROLA);
LL_SPI_DisableNSSPulseMgt(SPI1);
/* USER CODE BEGIN SPI1_Init 2 */
LL_SPI_Enable(SPI1);
void PcdInit(void)
{
GPIOB->BSRR = CS_MASK; //ChipSelect=1; added
PcdReset();
WriteRawRC(ModeReg, 0x3D); //Set the preset value for the CRC coprocessor for the CalcCRC command to 0x6363 (ISO 14443-3 part 6.2.4)
WriteRawRC(TModeReg, 0x80); // TAuto=1; timer starts automatically at the end of the transmission in all communication modes at all speeds
WriteRawRC(TPrescalerReg, 0xA9); // TPreScaler = TModeReg[3..0]:TPrescalerReg, ie 0x0A9 = 169 => f_timer=40kHz, ie a timer period of 25µs.
WriteRawRC(TReloadRegH, 0x03); // Reload timer with 0x3E8 = 1000, ie 25ms before timeout.
WriteRawRC(TReloadRegL, 0xE8);
WriteRawRC(TxASKReg, 0x40); //Force a 100 % ASK modulation independent of the ModGsPReg register setting
WriteRawRC(RFCfgReg, 0x7F); //RxGain = 48dB
ClearBitMask(Status2Reg,0x08); //MIFARE Crypto1=0
PcdAntennaOff();
PcdAntennaOn();
delay_ms(100);
//DELAY_80US;
//DELAY_80US;
}
void PcdReset(void)
{
GPIOB->BSRR = RST_MASK;//Reset=1
DELAY_80US; // change from 10US to 80US Pavan 13-Feb-19
GPIOB->BRR = RST_MASK;//Reset=0
DELAY_80US; // change from 10US to 80US Pavan 13-Feb-19
DELAY_80US;
//DELAY_80US
DELAY_80US; // change from 10US to 80US Pavan 13-Feb-19
GPIOB->BSRR = RST_MASK;//Reset=1
DELAY_80US;
WriteRawRC(CommandReg,PCD_RESETPHASE); //Soft Reset
WriteRawRC(CommandReg,PCD_RESETPHASE);
DELAY_80US;
}
rc522 read and write functions are:
void WriteRawRC(unsigned char Address, unsigned char value)
{
GPIOB->BRR = CS_MASK;//ChipSelect=0;
(void)SPI1_SendReceive((Address<<1)&0x7E); //Send byte through SPI
(void) SPI1_SendReceive(value); //Send byte through SPI
GPIOB->BSRR = CS_MASK; //ChipSelect=1;
}
unsigned char ReadRawRC(unsigned char Address)
{
unsigned char ucResult;
GPIOB->BRR = CS_MASK;//ChipSelect=0;
(void)SPI1_SendReceive(((Address<<1)&0x7E) | 0x80); //Send byte through SPI
ucResult = SPI1_SendReceive(0x00);//Read byte through SPI
GPIOB->BSRR = CS_MASK; //ChipSelect=1;
return ucResult;
}
uint8_t SPI1_SendReceive(uint8_t byData)
{
while(!(SPI1->SR & SPI_SR_TXE));
LL_SPI_TransmitData8(SPI1, byData);
while(!(SPI1->SR & SPI_SR_RXNE));
byData = LL_SPI_ReceiveData8(SPI1);
return byData;
}
after degugging i came to know that spi after few instructions read 00 from rc522 registers in pcdrequest function.:| few days ago it was working on the same code and now again i stuck on the same problem.
please help, :downcast_face_with_sweat: is there any hardware issue or my farmware is not correct?
thanks in advance.........