blob: 0c0050b24fda355f271891c07cb4f696c21603b1 [file] [log] [blame]
Brian Silverman259c4432018-01-15 14:34:21 -08001// This file has the main for the Teensy on the button board.
2
Brian Silverman259c4432018-01-15 14:34:21 -08003#include <inttypes.h>
Austin Schuh75c6af42018-03-09 21:16:07 -08004#include <stdio.h>
Brian Silverman259c4432018-01-15 14:34:21 -08005#include <atomic>
Austin Schuh75c6af42018-03-09 21:16:07 -08006#include <cmath>
Brian Silverman259c4432018-01-15 14:34:21 -08007
Brian Silverman259c4432018-01-15 14:34:21 -08008#include "motors/core/kinetis.h"
Brian Silverman4787a6e2018-10-06 16:00:54 -07009#include "motors/core/time.h"
Brian Silverman259c4432018-01-15 14:34:21 -080010#include "motors/peripheral/adc.h"
11#include "motors/peripheral/can.h"
Brian Silverman4787a6e2018-10-06 16:00:54 -070012#include "motors/print/print.h"
Brian Silverman259c4432018-01-15 14:34:21 -080013#include "motors/usb/cdc.h"
14#include "motors/usb/hid.h"
Brian Silverman4787a6e2018-10-06 16:00:54 -070015#include "motors/usb/usb.h"
Brian Silverman259c4432018-01-15 14:34:21 -080016#include "motors/util.h"
17
18namespace frc971 {
19namespace motors {
20namespace {
21
Brian Silverman9ed2cf12018-05-12 13:06:38 -070022struct JoystickAdcReadings {
23 uint16_t analog0, analog1, analog2, analog3;
24};
25
26void 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
39JoystickAdcReadings 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 Silverman259c4432018-01-15 14:34:21 -080062// The HID report descriptor we use.
Austin Schuh75c6af42018-03-09 21:16:07 -080063constexpr char kReportDescriptor1[] = {
Brian Silverman259c4432018-01-15 14:34:21 -080064 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 Schuh75c6af42018-03-09 21:16:07 -080079 0x95, 0x10, // Report Count (16),
Brian Silverman259c4432018-01-15 14:34:21 -080080 0x25, 0x01, // Logical Maximum (1),
81 0x45, 0x01, // Physical Maximum (1),
82 0x05, 0x09, // Usage Page (Button),
83 0x19, 0x01, // Usage Minimum (01),
Austin Schuh75c6af42018-03-09 21:16:07 -080084 0x29, 0x10, // Usage Maximum (16),
Brian Silverman259c4432018-01-15 14:34:21 -080085 0x81, 0x02, // Input (Variable),
Brian Silverman259c4432018-01-15 14:34:21 -080086 0xC0 // End Collection
87};
88
Austin Schuh75c6af42018-03-09 21:16:07 -080089constexpr uint16_t report_size() { return 1 * 4 + 2; }
Brian Silverman259c4432018-01-15 14:34:21 -080090
Austin Schuh75c6af42018-03-09 21:16:07 -080091char 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
107void SendJoystickData(teensy::HidFunction *joystick0,
108 teensy::HidFunction *joystick1) {
Brian Silverman259c4432018-01-15 14:34:21 -0800109 uint32_t start = micros();
110 while (true) {
Brian Silvermana96c1a42018-05-12 12:11:31 -0700111 JoystickAdcReadings adc;
Austin Schuh75c6af42018-03-09 21:16:07 -0800112 char report0[report_size()];
113 char report1[report_size()];
Brian Silverman259c4432018-01-15 14:34:21 -0800114 {
115 DisableInterrupts disable_interrupts;
Brian Silvermana96c1a42018-05-12 12:11:31 -0700116 adc = AdcReadJoystick(disable_interrupts);
Brian Silverman259c4432018-01-15 14:34:21 -0800117 }
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 Schuh75c6af42018-03-09 21:16:07 -0800124 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 Silverman259c4432018-01-15 14:34:21 -0800128
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500129 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 Schuh75c6af42018-03-09 21:16:07 -0800137 0xff;
Brian Silverman259c4432018-01-15 14:34:21 -0800138
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500139 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 Schuh75c6af42018-03-09 21:16:07 -0800147 0xff;
Brian Silverman259c4432018-01-15 14:34:21 -0800148
Austin Schuh75c6af42018-03-09 21:16:07 -0800149 report1[4] =
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500150 ((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 Schuh75c6af42018-03-09 21:16:07 -0800154 (DecodeAnalog(report1[1]) << 6)) ^
155 0x0f;
156 report1[5] = (DecodeAnalog(report1[2])) | (DecodeAnalog(report1[3]) << 2);
Brian Silverman259c4432018-01-15 14:34:21 -0800157
158 {
159 DisableInterrupts disable_interrupts;
Austin Schuh75c6af42018-03-09 21:16:07 -0800160 joystick0->UpdateReport(report0, sizeof(report0), disable_interrupts);
161 joystick1->UpdateReport(report1, sizeof(report1), disable_interrupts);
Brian Silverman259c4432018-01-15 14:34:21 -0800162 }
163
164 start = delay_from(start, 1);
165 }
166}
167
168void 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 Silverman5ffd2c22018-03-23 22:26:17 -0400216 FTM_SC_PS(6) /* Prescaler=64 */;
Brian Silverman259c4432018-01-15 14:34:21 -0800217
218 ftm->MODE &= ~FTM_MODE_WPDIS;
219}
220
221} // namespace
222
223extern "C" {
224
225void *__stack_chk_guard = (void *)0x67111971;
Brian Silverman4787a6e2018-10-06 16:00:54 -0700226void __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 Silverman259c4432018-01-15 14:34:21 -0800233 }
Brian Silverman259c4432018-01-15 14:34:21 -0800234}
235
Brian Silverman259c4432018-01-15 14:34:21 -0800236} // extern "C"
237
238extern "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 Silverman33eb5fa2018-02-11 18:36:19 -0500254 PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800255 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(1);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500256 PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800257 // LED0 FTM0_CH1
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500258 PERIPHERAL_BITBAND(GPIOC_PDOR, 2) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800259 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500260 PERIPHERAL_BITBAND(GPIOC_PDDR, 2) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800261 // LED1 FTM0_CH0
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500262 PERIPHERAL_BITBAND(GPIOC_PDOR, 1) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800263 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500264 PERIPHERAL_BITBAND(GPIOC_PDDR, 1) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800265 // LED2 FTM0_CH4
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500266 PERIPHERAL_BITBAND(GPIOD_PDOR, 4) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800267 PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500268 PERIPHERAL_BITBAND(GPIOD_PDDR, 4) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800269 // LED3 FTM0_CH3
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500270 PERIPHERAL_BITBAND(GPIOC_PDOR, 4) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800271 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500272 PERIPHERAL_BITBAND(GPIOC_PDDR, 4) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800273 // LED4 FTM3_CH4 yellow
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500274 PERIPHERAL_BITBAND(GPIOC_PDOR, 8) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800275 PORTC_PCR8 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(1);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500276 PERIPHERAL_BITBAND(GPIOC_PDDR, 8) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800277 // LED5 FTM3_CH5 green
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500278 PERIPHERAL_BITBAND(GPIOC_PDOR, 9) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800279 PORTC_PCR9 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(1);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500280 PERIPHERAL_BITBAND(GPIOC_PDDR, 9) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800281 // LED6 FTM3_CH6 red
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500282 PERIPHERAL_BITBAND(GPIOC_PDOR, 10) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800283 PORTC_PCR10 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(1);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500284 PERIPHERAL_BITBAND(GPIOC_PDDR, 10) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800285
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 Silvermanff7b3872018-03-10 18:08:30 -0800290 // .1ms filter time.
291 PORTA_DFWR = PORTB_DFWR = PORTC_DFWR = PORTD_DFWR = PORTE_DFWR = 6000;
292
Brian Silverman259c4432018-01-15 14:34:21 -0800293 // 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 Silvermanff7b3872018-03-10 18:08:30 -0800297 PORTD_DFER |= 1 << 5;
Brian Silverman259c4432018-01-15 14:34:21 -0800298 // BTN1
299 PORTD_PCR6 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800300 PORTD_DFER |= 1 << 6;
Brian Silverman259c4432018-01-15 14:34:21 -0800301 // BTN2
Brian Silverman772a3482018-02-05 23:54:00 -0500302 PORTB_PCR0 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800303 PORTB_DFER |= 1 << 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800304 // BTN3
305 PORTB_PCR1 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800306 PORTB_DFER |= 1 << 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800307 // BTN4
308 PORTA_PCR14 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800309 PORTA_DFER |= 1 << 14;
Brian Silverman259c4432018-01-15 14:34:21 -0800310 // BTN5
311 PORTE_PCR26 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800312 PORTE_DFER |= 1 << 26;
Brian Silverman259c4432018-01-15 14:34:21 -0800313 // BTN6
314 PORTA_PCR16 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800315 PORTA_DFER |= 1 << 16;
Brian Silverman259c4432018-01-15 14:34:21 -0800316 // BTN7
317 PORTA_PCR15 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800318 PORTA_DFER |= 1 << 15;
Brian Silverman259c4432018-01-15 14:34:21 -0800319 // BTN8
320 PORTE_PCR25 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800321 PORTE_DFER |= 1 << 25;
Brian Silverman259c4432018-01-15 14:34:21 -0800322 // BTN9
323 PORTE_PCR24 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800324 PORTE_DFER |= 1 << 24;
Brian Silverman259c4432018-01-15 14:34:21 -0800325 // BTN10
326 PORTC_PCR3 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800327 PORTC_DFER |= 1 << 3;
Brian Silverman259c4432018-01-15 14:34:21 -0800328 // BTN11
329 PORTC_PCR7 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800330 PORTC_DFER |= 1 << 7;
Brian Silverman259c4432018-01-15 14:34:21 -0800331 // BTN12
332 PORTD_PCR3 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800333 PORTD_DFER |= 1 << 3;
Brian Silverman259c4432018-01-15 14:34:21 -0800334 // BTN13
335 PORTD_PCR2 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800336 PORTD_DFER |= 1 << 2;
Brian Silverman259c4432018-01-15 14:34:21 -0800337 // BTN14
338 PORTD_PCR7 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800339 PORTD_DFER |= 1 << 7;
Brian Silverman259c4432018-01-15 14:34:21 -0800340 // BTN15
341 PORTA_PCR13 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800342 PORTA_DFER |= 1 << 13;
Brian Silverman259c4432018-01-15 14:34:21 -0800343 // BTN16
344 PORTA_PCR12 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800345 PORTA_DFER |= 1 << 12;
Brian Silverman259c4432018-01-15 14:34:21 -0800346 // BTN17
347 PORTD_PCR0 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800348 PORTD_DFER |= 1 << 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800349 // BTN18
350 PORTB_PCR17 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800351 PORTB_DFER |= 1 << 17;
Brian Silverman259c4432018-01-15 14:34:21 -0800352 // BTN19
353 PORTB_PCR16 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800354 PORTB_DFER |= 1 << 16;
Brian Silverman259c4432018-01-15 14:34:21 -0800355
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 Schuh75c6af42018-03-09 21:16:07 -0800361
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 Silverman259c4432018-01-15 14:34:21 -0800370 teensy::AcmTty tty1(&usb_device);
Brian Silverman4787a6e2018-10-06 16:00:54 -0700371 PrintingParameters printing_parameters;
372 printing_parameters.stdout_tty = &tty1;
373
374 const ::std::unique_ptr<PrintingImplementation> printing =
375 CreatePrinting(printing_parameters);
Brian Silverman259c4432018-01-15 14:34:21 -0800376 usb_device.Initialize();
Brian Silverman4787a6e2018-10-06 16:00:54 -0700377 printing->Initialize();
Brian Silverman259c4432018-01-15 14:34:21 -0800378
379 can_init(0, 1);
Brian Silvermana96c1a42018-05-12 12:11:31 -0700380 AdcInitJoystick();
Brian Silverman259c4432018-01-15 14:34:21 -0800381 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 Silverman33eb5fa2018-02-11 18:36:19 -0500389 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 Silverman259c4432018-01-15 14:34:21 -0800397
Austin Schuh75c6af42018-03-09 21:16:07 -0800398 SendJoystickData(&joystick0, &joystick1);
Brian Silverman259c4432018-01-15 14:34:21 -0800399
400 return 0;
401}
402
Brian Silverman259c4432018-01-15 14:34:21 -0800403} // namespace motors
404} // namespace frc971