Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 1 | // This file has the main for the Teensy on the button board. |
| 2 | |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 3 | #include <inttypes.h> |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 4 | #include <stdio.h> |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 5 | #include <atomic> |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 6 | #include <cmath> |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 7 | |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 8 | #include "motors/core/kinetis.h" |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 9 | #include "motors/core/time.h" |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 10 | #include "motors/peripheral/adc.h" |
| 11 | #include "motors/peripheral/can.h" |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 12 | #include "motors/print/print.h" |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 13 | #include "motors/usb/cdc.h" |
| 14 | #include "motors/usb/hid.h" |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 15 | #include "motors/usb/usb.h" |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 16 | #include "motors/util.h" |
| 17 | |
| 18 | namespace frc971 { |
| 19 | namespace motors { |
| 20 | namespace { |
| 21 | |
Brian Silverman | 9ed2cf1 | 2018-05-12 13:06:38 -0700 | [diff] [blame] | 22 | struct JoystickAdcReadings { |
| 23 | uint16_t analog0, analog1, analog2, analog3; |
| 24 | }; |
| 25 | |
| 26 | void AdcInitJoystick() { |
| 27 | AdcInitCommon(); |
| 28 | |
| 29 | // ANALOG0 ADC0_SE5b |
| 30 | PORTD_PCR1 = PORT_PCR_MUX(0); |
| 31 | // ANALOG1 ADC0_SE14 |
| 32 | PORTC_PCR0 = PORT_PCR_MUX(0); |
| 33 | // ANALOG2 ADC0_SE13 |
| 34 | PORTB_PCR3 = PORT_PCR_MUX(0); |
| 35 | // ANALOG3 ADC0_SE12 |
| 36 | PORTB_PCR2 = PORT_PCR_MUX(0); |
| 37 | } |
| 38 | |
| 39 | JoystickAdcReadings AdcReadJoystick(const DisableInterrupts &) { |
| 40 | JoystickAdcReadings r; |
| 41 | |
| 42 | ADC0_SC1A = 5; |
| 43 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 44 | } |
| 45 | ADC0_SC1A = 14; |
| 46 | r.analog0 = ADC0_RA; |
| 47 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 48 | } |
| 49 | ADC0_SC1A = 13; |
| 50 | r.analog1 = ADC0_RA; |
| 51 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 52 | } |
| 53 | ADC0_SC1A = 12; |
| 54 | r.analog2 = ADC0_RA; |
| 55 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 56 | } |
| 57 | r.analog3 = ADC0_RA; |
| 58 | |
| 59 | return r; |
| 60 | } |
| 61 | |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 62 | // The HID report descriptor we use. |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 63 | constexpr char kReportDescriptor1[] = { |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 64 | 0x05, 0x01, // Usage Page (Generic Desktop), |
| 65 | 0x09, 0x04, // Usage (Joystick), |
| 66 | 0xA1, 0x01, // Collection (Application), |
| 67 | 0x75, 0x08, // Report Size (8), |
| 68 | 0x95, 0x04, // Report Count (4), |
| 69 | 0x15, 0x00, // Logical Minimum (0), |
| 70 | 0x26, 0xFF, 0x00, // Logical Maximum (255), |
| 71 | 0x35, 0x00, // Physical Minimum (0), |
| 72 | 0x46, 0xFF, 0x00, // Physical Maximum (255), |
| 73 | 0x09, 0x30, // Usage (X), |
| 74 | 0x09, 0x31, // Usage (Y), |
| 75 | 0x09, 0x32, // Usage (Z), |
| 76 | 0x09, 0x33, // Usage (Rz), |
| 77 | 0x81, 0x02, // Input (Variable), |
| 78 | 0x75, 0x01, // Report Size (1), |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 79 | 0x95, 0x10, // Report Count (16), |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 80 | 0x25, 0x01, // Logical Maximum (1), |
| 81 | 0x45, 0x01, // Physical Maximum (1), |
| 82 | 0x05, 0x09, // Usage Page (Button), |
| 83 | 0x19, 0x01, // Usage Minimum (01), |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 84 | 0x29, 0x10, // Usage Maximum (16), |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 85 | 0x81, 0x02, // Input (Variable), |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 86 | 0xC0 // End Collection |
| 87 | }; |
| 88 | |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 89 | constexpr uint16_t report_size() { return 1 * 4 + 2; } |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 90 | |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 91 | char DecodeAnalog(int analog) { |
| 92 | // None: 132 |
| 93 | // Far: 71 |
| 94 | // Near: 103 |
| 95 | // Both: 0 |
| 96 | if (analog < 30) { |
| 97 | return 0x3; |
| 98 | } else if (::std::abs(analog - 71) < 10) { |
| 99 | return 0x2; |
| 100 | } else if (::std::abs(analog - 103) < 10) { |
| 101 | return 0x1; |
| 102 | } else { |
| 103 | return 0x0; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void SendJoystickData(teensy::HidFunction *joystick0, |
| 108 | teensy::HidFunction *joystick1) { |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 109 | uint32_t start = micros(); |
| 110 | while (true) { |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 111 | JoystickAdcReadings adc; |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 112 | char report0[report_size()]; |
| 113 | char report1[report_size()]; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 114 | { |
| 115 | DisableInterrupts disable_interrupts; |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 116 | adc = AdcReadJoystick(disable_interrupts); |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | FTM0->C1V = adc.analog0 / 4; |
| 120 | FTM0->C0V = adc.analog1 / 4; |
| 121 | FTM0->C4V = adc.analog2 / 4; |
| 122 | FTM0->C3V = adc.analog3 / 4; |
| 123 | FTM0->PWMLOAD = FTM_PWMLOAD_LDOK; |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 124 | report0[0] = report1[0] = adc.analog0 / 16; |
| 125 | report0[1] = report1[1] = adc.analog1 / 16; |
| 126 | report0[2] = report1[2] = adc.analog2 / 16; |
| 127 | report0[3] = report1[3] = adc.analog3 / 16; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 128 | |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 129 | report0[4] = ((PERIPHERAL_BITBAND(GPIOD_PDIR, 5) << 0) | |
| 130 | (PERIPHERAL_BITBAND(GPIOD_PDIR, 6) << 1) | |
| 131 | (PERIPHERAL_BITBAND(GPIOB_PDIR, 0) << 2) | |
| 132 | (PERIPHERAL_BITBAND(GPIOB_PDIR, 1) << 3) | |
| 133 | (PERIPHERAL_BITBAND(GPIOA_PDIR, 14) << 4) | |
| 134 | (PERIPHERAL_BITBAND(GPIOE_PDIR, 26) << 5) | |
| 135 | (PERIPHERAL_BITBAND(GPIOA_PDIR, 16) << 6) | |
| 136 | (PERIPHERAL_BITBAND(GPIOA_PDIR, 15) << 7)) ^ |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 137 | 0xff; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 138 | |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 139 | report0[5] = ((PERIPHERAL_BITBAND(GPIOE_PDIR, 25) << 0) | |
| 140 | (PERIPHERAL_BITBAND(GPIOE_PDIR, 24) << 1) | |
| 141 | (PERIPHERAL_BITBAND(GPIOC_PDIR, 3) << 2) | |
| 142 | (PERIPHERAL_BITBAND(GPIOC_PDIR, 7) << 3) | |
| 143 | (PERIPHERAL_BITBAND(GPIOD_PDIR, 3) << 4) | |
| 144 | (PERIPHERAL_BITBAND(GPIOD_PDIR, 2) << 5) | |
| 145 | (PERIPHERAL_BITBAND(GPIOD_PDIR, 7) << 6) | |
| 146 | (PERIPHERAL_BITBAND(GPIOA_PDIR, 13) << 7)) ^ |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 147 | 0xff; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 148 | |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 149 | report1[4] = |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 150 | ((PERIPHERAL_BITBAND(GPIOA_PDIR, 12) << 0) | |
| 151 | (PERIPHERAL_BITBAND(GPIOD_PDIR, 0) << 1) | |
| 152 | (PERIPHERAL_BITBAND(GPIOB_PDIR, 17) << 2) | |
| 153 | (PERIPHERAL_BITBAND(GPIOB_PDIR, 16) << 3) | (DecodeAnalog(report1[0]) << 4) | |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 154 | (DecodeAnalog(report1[1]) << 6)) ^ |
| 155 | 0x0f; |
| 156 | report1[5] = (DecodeAnalog(report1[2])) | (DecodeAnalog(report1[3]) << 2); |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 157 | |
| 158 | { |
| 159 | DisableInterrupts disable_interrupts; |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 160 | joystick0->UpdateReport(report0, sizeof(report0), disable_interrupts); |
| 161 | joystick1->UpdateReport(report1, sizeof(report1), disable_interrupts); |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | start = delay_from(start, 1); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void SetupLedFtm(BigFTM *ftm) { |
| 169 | // PWMSYNC doesn't matter because we set SYNCMODE down below. |
| 170 | ftm->MODE = FTM_MODE_WPDIS; |
| 171 | ftm->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN; |
| 172 | ftm->SC = FTM_SC_CLKS(0) /* Disable counting for now */; |
| 173 | |
| 174 | // Use center-aligned high-true for all the channels. |
| 175 | ftm->C0SC = FTM_CSC_ELSB; |
| 176 | ftm->C0V = 0; |
| 177 | ftm->C1SC = FTM_CSC_ELSB; |
| 178 | ftm->C1V = 0; |
| 179 | ftm->C2SC = FTM_CSC_ELSB; |
| 180 | ftm->C2V = 0; |
| 181 | ftm->C3SC = FTM_CSC_ELSB; |
| 182 | ftm->C3V = 0; |
| 183 | ftm->C4SC = FTM_CSC_ELSB; |
| 184 | ftm->C4V = 0; |
| 185 | ftm->C5SC = FTM_CSC_ELSB; |
| 186 | ftm->C5V = 0; |
| 187 | ftm->C6SC = FTM_CSC_ELSB; |
| 188 | ftm->C6V = 0; |
| 189 | ftm->C7SC = FTM_CSC_ELSB; |
| 190 | ftm->C7V = 0; |
| 191 | |
| 192 | ftm->COMBINE = FTM_COMBINE_SYNCEN3 /* Synchronize updates usefully */ | |
| 193 | FTM_COMBINE_SYNCEN2 /* Synchronize updates usefully */ | |
| 194 | FTM_COMBINE_SYNCEN1 /* Synchronize updates usefully */ | |
| 195 | FTM_COMBINE_SYNCEN0 /* Synchronize updates usefully */; |
| 196 | |
| 197 | ftm->CNTIN = 0; |
| 198 | ftm->CNT = 0; |
| 199 | ftm->MOD = 1024; |
| 200 | ftm->OUTINIT = 0; |
| 201 | ftm->POL = 0; |
| 202 | ftm->SYNCONF = |
| 203 | FTM_SYNCONF_HWWRBUF /* Hardware trigger flushes switching points */ | |
| 204 | FTM_SYNCONF_SWWRBUF /* Software trigger flushes switching points */ | |
| 205 | FTM_SYNCONF_SWRSTCNT /* Software trigger resets the count */ | |
| 206 | FTM_SYNCONF_SYNCMODE /* Use the new synchronization mode */; |
| 207 | // Don't want any intermediate loading points. |
| 208 | ftm->PWMLOAD = 0; |
| 209 | |
| 210 | ftm->SYNC = FTM_SYNC_SWSYNC /* Flush everything out right now */; |
| 211 | // Wait for the software synchronization to finish. |
| 212 | while (ftm->SYNC & FTM_SYNC_SWSYNC) { |
| 213 | } |
| 214 | ftm->SC = FTM_SC_CPWMS /* Center-aligned PWM */ | |
| 215 | FTM_SC_CLKS(1) /* Use the system clock */ | |
Brian Silverman | 5ffd2c2 | 2018-03-23 22:26:17 -0400 | [diff] [blame] | 216 | FTM_SC_PS(6) /* Prescaler=64 */; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 217 | |
| 218 | ftm->MODE &= ~FTM_MODE_WPDIS; |
| 219 | } |
| 220 | |
| 221 | } // namespace |
| 222 | |
| 223 | extern "C" { |
| 224 | |
| 225 | void *__stack_chk_guard = (void *)0x67111971; |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 226 | void __stack_chk_fail(void) { |
| 227 | while (true) { |
| 228 | GPIOC_PSOR = (1 << 5); |
| 229 | printf("Stack corruption detected\n"); |
| 230 | delay(1000); |
| 231 | GPIOC_PCOR = (1 << 5); |
| 232 | delay(1000); |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 233 | } |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 236 | } // extern "C" |
| 237 | |
| 238 | extern "C" int main(void) { |
| 239 | // for background about this startup delay, please see these conversations |
| 240 | // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980 |
| 241 | // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273 |
| 242 | delay(400); |
| 243 | |
| 244 | // Set all interrupts to the second-lowest priority to start with. |
| 245 | for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD); |
| 246 | |
| 247 | // Now set priorities for all the ones we care about. They only have meaning |
| 248 | // relative to each other, which means centralizing them here makes it a lot |
| 249 | // more manageable. |
| 250 | NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7); |
| 251 | |
| 252 | // Set all the LED pins to output, slew rate controlled, high drive strength. |
| 253 | // Builtin |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 254 | PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 1; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 255 | PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(1); |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 256 | PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 257 | // LED0 FTM0_CH1 |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 258 | PERIPHERAL_BITBAND(GPIOC_PDOR, 2) = 0; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 259 | PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4); |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 260 | PERIPHERAL_BITBAND(GPIOC_PDDR, 2) = 1; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 261 | // LED1 FTM0_CH0 |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 262 | PERIPHERAL_BITBAND(GPIOC_PDOR, 1) = 0; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 263 | PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4); |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 264 | PERIPHERAL_BITBAND(GPIOC_PDDR, 1) = 1; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 265 | // LED2 FTM0_CH4 |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 266 | PERIPHERAL_BITBAND(GPIOD_PDOR, 4) = 0; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 267 | PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4); |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 268 | PERIPHERAL_BITBAND(GPIOD_PDDR, 4) = 1; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 269 | // LED3 FTM0_CH3 |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 270 | PERIPHERAL_BITBAND(GPIOC_PDOR, 4) = 0; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 271 | PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4); |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 272 | PERIPHERAL_BITBAND(GPIOC_PDDR, 4) = 1; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 273 | // LED4 FTM3_CH4 yellow |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 274 | PERIPHERAL_BITBAND(GPIOC_PDOR, 8) = 0; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 275 | PORTC_PCR8 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(1); |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 276 | PERIPHERAL_BITBAND(GPIOC_PDDR, 8) = 1; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 277 | // LED5 FTM3_CH5 green |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 278 | PERIPHERAL_BITBAND(GPIOC_PDOR, 9) = 0; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 279 | PORTC_PCR9 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(1); |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 280 | PERIPHERAL_BITBAND(GPIOC_PDDR, 9) = 1; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 281 | // LED6 FTM3_CH6 red |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 282 | PERIPHERAL_BITBAND(GPIOC_PDOR, 10) = 0; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 283 | PORTC_PCR10 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(1); |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 284 | PERIPHERAL_BITBAND(GPIOC_PDDR, 10) = 1; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 285 | |
| 286 | // Set up the CAN pins. |
| 287 | PORTB_PCR18 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 288 | PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 289 | |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 290 | // .1ms filter time. |
| 291 | PORTA_DFWR = PORTB_DFWR = PORTC_DFWR = PORTD_DFWR = PORTE_DFWR = 6000; |
| 292 | |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 293 | // Set up the buttons. The LEDs pull them up to 5V, so the Teensy needs to not |
| 294 | // be set to pull up. |
| 295 | // BTN0 |
| 296 | PORTD_PCR5 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 297 | PORTD_DFER |= 1 << 5; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 298 | // BTN1 |
| 299 | PORTD_PCR6 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 300 | PORTD_DFER |= 1 << 6; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 301 | // BTN2 |
Brian Silverman | 772a348 | 2018-02-05 23:54:00 -0500 | [diff] [blame] | 302 | PORTB_PCR0 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 303 | PORTB_DFER |= 1 << 0; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 304 | // BTN3 |
| 305 | PORTB_PCR1 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 306 | PORTB_DFER |= 1 << 1; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 307 | // BTN4 |
| 308 | PORTA_PCR14 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 309 | PORTA_DFER |= 1 << 14; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 310 | // BTN5 |
| 311 | PORTE_PCR26 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 312 | PORTE_DFER |= 1 << 26; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 313 | // BTN6 |
| 314 | PORTA_PCR16 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 315 | PORTA_DFER |= 1 << 16; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 316 | // BTN7 |
| 317 | PORTA_PCR15 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 318 | PORTA_DFER |= 1 << 15; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 319 | // BTN8 |
| 320 | PORTE_PCR25 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 321 | PORTE_DFER |= 1 << 25; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 322 | // BTN9 |
| 323 | PORTE_PCR24 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 324 | PORTE_DFER |= 1 << 24; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 325 | // BTN10 |
| 326 | PORTC_PCR3 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 327 | PORTC_DFER |= 1 << 3; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 328 | // BTN11 |
| 329 | PORTC_PCR7 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 330 | PORTC_DFER |= 1 << 7; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 331 | // BTN12 |
| 332 | PORTD_PCR3 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 333 | PORTD_DFER |= 1 << 3; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 334 | // BTN13 |
| 335 | PORTD_PCR2 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 336 | PORTD_DFER |= 1 << 2; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 337 | // BTN14 |
| 338 | PORTD_PCR7 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 339 | PORTD_DFER |= 1 << 7; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 340 | // BTN15 |
| 341 | PORTA_PCR13 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 342 | PORTA_DFER |= 1 << 13; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 343 | // BTN16 |
| 344 | PORTA_PCR12 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 345 | PORTA_DFER |= 1 << 12; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 346 | // BTN17 |
| 347 | PORTD_PCR0 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 348 | PORTD_DFER |= 1 << 0; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 349 | // BTN18 |
| 350 | PORTB_PCR17 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 351 | PORTB_DFER |= 1 << 17; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 352 | // BTN19 |
| 353 | PORTB_PCR16 = PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 354 | PORTB_DFER |= 1 << 16; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 355 | |
| 356 | delay(100); |
| 357 | |
| 358 | teensy::UsbDevice usb_device(0, 0x16c0, 0x0492); |
| 359 | usb_device.SetManufacturer("FRC 971 Spartan Robotics"); |
| 360 | usb_device.SetProduct("Spartan Joystick Board"); |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 361 | |
| 362 | teensy::HidFunction joystick0(&usb_device, report_size()); |
| 363 | joystick0.set_report_descriptor( |
| 364 | ::std::string(kReportDescriptor1, sizeof(kReportDescriptor1))); |
| 365 | |
| 366 | teensy::HidFunction joystick1(&usb_device, report_size()); |
| 367 | joystick1.set_report_descriptor( |
| 368 | ::std::string(kReportDescriptor1, sizeof(kReportDescriptor1))); |
| 369 | |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 370 | teensy::AcmTty tty1(&usb_device); |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 371 | PrintingParameters printing_parameters; |
| 372 | printing_parameters.stdout_tty = &tty1; |
| 373 | |
| 374 | const ::std::unique_ptr<PrintingImplementation> printing = |
| 375 | CreatePrinting(printing_parameters); |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 376 | usb_device.Initialize(); |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 377 | printing->Initialize(); |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 378 | |
| 379 | can_init(0, 1); |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 380 | AdcInitJoystick(); |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 381 | SetupLedFtm(FTM0); |
| 382 | SetupLedFtm(FTM3); |
| 383 | |
| 384 | // Leave the LEDs on for a bit longer. |
| 385 | delay(300); |
| 386 | printf("Done starting up\n"); |
| 387 | |
| 388 | // Done starting up, now turn all the LEDs off. |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 389 | PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 0; |
| 390 | PERIPHERAL_BITBAND(GPIOC_PDOR, 2) = 1; |
| 391 | PERIPHERAL_BITBAND(GPIOC_PDOR, 1) = 1; |
| 392 | PERIPHERAL_BITBAND(GPIOD_PDOR, 4) = 1; |
| 393 | PERIPHERAL_BITBAND(GPIOC_PDOR, 4) = 1; |
| 394 | PERIPHERAL_BITBAND(GPIOC_PDOR, 8) = 1; |
| 395 | PERIPHERAL_BITBAND(GPIOC_PDOR, 9) = 1; |
| 396 | PERIPHERAL_BITBAND(GPIOC_PDOR, 10) = 1; |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 397 | |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 398 | SendJoystickData(&joystick0, &joystick1); |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
Brian Silverman | 259c443 | 2018-01-15 14:34:21 -0800 | [diff] [blame] | 403 | } // namespace motors |
| 404 | } // namespace frc971 |