Skip to content

Commit d83112b

Browse files
authored
Merge pull request nkolban#713 from PaulBreugnot/master
Add SPI slave select and release when reading / writting registers
2 parents 58086a8 + 913abdf commit d83112b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

cpp_utils/MFRC522.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ void MFRC522::PCD_WriteRegister(PCD_Register reg, byte value) {
4848
uint8_t data[2];
4949
data[0] = reg;
5050
data[1] = value;
51+
52+
ESP32CPP::GPIO::low((gpio_num_t)_chipSelectPin); // Select slave
5153
m_spi.transfer(data, 2);
54+
ESP32CPP::GPIO::high((gpio_num_t)_chipSelectPin); // Release slave
55+
5256
} // End PCD_WriteRegister()
5357

5458

@@ -63,7 +67,11 @@ void MFRC522::PCD_WriteRegister(PCD_Register reg, byte count, byte* values) {
6367
uint8_t* pData = new uint8_t[count + 1];
6468
pData[0] = reg;
6569
memcpy(pData + 1, values, count);
70+
71+
ESP32CPP::GPIO::low((gpio_num_t)_chipSelectPin); // Select slave
6672
m_spi.transfer(pData, count + 1);
73+
ESP32CPP::GPIO::high((gpio_num_t)_chipSelectPin); // Release slave
74+
6775
delete[] pData;
6876
} // End PCD_WriteRegister()
6977

@@ -77,7 +85,11 @@ byte MFRC522::PCD_ReadRegister(PCD_Register reg) {
7785
uint8_t data[2];
7886
data[0] = reg | 0x80;
7987
data[1] = 0;
88+
89+
ESP32CPP::GPIO::low((gpio_num_t)_chipSelectPin); // Select slave
8090
m_spi.transfer(data, 2);
91+
ESP32CPP::GPIO::high((gpio_num_t)_chipSelectPin); // Release slave
92+
8193
return data[1];
8294
} // End PCD_ReadRegister()
8395

@@ -97,6 +109,8 @@ void MFRC522::PCD_ReadRegister(PCD_Register reg, byte count, byte* values, byte
97109
byte address = 0x80 | reg; // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3.
98110
byte index = 0; // Index in values array.
99111
count--; // One read is performed outside of the loop
112+
113+
ESP32CPP::GPIO::low((gpio_num_t)_chipSelectPin); // Select slave
100114
m_spi.transferByte(address); // Tell MFRC522 which address we want to read
101115
if (rxAlign) { // Only update bit positions rxAlign..7 in values[0]
102116
// Create bit mask for bit positions rxAlign..7
@@ -112,6 +126,7 @@ void MFRC522::PCD_ReadRegister(PCD_Register reg, byte count, byte* values, byte
112126
index++;
113127
}
114128
values[index] = m_spi.transferByte(0); // Read the final byte. Send 0 to stop reading.
129+
ESP32CPP::GPIO::high((gpio_num_t)_chipSelectPin); // Release slave
115130
} // End PCD_ReadRegister()
116131

117132

@@ -181,6 +196,8 @@ MFRC522::StatusCode MFRC522::PCD_CalculateCRC(byte* data, byte length, byte* res
181196
*/
182197
void MFRC522::PCD_Init() {
183198
//m_spi.setHost(VSPI_HOST);
199+
200+
ESP32CPP::GPIO::setOutput((gpio_num_t)_chipSelectPin);
184201
m_spi.init();
185202

186203
bool hardReset = false;

0 commit comments

Comments
 (0)