ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
rc522_spi.cpp
Go to the documentation of this file.
1#include "rc522_spi.h"
2#include "esphome/core/log.h"
3
4// Based on:
5// - https://github.com/miguelbalboa/rfid
6
7namespace esphome {
8namespace rc522_spi {
9
10static const char *const TAG = "rc522_spi";
11
13 this->spi_setup();
14
15 RC522::setup();
16}
17
19 RC522::dump_config();
20 LOG_PIN(" CS Pin: ", this->cs_);
21}
22
28) {
29 uint8_t value;
30 enable();
31 transfer_byte(0x80 | reg);
32 value = read_byte();
33 disable();
34 ESP_LOGVV(TAG, "read_register_(%d) -> %d", reg, value);
35 return value;
36}
37
43 uint8_t count,
44 uint8_t *values,
45 uint8_t rx_align
46) {
47#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
48 std::string buf;
49 buf = "Rx";
50 char cstrb[20];
51#endif
52 if (count == 0) {
53 return;
54 }
55
56 // Serial.print(F("Reading ")); Serial.print(count); Serial.println(F(" uint8_ts from register."));
57 uint8_t address = 0x80 | reg; // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3.
58 uint8_t index = 0; // Index in values array.
59 enable();
60 count--; // One read is performed outside of the loop
61
62 write_byte(address); // Tell MFRC522 which address we want to read
63 if (rx_align) { // Only update bit positions rxAlign..7 in values[0]
64 // Create bit mask for bit positions rxAlign..7
65 uint8_t mask = 0xFF << rx_align;
66 // Read value and tell that we want to read the same address again.
67 uint8_t value = transfer_byte(address);
68 // Apply mask to both current value of values[0] and the new data in value.
69 values[0] = (values[0] & ~mask) | (value & mask);
70 index++;
71
72#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
73 sprintf(cstrb, " %x", values[0]);
74 buf.append(cstrb);
75#endif
76 }
77 while (index < count) {
78 values[index] = transfer_byte(address); // Read value and tell that we want to read the same address again.
79
80#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
81 sprintf(cstrb, " %x", values[index]);
82 buf.append(cstrb);
83#endif
84
85 index++;
86 }
87 values[index] = transfer_byte(0); // Read the final uint8_t. Send 0 to stop reading.
88
89#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
90 buf = buf + " ";
91 sprintf(cstrb, "%x", values[index]);
92 buf.append(cstrb);
93
94 ESP_LOGVV(TAG, "read_register_array_(%x, %d, , %d) -> %s", reg, count, rx_align, buf.c_str());
95#endif
96 disable();
97}
98
100 uint8_t value
101) {
102 enable();
103 // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3.
104 transfer_byte(reg);
105 transfer_byte(value);
106 disable();
107}
108
114 uint8_t count,
115 uint8_t *values
116) {
117#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
118 std::string buf;
119 buf = "Tx";
120 char cstrb[20];
121#endif
122
123 enable();
124 transfer_byte(reg);
125
126 for (uint8_t index = 0; index < count; index++) {
127 transfer_byte(values[index]);
128
129#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
130 sprintf(cstrb, " %x", values[index]);
131 buf.append(cstrb);
132#endif
133 }
134 disable();
135 ESP_LOGVV(TAG, "write_register_(%d, %d) -> %s", reg, count, buf.c_str());
136}
137
138} // namespace rc522_spi
139} // namespace esphome
uint8_t address
Definition bl0906.h:4
void pcd_write_register(PcdRegister reg, uint8_t value) override
Definition rc522_spi.cpp:99
uint8_t pcd_read_register(PcdRegister reg) override
Reads a uint8_t from the specified register in the MFRC522 chip.
Definition rc522_spi.cpp:27
void dump_config() override
Definition rc522_spi.cpp:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7