M5Unit-RFID 0.2.0 git rev:79c1939
Loading...
Searching...
No Matches
unit_JRD4035.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_RFID_UNIT_UNIT_JRD4035_HPP
11#define M5_UNIT_RFID_UNIT_UNIT_JRD4035_HPP
12
13#include <string>
14
15#include "m100_frame.hpp"
16#include "unit_UHFRFID.hpp"
17
18namespace m5 {
19namespace unit {
20
27 M5_UNIT_COMPONENT_HPP_BUILDER(UnitJRD4035, 0x00);
28
29public:
46
47 UnitJRD4035() : UHFRFIDComponent(DEFAULT_ADDRESS)
48 {
49 }
50 virtual ~UnitJRD4035() = default;
51
52 virtual bool begin() override;
53
57 inline config_t config() const
58 {
59 return _cfg_jrd4035;
60 }
62 inline void config(const config_t& cfg)
63 {
64 _cfg_jrd4035 = cfg;
66 }
68
71 virtual bool readModuleInformation(m5::uhf::ModuleInformation& info) override;
72 virtual bool readTransmitPower(int16_t& dbm100) override;
73 virtual bool writeTransmitPower(const int16_t dbm100) override;
74 virtual bool readRegion(m5::uhf::Region& region) override;
75 virtual bool writeRegion(const m5::uhf::Region region) override;
76 virtual bool readChannel(uint8_t& index) override;
77 virtual bool writeChannel(const uint8_t index) override;
78 virtual bool writeAutomaticFrequencyHopping(const bool enable) override;
79 virtual bool writeContinuousCarrier(const bool enable) override;
80 virtual bool readQueryParameters(m5::uhf::QueryParameters& qp) override;
81 virtual bool writeQueryParameters(const m5::uhf::QueryParameters& qp) override;
82 virtual bool writeAutoSleepTime(const uint8_t minutes) override;
83 virtual bool writeIdle(const bool enter, const uint8_t minutes = 0) override;
84 virtual bool wake() override;
85 virtual bool writeOperatingChannels(const std::vector<uint8_t>& channels) override;
86 virtual bool readBlockingSignal(m5::uhf::ChannelLevels& levels) override;
87 virtual bool readChannelRSSI(m5::uhf::ChannelLevels& levels) override;
88 virtual bool readSelectParameter(m5::uhf::SelectParameter& sp) override;
89
92
110
120 virtual bool sleep() override;
121
122 virtual bool writeSelectParameter(const m5::uhf::Bank bank, const uint32_t pointer_bits, const uint8_t* mask,
123 const size_t mask_len) override;
124 virtual bool writeSelectEnabled(const bool enable) override;
125 virtual TagResult readTagMemory(std::vector<uint8_t>& out, const m5::uhf::Bank bank, const uint16_t word_address,
126 const uint16_t word_count, const uint32_t access_password) override;
127 virtual TagResult writeTagMemory(const m5::uhf::Bank bank, const uint16_t word_address, const uint8_t* data,
128 const size_t len, const uint32_t access_password) override;
129 virtual TagResult lockTagMemory(const uint32_t payload, const uint32_t access_password) override;
130 virtual TagResult killTag(const uint32_t kill_password) override;
131 virtual TagResult blockPermalock(std::vector<uint8_t>& out, const m5::uhf::Bank bank, const uint16_t block_pointer,
132 const uint8_t block_range, const uint8_t* mask, const size_t mask_len,
133 const uint32_t access_password, const bool allow_permanent) override;
134 virtual TagResult qtCommand(uint16_t& control, const bool write, const bool persistent,
135 const uint32_t access_password) override;
136 virtual TagResult nxpChangeConfig(uint16_t& config, const uint16_t toggle, const uint32_t access_password) override;
137 virtual TagResult nxpChangeEAS(const bool enable, const uint32_t access_password) override;
138 virtual bool nxpEASAlarm(std::vector<uint8_t>& alarm) override;
139 virtual TagResult nxpReadProtect(const bool protect, const uint32_t access_password) override;
140 virtual m5::uhf::Reason classify(const uint8_t error_code) const override;
141
142protected:
143 virtual bool pump(const uint32_t timeout_ms) override;
144 virtual bool start_polling_command(const uint16_t count) override;
145 virtual bool stop_polling_command() override;
146
147 // Send a command frame
148 bool send_command(const uint8_t command, const uint8_t* param, const uint16_t param_len);
149 /*
150 Send a command and wait for its response, routing notifications to the tag queue
151 @param[out] response Response frame
152 @param command Command to send
153 @param param Parameter
154 @param param_len Parameter length
155 @param timeout_ms How long to wait, or 0 to wait config_t::command_timeout_ms
156 @return True if successful
157 @param answer_command Code the answer comes back under, where that is not the code sent
158 @note Nearly every command is answered under the code it was sent, the two the protocol
159 document says come back as something else included. A Monza QT write is the exception
160 */
161 bool send_and_wait(m100::Frame& response, const uint8_t command, const uint8_t* param, const uint16_t param_len,
162 const uint32_t timeout_ms = 0, const uint8_t answer_command = 0);
163 // Read one frame from the stream. Returns false when nothing is pending
164 bool read_frame(m100::Frame& out, const uint32_t timeout_ms);
165 // Throw away everything received, in the driver and in the staging buffer alike
166 void flush_rx();
167 // Route a received frame
168 void route_frame(const m100::Frame& f);
169 // Wait out an answer that may still be on its way, so that it cannot answer the next command
170 void resynchronize();
171 // Read the module information for one kind (0x00 hardware / 0x01 software / 0x02 manufacturer)
172 bool read_module_information_kind(std::string& out, const uint8_t kind);
173 // Run one of the channel scans and decode its contiguous range of levels
174 bool read_channel_levels(m5::uhf::ChannelLevels& levels, const uint8_t command);
175 /*
176 Did the module answer with a result rather than a failure?
177 @param response Response frame
178 @param what Name of the operation, used in the warning
179 @return True when the response carries a result
180 @details send_and_wait also returns true for a failure notification, since the module
181 answering at all is what it waits for. Every tag operation has to tell the two apart
182 */
183 bool succeeded(const m100::Frame& response, const char* what) const;
184 /*
185 Did the tag report that it carried the operation out?
186 @param response Response frame of a Write, Lock or Kill
187 @param what Name of the operation, used in the warning
188 @return Nothing when the tag answered with a success status
189 @details The module answering says the tag was reached; the status byte inside says the
190 tag actually did what it was told. That status is not one of the module's error codes, so
191 a failure here reports READER_BALKED rather than a code of its own
192 */
193 TagResult tag_carried_it_out(const m100::Frame& response, const char* what) const;
194 /*
195 The error code a failure notification carries
196 @param response Response frame
197 @return The code, or READER_SILENT where the notification carries none
198 */
199 static uint8_t error_code_of(const m100::Frame& response);
200 /*
201 Send a tag operation, trying again when the tag simply did not answer
202 @param[out] response Response frame
203 @param command Command to send
204 @param param Parameter
205 @param param_len Parameter length
206 @param what Name of the operation, used in the warnings
207 @return Nothing when the module answered with a result rather than a failure
208 @details Only a failure that says the tag did not answer is tried again. One where the tag
209 answered with a reason of its own is reported at once, since asking again would produce
210 the same reason more slowly
211 @note A kill that is carried out but whose answer is lost reads as a failure here and on
212 every attempt after it, because a killed tag answers nothing. There is no way to tell that
213 apart from a kill that never happened
214 */
215 TagResult send_tag_operation(m100::Frame& response, const uint8_t command, const uint8_t* param,
216 const uint16_t param_len, const char* what, const uint8_t answer_command = 0);
217
218 // Frame header. A derived class for the R200 family sets this to m100::r200::FRAME_HEADER
219 uint8_t _frame_header{m100::jrd::FRAME_HEADER};
220 // Frame end. A derived class for the R200 family sets this to m100::r200::FRAME_END
221 uint8_t _frame_end{m100::jrd::FRAME_END};
222
223 // Bytes taken from the UART that have not been made into a frame yet. Keeping them
224 // is what lets a frame that arrived in pieces be finished off by the next read
225 std::vector<uint8_t> _rx{};
226 // Response slot filled by route_frame while send_and_wait is pumping
227 m100::Frame _response{};
228 bool _response_pending{};
229 uint8_t _awaiting_command{};
230
231 // Settings this class reads. The setter passes the part it shares with
232 // UHFRFIDComponent::_cfg down, which is what the base reads
233 config_t _cfg_jrd4035{};
234};
235
236} // namespace unit
237} // namespace m5
238#endif
Non-instantiable base class for UHF-RFID reader units.
Definition unit_UHFRFID.hpp:64
config_t config() const
Gets the configuration.
Definition unit_UHFRFID.hpp:102
UHF-RFID unit based on the JRD-4035 module (magicRF M100)
Definition unit_JRD4035.hpp:26
virtual bool writeTransmitPower(const int16_t dbm100) override
Write the transmit power in 1/100 dBm.
Definition unit_JRD4035.cpp:582
virtual bool writeQueryParameters(const m5::uhf::QueryParameters &qp) override
Write the query parameters.
Definition unit_JRD4035.cpp:668
virtual bool writeSelectEnabled(const bool enable) override
Apply the stored mask to tag operations, or stop applying it.
Definition unit_JRD4035.cpp:1163
virtual bool readBlockingSignal(m5::uhf::ChannelLevels &levels) override
Measure the blocking signal on every channel of the operating region.
Definition unit_JRD4035.cpp:1006
bool readDemodulatorParameters(m100::DemodulatorParameters &dp)
Read the receiver demodulator settings.
Definition unit_JRD4035.cpp:1053
virtual bool writeRegion(const m5::uhf::Region region) override
Write the operating region.
Definition unit_JRD4035.cpp:605
virtual TagResult nxpChangeEAS(const bool enable, const uint32_t access_password) override
Set or clear the Product Status Flag of an NXP UCODE G2X.
Definition unit_JRD4035.cpp:818
virtual bool readTransmitPower(int16_t &dbm100) override
Read the transmit power in 1/100 dBm.
Definition unit_JRD4035.cpp:568
virtual TagResult killTag(const uint32_t kill_password) override
Kill the addressed tag permanently.
Definition unit_JRD4035.cpp:1275
virtual TagResult lockTagMemory(const uint32_t payload, const uint32_t access_password) override
Apply a 20-bit Gen2 lock payload.
Definition unit_JRD4035.cpp:1256
config_t config() const
Gets the configuration.
Definition unit_JRD4035.hpp:57
virtual TagResult nxpChangeConfig(uint16_t &config, const uint16_t toggle, const uint32_t access_password) override
Read the Config-Word of an NXP UCODE G2X, or invert bits of it.
Definition unit_JRD4035.cpp:794
virtual bool wake() override
Wake the module from low power sleep.
Definition unit_JRD4035.cpp:930
virtual bool readModuleInformation(m5::uhf::ModuleInformation &info) override
Read the module information.
Definition unit_JRD4035.cpp:551
virtual bool writeIdle(const bool enter, const uint8_t minutes=0) override
Put the module into IDLE, or bring it out.
Definition unit_JRD4035.cpp:903
virtual bool readChannelRSSI(m5::uhf::ChannelLevels &levels) override
Measure the RSSI on every channel of the operating region.
Definition unit_JRD4035.cpp:1014
virtual bool sleep() override
Put the module into low power sleep.
Definition unit_JRD4035.cpp:917
virtual bool writeSelectParameter(const m5::uhf::Bank bank, const uint32_t pointer_bits, const uint8_t *mask, const size_t mask_len) override
Store the mask that picks out one tag.
Definition unit_JRD4035.cpp:1112
void config(const config_t &cfg)
Sets the configuration.
Definition unit_JRD4035.hpp:62
virtual TagResult blockPermalock(std::vector< uint8_t > &out, const m5::uhf::Bank bank, const uint16_t block_pointer, const uint8_t block_range, const uint8_t *mask, const size_t mask_len, const uint32_t access_password, const bool allow_permanent) override
Read or set which blocks of a bank are permanently locked.
Definition unit_JRD4035.cpp:691
virtual bool writeContinuousCarrier(const bool enable) override
Turn the unmodulated carrier on or off.
Definition unit_JRD4035.cpp:877
virtual TagResult writeTagMemory(const m5::uhf::Bank bank, const uint16_t word_address, const uint8_t *data, const size_t len, const uint32_t access_password) override
Write len bytes to bank, starting at word_address.
Definition unit_JRD4035.cpp:1228
virtual TagResult nxpReadProtect(const bool protect, const uint32_t access_password) override
Turn the read protection of an NXP UCODE G2X on or off.
Definition unit_JRD4035.cpp:861
virtual bool readRegion(m5::uhf::Region &region) override
Read the operating region.
Definition unit_JRD4035.cpp:592
virtual bool writeAutomaticFrequencyHopping(const bool enable) override
Enable or disable automatic frequency hopping.
Definition unit_JRD4035.cpp:643
bool writeDemodulatorParameters(const m100::DemodulatorParameters &dp)
Write the receiver demodulator settings.
Definition unit_JRD4035.cpp:1069
virtual TagResult qtCommand(uint16_t &control, const bool write, const bool persistent, const uint32_t access_password) override
Read or write the QT control word of the addressed tag.
Definition unit_JRD4035.cpp:750
virtual bool readChannel(uint8_t &index) override
Read the operating channel index.
Definition unit_JRD4035.cpp:620
virtual bool readSelectParameter(m5::uhf::SelectParameter &sp) override
Read back the select mask the reader holds.
Definition unit_JRD4035.cpp:1037
virtual bool writeOperatingChannels(const std::vector< uint8_t > &channels) override
Replace the channel list used for automatic frequency hopping.
Definition unit_JRD4035.cpp:962
virtual bool writeChannel(const uint8_t index) override
Write the operating channel index.
Definition unit_JRD4035.cpp:633
virtual m5::uhf::Reason classify(const uint8_t error_code) const override
Say what one of this reader's error codes means in EPC Gen2 terms.
Definition unit_JRD4035.cpp:1313
virtual TagResult readTagMemory(std::vector< uint8_t > &out, const m5::uhf::Bank bank, const uint16_t word_address, const uint16_t word_count, const uint32_t access_password) override
Read word_count 16-bit words from bank, starting at word_address.
Definition unit_JRD4035.cpp:1187
virtual bool readQueryParameters(m5::uhf::QueryParameters &qp) override
Read the query parameters.
Definition unit_JRD4035.cpp:654
virtual bool writeAutoSleepTime(const uint8_t minutes) override
Write the inactivity period after which the module sleeps automatically.
Definition unit_JRD4035.cpp:889
virtual bool nxpEASAlarm(std::vector< uint8_t > &alarm) override
Ask the field whether any tag has its Product Status Flag asserted.
Definition unit_JRD4035.cpp:834
Frame codec for the magicRF M100 family (JRD-4035 / JRD-100 / R200)
Top level namespace of M5Stack.
Unit-related namespace.
Signal level measured on each channel of the operating region.
Definition uhf.hpp:1454
Reader module information.
Definition uhf.hpp:1442
EPC Gen2 query parameters.
Definition uhf.hpp:1302
The mask the reader holds, and how it is matched.
Definition uhf.hpp:1424
Settings for begin.
Definition unit_UHFRFID.hpp:70
Settings for begin.
Definition unit_JRD4035.hpp:36
m100::DemodulatorParameters demodulator
Definition unit_JRD4035.hpp:44
Receiver settings that decide how weak a reply the reader can still make sense of.
Definition m100_frame.hpp:757
Parsed frame.
Definition m100_frame.hpp:117
Reason
Why an operation meant to change a tag did not change it.
Definition uhf.hpp:90
Bank
EPC Gen2 memory bank.
Definition uhf.hpp:32
Region
Operating region.
Definition uhf.hpp:44
Base class for UHF-RFID reader units.
m5::stl::expected< void, uint8_t > TagResult
What became of an operation on a tag.
Definition unit_UHFRFID.hpp:35