Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 1 | // This file has the main for the Teensy in the driver's station that |
| 2 | // communicates over CAN with the one in the pistol grip controller. |
| 3 | |
| 4 | #include <stdio.h> |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 5 | #include <atomic> |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 6 | |
| 7 | #include "motors/core/time.h" |
| 8 | #include "motors/core/kinetis.h" |
| 9 | #include "motors/usb/usb.h" |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 10 | #include "motors/usb/cdc.h" |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 11 | #include "motors/usb/hid.h" |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 12 | #include "motors/util.h" |
| 13 | |
| 14 | namespace frc971 { |
| 15 | namespace motors { |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 16 | namespace { |
| 17 | |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 18 | ::std::atomic<teensy::AcmTty *> global_stdout{nullptr}; |
| 19 | |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 20 | void EchoChunks(teensy::AcmTty *tty1) { |
| 21 | while (true) { |
| 22 | char buffer[512]; |
| 23 | size_t buffered = 0; |
| 24 | while (buffered < sizeof(buffer)) { |
| 25 | const size_t chunk = |
| 26 | tty1->Read(&buffer[buffered], sizeof(buffer) - buffered); |
| 27 | buffered += chunk; |
| 28 | } |
| 29 | size_t written = 0; |
| 30 | while (written < buffered) { |
| 31 | const size_t chunk = tty1->Write(&buffer[written], buffered - written); |
| 32 | written += chunk; |
| 33 | } |
| 34 | |
| 35 | GPIOC_PTOR = 1 << 5; |
| 36 | for (int i = 0; i < 100000000; ++i) { |
| 37 | GPIOC_PSOR = 0; |
| 38 | } |
| 39 | GPIOC_PTOR = 1 << 5; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void EchoImmediately(teensy::AcmTty *tty1) { |
| 44 | while (true) { |
| 45 | if (false) { |
| 46 | // Delay for a while. |
| 47 | for (int i = 0; i < 100000000; ++i) { |
| 48 | GPIOC_PSOR = 0; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | char buffer[64]; |
| 53 | const size_t chunk = tty1->Read(buffer, sizeof(buffer)); |
| 54 | size_t written = 0; |
| 55 | while (written < chunk) { |
| 56 | written += tty1->Write(&buffer[written], chunk - written); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void WriteData(teensy::AcmTty *tty1) { |
| 62 | GPIOC_PTOR = 1 << 5; |
| 63 | for (int i = 0; i < 100000000; ++i) { |
| 64 | GPIOC_PSOR = 0; |
| 65 | } |
| 66 | GPIOC_PTOR = 1 << 5; |
| 67 | for (int i = 0; i < 100000000; ++i) { |
| 68 | GPIOC_PSOR = 0; |
| 69 | } |
| 70 | |
| 71 | const char data[] = |
| 72 | "Running command lineRunning command lineRunning command lineRunning " |
| 73 | "command lineRunning command lineRunning command lineRunning command " |
| 74 | "lineRunning command lineRunning command lineRunning command lineRunning " |
| 75 | "command lineRunning command lineRunning command lineRunning command " |
| 76 | "lineRunning command lineRunning command lineRunning command lineRunning " |
| 77 | "command lineRunning command lineRunning command lineRunning command " |
| 78 | "lineRunning command lineRunning command lineRunning command lineRunning " |
| 79 | "command lineRunning command lineRunning command lineRunning command " |
| 80 | "lineRunning command lineRunning command lineRunning command lineRunning " |
| 81 | "command lineRunning command lineRunning command lineRunning command " |
| 82 | "lineRunning command lineRunning command lineRunning command lineRunning " |
| 83 | "command lineRunning command lineRunning command lineRunning command " |
| 84 | "lineRunning command lineRunning command lineRunning command lineRunning " |
| 85 | "command lineRunning command lineRunning command lineRunning command " |
| 86 | "lineRunning command lineRunning command lineRunning command lineRunning " |
| 87 | "command lineRunning command lineRunning command lineRunning command " |
| 88 | "lineRunning command lineRunning command lineRunning command lineRunning " |
| 89 | "command lineRunning command lineRunning command lineRunning command " |
| 90 | "lineRunning command line\n"; |
| 91 | size_t written = 0; |
| 92 | while (written < sizeof(data)) { |
| 93 | written += tty1->Write(&data[written], sizeof(data) - written); |
| 94 | } |
| 95 | GPIOC_PSOR = 1 << 5; |
| 96 | while (true) { |
| 97 | } |
| 98 | } |
| 99 | |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 100 | void MoveJoysticks(teensy::HidFunction *joysticks) { |
| 101 | static uint8_t x_axis = 0, y_axis = 97, z_axis = 5, rz_axis = 8; |
| 102 | while (true) { |
| 103 | { |
| 104 | DisableInterrupts disable_interrupts; |
| 105 | uint8_t buttons = 0; |
| 106 | if (x_axis % 8u) { |
| 107 | buttons = 2; |
| 108 | } |
| 109 | uint8_t report[10] = {x_axis, 0, y_axis, 0, z_axis, |
| 110 | rz_axis, 0, 0, buttons, 0}; |
| 111 | joysticks->UpdateReport(report, 10, disable_interrupts); |
| 112 | } |
| 113 | delay(1); |
| 114 | x_axis += 1; |
| 115 | y_axis += 3; |
| 116 | z_axis += 5; |
| 117 | rz_axis += 8; |
| 118 | } |
| 119 | } |
| 120 | |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 121 | } // namespace |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 122 | |
| 123 | extern "C" { |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 124 | |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 125 | void *__stack_chk_guard = (void *)0x67111971; |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 126 | |
| 127 | int _write(int /*file*/, char *ptr, int len) { |
| 128 | teensy::AcmTty *const tty = global_stdout.load(::std::memory_order_acquire); |
| 129 | if (tty != nullptr) { |
| 130 | return tty->Write(ptr, len); |
| 131 | } |
| 132 | return 0; |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 133 | } |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 134 | |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 135 | void __stack_chk_fail(void); |
| 136 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 137 | } // extern "C" |
| 138 | |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 139 | extern "C" int main(void) { |
| 140 | // for background about this startup delay, please see these conversations |
| 141 | // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980 |
| 142 | // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273 |
| 143 | delay(400); |
| 144 | |
| 145 | // Set all interrupts to the second-lowest priority to start with. |
| 146 | for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD); |
| 147 | |
| 148 | // Now set priorities for all the ones we care about. They only have meaning |
| 149 | // relative to each other, which means centralizing them here makes it a lot |
| 150 | // more manageable. |
| 151 | NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7); |
| 152 | |
| 153 | // Set the LED's pin to output mode. |
| 154 | GPIO_BITBAND(GPIOC_PDDR, 5) = 1; |
| 155 | PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 156 | |
| 157 | delay(100); |
| 158 | |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 159 | teensy::UsbDevice usb_device(0, 0x16c0, 0x0492); |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 160 | usb_device.SetManufacturer("FRC 971 Spartan Robotics"); |
| 161 | usb_device.SetProduct("Pistol Grip Controller interface"); |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 162 | teensy::HidFunction joysticks(&usb_device, 10); |
| 163 | // TODO(Brian): Figure out why Windows refuses to recognize the joystick along |
| 164 | // with a TTY or two. |
| 165 | #if 0 |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 166 | teensy::AcmTty tty1(&usb_device); |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 167 | teensy::AcmTty tty2(&usb_device); |
| 168 | global_stdout.store(&tty2, ::std::memory_order_release); |
| 169 | #endif |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 170 | usb_device.Initialize(); |
| 171 | |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 172 | MoveJoysticks(&joysticks); |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | void __stack_chk_fail(void) { |
| 178 | while (true) { |
| 179 | GPIOC_PSOR = (1 << 5); |
| 180 | printf("Stack corruption detected\n"); |
| 181 | delay(1000); |
| 182 | GPIOC_PCOR = (1 << 5); |
| 183 | delay(1000); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } // namespace motors |
| 188 | } // namespace frc971 |