blob: fb17328459c64dbbc3717ceb43049751a6f290eb [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>
Philipp Schrader790cb542023-07-05 21:06:52 -07005
Brian Silverman259c4432018-01-15 14:34:21 -08006#include <atomic>
Austin Schuh75c6af42018-03-09 21:16:07 -08007#include <cmath>
Brian Silverman259c4432018-01-15 14:34:21 -08008
Brian Silverman259c4432018-01-15 14:34:21 -08009#include "motors/core/kinetis.h"
Brian Silverman4787a6e2018-10-06 16:00:54 -070010#include "motors/core/time.h"
Brian Silverman259c4432018-01-15 14:34:21 -080011#include "motors/peripheral/adc.h"
12#include "motors/peripheral/can.h"
Brian Silverman4787a6e2018-10-06 16:00:54 -070013#include "motors/print/print.h"
Brian Silverman259c4432018-01-15 14:34:21 -080014#include "motors/usb/cdc.h"
15#include "motors/usb/hid.h"
Brian Silverman4787a6e2018-10-06 16:00:54 -070016#include "motors/usb/usb.h"
Brian Silverman259c4432018-01-15 14:34:21 -080017#include "motors/util.h"
18
19namespace frc971 {
20namespace motors {
21namespace {
22
Brian Silverman9ed2cf12018-05-12 13:06:38 -070023struct JoystickAdcReadings {
24 uint16_t analog0, analog1, analog2, analog3;
25};
26
27void AdcInitJoystick() {
28 AdcInitCommon();
29
30 // ANALOG0 ADC0_SE5b
31 PORTD_PCR1 = PORT_PCR_MUX(0);
32 // ANALOG1 ADC0_SE14
33 PORTC_PCR0 = PORT_PCR_MUX(0);
34 // ANALOG2 ADC0_SE13
35 PORTB_PCR3 = PORT_PCR_MUX(0);
36 // ANALOG3 ADC0_SE12
37 PORTB_PCR2 = PORT_PCR_MUX(0);
38}
39
40JoystickAdcReadings AdcReadJoystick(const DisableInterrupts &) {
41 JoystickAdcReadings r;
42
43 ADC0_SC1A = 5;
44 while (!(ADC0_SC1A & ADC_SC1_COCO)) {
45 }
46 ADC0_SC1A = 14;
47 r.analog0 = ADC0_RA;
48 while (!(ADC0_SC1A & ADC_SC1_COCO)) {
49 }
50 ADC0_SC1A = 13;
51 r.analog1 = ADC0_RA;
52 while (!(ADC0_SC1A & ADC_SC1_COCO)) {
53 }
54 ADC0_SC1A = 12;
55 r.analog2 = ADC0_RA;
56 while (!(ADC0_SC1A & ADC_SC1_COCO)) {
57 }
58 r.analog3 = ADC0_RA;
59
60 return r;
61}
62
Brian Silverman259c4432018-01-15 14:34:21 -080063// The HID report descriptor we use.
Austin Schuh75c6af42018-03-09 21:16:07 -080064constexpr char kReportDescriptor1[] = {
Brian Silverman259c4432018-01-15 14:34:21 -080065 0x05, 0x01, // Usage Page (Generic Desktop),
66 0x09, 0x04, // Usage (Joystick),
67 0xA1, 0x01, // Collection (Application),
68 0x75, 0x08, // Report Size (8),
69 0x95, 0x04, // Report Count (4),
70 0x15, 0x00, // Logical Minimum (0),
71 0x26, 0xFF, 0x00, // Logical Maximum (255),
72 0x35, 0x00, // Physical Minimum (0),
73 0x46, 0xFF, 0x00, // Physical Maximum (255),
74 0x09, 0x30, // Usage (X),
75 0x09, 0x31, // Usage (Y),
76 0x09, 0x32, // Usage (Z),
77 0x09, 0x33, // Usage (Rz),
78 0x81, 0x02, // Input (Variable),
79 0x75, 0x01, // Report Size (1),
Austin Schuh75c6af42018-03-09 21:16:07 -080080 0x95, 0x10, // Report Count (16),
Brian Silverman259c4432018-01-15 14:34:21 -080081 0x25, 0x01, // Logical Maximum (1),
82 0x45, 0x01, // Physical Maximum (1),
83 0x05, 0x09, // Usage Page (Button),
84 0x19, 0x01, // Usage Minimum (01),
Austin Schuh75c6af42018-03-09 21:16:07 -080085 0x29, 0x10, // Usage Maximum (16),
Brian Silverman259c4432018-01-15 14:34:21 -080086 0x81, 0x02, // Input (Variable),
Brian Silverman259c4432018-01-15 14:34:21 -080087 0xC0 // End Collection
88};
89
Austin Schuh75c6af42018-03-09 21:16:07 -080090constexpr uint16_t report_size() { return 1 * 4 + 2; }
Brian Silverman259c4432018-01-15 14:34:21 -080091
Austin Schuh75c6af42018-03-09 21:16:07 -080092char DecodeAnalog(int analog) {
93 // None: 132
94 // Far: 71
95 // Near: 103
96 // Both: 0
97 if (analog < 30) {
98 return 0x3;
99 } else if (::std::abs(analog - 71) < 10) {
100 return 0x2;
101 } else if (::std::abs(analog - 103) < 10) {
102 return 0x1;
103 } else {
104 return 0x0;
105 }
106}
107
108void SendJoystickData(teensy::HidFunction *joystick0,
109 teensy::HidFunction *joystick1) {
Brian Silverman259c4432018-01-15 14:34:21 -0800110 uint32_t start = micros();
111 while (true) {
Brian Silvermana96c1a42018-05-12 12:11:31 -0700112 JoystickAdcReadings adc;
Austin Schuh75c6af42018-03-09 21:16:07 -0800113 char report0[report_size()];
114 char report1[report_size()];
Brian Silverman259c4432018-01-15 14:34:21 -0800115 {
116 DisableInterrupts disable_interrupts;
Brian Silvermana96c1a42018-05-12 12:11:31 -0700117 adc = AdcReadJoystick(disable_interrupts);
Brian Silverman259c4432018-01-15 14:34:21 -0800118 }
119
120 FTM0->C1V = adc.analog0 / 4;
121 FTM0->C0V = adc.analog1 / 4;
122 FTM0->C4V = adc.analog2 / 4;
123 FTM0->C3V = adc.analog3 / 4;
124 FTM0->PWMLOAD = FTM_PWMLOAD_LDOK;
Austin Schuh75c6af42018-03-09 21:16:07 -0800125 report0[0] = report1[0] = adc.analog0 / 16;
126 report0[1] = report1[1] = adc.analog1 / 16;
127 report0[2] = report1[2] = adc.analog2 / 16;
128 report0[3] = report1[3] = adc.analog3 / 16;
Brian Silverman259c4432018-01-15 14:34:21 -0800129
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500130 report0[4] = ((PERIPHERAL_BITBAND(GPIOD_PDIR, 5) << 0) |
131 (PERIPHERAL_BITBAND(GPIOD_PDIR, 6) << 1) |
132 (PERIPHERAL_BITBAND(GPIOB_PDIR, 0) << 2) |
133 (PERIPHERAL_BITBAND(GPIOB_PDIR, 1) << 3) |
134 (PERIPHERAL_BITBAND(GPIOA_PDIR, 14) << 4) |
135 (PERIPHERAL_BITBAND(GPIOE_PDIR, 26) << 5) |
136 (PERIPHERAL_BITBAND(GPIOA_PDIR, 16) << 6) |
137 (PERIPHERAL_BITBAND(GPIOA_PDIR, 15) << 7)) ^
Austin Schuh75c6af42018-03-09 21:16:07 -0800138 0xff;
Brian Silverman259c4432018-01-15 14:34:21 -0800139
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500140 report0[5] = ((PERIPHERAL_BITBAND(GPIOE_PDIR, 25) << 0) |
141 (PERIPHERAL_BITBAND(GPIOE_PDIR, 24) << 1) |
142 (PERIPHERAL_BITBAND(GPIOC_PDIR, 3) << 2) |
143 (PERIPHERAL_BITBAND(GPIOC_PDIR, 7) << 3) |
144 (PERIPHERAL_BITBAND(GPIOD_PDIR, 3) << 4) |
145 (PERIPHERAL_BITBAND(GPIOD_PDIR, 2) << 5) |
146 (PERIPHERAL_BITBAND(GPIOD_PDIR, 7) << 6) |
147 (PERIPHERAL_BITBAND(GPIOA_PDIR, 13) << 7)) ^
Austin Schuh75c6af42018-03-09 21:16:07 -0800148 0xff;
Brian Silverman259c4432018-01-15 14:34:21 -0800149
Austin Schuh75c6af42018-03-09 21:16:07 -0800150 report1[4] =
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500151 ((PERIPHERAL_BITBAND(GPIOA_PDIR, 12) << 0) |
152 (PERIPHERAL_BITBAND(GPIOD_PDIR, 0) << 1) |
153 (PERIPHERAL_BITBAND(GPIOB_PDIR, 17) << 2) |
Philipp Schrader790cb542023-07-05 21:06:52 -0700154 (PERIPHERAL_BITBAND(GPIOB_PDIR, 16) << 3) |
155 (DecodeAnalog(report1[0]) << 4) | (DecodeAnalog(report1[1]) << 6)) ^
Austin Schuh75c6af42018-03-09 21:16:07 -0800156 0x0f;
157 report1[5] = (DecodeAnalog(report1[2])) | (DecodeAnalog(report1[3]) << 2);
Brian Silverman259c4432018-01-15 14:34:21 -0800158
159 {
160 DisableInterrupts disable_interrupts;
Austin Schuh75c6af42018-03-09 21:16:07 -0800161 joystick0->UpdateReport(report0, sizeof(report0), disable_interrupts);
162 joystick1->UpdateReport(report1, sizeof(report1), disable_interrupts);
Brian Silverman259c4432018-01-15 14:34:21 -0800163 }
164
165 start = delay_from(start, 1);
166 }
167}
168
169void SetupLedFtm(BigFTM *ftm) {
170 // PWMSYNC doesn't matter because we set SYNCMODE down below.
171 ftm->MODE = FTM_MODE_WPDIS;
172 ftm->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN;
173 ftm->SC = FTM_SC_CLKS(0) /* Disable counting for now */;
174
175 // Use center-aligned high-true for all the channels.
176 ftm->C0SC = FTM_CSC_ELSB;
177 ftm->C0V = 0;
178 ftm->C1SC = FTM_CSC_ELSB;
179 ftm->C1V = 0;
180 ftm->C2SC = FTM_CSC_ELSB;
181 ftm->C2V = 0;
182 ftm->C3SC = FTM_CSC_ELSB;
183 ftm->C3V = 0;
184 ftm->C4SC = FTM_CSC_ELSB;
185 ftm->C4V = 0;
186 ftm->C5SC = FTM_CSC_ELSB;
187 ftm->C5V = 0;
188 ftm->C6SC = FTM_CSC_ELSB;
189 ftm->C6V = 0;
190 ftm->C7SC = FTM_CSC_ELSB;
191 ftm->C7V = 0;
192
193 ftm->COMBINE = FTM_COMBINE_SYNCEN3 /* Synchronize updates usefully */ |
194 FTM_COMBINE_SYNCEN2 /* Synchronize updates usefully */ |
195 FTM_COMBINE_SYNCEN1 /* Synchronize updates usefully */ |
196 FTM_COMBINE_SYNCEN0 /* Synchronize updates usefully */;
197
198 ftm->CNTIN = 0;
199 ftm->CNT = 0;
200 ftm->MOD = 1024;
201 ftm->OUTINIT = 0;
202 ftm->POL = 0;
203 ftm->SYNCONF =
204 FTM_SYNCONF_HWWRBUF /* Hardware trigger flushes switching points */ |
205 FTM_SYNCONF_SWWRBUF /* Software trigger flushes switching points */ |
206 FTM_SYNCONF_SWRSTCNT /* Software trigger resets the count */ |
207 FTM_SYNCONF_SYNCMODE /* Use the new synchronization mode */;
208 // Don't want any intermediate loading points.
209 ftm->PWMLOAD = 0;
210
211 ftm->SYNC = FTM_SYNC_SWSYNC /* Flush everything out right now */;
212 // Wait for the software synchronization to finish.
213 while (ftm->SYNC & FTM_SYNC_SWSYNC) {
214 }
215 ftm->SC = FTM_SC_CPWMS /* Center-aligned PWM */ |
216 FTM_SC_CLKS(1) /* Use the system clock */ |
Brian Silverman5ffd2c22018-03-23 22:26:17 -0400217 FTM_SC_PS(6) /* Prescaler=64 */;
Brian Silverman259c4432018-01-15 14:34:21 -0800218
219 ftm->MODE &= ~FTM_MODE_WPDIS;
220}
221
222} // namespace
223
224extern "C" {
225
226void *__stack_chk_guard = (void *)0x67111971;
Brian Silverman4787a6e2018-10-06 16:00:54 -0700227void __stack_chk_fail(void) {
228 while (true) {
229 GPIOC_PSOR = (1 << 5);
230 printf("Stack corruption detected\n");
231 delay(1000);
232 GPIOC_PCOR = (1 << 5);
233 delay(1000);
Brian Silverman259c4432018-01-15 14:34:21 -0800234 }
Brian Silverman259c4432018-01-15 14:34:21 -0800235}
236
Brian Silverman259c4432018-01-15 14:34:21 -0800237} // extern "C"
238
239extern "C" int main(void) {
240 // for background about this startup delay, please see these conversations
241 // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
242 // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
243 delay(400);
244
245 // Set all interrupts to the second-lowest priority to start with.
246 for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD);
247
248 // Now set priorities for all the ones we care about. They only have meaning
249 // relative to each other, which means centralizing them here makes it a lot
250 // more manageable.
251 NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7);
252
253 // Set all the LED pins to output, slew rate controlled, high drive strength.
254 // Builtin
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500255 PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800256 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(1);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500257 PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800258 // LED0 FTM0_CH1
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500259 PERIPHERAL_BITBAND(GPIOC_PDOR, 2) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800260 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500261 PERIPHERAL_BITBAND(GPIOC_PDDR, 2) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800262 // LED1 FTM0_CH0
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500263 PERIPHERAL_BITBAND(GPIOC_PDOR, 1) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800264 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500265 PERIPHERAL_BITBAND(GPIOC_PDDR, 1) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800266 // LED2 FTM0_CH4
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500267 PERIPHERAL_BITBAND(GPIOD_PDOR, 4) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800268 PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500269 PERIPHERAL_BITBAND(GPIOD_PDDR, 4) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800270 // LED3 FTM0_CH3
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500271 PERIPHERAL_BITBAND(GPIOC_PDOR, 4) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800272 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(4);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500273 PERIPHERAL_BITBAND(GPIOC_PDDR, 4) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800274 // LED4 FTM3_CH4 yellow
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500275 PERIPHERAL_BITBAND(GPIOC_PDOR, 8) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800276 PORTC_PCR8 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(1);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500277 PERIPHERAL_BITBAND(GPIOC_PDDR, 8) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800278 // LED5 FTM3_CH5 green
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500279 PERIPHERAL_BITBAND(GPIOC_PDOR, 9) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800280 PORTC_PCR9 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(1);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500281 PERIPHERAL_BITBAND(GPIOC_PDDR, 9) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800282 // LED6 FTM3_CH6 red
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500283 PERIPHERAL_BITBAND(GPIOC_PDOR, 10) = 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800284 PORTC_PCR10 = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_MUX(1);
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500285 PERIPHERAL_BITBAND(GPIOC_PDDR, 10) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800286
287 // Set up the CAN pins.
288 PORTB_PCR18 = PORT_PCR_DSE | PORT_PCR_MUX(2);
289 PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(2);
290
Brian Silvermanff7b3872018-03-10 18:08:30 -0800291 // .1ms filter time.
292 PORTA_DFWR = PORTB_DFWR = PORTC_DFWR = PORTD_DFWR = PORTE_DFWR = 6000;
293
Brian Silverman259c4432018-01-15 14:34:21 -0800294 // Set up the buttons. The LEDs pull them up to 5V, so the Teensy needs to not
295 // be set to pull up.
296 // BTN0
297 PORTD_PCR5 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800298 PORTD_DFER |= 1 << 5;
Brian Silverman259c4432018-01-15 14:34:21 -0800299 // BTN1
300 PORTD_PCR6 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800301 PORTD_DFER |= 1 << 6;
Brian Silverman259c4432018-01-15 14:34:21 -0800302 // BTN2
Brian Silverman772a3482018-02-05 23:54:00 -0500303 PORTB_PCR0 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800304 PORTB_DFER |= 1 << 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800305 // BTN3
306 PORTB_PCR1 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800307 PORTB_DFER |= 1 << 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800308 // BTN4
309 PORTA_PCR14 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800310 PORTA_DFER |= 1 << 14;
Brian Silverman259c4432018-01-15 14:34:21 -0800311 // BTN5
312 PORTE_PCR26 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800313 PORTE_DFER |= 1 << 26;
Brian Silverman259c4432018-01-15 14:34:21 -0800314 // BTN6
315 PORTA_PCR16 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800316 PORTA_DFER |= 1 << 16;
Brian Silverman259c4432018-01-15 14:34:21 -0800317 // BTN7
318 PORTA_PCR15 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800319 PORTA_DFER |= 1 << 15;
Brian Silverman259c4432018-01-15 14:34:21 -0800320 // BTN8
321 PORTE_PCR25 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800322 PORTE_DFER |= 1 << 25;
Brian Silverman259c4432018-01-15 14:34:21 -0800323 // BTN9
324 PORTE_PCR24 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800325 PORTE_DFER |= 1 << 24;
Brian Silverman259c4432018-01-15 14:34:21 -0800326 // BTN10
327 PORTC_PCR3 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800328 PORTC_DFER |= 1 << 3;
Brian Silverman259c4432018-01-15 14:34:21 -0800329 // BTN11
330 PORTC_PCR7 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800331 PORTC_DFER |= 1 << 7;
Brian Silverman259c4432018-01-15 14:34:21 -0800332 // BTN12
333 PORTD_PCR3 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800334 PORTD_DFER |= 1 << 3;
Brian Silverman259c4432018-01-15 14:34:21 -0800335 // BTN13
336 PORTD_PCR2 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800337 PORTD_DFER |= 1 << 2;
Brian Silverman259c4432018-01-15 14:34:21 -0800338 // BTN14
339 PORTD_PCR7 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800340 PORTD_DFER |= 1 << 7;
Brian Silverman259c4432018-01-15 14:34:21 -0800341 // BTN15
342 PORTA_PCR13 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800343 PORTA_DFER |= 1 << 13;
Brian Silverman259c4432018-01-15 14:34:21 -0800344 // BTN16
345 PORTA_PCR12 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800346 PORTA_DFER |= 1 << 12;
Brian Silverman259c4432018-01-15 14:34:21 -0800347 // BTN17
348 PORTD_PCR0 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800349 PORTD_DFER |= 1 << 0;
Brian Silverman259c4432018-01-15 14:34:21 -0800350 // BTN18
351 PORTB_PCR17 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800352 PORTB_DFER |= 1 << 17;
Brian Silverman259c4432018-01-15 14:34:21 -0800353 // BTN19
354 PORTB_PCR16 = PORT_PCR_MUX(1);
Brian Silvermanff7b3872018-03-10 18:08:30 -0800355 PORTB_DFER |= 1 << 16;
Brian Silverman259c4432018-01-15 14:34:21 -0800356
357 delay(100);
358
359 teensy::UsbDevice usb_device(0, 0x16c0, 0x0492);
360 usb_device.SetManufacturer("FRC 971 Spartan Robotics");
361 usb_device.SetProduct("Spartan Joystick Board");
Austin Schuh75c6af42018-03-09 21:16:07 -0800362
363 teensy::HidFunction joystick0(&usb_device, report_size());
364 joystick0.set_report_descriptor(
365 ::std::string(kReportDescriptor1, sizeof(kReportDescriptor1)));
366
367 teensy::HidFunction joystick1(&usb_device, report_size());
368 joystick1.set_report_descriptor(
369 ::std::string(kReportDescriptor1, sizeof(kReportDescriptor1)));
370
Brian Silverman259c4432018-01-15 14:34:21 -0800371 teensy::AcmTty tty1(&usb_device);
Brian Silverman4787a6e2018-10-06 16:00:54 -0700372 PrintingParameters printing_parameters;
373 printing_parameters.stdout_tty = &tty1;
374
375 const ::std::unique_ptr<PrintingImplementation> printing =
376 CreatePrinting(printing_parameters);
Brian Silverman259c4432018-01-15 14:34:21 -0800377 usb_device.Initialize();
Brian Silverman4787a6e2018-10-06 16:00:54 -0700378 printing->Initialize();
Brian Silverman259c4432018-01-15 14:34:21 -0800379
380 can_init(0, 1);
Brian Silvermana96c1a42018-05-12 12:11:31 -0700381 AdcInitJoystick();
Brian Silverman259c4432018-01-15 14:34:21 -0800382 SetupLedFtm(FTM0);
383 SetupLedFtm(FTM3);
384
385 // Leave the LEDs on for a bit longer.
386 delay(300);
387 printf("Done starting up\n");
388
389 // Done starting up, now turn all the LEDs off.
Brian Silverman33eb5fa2018-02-11 18:36:19 -0500390 PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 0;
391 PERIPHERAL_BITBAND(GPIOC_PDOR, 2) = 1;
392 PERIPHERAL_BITBAND(GPIOC_PDOR, 1) = 1;
393 PERIPHERAL_BITBAND(GPIOD_PDOR, 4) = 1;
394 PERIPHERAL_BITBAND(GPIOC_PDOR, 4) = 1;
395 PERIPHERAL_BITBAND(GPIOC_PDOR, 8) = 1;
396 PERIPHERAL_BITBAND(GPIOC_PDOR, 9) = 1;
397 PERIPHERAL_BITBAND(GPIOC_PDOR, 10) = 1;
Brian Silverman259c4432018-01-15 14:34:21 -0800398
Austin Schuh75c6af42018-03-09 21:16:07 -0800399 SendJoystickData(&joystick0, &joystick1);
Brian Silverman259c4432018-01-15 14:34:21 -0800400
401 return 0;
402}
403
Brian Silverman259c4432018-01-15 14:34:21 -0800404} // namespace motors
405} // namespace frc971