blob: b6f2a4562d0ebf9be30187beb2b1738877099d6f [file] [log] [blame]
Brian Silvermand9566392018-06-10 15:02:03 -07001#include <inttypes.h>
2#include <stdio.h>
3
4#include <atomic>
5
Philipp Schrader790cb542023-07-05 21:06:52 -07006#include "motors/core/kinetis.h"
Brian Silvermand9566392018-06-10 15:02:03 -07007#include "motors/core/time.h"
8#include "motors/fet12/motor_controls.h"
9#include "motors/motor.h"
10#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 Silvermand9566392018-06-10 15:02:03 -070013#include "motors/util.h"
14
15namespace frc971 {
16namespace motors {
17namespace {
18
19struct Fet12AdcReadings {
20 uint16_t motor_currents[3];
21};
22
23void AdcInitFet12() {
24 AdcInitCommon();
25
26 // M_CH0V ADC1_SE13
27 PORTB_PCR7 = PORT_PCR_MUX(0);
28
29 // M_CH1V ADC1_SE14
30 PORTB_PCR10 = PORT_PCR_MUX(0);
31
32 // M_CH2V ADC1_SE4b
33 PORTC_PCR10 = PORT_PCR_MUX(0);
34
35 // M_CH0F ADC0_SE16
36 // dedicated
37
38 // M_CH1F ADC0_SE18
39 PORTE_PCR24 = PORT_PCR_MUX(0);
40
41 // M_CH2F ADC0_SE23
42 // dedicated
43}
44
45Fet12AdcReadings AdcReadFet12(const DisableInterrupts &) {
46 Fet12AdcReadings r;
47
48 ADC0_SC1A = 16;
49 while (!(ADC0_SC1A & ADC_SC1_COCO)) {
50 }
51 ADC0_SC1A = 18;
52 r.motor_currents[0] = ADC0_RA;
53 while (!(ADC0_SC1A & ADC_SC1_COCO)) {
54 }
55 ADC0_SC1A = 23;
56 r.motor_currents[1] = ADC0_RA;
57 while (!(ADC0_SC1A & ADC_SC1_COCO)) {
58 }
59 r.motor_currents[2] = ADC0_RA;
60
61 return r;
62}
63
64::std::atomic<Motor *> global_motor{nullptr};
Brian Silvermand9566392018-06-10 15:02:03 -070065
66extern "C" {
67
68void *__stack_chk_guard = (void *)0x67111971;
69void __stack_chk_fail(void) {
70 while (true) {
71 GPIOC_PSOR = (1 << 5);
72 printf("Stack corruption detected\n");
73 delay(1000);
74 GPIOC_PCOR = (1 << 5);
75 delay(1000);
76 }
77}
78
Brian Silvermand9566392018-06-10 15:02:03 -070079extern char *__brkval;
80extern uint32_t __bss_ram_start__[];
81extern uint32_t __heap_start__[];
82extern uint32_t __stack_end__[];
83
84void ftm0_isr(void) {
85 Fet12AdcReadings adc_readings;
86 {
87 DisableInterrupts disable_interrupts;
88 adc_readings = AdcReadFet12(disable_interrupts);
89 }
90#if 1
91 printf("%" PRIu16 " %" PRIu16 " %" PRIu16 "\n",
92 adc_readings.motor_currents[0], adc_readings.motor_currents[1],
93 adc_readings.motor_currents[2]);
94#endif
95 const BalancedReadings balanced =
96 BalanceSimpleReadings(adc_readings.motor_currents);
97
98#if 0
99 global_motor.load(::std::memory_order_relaxed)->HandleInterrupt(
100 balanced,
101 global_motor.load(::std::memory_order_relaxed)->wrapped_encoder());
102#else
103 (void)balanced;
104#endif
105 FTM0->SC &= ~FTM_SC_TOF;
106 FTM0->C0V = 0;
107 FTM0->C1V = 0;
108 FTM0->C2V = 0;
109 FTM0->C3V = 0;
110 FTM0->C4V = 0;
111 FTM0->C5V = 80;
112 FTM0->PWMLOAD = FTM_PWMLOAD_LDOK;
113}
114
115} // extern "C"
116
117void ConfigurePwmFtm(BigFTM *pwm_ftm) {
118 // Put them all into combine active-high mode, and all the low ones staying on
119 // all the time by default.
120 pwm_ftm->C0SC = FTM_CSC_ELSA;
121 pwm_ftm->C0V = 0;
122 pwm_ftm->C1SC = FTM_CSC_ELSA;
123 pwm_ftm->C1V = 0;
124 pwm_ftm->C2SC = FTM_CSC_ELSA;
125 pwm_ftm->C2V = 0;
126 pwm_ftm->C3SC = FTM_CSC_ELSA;
127 pwm_ftm->C3V = 0;
128 pwm_ftm->C4SC = FTM_CSC_ELSA;
129 pwm_ftm->C4V = 0;
130 pwm_ftm->C5SC = FTM_CSC_ELSA;
131 pwm_ftm->C5V = 0;
132 pwm_ftm->C6SC = FTM_CSC_ELSA;
133 pwm_ftm->C6V = 0;
134 pwm_ftm->C7SC = FTM_CSC_ELSA;
135 pwm_ftm->C7V = 0;
136
137 pwm_ftm->COMBINE = FTM_COMBINE_SYNCEN3 /* Synchronize updates usefully */ |
138 FTM_COMBINE_DTEN3 /* Enable deadtime */ |
139 FTM_COMBINE_COMP3 /* Make them complementary */ |
140 FTM_COMBINE_COMBINE3 /* Combine the channels */ |
141 FTM_COMBINE_SYNCEN2 /* Synchronize updates usefully */ |
142 FTM_COMBINE_DTEN2 /* Enable deadtime */ |
143 FTM_COMBINE_COMP2 /* Make them complementary */ |
144 FTM_COMBINE_COMBINE2 /* Combine the channels */ |
145 FTM_COMBINE_SYNCEN1 /* Synchronize updates usefully */ |
146 FTM_COMBINE_DTEN1 /* Enable deadtime */ |
147 FTM_COMBINE_COMP1 /* Make them complementary */ |
148 FTM_COMBINE_COMBINE1 /* Combine the channels */ |
149 FTM_COMBINE_SYNCEN0 /* Synchronize updates usefully */ |
150 FTM_COMBINE_DTEN0 /* Enable deadtime */ |
151 FTM_COMBINE_COMP0 /* Make them complementary */ |
152 FTM_COMBINE_COMBINE0 /* Combine the channels */;
153
154 // Set the deadtime.
155 pwm_ftm->DEADTIME =
156 FTM_DEADTIME_DTPS(0) /* Prescaler of 1 */ | FTM_DEADTIME_DTVAL(9);
157}
158
159// Zeros the encoder. This involves blocking for an arbitrary length of time
160// with interrupts disabled.
161void ZeroMotor() {
162#if 0
163 while (true) {
164 if (PERIPHERAL_BITBAND(GPIOA_PDIR, 7)) {
165 encoder_ftm_->CNT = 0;
166 break;
167 }
168 }
169#else
170 uint32_t scratch;
171 __disable_irq();
172 // Stuff all of this in an inline assembly statement so we can make sure the
173 // compiler doesn't decide sticking constant loads etc in the middle of
174 // the loop is a good idea, because that increases the latency of recognizing
175 // the index pulse edge which makes velocity affect the zeroing accuracy.
176 __asm__ __volatile__(
177 // A label to restart the loop.
178 "0:\n"
179 // Load the current PDIR value for the pin we care about.
180 "ldr %[scratch], [%[pdir_word]]\n"
181 // Terminate the loop if it's non-0.
182 "cbnz %[scratch], 1f\n"
183 // Go back around again.
184 "b 0b\n"
185 // A label to finish the loop.
186 "1:\n"
187 // Reset the count once we're down here. It doesn't actually matter what
188 // value we store because writing anything resets it to CNTIN (ie 0).
189 "str %[scratch], [%[cnt]]\n"
190 : [scratch] "=&l"(scratch)
Philipp Schrader790cb542023-07-05 21:06:52 -0700191 : [pdir_word] "l"(&PERIPHERAL_BITBAND(GPIOA_PDIR, 7)), [cnt] "l"(
192 &FTM1->CNT));
Brian Silvermand9566392018-06-10 15:02:03 -0700193 __enable_irq();
194#endif
195}
196
197} // namespace
198
199extern "C" int main(void) {
200 // for background about this startup delay, please see these conversations
201 // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
202 // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
203 delay(400);
204
205 // Set all interrupts to the second-lowest priority to start with.
206 for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD);
207
208 // Now set priorities for all the ones we care about. They only have meaning
209 // relative to each other, which means centralizing them here makes it a lot
210 // more manageable.
211 NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7);
212 NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0x3);
213
214 // Set the LED's pin to output mode.
215 PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1;
216 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1);
217
218#if 0
219 PERIPHERAL_BITBAND(GPIOA_PDDR, 15) = 1;
220 PORTA_PCR15 = PORT_PCR_DSE | PORT_PCR_MUX(1);
221#endif
222
223 // Set up the CAN pins.
224 PORTA_PCR12 = PORT_PCR_DSE | PORT_PCR_MUX(2);
225 PORTA_PCR13 = PORT_PCR_DSE | PORT_PCR_MUX(2);
226
Brian Silverman45564a82018-09-02 16:35:22 -0700227 DMA.CR = M_DMA_EMLM;
Brian Silvermand9566392018-06-10 15:02:03 -0700228
Brian Silverman4787a6e2018-10-06 16:00:54 -0700229 PrintingParameters printing_parameters;
230 printing_parameters.dedicated_usb = true;
231 const ::std::unique_ptr<PrintingImplementation> printing =
232 CreatePrinting(printing_parameters);
233 printing->Initialize();
Brian Silvermand9566392018-06-10 15:02:03 -0700234
235 AdcInitFet12();
236 MathInit();
237 delay(1000);
238 can_init(0, 1);
239
240#if 0
241 GPIOD_PCOR = 1 << 3;
242 PERIPHERAL_BITBAND(GPIOD_PDDR, 3) = 1;
243 PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(1);
244 delay(1000);
245 GPIOD_PSOR = 1 << 3;
246 delay(1000);
247 GPIOD_PCOR = 1 << 3;
248 delay(1000);
249#endif
250
251 MotorControlsImplementation controls;
252
253 delay(1000);
254
255 // Index pin
256 PORTA_PCR7 = PORT_PCR_MUX(1);
257 // FTM1_QD_PH{A,B}
258 PORTB_PCR0 = PORT_PCR_MUX(6);
259 PORTB_PCR1 = PORT_PCR_MUX(6);
260
261 // FTM0_CH[0-5]
262 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(4);
263 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(4);
264 PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(4);
265 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4);
266 PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4);
267 PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(4);
268
269 Motor motor(FTM0, FTM1, &controls, {&FTM0->C0V, &FTM0->C2V, &FTM0->C4V});
270 motor.set_encoder_offset(810);
271 motor.set_deadtime_compensation(9);
272 ConfigurePwmFtm(FTM0);
273 motor.Init();
274 global_motor.store(&motor, ::std::memory_order_relaxed);
275 // Output triggers to things like the PDBs on initialization.
276 FTM0_EXTTRIG = FTM_EXTTRIG_INITTRIGEN;
277 // Don't let any memory accesses sneak past here, because we actually
278 // need everything to be starting up.
Philipp Schrader790cb542023-07-05 21:06:52 -0700279 __asm__("" ::: "memory");
Brian Silvermand9566392018-06-10 15:02:03 -0700280
281 // Give everything a chance to get going.
282 delay(100);
283
284 printf("Ram start: %p\n", __bss_ram_start__);
285 printf("Heap start: %p\n", __heap_start__);
286 printf("Heap end: %p\n", __brkval);
287 printf("Stack start: %p\n", __stack_end__);
288
289 printf("Going silent to zero motors...\n");
290 // Give the print a chance to make it out.
291 delay(1000);
292#if 0
293 ZeroMotor();
Brian Silverman6c8b88b2018-09-03 18:17:02 -0700294#else
295 (void)ZeroMotor;
Brian Silvermand9566392018-06-10 15:02:03 -0700296#endif
297
298 printf("Zeroed motor!\n");
299 // Give stuff a chance to recover from interrupts-disabled.
300 delay(100);
301 motor.Start();
302 NVIC_ENABLE_IRQ(IRQ_FTM0);
303 GPIOC_PSOR = 1 << 5;
304
305 float current_command = 0;
306 while (true) {
307 unsigned char command_data[8];
308 int command_length;
309 can_receive(command_data, &command_length, 0);
310 if (command_length == 4) {
311 uint32_t result = command_data[0] << 24 | command_data[1] << 16 |
312 command_data[2] << 8 | command_data[3];
313 float current = static_cast<float>(result) / 1000.0f;
314
315 static bool high_gear = false;
316 if (controls.estimated_velocity() < -2015) {
317 high_gear = true;
318 }
319 if (current < 1) {
320 high_gear = false;
321 }
322 if (!high_gear) {
323 current = current_command * -120.0f / 120.0f;
324 } else {
325 current = current_command * 115.0f / 120.0f;
326 }
327 motor.SetGoalCurrent(current);
328 current_command = current;
329 }
330 }
331
332 return 0;
333}
334
335} // namespace motors
336} // namespace frc971