4 #include "teensy3/core_pins.h"
5 #include "teensy3/usb_seremu.h"
6 #include "teensy3/usb_rawhid.h"
10 #define STREAM_BUF_SIZE 512
11 #define STREAM_BUF_MASK (512 - 1)
13 /* ?0SA 00DU, ?1CB RLDU */
15 uint8_t stream[STREAM_BUF_SIZE][2];
16 uint8_t fixed_state[4];
17 uint32_t stream_enable:1;
18 uint32_t stream_started:1;
19 uint32_t stream_received:1;
25 #define STREAM_EL_SZ sizeof(g.stream[0])
27 ssize_t _write(int fd, const void *buf, size_t nbyte)
32 if (fd != 1 && fd != 2) {
33 snprintf(tbuf, sizeof(tbuf), "write to fd %d\n", fd);
34 usb_seremu_write(tbuf, strlen(tbuf));
37 ret = usb_seremu_write(buf, nbyte);
38 return ret < 0 ? ret : nbyte;
45 static void my_portb_isr(void)
49 //printf("irq, GPIOB_PDIR: %08x\n", GPIOB_PDIR);
53 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
55 if (g.stream_started) {
56 GPIOD_PDOR = g.stream[g.o][th];
58 g.o = (g.o + 1) & STREAM_BUF_MASK;
66 GPIOD_PDOR = g.fixed_state[th];
71 static void udelay(uint32_t us)
73 uint32_t start = micros();
75 while ((micros() - start) < us) {
76 asm volatile("nop; nop; nop; nop");
81 static void do_start_seq(void)
83 uint32_t edge_cnt_last;
85 uint32_t start, t1, t2;
89 edge_cnt = g.edge_cnt;
93 g.fixed_state[1] = 0x25;
95 for (tout = 10000; tout > 0; tout--) {
96 edge_cnt_last = edge_cnt;
98 edge_cnt = g.edge_cnt;
100 if (edge_cnt != edge_cnt_last)
102 if (!(GPIOB_PDIR & CORE_PIN0_BITMASK))
106 g.fixed_state[0] = 0x33;
107 g.fixed_state[1] = 0x3f;
112 printf("start_seq timeout1, t=%u\n", t1 - start);
116 for (tout = 100000; tout > 0; tout--) {
119 if (GPIOB_PDIR & CORE_PIN0_BITMASK)
125 printf("start_seq timeout2, t1=%u, t2=%u\n",
126 t1 - start, t2 - t1);
130 //printf(" t1=%u, t2=%u\n", t1 - start, t2 - t1);
132 if (g.stream_started) {
133 printf("got start_seq when already started\n");
137 if (!g.stream_enable) {
138 printf("got start_seq, without enable from USB\n");
143 printf("got start_seq while buffer is empty\n");
147 g.stream_started = 1;
150 static int get_space(void)
152 return STREAM_BUF_SIZE - ((g.i - g.o) & STREAM_BUF_MASK);
155 static void do_usb(void *buf)
157 struct tas_pkt *pkt = buf;
162 case PKT_FIXED_STATE:
163 memcpy(g.fixed_state, pkt->data, sizeof(g.fixed_state));
165 case PKT_STREAM_ENABLE:
167 g.stream_started = 0;
168 g.stream_received = 0;
172 g.stream_received = 1;
173 printf("end of data\n");
175 case PKT_STREAM_DATA:
178 if (space <= sizeof(pkt->data) / STREAM_EL_SZ) {
179 printf("got data pkt while space=%d\n", space);
182 for (i = 0; i < sizeof(pkt->data) / STREAM_EL_SZ; i++) {
183 memcpy(&g.stream[g_i++],
184 pkt->data + i * STREAM_EL_SZ,
186 g_i &= STREAM_BUF_MASK;
191 printf("got unknown pkt type: %04x\n", pkt->type);
198 uint32_t edge_cnt_last;
204 delay(1000); // wait for usb..
206 /* ?0SA 00DU, ?1CB RLDU */
207 g.fixed_state[0] = 0x33;
208 g.fixed_state[1] = 0x3f;
210 printf("starting, rawhid: %d\n", usb_rawhid_available());
212 // md pin th tr tl r l d u
213 // md bit* 6 5 4 3 2 1 0
214 // t bit b16 d5 d4 d3 d2 d1 d0
215 // t pin 0 20 6 8 7 14 2
216 // * - note: tl/tr mixed in most docs
218 attachInterrupt(0, my_portb_isr, CHANGE);
219 attachInterruptVector(IRQ_PORTB, my_portb_isr);
221 // NVIC_SET_PRIORITY(104, 0); // portb
222 NVIC_SET_PRIORITY(IRQ_PORTB, 0); // portb
234 // CORE_PIN0_PORTSET CORE_PIN0_BITMASK PORTB_PCR16
235 printf("GPIOC PDDR, PDIR: %08x %08x\n", GPIOC_PDIR, GPIOC_PDDR);
236 printf("GPIOD PDDR, PDIR: %08x %08x\n", GPIOD_PDIR, GPIOD_PDDR);
237 printf("PORTB_PCR16: %08x\n", PORTB_PCR16);
239 asm("mrs %0, BASEPRI" : "=r"(ret));
240 printf("BASEPRI: %d\n", ret);
243 edge_cnt_last = g.edge_cnt;
247 while (g.stream_enable && !g.stream_received
248 && get_space() > sizeof(pkt.data) / STREAM_EL_SZ)
250 pkt.type = PKT_STREAM_REQ;
251 ret = usb_rawhid_send(&pkt, 1000);
252 if (ret != sizeof(pkt)) {
253 printf("send STREAM_REQ: %d\n", ret);
257 ret = usb_rawhid_recv(buf, 1000);
259 printf("usb_rawhid_recv/s: %d\n", ret);
264 if (timeout == 1000) {
265 edge_cnt = g.edge_cnt;
266 //printf("e: %d th: %d\n", edge_cnt - edge_cnt_last,
267 // (GPIOB_PDIR >> CORE_PIN0_BIT) & 1);
268 if (edge_cnt - edge_cnt_last > 10000) {
270 edge_cnt = g.edge_cnt;
272 edge_cnt_last = edge_cnt;
275 ret = usb_rawhid_recv(buf, timeout);
277 CORE_PIN13_PORTSET = CORE_PIN13_BITMASK;
283 CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK;
287 printf("usb_rawhid_recv: %d\n", ret);