mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2026-11-13 14:47:33 +00:00
* RFID: share the duplicated write_data bodies (-992 B of flash) Nearly every LF RFID protocol filled a write request the same way and differed only in its config word: protocol_viking_write_data() and protocol_jablotron_write_data() were byte-identical except for four constants, and six protocols carried a verbatim copy of the same 64-bit reversal loop. 27 call sites across 20 protocols now share two bodies in protocols/lfrfid_write_fill.c. The block or word count is read out of the config word's maxblock field rather than passed alongside it - it is the same number in both places at all 27 sites, and the chip streams what that field says, so the two could never legitimately disagree. Behaviour is unchanged. Each original inline loop was transcribed and compared against the shared body over random vectors for every bit width in use, and the derived counts were checked against the ones the call sites used to pass. No API change: the per-protocol callbacks were never exported, and protocol_dict_get_write_data() and the lfrfid_protocols vtable are untouched. Left alone on purpose: em4100, electra and h10301 keep their encoded data as a uint64_t or uint32_t[] rather than an MSB-first byte buffer, so a bit_lib helper would byte-swap them; and gallagher's EM4305 arm reads its payload one bit higher than every sibling (#1181), which folding would silently normalise. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JoYukcdV7vyusuaXtYcEdu * upd changelog Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JoYukcdV7vyusuaXtYcEdu --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
45 lines
1.5 KiB
C
45 lines
1.5 KiB
C
#include "lfrfid_write_fill.h"
|
|
|
|
#include <bit_lib/bit_lib.h>
|
|
|
|
// The maxblock field is 3 bits wide. t5577.h names the shift but not the width.
|
|
#define LFRFID_T5577_MAXBLOCK_FIELD_MASK (0x7)
|
|
|
|
void lfrfid_write_fill_t5577(LFRFIDWriteRequest* request, uint32_t block0, const uint8_t* bits) {
|
|
const uint8_t blocks = (block0 >> LFRFID_T5577_MAXBLOCK_SHIFT) &
|
|
LFRFID_T5577_MAXBLOCK_FIELD_MASK;
|
|
|
|
request->t5577.block[0] = block0;
|
|
|
|
for(uint8_t block = 1; block <= blocks; block++) {
|
|
request->t5577.block[block] = bit_lib_get_bits_32(bits, (block - 1) * 32, 32);
|
|
}
|
|
|
|
request->t5577.blocks_to_write = blocks + 1;
|
|
}
|
|
|
|
void lfrfid_write_fill_em4305(
|
|
LFRFIDWriteRequest* request,
|
|
uint32_t word4,
|
|
const uint8_t* bits,
|
|
bool invert) {
|
|
const uint8_t words = EM4x05_GET_NUM_BLOCKS(word4);
|
|
|
|
request->em4305.word[EM4x05_FIRST_USER_BLOCK - 1] = word4;
|
|
|
|
for(uint8_t word = 0; word < words; word++) {
|
|
uint32_t value = bit_lib_get_bits_32(bits, word * 32, 32);
|
|
|
|
// Halves swap as they reverse, so the whole word comes out reversed.
|
|
value = ((uint32_t)bit_lib_reverse_16_fast(value & 0xFFFF) << 16) |
|
|
bit_lib_reverse_16_fast(value >> 16);
|
|
|
|
if(invert) value = ~value;
|
|
|
|
request->em4305.word[EM4x05_FIRST_USER_BLOCK + word] = value;
|
|
}
|
|
|
|
// The config word is written with the data, so the mask covers it too.
|
|
request->em4305.mask = ((1u << (words + 1)) - 1) << (EM4x05_FIRST_USER_BLOCK - 1);
|
|
}
|