+AT91PS_TC timer;
+
+static void setup_timer(void)
+{
+ /* Set up Timer 1 to use for measuring time between pulses. Since we're bit-banging
+ * this it won't be terribly accurate but should be good enough.
+ */
+ AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC1);
+ timer = AT91C_BASE_TC1;
+ timer->TC_CCR = AT91C_TC_CLKDIS;
+ timer->TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK3;
+ timer->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
+
+/* At TIMER_CLOCK3 (MCK/32) */
+#define RWD_TIME_1 150 /* RWD_TIME_PAUSE off, 80us on = 100us */
+#define RWD_TIME_0 90 /* RWD_TIME_PAUSE off, 40us on = 60us */
+#define RWD_TIME_PAUSE 30 /* 20us */
+#define RWD_TIME_FUZZ 20 /* rather generous 13us, since the peak detector + hysteresis fuzz quite a bit */
+#define TAG_TIME_BIT 150 /* 100us for every bit */
+#define TAG_TIME_WAIT 490 /* time from RWD frame end to tag frame start, experimentally determined */
+
+}
+
+#define FUZZ_EQUAL(value, target, fuzz) ((value) > ((target)-(fuzz)) && (value) < ((target)+(fuzz)))