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