Other Parts Discussed in Thread: CC3235SF
HI Ti,
i want to established firmware version of PN532 Rfid(slave) through i2c communication with CC3232sf(Master) but i try many times but failed so check my code and solved this.
uint32_t getFirmwareVersion(void)
{
uint32_t response;
pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
if (!sendCommandCheckAck(pn532_packetbuffer, 1, 1000))
{
return 0;
}
// read data packet
readdata(pn532_packetbuffer, 12);
// check some basic stuff
if (0!= memcmp((char*) pn532_packetbuffer,
(char*) pn532response_firmwarevers, 6))
{
return 0;
}
int offset = 6;
response = pn532_packetbuffer[offset++];
response <<= 8;
response |= pn532_packetbuffer[offset++];
response <<= 8;
response |= pn532_packetbuffer[offset++];
response <<= 8;
response |= pn532_packetbuffer[offset++];
return response;
}
bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout)
{
// write the command
writecommand(cmd, cmdlen);
// Wait for chip to say its ready!
if (!waitready(timeout))
{
return false;
}
// read acknowledgement
if (!readack())
{
#ifdef PN532DEBUG
printf("No ACK frame received!\n\r");
#endif
return false;
}
// For SPI only wait for the chip to be ready again.
// This is unnecessary with I2C.
if (!waitready(timeout))
{
return false;
}
return true; // ack'd command
}
/**************************************************************************/
/*!
@brief Writes a command to the PN532, automatically inserting the
preamble and required frame details (checksum, len, etc.)
@param cmd Pointer to the command buffer
@param cmdlen Command length in bytes
*/
/**************************************************************************/
void writecommand(uint8_t *cmd, uint8_t cmdlen)
{
// I2C command write.
uint8_t checksum;
uint8_t txBuffer[9];
uint8_t rxBuffer[0];
cmdlen++;
usleep(2000);
#ifdef PN532DEBUG
Display_printf(display, 0, 0, "\nSending: ");
#endif
// I2C START
i2cTransaction.slaveAddress = PN532_I2C_ADDRESS;
checksum = PN532_PREAMBLE + PN532_PREAMBLE + PN532_STARTCODE2;
txBuffer[0] = PN532_PREAMBLE; // 0x00
txBuffer[1] = PN532_PREAMBLE; // 0x00
txBuffer[2] = PN532_STARTCODE2;// 0xFF
txBuffer[3] = cmdlen;
txBuffer[4] = ~cmdlen + 1;
txBuffer[5] = PN532_HOSTTOPN532;// 0xD4
checksum += PN532_HOSTTOPN532;
uint8_t i = 0;
for (i = 0; i < cmdlen - 1; i++)
{
txBuffer[6]=(cmd[i]);
// i2cTransaction.writeBuf = &cmd[i];
// i2cTransaction.readBuf = rxBuffer;
// i2cTransaction.writeCount = strlen((const char*)txBuffer);
// i2cTransaction.readCount = 0;
// I2C_transfer(i2c, &i2cTransaction);
checksum += cmd[i];
#ifdef PN532DEBUG
Display_printf(display, 0, 0," 0x");
Display_printf(display, 0, 0, "%x", (byte)cmd[i]);
#endif
}
txBuffer[7] = ~checksum;
// i2cTransaction.writeBuf = &txBuffer[7];
// i2cTransaction.readBuf = rxBuffer;
// i2cTransaction.writeCount = strlen((const char*)txBuffer);
// i2cTransaction.readCount = 0;
// I2C_transfer(i2c, &i2cTransaction);
txBuffer[8] = PN532_POSTAMBLE;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.writeCount = 9;
i2cTransaction.readCount = 0;
I2C_transfer(i2c, &i2cTransaction);
// I2C STOP
I2C_close(i2c);
/**************************************************************************/
/*!
@brief Return true if the PN532 is ready with a response.
*/
/**************************************************************************/
bool isready()
{
uint8_t x;
x = GPIO_read(IRQ_PIN);
if (x==0)
{
return 1;
} else {
return 0;
}
}
/*!
@brief Waits until the PN532 is ready.
@param timeout Timeout before giving up
*/
/**************************************************************************/
bool waitready(uint16_t timeout)
{
uint16_t timer = 0;
while (!isready())
{
if (timeout != 0)
{
timer += 10;
if (timer > timeout)
{
Display_printf(display, 0, 0, "waitready TIMEOUT!\n\r");
return false;
}
}
}
return true;
}
/*!
@brief Tries to read the SPI or I2C ACK signal
*/
/**************************************************************************/
bool readack()
{
uint8_t ackbuff[6];
readdata(ackbuff, 6);
return (0 == memcmp((char*) ackbuff, (char*) pn532ack, 6));
}
/**************************************************************************/
/*!
@brief Reads n bytes of data from the PN532 via I2C.
@param buff Pointer to the buffer where data will be written
@param n Number of bytes to be read
*/
/**************************************************************************/
void readdata(uint8_t *buff, uint8_t n)
{
i2cTransaction.slaveAddress = PN532_I2C_ADDRESS;
i2cTransaction.writeBuf =NULL ;
i2cTransaction.readBuf =buff;
i2cTransaction.writeCount =0;
i2cTransaction.readCount = n+2;
I2C_transfer(i2c, &i2cTransaction);
uint8_t i = 0;
for (i = 0; i < n-1; i++)
{
buff[i] = buff[i+1];
}
}