Introduce stdint.h

- replace long to int32_t
- fix documentation mu-sign
- add code style to doc
This commit is contained in:
Rotzbua
2016-04-26 16:20:10 +02:00
parent 452728d2c1
commit 0ba37f39e9
4 changed files with 34 additions and 25 deletions

View File

@@ -167,8 +167,9 @@ MFRC522::StatusCode MFRC522::PCD_CalculateCRC( byte *data, ///< In: Pointer to
PCD_WriteRegister(FIFODataReg, length, data); // Write data to the FIFO PCD_WriteRegister(FIFODataReg, length, data); // Write data to the FIFO
PCD_WriteRegister(CommandReg, PCD_CalcCRC); // Start the calculation PCD_WriteRegister(CommandReg, PCD_CalcCRC); // Start the calculation
// Wait for the CRC calculation to complete. Each iteration of the while-loop takes 17.73<EFBFBD>s. // Wait for the CRC calculation to complete. Each iteration of the while-loop takes 17.73μs.
word i = 5000; // TODO check/modify for other architectures than Arduino Uno 16bit
uint16_t i = 5000;
byte n; byte n;
while (1) { while (1) {
n = PCD_ReadRegister(DivIrqReg); // DivIrqReg[7..0] bits are: Set2 reserved reserved MfinActIRq reserved CRCIRq reserved reserved n = PCD_ReadRegister(DivIrqReg); // DivIrqReg[7..0] bits are: Set2 reserved reserved MfinActIRq reserved CRCIRq reserved reserved
@@ -205,7 +206,7 @@ void MFRC522::PCD_Init() {
if (digitalRead(_resetPowerDownPin) == LOW) { //The MFRC522 chip is in power down mode. if (digitalRead(_resetPowerDownPin) == LOW) { //The MFRC522 chip is in power down mode.
digitalWrite(_resetPowerDownPin, HIGH); // Exit power down mode. This triggers a hard reset. digitalWrite(_resetPowerDownPin, HIGH); // Exit power down mode. This triggers a hard reset.
// Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74<EFBFBD>s. Let us be generous: 50ms. // Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74μs. Let us be generous: 50ms.
delay(50); delay(50);
} }
else { // Perform a soft reset else { // Perform a soft reset
@@ -216,7 +217,7 @@ void MFRC522::PCD_Init() {
// f_timer = 13.56 MHz / (2*TPreScaler+1) where TPreScaler = [TPrescaler_Hi:TPrescaler_Lo]. // f_timer = 13.56 MHz / (2*TPreScaler+1) where TPreScaler = [TPrescaler_Hi:TPrescaler_Lo].
// TPrescaler_Hi are the four low bits in TModeReg. TPrescaler_Lo is TPrescalerReg. // TPrescaler_Hi are the four low bits in TModeReg. TPrescaler_Lo is TPrescalerReg.
PCD_WriteRegister(TModeReg, 0x80); // TAuto=1; timer starts automatically at the end of the transmission in all communication modes at all speeds PCD_WriteRegister(TModeReg, 0x80); // TAuto=1; timer starts automatically at the end of the transmission in all communication modes at all speeds
PCD_WriteRegister(TPrescalerReg, 0xA9); // TPreScaler = TModeReg[3..0]:TPrescalerReg, ie 0x0A9 = 169 => f_timer=40kHz, ie a timer period of 25<EFBFBD>s. PCD_WriteRegister(TPrescalerReg, 0xA9); // TPreScaler = TModeReg[3..0]:TPrescalerReg, ie 0x0A9 = 169 => f_timer=40kHz, ie a timer period of 25μs.
PCD_WriteRegister(TReloadRegH, 0x03); // Reload timer with 0x3E8 = 1000, ie 25ms before timeout. PCD_WriteRegister(TReloadRegH, 0x03); // Reload timer with 0x3E8 = 1000, ie 25ms before timeout.
PCD_WriteRegister(TReloadRegL, 0xE8); PCD_WriteRegister(TReloadRegL, 0xE8);
@@ -252,7 +253,7 @@ void MFRC522::PCD_Reset() {
PCD_WriteRegister(CommandReg, PCD_SoftReset); // Issue the SoftReset command. PCD_WriteRegister(CommandReg, PCD_SoftReset); // Issue the SoftReset command.
// The datasheet does not mention how long the SoftRest command takes to complete. // The datasheet does not mention how long the SoftRest command takes to complete.
// But the MFRC522 might have been in soft power-down mode (triggered by bit 4 of CommandReg) // But the MFRC522 might have been in soft power-down mode (triggered by bit 4 of CommandReg)
// Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74<EFBFBD>s. Let us be generous: 50ms. // Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74μs. Let us be generous: 50ms.
delay(50); delay(50);
// Wait for the PowerDown bit in CommandReg to be cleared // Wait for the PowerDown bit in CommandReg to be cleared
while (PCD_ReadRegister(CommandReg) & (1<<4)) { while (PCD_ReadRegister(CommandReg) & (1<<4)) {
@@ -328,9 +329,8 @@ bool MFRC522::PCD_PerformSelfTest() {
PCD_WriteRegister(CommandReg, PCD_CalcCRC); PCD_WriteRegister(CommandReg, PCD_CalcCRC);
// 6. Wait for self-test to complete // 6. Wait for self-test to complete
word i;
byte n; byte n;
for (i = 0; i < 0xFF; i++) { for (uint8_t i = 0; i < 0xFF; i++) {
// The datasheet does not specify exact completion condition except // The datasheet does not specify exact completion condition except
// that FIFO buffer should contain 64 bytes. // that FIFO buffer should contain 64 bytes.
// While selftest is initiated by CalcCRC command // While selftest is initiated by CalcCRC command
@@ -376,7 +376,7 @@ bool MFRC522::PCD_PerformSelfTest() {
} }
// Verify that the results match up to our expectations // Verify that the results match up to our expectations
for (i = 0; i < 64; i++) { for (uint8_t i = 0; i < 64; i++) {
if (result[i] != pgm_read_byte(&(reference[i]))) { if (result[i] != pgm_read_byte(&(reference[i]))) {
return false; return false;
} }
@@ -425,7 +425,7 @@ MFRC522::StatusCode MFRC522::PCD_CommunicateWithPICC( byte command, ///< The co
bool checkCRC ///< In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated. bool checkCRC ///< In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated.
) { ) {
byte n, _validBits; byte n, _validBits;
unsigned int i; uint16_t i;
// Prepare values for BitFramingReg // Prepare values for BitFramingReg
byte txLastBits = validBits ? *validBits : 0; byte txLastBits = validBits ? *validBits : 0;
@@ -443,7 +443,8 @@ MFRC522::StatusCode MFRC522::PCD_CommunicateWithPICC( byte command, ///< The co
// Wait for the command to complete. // Wait for the command to complete.
// In PCD_Init() we set the TAuto flag in TModeReg. This means the timer automatically starts when the PCD stops transmitting. // In PCD_Init() we set the TAuto flag in TModeReg. This means the timer automatically starts when the PCD stops transmitting.
// Each iteration of the do-while-loop takes 17.86<EFBFBD>s. // Each iteration of the do-while-loop takes 17.86μs.
// TODO check/modify for other architectures than Arduino Uno 16bit
i = 2000; i = 2000;
while (1) { while (1) {
n = PCD_ReadRegister(ComIrqReg); // ComIrqReg[7..0] bits are: Set1 TxIRq RxIRq IdleIRq HiAlertIRq LoAlertIRq ErrIRq TimerIRq n = PCD_ReadRegister(ComIrqReg); // ComIrqReg[7..0] bits are: Set1 TxIRq RxIRq IdleIRq HiAlertIRq LoAlertIRq ErrIRq TimerIRq
@@ -991,7 +992,7 @@ MFRC522::StatusCode MFRC522::MIFARE_Ultralight_Write( byte page, ///< The page
* @return STATUS_OK on success, STATUS_??? otherwise. * @return STATUS_OK on success, STATUS_??? otherwise.
*/ */
MFRC522::StatusCode MFRC522::MIFARE_Decrement( byte blockAddr, ///< The block (0-0xff) number. MFRC522::StatusCode MFRC522::MIFARE_Decrement( byte blockAddr, ///< The block (0-0xff) number.
long delta ///< This number is subtracted from the value of block blockAddr. int32_t delta ///< This number is subtracted from the value of block blockAddr.
) { ) {
return MIFARE_TwoStepHelper(PICC_CMD_MF_DECREMENT, blockAddr, delta); return MIFARE_TwoStepHelper(PICC_CMD_MF_DECREMENT, blockAddr, delta);
} // End MIFARE_Decrement() } // End MIFARE_Decrement()
@@ -1005,7 +1006,7 @@ MFRC522::StatusCode MFRC522::MIFARE_Decrement( byte blockAddr, ///< The block (0
* @return STATUS_OK on success, STATUS_??? otherwise. * @return STATUS_OK on success, STATUS_??? otherwise.
*/ */
MFRC522::StatusCode MFRC522::MIFARE_Increment( byte blockAddr, ///< The block (0-0xff) number. MFRC522::StatusCode MFRC522::MIFARE_Increment( byte blockAddr, ///< The block (0-0xff) number.
long delta ///< This number is added to the value of block blockAddr. int32_t delta ///< This number is added to the value of block blockAddr.
) { ) {
return MIFARE_TwoStepHelper(PICC_CMD_MF_INCREMENT, blockAddr, delta); return MIFARE_TwoStepHelper(PICC_CMD_MF_INCREMENT, blockAddr, delta);
} // End MIFARE_Increment() } // End MIFARE_Increment()
@@ -1032,7 +1033,7 @@ MFRC522::StatusCode MFRC522::MIFARE_Restore( byte blockAddr ///< The block (0-0x
*/ */
MFRC522::StatusCode MFRC522::MIFARE_TwoStepHelper( byte command, ///< The command to use MFRC522::StatusCode MFRC522::MIFARE_TwoStepHelper( byte command, ///< The command to use
byte blockAddr, ///< The block (0-0xff) number. byte blockAddr, ///< The block (0-0xff) number.
long data ///< The data to transfer in step 2 int32_t data ///< The data to transfer in step 2
) { ) {
MFRC522::StatusCode result; MFRC522::StatusCode result;
byte cmdBuffer[2]; // We only need room for 2 bytes. byte cmdBuffer[2]; // We only need room for 2 bytes.
@@ -1087,7 +1088,7 @@ MFRC522::StatusCode MFRC522::MIFARE_Transfer( byte blockAddr ///< The block (0-0
* @param[out] value Current value of the Value Block. * @param[out] value Current value of the Value Block.
* @return STATUS_OK on success, STATUS_??? otherwise. * @return STATUS_OK on success, STATUS_??? otherwise.
*/ */
MFRC522::StatusCode MFRC522::MIFARE_GetValue(byte blockAddr, long *value) { MFRC522::StatusCode MFRC522::MIFARE_GetValue(byte blockAddr, int32_t *value) {
MFRC522::StatusCode status; MFRC522::StatusCode status;
byte buffer[18]; byte buffer[18];
byte size = sizeof(buffer); byte size = sizeof(buffer);
@@ -1096,7 +1097,7 @@ MFRC522::StatusCode MFRC522::MIFARE_GetValue(byte blockAddr, long *value) {
status = MIFARE_Read(blockAddr, buffer, &size); status = MIFARE_Read(blockAddr, buffer, &size);
if (status == STATUS_OK) { if (status == STATUS_OK) {
// Extract the value // Extract the value
*value = (long(buffer[3])<<24) | (long(buffer[2])<<16) | (long(buffer[1])<<8) | long(buffer[0]); *value = (int32_t(buffer[3])<<24) | (int32_t(buffer[2])<<16) | (int32_t(buffer[1])<<8) | int32_t(buffer[0]);
} }
return status; return status;
} // End MIFARE_GetValue() } // End MIFARE_GetValue()
@@ -1112,10 +1113,10 @@ MFRC522::StatusCode MFRC522::MIFARE_GetValue(byte blockAddr, long *value) {
* @param[in] value New value of the Value Block. * @param[in] value New value of the Value Block.
* @return STATUS_OK on success, STATUS_??? otherwise. * @return STATUS_OK on success, STATUS_??? otherwise.
*/ */
MFRC522::StatusCode MFRC522::MIFARE_SetValue(byte blockAddr, long value) { MFRC522::StatusCode MFRC522::MIFARE_SetValue(byte blockAddr, int32_t value) {
byte buffer[18]; byte buffer[18];
// Translate the long into 4 bytes; repeated 2x in value block // Translate the int32_t into 4 bytes; repeated 2x in value block
buffer[0] = buffer[ 8] = (value & 0xFF); buffer[0] = buffer[ 8] = (value & 0xFF);
buffer[1] = buffer[ 9] = (value & 0xFF00) >> 8; buffer[1] = buffer[ 9] = (value & 0xFF00) >> 8;
buffer[2] = buffer[10] = (value & 0xFF0000) >> 16; buffer[2] = buffer[10] = (value & 0xFF0000) >> 16;
@@ -1572,7 +1573,7 @@ void MFRC522::PICC_DumpMifareClassicSectorToSerial(Uid *uid, ///< Pointer to U
} }
if (group != 3 && (g[group] == 1 || g[group] == 6)) { // Not a sector trailer, a value block if (group != 3 && (g[group] == 1 || g[group] == 6)) { // Not a sector trailer, a value block
long value = (long(buffer[3])<<24) | (long(buffer[2])<<16) | (long(buffer[1])<<8) | long(buffer[0]); int32_t value = (int32_t(buffer[3])<<24) | (int32_t(buffer[2])<<16) | (int32_t(buffer[1])<<8) | int32_t(buffer[0]);
Serial.print(F(" Value=0x")); Serial.print(value, HEX); Serial.print(F(" Value=0x")); Serial.print(value, HEX);
Serial.print(F(" Adr=0x")); Serial.print(buffer[12], HEX); Serial.print(F(" Adr=0x")); Serial.print(buffer[12], HEX);
} }
@@ -1786,7 +1787,7 @@ bool MFRC522::MIFARE_SetUid(byte *newUid, byte uidSize, bool logErrors) {
// Write new UID to the data we just read, and calculate BCC byte // Write new UID to the data we just read, and calculate BCC byte
byte bcc = 0; byte bcc = 0;
for (int i = 0; i < uidSize; i++) { for (uint8_t i = 0; i < uidSize; i++) {
block0_buffer[i] = newUid[i]; block0_buffer[i] = newUid[i];
bcc ^= newUid[i]; bcc ^= newUid[i];
} }

View File

@@ -369,12 +369,12 @@ public:
StatusCode MIFARE_Read(byte blockAddr, byte *buffer, byte *bufferSize); StatusCode MIFARE_Read(byte blockAddr, byte *buffer, byte *bufferSize);
StatusCode MIFARE_Write(byte blockAddr, byte *buffer, byte bufferSize); StatusCode MIFARE_Write(byte blockAddr, byte *buffer, byte bufferSize);
StatusCode MIFARE_Ultralight_Write(byte page, byte *buffer, byte bufferSize); StatusCode MIFARE_Ultralight_Write(byte page, byte *buffer, byte bufferSize);
StatusCode MIFARE_Decrement(byte blockAddr, long delta); StatusCode MIFARE_Decrement(byte blockAddr, int32_t delta);
StatusCode MIFARE_Increment(byte blockAddr, long delta); StatusCode MIFARE_Increment(byte blockAddr, int32_t delta);
StatusCode MIFARE_Restore(byte blockAddr); StatusCode MIFARE_Restore(byte blockAddr);
StatusCode MIFARE_Transfer(byte blockAddr); StatusCode MIFARE_Transfer(byte blockAddr);
StatusCode MIFARE_GetValue(byte blockAddr, long *value); StatusCode MIFARE_GetValue(byte blockAddr, int32_t *value);
StatusCode MIFARE_SetValue(byte blockAddr, long value); StatusCode MIFARE_SetValue(byte blockAddr, int32_t value);
StatusCode PCD_NTAG216_AUTH(byte *passWord, byte pACK[]); StatusCode PCD_NTAG216_AUTH(byte *passWord, byte pACK[]);
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
@@ -412,7 +412,7 @@ public:
private: private:
byte _chipSelectPin; // Arduino pin connected to MFRC522's SPI slave select input (Pin 24, NSS, active low) byte _chipSelectPin; // Arduino pin connected to MFRC522's SPI slave select input (Pin 24, NSS, active low)
byte _resetPowerDownPin; // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) byte _resetPowerDownPin; // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low)
StatusCode MIFARE_TwoStepHelper(byte command, byte blockAddr, long data); StatusCode MIFARE_TwoStepHelper(byte command, byte blockAddr, int32_t data);
}; };
#endif #endif

View File

@@ -84,6 +84,13 @@ Support/issue
Open an issue on github. Open an issue on github.
.. _code style:
Code style
----------
Please use ``fixed integers``, see `stdint.h`_. Why? This library is compatible to different boards which use different architectures (16bit vs 32bit). So unfixed ``int`` has different sizes on different environments and may cause unpredictable behaviour.
.. _pin layout: .. _pin layout:
Pin Layout Pin Layout
---------- ----------
@@ -269,3 +276,4 @@ It has been extended with functionality to alter sector 0 on Chinese UID changea
.. _broken: https://eprint.iacr.org/2008/166 .. _broken: https://eprint.iacr.org/2008/166
.. _supported by hardware: https://web.archive.org/web/20151210045625/http://www.nxp.com/documents/leaflet/939775017564.pdf .. _supported by hardware: https://web.archive.org/web/20151210045625/http://www.nxp.com/documents/leaflet/939775017564.pdf
.. _Arduino forum: https://forum.arduino.cc .. _Arduino forum: https://forum.arduino.cc
.. _stdint.h: https://en.wikibooks.org/wiki/C_Programming/C_Reference/stdint.h

View File

@@ -98,7 +98,7 @@ void loop() {
MFRC522::StatusCode status; MFRC522::StatusCode status;
byte buffer[18]; byte buffer[18];
byte size = sizeof(buffer); byte size = sizeof(buffer);
long value; int32_t value;
// Authenticate using key A // Authenticate using key A
Serial.println(F("Authenticating using key A...")); Serial.println(F("Authenticating using key A..."));