ESPHome
2025.5.0
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
ESP_LOGI(TAG,
"SPI Setup"
);
14
this->
spi_setup
();
15
16
RC522::setup();
17
}
18
19
void
RC522Spi::dump_config
() {
20
RC522::dump_config();
21
LOG_PIN(
" CS Pin: "
, this->
cs_
);
22
}
23
28
uint8_t
RC522Spi::pcd_read_register
(
PcdRegister
reg
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
43
void
RC522Spi::pcd_read_register
(
PcdRegister
reg,
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
100
void
RC522Spi::pcd_write_register
(
PcdRegister
reg,
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
114
void
RC522Spi::pcd_write_register
(
PcdRegister
reg,
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
address
uint8_t address
Definition
bl0906.h:4
esphome::rc522::RC522::PcdRegister
PcdRegister
Definition
rc522.h:60
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:100
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:28
esphome::rc522_spi::RC522Spi::dump_config
void dump_config() override
Definition
rc522_spi.cpp:19
esphome::spi::SPIClient::cs_
GPIOPin * cs_
Definition
spi.h:408
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:493
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:430
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:444
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:470
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:495
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:479
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