ESPHome
2025.9.0-dev
Loading...
Searching...
No Matches
esphome
components
rc522_spi
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
12
void
RC522Spi::setup
() {
13
this->
spi_setup
();
14
15
RC522::setup();
16
}
17
18
void
RC522Spi::dump_config
() {
19
RC522::dump_config();
20
LOG_PIN(
" CS Pin: "
, this->
cs_
);
21
}
22
27
uint8_t
RC522Spi::pcd_read_register
(
PcdRegister
reg
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
42
void
RC522Spi::pcd_read_register
(
PcdRegister
reg,
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
99
void
RC522Spi::pcd_write_register
(
PcdRegister
reg,
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
113
void
RC522Spi::pcd_write_register
(
PcdRegister
reg,
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
address
uint8_t address
Definition
bl0906.h:4
esphome::rc522::RC522::PcdRegister
PcdRegister
Definition
rc522.h:59
esphome::rc522_spi::RC522Spi::setup
void setup() override
Definition
rc522_spi.cpp:12
esphome::rc522_spi::RC522Spi::pcd_write_register
void pcd_write_register(PcdRegister reg, uint8_t value) override
Definition
rc522_spi.cpp:99
esphome::rc522_spi::RC522Spi::pcd_read_register
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
esphome::rc522_spi::RC522Spi::dump_config
void dump_config() override
Definition
rc522_spi.cpp:18
esphome::spi::SPIClient::cs_
GPIOPin * cs_
Definition
spi.h:412
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_4MHZ >::enable
void enable()
Definition
spi.h:499
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_4MHZ >::spi_setup
void spi_setup() override
Definition
spi.h:436
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_4MHZ >::read_byte
uint8_t read_byte()
Definition
spi.h:450
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_4MHZ >::write_byte
void write_byte(uint8_t data)
Definition
spi.h:476
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_4MHZ >::disable
void disable()
Definition
spi.h:501
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_4MHZ >::transfer_byte
uint8_t transfer_byte(uint8_t data)
Definition
spi.h:485
log.h
esphome
Providing packet encoding functions for exchanging data with a remote host.
Definition
a01nyub.cpp:7
rc522_spi.h
Generated by
1.12.0