4 #include "teensy3/core_pins.h"
5 #include "teensy3/usb_seremu.h"
6 #include "teensy3/usb_rawhid.h"
9 #define noinline __attribute__((noinline))
12 #define STREAM_BUF_SIZE 512
13 #define STREAM_BUF_MASK (512 - 1)
15 /* ?0SA 00DU, ?1CB RLDU */
16 #define STREAM_EL_SZ 2
19 uint8_t stream_to[STREAM_BUF_SIZE][STREAM_EL_SZ];
20 uint8_t stream_from[STREAM_BUF_SIZE][STREAM_EL_SZ];
22 uint8_t fixed_state[4];
23 uint32_t fixed_state32;
26 uint8_t pending_state[4];
27 uint32_t pending_state32;
29 uint32_t stream_enable_to:1;
30 uint32_t stream_enable_from:1;
31 uint32_t stream_started:1;
32 uint32_t stream_ended:1;
33 uint32_t use_readinc:1;
34 uint32_t use_pending:1;
43 ssize_t _write(int fd, const void *buf, size_t nbyte)
48 if (fd != 1 && fd != 2) {
49 snprintf(tbuf, sizeof(tbuf), "write to fd %d\n", fd);
50 usb_seremu_write(tbuf, strlen(tbuf));
53 ret = usb_seremu_write(buf, nbyte);
54 return ret < 0 ? ret : nbyte;
61 /* portb handles TH */
62 static void portb_isr_fixed(void)
68 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
70 GPIOD_PDOR = g.fixed_state[th];
74 static void portb_isr_do_to_inc(void)
80 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
82 GPIOD_PDOR = g.stream_to[g.t_o][th];
84 g.t_o = (g.t_o + 1) & STREAM_BUF_MASK;
87 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
93 static void portb_isr_do_to(void)
99 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
101 GPIOD_PDOR = g.stream_to[g.t_o][th];
105 static void portc_isr_nop(void)
113 // /vsync starts at line 235/259 (ntsc/pal), just as vcounter jumps back
114 // we care when it comes out (/vsync goes high) after 3 lines at 238/262
115 static void portc_isr_frameinc(void)
122 g.t_o = (g.t_o + 1) & STREAM_BUF_MASK;
123 if (g.t_o == g.t_i) {
124 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
125 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
130 /* "recording" data */
131 static noinline void do_from_step(void)
135 // should hopefully give atomic fixed_state read..
137 g.fixed_state32 = g.pending_state32;
138 g.stream_from[g.f_i][0] = s;
139 g.stream_from[g.f_i][1] = s >> 8;
140 g.f_i = (g.f_i + 1) & STREAM_BUF_MASK;
143 static void portb_isr_fixed_do_from(void)
149 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
151 GPIOD_PDOR = g.fixed_state[th];
157 static void portc_isr_frameinc_do_from(void)
168 static void udelay(uint32_t us)
170 uint32_t start = micros();
172 while ((micros() - start) < us) {
173 asm volatile("nop; nop; nop; nop");
178 static void do_start_seq(void)
180 uint32_t edge_cnt_last;
182 uint32_t start, t1, t2;
186 edge_cnt = g.edge_cnt;
190 g.fixed_state[1] = 0x25;
192 for (tout = 10000; tout > 0; tout--) {
193 edge_cnt_last = edge_cnt;
195 edge_cnt = g.edge_cnt;
197 if (edge_cnt != edge_cnt_last)
199 if (!(GPIOB_PDIR & CORE_PIN0_BITMASK))
203 g.fixed_state[0] = 0x33;
204 g.fixed_state[1] = 0x3f;
209 printf("start_seq timeout1, t=%u\n", t1 - start);
213 for (tout = 100000; tout > 0; tout--) {
216 if (GPIOB_PDIR & CORE_PIN0_BITMASK)
222 printf("start_seq timeout2, t1=%u, t2=%u\n",
223 t1 - start, t2 - t1);
227 //printf(" t1=%u, t2=%u\n", t1 - start, t2 - t1);
229 if (g.stream_started) {
230 printf("got start_seq when already started\n");
234 if (!g.stream_enable_to && !g.stream_enable_from) {
235 printf("got start_seq, without enable from USB\n");
239 if (g.stream_enable_to && g.t_i == g.t_o) {
240 printf("got start_seq while stream_to is empty\n");
244 if (g.stream_enable_from && g.f_i != g.f_o) {
245 printf("got start_seq while stream_from is not empty\n");
250 g.stream_started = 1;
251 if (g.stream_enable_to) {
253 attachInterruptVector(IRQ_PORTB, portb_isr_do_to_inc);
254 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
257 attachInterruptVector(IRQ_PORTB, portb_isr_do_to);
258 attachInterruptVector(IRQ_PORTC, portc_isr_frameinc);
261 else if (g.stream_enable_from) {
264 attachInterruptVector(IRQ_PORTB,
265 portb_isr_fixed_do_from);
266 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
269 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
270 attachInterruptVector(IRQ_PORTC,
271 portc_isr_frameinc_do_from);
277 // callers must disable IRQs
278 static void clear_state(void)
280 g.stream_enable_to = 0;
281 g.stream_enable_from = 0;
282 g.stream_started = 0;
289 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
290 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
293 static int get_space_to(void)
295 return STREAM_BUF_SIZE - ((g.t_i - g.t_o) & STREAM_BUF_MASK);
298 static int get_used_from(void)
300 return (g.f_i - g.f_o) & STREAM_BUF_MASK;
303 static void do_usb(void *buf)
305 struct tas_pkt *pkt = buf;
310 case PKT_FIXED_STATE:
311 memcpy(&i, pkt->data, sizeof(i));
313 g.pending_state32 = i;
317 case PKT_STREAM_ENABLE:
320 /* wait for start from MD */
321 g.stream_enable_to = pkt->enable.stream_to;
322 g.stream_enable_from = pkt->enable.stream_from;
323 g.use_readinc = pkt->enable.use_readinc;
326 case PKT_STREAM_ABORT:
333 printf("end of stream\n");
335 case PKT_STREAM_DATA_TO:
337 space = get_space_to();
338 if (space <= pkt->size / STREAM_EL_SZ) {
339 printf("got data pkt while space=%d\n", space);
342 for (i = 0; i < pkt->size / STREAM_EL_SZ; i++) {
343 memcpy(&g.stream_to[t_i++],
344 pkt->data + i * STREAM_EL_SZ,
346 t_i &= STREAM_BUF_MASK;
351 printf("got unknown pkt type: %04x\n", pkt->type);
358 uint32_t edge_cnt_last;
364 delay(1000); // wait for usb..
366 /* ?0SA 00DU, ?1CB RLDU */
367 g.fixed_state[0] = 0x33;
368 g.fixed_state[1] = 0x3f;
370 printf("starting, rawhid: %d\n", usb_rawhid_available());
372 // md pin th tr tl r l d u vsync
373 // md bit* 6 5 4 3 2 1 0
374 // t bit b16 d5 d4 d3 d2 d1 d0 c6
375 // t pin 0 20 6 8 7 14 2 11
376 // * - note: tl/tr mixed in most docs
378 attachInterrupt(0, portb_isr_fixed, CHANGE);
379 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
381 attachInterrupt(11, portc_isr_nop, RISING);
382 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
384 NVIC_SET_PRIORITY(IRQ_PORTB, 0);
385 NVIC_SET_PRIORITY(IRQ_PORTC, 16);
397 // CORE_PIN0_PORTSET CORE_PIN0_BITMASK PORTB_PCR16
398 printf("GPIOB PDDR, PDIR: %08x %08x\n", GPIOB_PDIR, GPIOB_PDDR);
399 printf("GPIOC PDDR, PDIR: %08x %08x\n", GPIOC_PDIR, GPIOC_PDDR);
400 printf("GPIOD PDDR, PDIR: %08x %08x\n", GPIOD_PDIR, GPIOD_PDDR);
401 printf("PORTB_PCR16: %08x\n", PORTB_PCR16);
402 printf("PORTC_PCR6: %08x\n", PORTC_PCR6);
404 asm("mrs %0, BASEPRI" : "=r"(ret));
405 printf("BASEPRI: %d\n", ret);
408 edge_cnt_last = g.edge_cnt;
413 while (g.stream_enable_to && !g.stream_ended
414 && get_space_to() > sizeof(pkt.data) / STREAM_EL_SZ)
416 pkt.type = PKT_STREAM_REQ;
417 pkt.req.frame = g.frame_cnt;
419 ret = usb_rawhid_send(&pkt, 1000);
420 if (ret != sizeof(pkt)) {
421 printf("send STREAM_REQ: %d\n", ret);
425 ret = usb_rawhid_recv(buf, 1000);
427 printf("usb_rawhid_recv/s: %d\n", ret);
432 while (g.stream_enable_from && !g.stream_ended
433 && get_used_from() >= sizeof(pkt.data) / STREAM_EL_SZ)
439 for (i = 0; i < sizeof(pkt.data); i += STREAM_EL_SZ) {
440 memcpy(pkt.data + i, &g.stream_from[f_o++],
442 f_o &= STREAM_BUF_MASK;
446 pkt.type = PKT_STREAM_DATA_FROM;
449 ret = usb_rawhid_send(&pkt, 1000);
450 if (ret != sizeof(pkt)) {
451 printf("send DATA_FROM: %d\n", ret);
456 if (timeout == 1000) {
457 edge_cnt = g.edge_cnt;
458 //printf("e: %d th: %d\n", edge_cnt - edge_cnt_last,
459 // (GPIOB_PDIR >> CORE_PIN0_BIT) & 1);
460 if (edge_cnt - edge_cnt_last > 10000) {
462 edge_cnt = g.edge_cnt;
464 edge_cnt_last = edge_cnt;
467 ret = usb_rawhid_recv(buf, timeout);
469 CORE_PIN13_PORTSET = CORE_PIN13_BITMASK;
475 CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK;
479 printf("usb_rawhid_recv: %d\n", ret);