+/* "recording" data */
+static noinline void do_from_step(void)
+{
+ uint32_t s;
+
+ // should hopefully give atomic fixed_state read..
+ s = g.pl[0].fixed_state32;
+ g.pl[0].fixed_state32 = g.pl[0].pending_state32;
+ g.stream_from[g.pos_from.i][0] = s;
+ g.stream_from[g.pos_from.i][1] = s >> 8;
+ g.pos_from.i = (g.pos_from.i + 1) & STREAM_BUF_MASK;
+}
+
+static void pl1th_isr_fixed_do_from(void)
+{
+ uint32_t isfr, th;
+
+ isfr = PL1_ISFR;
+ PL1_ISFR = isfr;
+ th = PL1_TH();
+
+ GPIOD_PDOR = g.pl[0].fixed_state[th];
+ if (th)
+ do_from_step();
+ g.edge_cnt++;
+}
+
+static void vsync_isr_frameinc_do_from(void)
+{
+ uint32_t isfr;
+
+ isfr = VSYNC_ISFR;
+ VSYNC_ISFR = isfr;
+
+ do_from_step();
+ g.frame_cnt++;
+}
+
+/* * */
+static void choose_isrs(void)
+{
+ void (*pl1th_handler)(void) = pl1th_isr_fixed;
+ void (*pl2th_handler)(void) = pl2th_isr_fixed;
+ void (*vsync_handler)(void) = vsync_isr_nop;
+
+ if (g.stream_enable_to) {
+ switch (g.inc_mode) {
+ case INC_MODE_VSYNC:
+ pl1th_handler = pl1th_isr_do_to;
+ pl2th_handler = pl2th_isr_do_to;
+ vsync_handler = vsync_isr_frameinc;
+ break;
+ case INC_MODE_SHARED_PL1:
+ pl1th_handler = pl1th_isr_do_to_inc;
+ pl2th_handler = pl2th_isr_do_to;
+ break;
+ case INC_MODE_SHARED_PL2:
+ pl1th_handler = pl1th_isr_do_to;
+ pl2th_handler = pl2th_isr_do_to_inc_pl1;
+ break;
+ }
+ }
+ else if (g.stream_enable_from) {
+ g.use_pending = 1;
+ switch (g.inc_mode) {
+ case INC_MODE_VSYNC:
+ vsync_handler = vsync_isr_frameinc_do_from;
+ break;
+ case INC_MODE_SHARED_PL1:
+ pl1th_handler = pl1th_isr_fixed_do_from;
+ break;
+ case INC_MODE_SHARED_PL2:
+ /* TODO */
+ break;
+ }
+ }
+
+ attachInterruptVector(IRQ_PORTD, pl1th_handler);
+ attachInterruptVector(IRQ_PORTC, pl2th_handler);
+ attachInterruptVector(IRQ_PORTA, vsync_handler);
+}
+
+static noinline void choose_isrs_idle(void)
+{
+ attachInterruptVector(IRQ_PORTD, pl1th_isr_fixed);
+ attachInterruptVector(IRQ_PORTC, pl2th_isr_fixed);
+ attachInterruptVector(IRQ_PORTA, vsync_isr_nop);
+}
+
+static void udelay(uint32_t us)
+{
+ uint32_t start = micros();
+
+ while ((micros() - start) < us) {
+ asm volatile("nop; nop; nop; nop");
+ yield();
+ }
+}
+
+static void do_start_seq(void)
+{
+ uint32_t edge_cnt_last;
+ uint32_t edge_cnt;
+ uint32_t start, t1, t2;
+ int tout;
+
+ start = micros();
+ edge_cnt = g.edge_cnt;
+
+ /* magic value */
+ g.pl[0].fixed_state[0] =
+ g.pl[0].fixed_state[1] = 0x25;
+
+ for (tout = 10000; tout > 0; tout--) {
+ edge_cnt_last = edge_cnt;
+ udelay(100);
+ edge_cnt = g.edge_cnt;
+
+ if (edge_cnt != edge_cnt_last)
+ continue;
+ if (!PL1_TH())
+ break;
+ }
+
+ g.pl[0].fixed_state[0] = 0x33;
+ g.pl[0].fixed_state[1] = 0x3f;
+ GPIOD_PDOR = 0x33;
+
+ t1 = micros();
+ if (tout == 0) {
+ printf("start_seq timeout1, t=%u\n", t1 - start);
+ return;
+ }
+
+ for (tout = 100000; tout > 0; tout--) {
+ udelay(1);
+
+ if (PL1_TH())
+ break;
+ }
+
+ t2 = micros();
+ if (tout == 0) {
+ printf("start_seq timeout2, t1=%u, t2=%u\n",
+ t1 - start, t2 - t1);
+ return;
+ }
+
+ //printf(" t1=%u, t2=%u\n", t1 - start, t2 - t1);
+
+ if (g.stream_started) {
+ printf("got start_seq when already started\n");
+ return;
+ }
+
+ if (!g.stream_enable_to && !g.stream_enable_from) {
+ printf("got start_seq, without enable from USB\n");
+ return;
+ }
+
+ if (g.stream_enable_to && g.pos_to_p[0].i == g.pos_to_p[0].o) {
+ printf("got start_seq while stream_to is empty\n");
+ return;
+ }
+
+ if (g.stream_enable_from && g.pos_from.i != g.pos_from.o) {
+ printf("got start_seq while stream_from is not empty\n");
+ return;
+ }
+
+ __disable_irq();
+ choose_isrs();
+ g.stream_started = 1;
+ __enable_irq();
+}
+
+// callers must disable IRQs
+static void clear_state(void)
+{
+ int i;
+
+ g.stream_enable_to = 0;
+ g.stream_enable_from = 0;
+ g.stream_started = 0;
+ g.stream_ended = 0;
+ g.inc_mode = INC_MODE_VSYNC;
+ g.use_pending = 0;
+ for (i = 0; i < ARRAY_SIZE(g.pos_to_p); i++)
+ g.pos_to_p[i].i = g.pos_to_p[i].o = 0;
+ g.pos_from.i = g.pos_from.o = 0;
+ g.frame_cnt = 0;
+ choose_isrs_idle();
+}
+
+static int get_space_to(int p)
+{
+ return STREAM_BUF_SIZE - ((g.pos_to_p[p].i - g.pos_to_p[p].o)
+ & STREAM_BUF_MASK);
+}
+
+static int get_used_from(void)
+{
+ return (g.pos_from.i - g.pos_from.o) & STREAM_BUF_MASK;
+}
+
+static void do_usb(void *buf)