- if(bit == -1) {
- frame_handle(¤t_frame);
- frame_clean(¤t_frame);
- } else if(bit == 0) {
- frame_append_bit(¤t_frame, 0);
- } else if(bit == 1) {
- frame_append_bit(¤t_frame, 1);
+ /* 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;