-/* At TIMER_CLOCK3 (MCK/32) */
-#define BIT_TIME_1 150
-#define BIT_TIME_0 90
-#define BIT_TIME_FUZZ 20
-
- int active = 0;
- while(!BUTTON_PRESS()) {
- int level = !!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_DIN);
- int time = AT91C_BASE_TC1->TC_CV;
-
- if(level != old_level) {
- if(level == 1) {
- AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
- if(time > (BIT_TIME_1-BIT_TIME_FUZZ) && time < (BIT_TIME_1+BIT_TIME_FUZZ)) {
- /* 1 bit */
- emit(1);
- active = 1;
- LED_B_ON();
- } else if(time > (BIT_TIME_0-BIT_TIME_FUZZ) && time < (BIT_TIME_0+BIT_TIME_FUZZ)) {
- /* 0 bit */
- emit(0);
- active = 1;
- LED_B_ON();
- } else if(active) {
- /* invalid */
- emit(-1);
- active = 0;
- LED_B_OFF();
- }
- }
- }
-
- if(time >= (BIT_TIME_1+BIT_TIME_FUZZ) && active) {
- /* Frame end */
- emit(-1);
- active = 0;
- LED_B_OFF();
- }
-
- if(time >= (20*BIT_TIME_1) && (AT91C_BASE_TC1->TC_SR & AT91C_TC_CLKSTA)) {
- AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
+ /* Bitbang the transmitter */
+ AT91C_BASE_PIOA->PIO_CODR = GPIO_SSC_DOUT;
+ AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
+ AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT;
+
+ setup_timer();
+
+ crc_init(&legic_crc, 4, 0x19 >> 1, 0x5, 0);
+}
+
+static void switch_off_tag_rwd(void)
+{
+ /* Switch off carrier, make sure tag is reset */
+ AT91C_BASE_PIOA->PIO_CODR = GPIO_SSC_DOUT;
+ SpinDelay(10);
+
+ WDT_HIT();
+}
+/* calculate crc for a legic command */
+static int LegicCRC(int byte_index, int value, int cmd_sz) {
+ crc_clear(&legic_crc);
+ crc_update(&legic_crc, 1, 1); /* CMD_READ */
+ crc_update(&legic_crc, byte_index, cmd_sz-1);
+ crc_update(&legic_crc, value, 8);
+ return crc_finish(&legic_crc);
+}
+
+int legic_read_byte(int byte_index, int cmd_sz) {
+ int byte;
+
+ legic_prng_forward(4); /* we wait anyways */
+ while(timer->TC_CV < 387) ; /* ~ 258us + 100us*delay */
+
+ frame_send_rwd(1 | (byte_index << 1), cmd_sz);
+ frame_clean(¤t_frame);
+
+ frame_receive_rwd(¤t_frame, 12, 1);
+
+ byte = current_frame.data & 0xff;
+ if( LegicCRC(byte_index, byte, cmd_sz) != (current_frame.data >> 8) ) {
+ Dbprintf("!!! crc mismatch: expected %x but got %x !!!", LegicCRC(byte_index, current_frame.data & 0xff, cmd_sz), current_frame.data >> 8);
+ return -1;
+ }
+
+ return byte;
+}
+
+/* legic_write_byte() is not included, however it's trivial to implement
+ * and here are some hints on what remains to be done:
+ *
+ * * assemble a write_cmd_frame with crc and send it
+ * * wait until the tag sends back an ACK ('1' bit unencrypted)
+ * * forward the prng based on the timing
+ */
+
+
+void LegicRfReader(int offset, int bytes) {
+ int byte_index=0, cmd_sz=0, card_sz=0;
+
+ LegicCommonInit();
+
+ memset(BigBuf, 0, 1024);
+
+ DbpString("setting up legic card");
+ uint32_t tag_type = perform_setup_phase_rwd(0x55);
+ switch(tag_type) {
+ case 0x1d:
+ DbpString("MIM 256 card found, reading card ...");
+ cmd_sz = 9;
+ card_sz = 256;
+ break;
+ case 0x3d:
+ DbpString("MIM 1024 card found, reading card ...");
+ cmd_sz = 11;
+ card_sz = 1024;
+ break;
+ default:
+ Dbprintf("Unknown card format: %x",tag_type);
+ switch_off_tag_rwd();
+ return;
+ }
+ if(bytes == -1) {
+ bytes = card_sz;
+ }
+ if(bytes+offset >= card_sz) {
+ bytes = card_sz-offset;
+ }
+
+ switch_off_tag_rwd(); //we lost to mutch time with dprintf
+ perform_setup_phase_rwd(0x55);
+
+ while(byte_index < bytes) {
+ int r = legic_read_byte(byte_index+offset, cmd_sz);
+ if(r == -1) {
+ Dbprintf("aborting");
+ switch_off_tag_rwd();
+ return;