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;
25 uint32_t stream_enable_to:1;
26 uint32_t stream_enable_from:1;
27 uint32_t stream_started:1;
28 uint32_t stream_ended:1;
29 uint32_t use_readinc:1;
38 ssize_t _write(int fd, const void *buf, size_t nbyte)
43 if (fd != 1 && fd != 2) {
44 snprintf(tbuf, sizeof(tbuf), "write to fd %d\n", fd);
45 usb_seremu_write(tbuf, strlen(tbuf));
48 ret = usb_seremu_write(buf, nbyte);
49 return ret < 0 ? ret : nbyte;
56 static void portb_isr_fixed(void)
62 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
64 GPIOD_PDOR = g.fixed_state[th];
68 static void portb_isr_do_to_inc(void)
74 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
76 GPIOD_PDOR = g.stream_to[g.t_o][th];
78 g.t_o = (g.t_o + 1) & STREAM_BUF_MASK;
81 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
87 static void portb_isr_do_to(void)
93 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
95 GPIOD_PDOR = g.stream_to[g.t_o][th];
99 static void portc_isr_nop(void)
107 // /vsync starts at line 235/259 (ntsc/pal), just as vcounter jumps back
108 // we care when it comes out (/vsync goes high) after 3 lines at 238/262
109 static void portc_isr_frameinc(void)
116 g.t_o = (g.t_o + 1) & STREAM_BUF_MASK;
117 if (g.t_o == g.t_i) {
118 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
119 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
124 /* "recording" data */
125 static noinline void do_from_step(void)
129 // should hopefully give atomic fixed_state read..
131 g.stream_from[g.f_i][0] = s;
132 g.stream_from[g.f_i][1] = s >> 8;
133 g.f_i = (g.f_i + 1) & STREAM_BUF_MASK;
136 static void portb_isr_fixed_do_from(void)
142 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
144 GPIOD_PDOR = g.fixed_state[th];
150 static void portc_isr_frameinc_do_from(void)
161 static void udelay(uint32_t us)
163 uint32_t start = micros();
165 while ((micros() - start) < us) {
166 asm volatile("nop; nop; nop; nop");
171 static void do_start_seq(void)
173 uint32_t edge_cnt_last;
175 uint32_t start, t1, t2;
179 edge_cnt = g.edge_cnt;
183 g.fixed_state[1] = 0x25;
185 for (tout = 10000; tout > 0; tout--) {
186 edge_cnt_last = edge_cnt;
188 edge_cnt = g.edge_cnt;
190 if (edge_cnt != edge_cnt_last)
192 if (!(GPIOB_PDIR & CORE_PIN0_BITMASK))
196 g.fixed_state[0] = 0x33;
197 g.fixed_state[1] = 0x3f;
202 printf("start_seq timeout1, t=%u\n", t1 - start);
206 for (tout = 100000; tout > 0; tout--) {
209 if (GPIOB_PDIR & CORE_PIN0_BITMASK)
215 printf("start_seq timeout2, t1=%u, t2=%u\n",
216 t1 - start, t2 - t1);
220 //printf(" t1=%u, t2=%u\n", t1 - start, t2 - t1);
222 if (g.stream_started) {
223 printf("got start_seq when already started\n");
227 if (!g.stream_enable_to && !g.stream_enable_from) {
228 printf("got start_seq, without enable from USB\n");
232 if (g.stream_enable_to && g.t_i == g.t_o) {
233 printf("got start_seq while stream_to is empty\n");
237 if (g.stream_enable_from && g.f_i != g.f_o) {
238 printf("got start_seq while stream_from is not empty\n");
243 g.stream_started = 1;
244 if (g.stream_enable_to) {
246 attachInterruptVector(IRQ_PORTB, portb_isr_do_to_inc);
247 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
250 attachInterruptVector(IRQ_PORTB, portb_isr_do_to);
251 attachInterruptVector(IRQ_PORTC, portc_isr_frameinc);
254 else if (g.stream_enable_from) {
256 attachInterruptVector(IRQ_PORTB,
257 portb_isr_fixed_do_from);
258 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
261 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
262 attachInterruptVector(IRQ_PORTC,
263 portc_isr_frameinc_do_from);
266 // prep first frame already
272 // callers must disable IRQs
273 static void clear_state(void)
275 g.stream_enable_to = 0;
276 g.stream_enable_from = 0;
278 g.stream_started = 0;
283 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
284 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
287 static int get_space_to(void)
289 return STREAM_BUF_SIZE - ((g.t_i - g.t_o) & STREAM_BUF_MASK);
292 static int get_used_from(void)
294 return (g.f_i - g.f_o) & STREAM_BUF_MASK;
297 static void do_usb(void *buf)
299 struct tas_pkt *pkt = buf;
304 case PKT_FIXED_STATE:
305 memcpy(g.fixed_state, pkt->data, sizeof(g.fixed_state));
307 case PKT_STREAM_ENABLE:
310 /* wait for start from MD */
311 g.stream_enable_to = pkt->enable.stream_to;
312 g.stream_enable_from = pkt->enable.stream_from;
313 g.use_readinc = pkt->enable.use_readinc;
316 case PKT_STREAM_ABORT:
323 printf("end of stream\n");
325 case PKT_STREAM_DATA_TO:
327 space = get_space_to();
328 if (space <= pkt->size / STREAM_EL_SZ) {
329 printf("got data pkt while space=%d\n", space);
332 for (i = 0; i < pkt->size / STREAM_EL_SZ; i++) {
333 memcpy(&g.stream_to[t_i++],
334 pkt->data + i * STREAM_EL_SZ,
336 t_i &= STREAM_BUF_MASK;
341 printf("got unknown pkt type: %04x\n", pkt->type);
348 uint32_t edge_cnt_last;
354 delay(1000); // wait for usb..
356 /* ?0SA 00DU, ?1CB RLDU */
357 g.fixed_state[0] = 0x33;
358 g.fixed_state[1] = 0x3f;
360 printf("starting, rawhid: %d\n", usb_rawhid_available());
362 // md pin th tr tl r l d u vsync
363 // md bit* 6 5 4 3 2 1 0
364 // t bit b16 d5 d4 d3 d2 d1 d0 c6
365 // t pin 0 20 6 8 7 14 2 11
366 // * - note: tl/tr mixed in most docs
368 attachInterrupt(0, portb_isr_fixed, CHANGE);
369 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
371 attachInterrupt(11, portc_isr_nop, RISING);
372 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
374 NVIC_SET_PRIORITY(IRQ_PORTB, 0);
375 NVIC_SET_PRIORITY(IRQ_PORTC, 16);
387 // CORE_PIN0_PORTSET CORE_PIN0_BITMASK PORTB_PCR16
388 printf("GPIOB PDDR, PDIR: %08x %08x\n", GPIOB_PDIR, GPIOB_PDDR);
389 printf("GPIOC PDDR, PDIR: %08x %08x\n", GPIOC_PDIR, GPIOC_PDDR);
390 printf("GPIOD PDDR, PDIR: %08x %08x\n", GPIOD_PDIR, GPIOD_PDDR);
391 printf("PORTB_PCR16: %08x\n", PORTB_PCR16);
392 printf("PORTC_PCR6: %08x\n", PORTC_PCR6);
394 asm("mrs %0, BASEPRI" : "=r"(ret));
395 printf("BASEPRI: %d\n", ret);
398 edge_cnt_last = g.edge_cnt;
403 while (g.stream_enable_to && !g.stream_ended
404 && get_space_to() > sizeof(pkt.data) / STREAM_EL_SZ)
406 pkt.type = PKT_STREAM_REQ;
407 pkt.req.frame = g.frame_cnt;
409 ret = usb_rawhid_send(&pkt, 1000);
410 if (ret != sizeof(pkt)) {
411 printf("send STREAM_REQ: %d\n", ret);
415 ret = usb_rawhid_recv(buf, 1000);
417 printf("usb_rawhid_recv/s: %d\n", ret);
422 while (g.stream_enable_from && !g.stream_ended
423 && get_used_from() >= sizeof(pkt.data) / STREAM_EL_SZ)
429 for (i = 0; i < sizeof(pkt.data); i += STREAM_EL_SZ) {
430 memcpy(pkt.data + i, &g.stream_from[f_o++],
432 f_o &= STREAM_BUF_MASK;
436 pkt.type = PKT_STREAM_DATA_FROM;
439 ret = usb_rawhid_send(&pkt, 1000);
440 if (ret != sizeof(pkt)) {
441 printf("send DATA_FROM: %d\n", ret);
446 if (timeout == 1000) {
447 edge_cnt = g.edge_cnt;
448 //printf("e: %d th: %d\n", edge_cnt - edge_cnt_last,
449 // (GPIOB_PDIR >> CORE_PIN0_BIT) & 1);
450 if (edge_cnt - edge_cnt_last > 10000) {
452 edge_cnt = g.edge_cnt;
454 edge_cnt_last = edge_cnt;
457 ret = usb_rawhid_recv(buf, timeout);
459 CORE_PIN13_PORTSET = CORE_PIN13_BITMASK;
465 CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK;
469 printf("usb_rawhid_recv: %d\n", ret);