| 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include "teensy3/core_pins.h" |
| 5 | #include "teensy3/usb_seremu.h" |
| 6 | #include "teensy3/usb_rawhid.h" |
| 7 | |
| 8 | /* ?0SA 00DU, ?1CB RLDU */ |
| 9 | static uint8_t fixed_state[4] = { 0x33, 0x3f }; |
| 10 | |
| 11 | ssize_t _write(int fd, const void *buf, size_t nbyte) |
| 12 | { |
| 13 | char tbuf[64]; |
| 14 | int ret; |
| 15 | |
| 16 | if (fd != 1 && fd != 2) { |
| 17 | snprintf(tbuf, sizeof(tbuf), "write to fd %d\n", fd); |
| 18 | usb_seremu_write(tbuf, strlen(tbuf)); |
| 19 | } |
| 20 | |
| 21 | ret = usb_seremu_write(buf, nbyte); |
| 22 | return ret < 0 ? ret : nbyte; |
| 23 | } |
| 24 | |
| 25 | void yield(void) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | static void pin0_irq(void) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void portb_isr(void) |
| 34 | { |
| 35 | uint32_t isfr; |
| 36 | |
| 37 | //printf("irq, GPIOB_PDIR: %08x\n", GPIOB_PDIR); |
| 38 | |
| 39 | GPIOD_PDOR = fixed_state[(GPIOB_PDIR >> CORE_PIN0_BIT) & 1]; |
| 40 | |
| 41 | isfr = PORTB_ISFR; |
| 42 | PORTB_ISFR = isfr; |
| 43 | } |
| 44 | |
| 45 | int main(void) |
| 46 | { |
| 47 | char buf[64]; |
| 48 | int timeout; |
| 49 | int ret; |
| 50 | |
| 51 | delay(1000); // wait for usb.. |
| 52 | |
| 53 | printf("starting, rawhid: %d\n", usb_rawhid_available()); |
| 54 | |
| 55 | // md pin th tr tl r l d u |
| 56 | // md bit* 6 5 4 3 2 1 0 |
| 57 | // t bit b16 d5 d4 d3 d2 d1 d0 |
| 58 | // t pin 0 20 6 8 7 14 2 |
| 59 | // * - note: tl/tr mixed in most docs |
| 60 | pinMode(0, INPUT); |
| 61 | attachInterrupt(0, pin0_irq, CHANGE); |
| 62 | |
| 63 | pinMode( 2, OUTPUT); |
| 64 | pinMode(14, OUTPUT); |
| 65 | pinMode( 7, OUTPUT); |
| 66 | pinMode( 8, OUTPUT); |
| 67 | pinMode( 6, OUTPUT); |
| 68 | pinMode(20, OUTPUT); |
| 69 | |
| 70 | // led |
| 71 | pinMode(13, OUTPUT); |
| 72 | // CORE_PIN13_PORTSET = CORE_PIN13_BITMASK; |
| 73 | // CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK; |
| 74 | |
| 75 | // CORE_PIN0_PORTSET CORE_PIN0_BITMASK PORTB_PCR16 |
| 76 | printf("GPIOC PDDR, PDIR: %08x %08x\n", GPIOC_PDIR, GPIOC_PDDR); |
| 77 | printf("GPIOD PDDR, PDIR: %08x %08x\n", GPIOD_PDIR, GPIOD_PDDR); |
| 78 | printf("PORTB_PCR16: %08x\n", PORTB_PCR16); |
| 79 | |
| 80 | // ret = usb_rawhid_send(buf, 2000); |
| 81 | |
| 82 | timeout = 1000; |
| 83 | |
| 84 | while (1) { |
| 85 | ret = usb_rawhid_recv(buf, timeout); |
| 86 | if (ret == 64) { |
| 87 | CORE_PIN13_PORTSET = CORE_PIN13_BITMASK; |
| 88 | |
| 89 | memcpy(fixed_state, buf, sizeof(fixed_state)); |
| 90 | timeout = 20; |
| 91 | } |
| 92 | else if (ret == 0) { |
| 93 | CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK; |
| 94 | timeout = 1000; |
| 95 | } |
| 96 | else { |
| 97 | printf("usb_rawhid_recv: %d\n", ret); |
| 98 | timeout = 1000; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |