# rfid / avrfid2.c

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 #include /* Use r16 and r17 to track the state of the pins */ volatile register uint8_t r16 __asm__("r16"); volatile register uint8_t r17 __asm__("r17"); /** * Delay a specific number of clock cycles. * * rjmp is 2 clocks, nop is 1. * So do one nop if the delay is an odd value and then rjmp's for n/2. */ static inline void delay( const uint8_t n ) { switch (n/2) { case 8: asm("rjmp .+0"); case 7: asm("rjmp .+0"); case 6: asm("rjmp .+0"); case 5: asm("rjmp .+0"); case 4: asm("rjmp .+0"); case 3: asm("rjmp .+0"); case 2: asm("rjmp .+0"); case 1: asm("rjmp .+0"); case 0: break; } if (n % 2 == 1) asm("nop"); } static void __attribute__((__always_inline__)) toggle( const uint8_t n ) { delay(n-2); asm( "eor r16, r17\n" "out 0x17, r16\n" ); } static void __attribute__((__always_inline__)) baseband_0(void //uint8_t startloop ) { //if (startloop) //toggle(2); //else toggle(4); toggle(4); // 8 toggle(4); // 12 toggle(4); // 16 toggle(4); // 20 toggle(4); // 24 toggle(4); // 28 toggle(4); // 32 toggle(4); // 36 toggle(4); // 40 toggle(4); // 44 toggle(4); // 48 toggle(4); // 52 } static void __attribute__((__always_inline__)) baseband_1(void //uint8_t startloop ) { //if (startloop) //toggle(2); //else toggle(5); toggle(5); // 10 toggle(5); // 15 toggle(5); // 20 toggle(5); // 25 toggle(5); // 30 toggle(5); // 35 toggle(5); // 40 toggle(5); // 45 toggle(5); // 50 } static inline void header(void) { baseband_0(); baseband_0(); baseband_0(); baseband_1(); baseband_1(); baseband_1(); } static void __attribute__((__always_inline__)) manchester_bit( const uint8_t x ) { if (x) { baseband_1(); baseband_0(); } else { baseband_0(); baseband_1(); } } static inline void __attribute__((__always_inline__)) manchester( const uint32_t x, const uint8_t bits ) { switch(bits - 1) { case 20: manchester_bit(x >> 20); case 19: manchester_bit(x >> 19); case 18: manchester_bit(x >> 18); case 17: manchester_bit(x >> 17); case 16: manchester_bit(x >> 16); case 15: manchester_bit(x >> 15); case 14: manchester_bit(x >> 14); case 13: manchester_bit(x >> 13); case 12: manchester_bit(x >> 12); case 11: manchester_bit(x >> 11); case 10: manchester_bit(x >> 10); case 9: manchester_bit(x >> 9); case 8: manchester_bit(x >> 8); case 7: manchester_bit(x >> 7); case 6: manchester_bit(x >> 6); case 5: manchester_bit(x >> 5); case 4: manchester_bit(x >> 4); case 3: manchester_bit(x >> 3); case 2: manchester_bit(x >> 2); case 1: manchester_bit(x >> 1); case 0: manchester_bit(x >> 0); } } #define HID_MFG_CODE 0x01002 // Do not modify #define HID_SITE_CODE 42 #define HID_UNIQUE_ID 23946 // May be written on the back of the card static void __attribute__((__noinline__)) //__attribute__((section(".fini8"))) hid_output(void) { header(); manchester(HID_MFG_CODE, 20); manchester(HID_SITE_CODE, 8); manchester(HID_UNIQUE_ID, 16); manchester(0, 1); } int __attribute__((section(".init9"))) main(void) { r16 = 0; r17 = _BV(PINB3) | _BV(PINB4); while (1) hid_output(); }