+#ifndef SHORT_COIL
+//#define LOW(x) AT91C_BASE_PIOA->PIO_CODR = (x)
+# define SHORT_COIL() LOW(GPIO_SSC_DOUT);
+#endif
+#ifndef OPEN_COIL
+//#define HIGH(x) AT91C_BASE_PIOA->PIO_SODR = (x)
+# define OPEN_COIL() HIGH(GPIO_SSC_DOUT);
+#endif
+
+uint32_t stop_send_frame_us = 0;
+
+// ~ 258us + 100us*delay
+#define WAIT(delay) SpinDelayUs(delay);
+#define WAIT_100 WAIT(100)
+
+#define COIL_PULSE(delay) \
+ SHORT_COIL() \
+ SpinDelayUs(RWD_TIME_PAUSE); \
+ OPEN_COIL() \
+ SpinDelayUs(delay);
+
+// ToDo: define a meaningful maximum size for auth_table. The bigger this is, the lower will be the available memory for traces.
+// Historically it used to be FREE_BUFFER_SIZE, which was 2744.
+#define LEGIC_CARD_MEMSIZE 1024
+static uint8_t* cardmem;
+
+// Starts Clock and waits until its reset
+static void Reset(AT91PS_TC clock){
+ clock->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
+ while(clock->TC_CV > 1) ;
+}
+
+// Starts Clock and waits until its reset
+static void ResetClock(void){
+ Reset(timer);
+}
+
+// Prng works when waiting in 99.1us cycles.
+// and while sending/receiving in bit frames (100, 60)
+static void CalibratePrng( uint32_t time){
+ // Calculate Cycles based on timer 100us
+ uint32_t i = (time - stop_send_frame_us) / 100 ;
+
+ // substract cycles of finished frames
+ int k = i - legic_prng_count()+1;
+
+ // substract current frame length, rewind to beginning
+ if ( k > 0 )
+ legic_prng_forward(k);
+}
+