blob: 5f8568b5d422445fe0134c52074b9355f4e01bd0 [file] [log] [blame]
James Kuszmaul998d3032018-09-08 15:41:41 -07001#include "motors/core/kinetis.h"
2
3#include <inttypes.h>
4#include <stdio.h>
5
6#include <atomic>
7
8#include "motors/core/time.h"
9#include "motors/fet12/current_equalization.h"
10#include "motors/fet12/motor_controls.h"
11#include "motors/motor.h"
12#include "motors/peripheral/adc.h"
James Kuszmaul7c8aad62018-09-08 18:16:18 -070013#include "motors/peripheral/adc_dma.h"
James Kuszmaul998d3032018-09-08 15:41:41 -070014#include "motors/peripheral/can.h"
15#include "motors/peripheral/uart.h"
16#include "motors/util.h"
17#include "third_party/GSL/include/gsl/gsl"
18
19namespace frc971 {
20namespace motors {
21namespace {
22
23constexpr double Kv = 22000.0 * 2.0 * M_PI / 60.0 / 30.0 * 3.6;
24constexpr double kVcc = 31.5;
25constexpr double kIcc = 125.0;
26constexpr double kR = 0.0084;
27
28struct Fet12AdcReadings {
James Kuszmaul7c8aad62018-09-08 18:16:18 -070029 // Averages of the pairs of ADC DMA channels corresponding with each channel
30 // pair. Individual values in motor_currents correspond to current sensor
31 // values, rather than the actual currents themselves (and so they still need
32 // to be decoupled).
James Kuszmaul998d3032018-09-08 15:41:41 -070033 int16_t motor_currents[3];
34 int16_t throttle, fuse_voltage;
35};
36
37void AdcInitFet12() {
38 AdcInitCommon(AdcChannels::kB, AdcChannels::kA);
39
40 // M_CH0V ADC0_SE5b
41 PORTD_PCR1 = PORT_PCR_MUX(0);
42
43 // M_CH1V ADC0_SE7b
44 PORTD_PCR6 = PORT_PCR_MUX(0);
45
46 // M_CH2V ADC0_SE14
47 PORTC_PCR0 = PORT_PCR_MUX(0);
48
49 // M_CH0F ADC1_SE5a
50 PORTE_PCR1 = PORT_PCR_MUX(0);
51
52 // M_CH1F ADC1_SE6a
53 PORTE_PCR2 = PORT_PCR_MUX(0);
54
55 // M_CH2F ADC1_SE7a
56 PORTE_PCR3 = PORT_PCR_MUX(0);
57
58 // SENSE0 ADC0_SE23
59 // dedicated
60
61 // SENSE1 ADC0_SE13
62 PORTB_PCR3 = PORT_PCR_MUX(0);
63}
64
James Kuszmaul998d3032018-09-08 15:41:41 -070065::std::atomic<Motor *> global_motor{nullptr};
James Kuszmaul7c8aad62018-09-08 18:16:18 -070066::std::atomic<teensy::AdcDmaSampler *> global_adc_dma{nullptr};
67
James Kuszmaul998d3032018-09-08 15:41:41 -070068::std::atomic<teensy::InterruptBufferedUart *> global_stdout{nullptr};
69
70extern "C" {
71
72void uart0_status_isr(void) {
73 teensy::InterruptBufferedUart *const tty =
74 global_stdout.load(::std::memory_order_relaxed);
75 DisableInterrupts disable_interrupts;
76 tty->HandleInterrupt(disable_interrupts);
77}
78
79void *__stack_chk_guard = (void *)0x67111971;
80void __stack_chk_fail(void) {
81 while (true) {
82 GPIOC_PSOR = (1 << 5);
83 printf("Stack corruption detected\n");
84 delay(1000);
85 GPIOC_PCOR = (1 << 5);
86 delay(1000);
87 }
88}
89
90int _write(int /*file*/, char *ptr, int len) {
91 teensy::InterruptBufferedUart *const tty =
92 global_stdout.load(::std::memory_order_acquire);
93 if (tty != nullptr) {
Brian Silverman12fec3f2018-09-09 16:09:50 -070094 tty->Write(gsl::make_span(ptr, len));
James Kuszmaul998d3032018-09-08 15:41:41 -070095 return len;
96 }
97 return 0;
98}
99
100void __stack_chk_fail(void);
101
102extern char *__brkval;
103extern uint32_t __bss_ram_start__[];
104extern uint32_t __heap_start__[];
105extern uint32_t __stack_end__[];
106
107struct DebugBuffer {
108 struct Sample {
109 ::std::array<int16_t, 3> currents;
110 ::std::array<int16_t, 3> commanded_currents;
111 ::std::array<uint16_t, 3> commands;
112 uint16_t position;
113 // Driver requested current.
114 float driver_request;
115 // Requested current.
116 int16_t total_command;
117
118 float est_omega;
119 float fuse_voltage;
120 int16_t fuse_current;
121
122 float fuse_badness;
123 uint32_t cycles_since_start;
124 };
125
126 // The amount of data in the buffer. This will never decrement. This will be
127 // transferred out the serial port after it fills up.
128 ::std::atomic<size_t> size{0};
129 ::std::atomic<uint32_t> count{0};
130 // The data.
131 ::std::array<Sample, 512> samples;
132};
133
134DebugBuffer global_debug_buffer;
135
136void ftm0_isr(void) {
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700137 static uint32_t i = 0;
138 teensy::AdcDmaSampler *const adc_dma =
139 global_adc_dma.load(::std::memory_order_relaxed);
140
James Kuszmaul998d3032018-09-08 15:41:41 -0700141 Fet12AdcReadings adc_readings;
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700142 // TODO(Brian): Switch to the DMA interrupt instead of spinning.
143 while (!adc_dma->CheckDone()) {
James Kuszmaul998d3032018-09-08 15:41:41 -0700144 }
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700145
146 adc_readings.motor_currents[0] =
147 (adc_dma->adc_result(0, 0) + adc_dma->adc_result(0, 1)) / 2;
148 adc_readings.motor_currents[1] =
149 (adc_dma->adc_result(0, 2) + adc_dma->adc_result(1, 1)) / 2;
150 adc_readings.motor_currents[2] =
151 (adc_dma->adc_result(1, 0) + adc_dma->adc_result(1, 2)) / 2;
152 adc_readings.throttle = adc_dma->adc_result(0, 3);
James Kuszmaul998d3032018-09-08 15:41:41 -0700153 const ::std::array<float, 3> decoupled =
154 DecoupleCurrents(adc_readings.motor_currents);
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700155 adc_dma->Reset();
156 const uint32_t wrapped_encoder =
157 global_motor.load(::std::memory_order_relaxed)->wrapped_encoder();
158#if 1
James Kuszmaul998d3032018-09-08 15:41:41 -0700159 const BalancedReadings balanced =
160 BalanceSimpleReadings(decoupled);
161
James Kuszmaul998d3032018-09-08 15:41:41 -0700162 static float fuse_badness = 0;
163
164 static uint32_t cycles_since_start = 0u;
165 ++cycles_since_start;
166#if 0
167 static int count = 0;
168 ++count;
169 static float currents[3] = {0.0f, 0.0f, 0.0f};
170 for (int ii = 0; ii < 3; ++ii) {
171 currents[ii] += static_cast<float>(adc_readings.motor_currents[ii]);
172 }
173
174 if (i == 0) {
175 printf(
176 "foo %d.0, %d.0, %d.0, %.3d %.3d %.3d, switching %d %d %d enc %d\n",
177 static_cast<int>(currents[0] / static_cast<float>(count)),
178 static_cast<int>(currents[1] / static_cast<float>(count)),
179 static_cast<int>(currents[2] / static_cast<float>(count)),
180 static_cast<int>(decoupled[0] * 1.0f),
181 static_cast<int>(decoupled[1] * 1.0f),
182 static_cast<int>(decoupled[2] * 1.0f),
183 global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(0),
184 global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(1),
185 global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(2),
186 static_cast<int>(
187 global_motor.load(::std::memory_order_relaxed)->wrapped_encoder()));
188 count = 0;
189 currents[0] = 0.0f;
190 currents[1] = 0.0f;
191 currents[2] = 0.0f;
192 }
193#endif
194#if 1
195 constexpr float kAlpha = 0.995f;
196 constexpr float kFuseAlpha = 0.95f;
197
198 // 3400 - 760
199 static float filtered_throttle = 0.0f;
200 constexpr int kMaxThrottle = 3400;
201 constexpr int kMinThrottle = 760;
202 const float throttle = ::std::max(
203 0.0f,
204 ::std::min(1.0f,
205 static_cast<float>(static_cast<int>(adc_readings.throttle) -
206 kMinThrottle) /
207 static_cast<float>(kMaxThrottle - kMinThrottle)));
208
209 // y(n) = x(n) + a * (y(n-1) - x(n))
210 filtered_throttle = throttle + kAlpha * (filtered_throttle - throttle);
211
212 const float fuse_voltage = static_cast<float>(adc_readings.fuse_voltage);
213 static float filtered_fuse_voltage = 0.0f;
214
215 filtered_fuse_voltage =
216 fuse_voltage + kFuseAlpha * (filtered_fuse_voltage - fuse_voltage);
217
218 const float velocity =
219 global_motor.load(::std::memory_order_relaxed)->estimated_velocity();
220 const float bemf = velocity / (static_cast<float>(Kv) / 1.5f);
221 const float abs_bemf = ::std::abs(bemf);
222 constexpr float kPeakCurrent = 300.0f;
223 constexpr float kLimitedCurrent = 75.0f;
224 const float max_bat_cur =
225 fuse_badness > (kLimitedCurrent * kLimitedCurrent * 0.95f)
226 ? kLimitedCurrent
227 : static_cast<float>(kIcc);
228 const float throttle_limit = ::std::min(
229 kPeakCurrent,
230 (-abs_bemf + ::std::sqrt(static_cast<float>(
231 bemf * bemf +
232 4.0f * static_cast<float>(kR) * 1.5f *
233 static_cast<float>(kVcc) * max_bat_cur))) /
234 (2.0f * 1.5f * static_cast<float>(kR)));
235
236 constexpr float kNegativeCurrent = 80.0f;
237 float goal_current = -::std::min(
238 filtered_throttle * (kPeakCurrent + kNegativeCurrent) - kNegativeCurrent,
239 throttle_limit);
240
241 if (velocity > -500) {
242 if (goal_current > 0.0f) {
243 goal_current = 0.0f;
244 }
245 }
246 //float goal_current =
247 //-::std::min(filtered_throttle * kPeakCurrent, throttle_limit);
248 const float fuse_current =
249 goal_current *
250 (bemf + goal_current * static_cast<float>(kR) * 1.5f) /
251 static_cast<float>(kVcc);
252 const int16_t fuse_current_10 = static_cast<int16_t>(10.0f * fuse_current);
253 fuse_badness += 0.00002f * (fuse_current * fuse_current - fuse_badness);
254
255 global_motor.load(::std::memory_order_relaxed)
256 ->SetGoalCurrent(goal_current);
257 global_motor.load(::std::memory_order_relaxed)
258 ->HandleInterrupt(balanced, wrapped_encoder);
James Kuszmaul998d3032018-09-08 15:41:41 -0700259
260 global_debug_buffer.count.fetch_add(1);
261
262 const bool trigger = false;
263 // global_debug_buffer.count.load(::std::memory_order_relaxed) >= 0;
264 size_t buffer_size =
265 global_debug_buffer.size.load(::std::memory_order_relaxed);
266 if ((buffer_size > 0 || trigger) &&
267 buffer_size != global_debug_buffer.samples.size()) {
268 global_debug_buffer.samples[buffer_size].currents[0] =
269 static_cast<int16_t>(balanced.readings[0] * 10.0f);
270 global_debug_buffer.samples[buffer_size].currents[1] =
271 static_cast<int16_t>(balanced.readings[1] * 10.0f);
272 global_debug_buffer.samples[buffer_size].currents[2] =
273 static_cast<int16_t>(balanced.readings[2] * 10.0f);
274 global_debug_buffer.samples[buffer_size].position =
275 global_motor.load(::std::memory_order_relaxed)->wrapped_encoder();
276 global_debug_buffer.samples[buffer_size].est_omega =
277 global_motor.load(::std::memory_order_relaxed)->estimated_velocity();
278 global_debug_buffer.samples[buffer_size].commands[0] =
279 global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(0);
280 global_debug_buffer.samples[buffer_size].commands[1] =
281 global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(1);
282 global_debug_buffer.samples[buffer_size].commands[2] =
283 global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(2);
284 global_debug_buffer.samples[buffer_size].commanded_currents[0] =
285 global_motor.load(::std::memory_order_relaxed)->i_goal(0);
286 global_debug_buffer.samples[buffer_size].commanded_currents[1] =
287 global_motor.load(::std::memory_order_relaxed)->i_goal(1);
288 global_debug_buffer.samples[buffer_size].commanded_currents[2] =
289 global_motor.load(::std::memory_order_relaxed)->i_goal(2);
290 global_debug_buffer.samples[buffer_size].total_command =
291 global_motor.load(::std::memory_order_relaxed)->goal_current();
292 global_debug_buffer.samples[buffer_size].fuse_voltage =
293 filtered_fuse_voltage;
294 global_debug_buffer.samples[buffer_size].fuse_current = fuse_current_10;
295 global_debug_buffer.samples[buffer_size].driver_request =
296 ::std::max(filtered_throttle * (kPeakCurrent + kNegativeCurrent) -
297 kNegativeCurrent,
298 0.0f);
299 global_debug_buffer.samples[buffer_size].fuse_badness = fuse_badness;
300 global_debug_buffer.samples[buffer_size].cycles_since_start = cycles_since_start;
301
302 global_debug_buffer.size.fetch_add(1);
303 }
304
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700305 ++i;
James Kuszmaul998d3032018-09-08 15:41:41 -0700306 if (buffer_size == global_debug_buffer.samples.size()) {
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700307 i = 0;
James Kuszmaul998d3032018-09-08 15:41:41 -0700308 GPIOC_PCOR = (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
309 GPIOD_PCOR = (1 << 4) | (1 << 5);
310
311 PERIPHERAL_BITBAND(GPIOC_PDDR, 1) = 1;
312 PERIPHERAL_BITBAND(GPIOC_PDDR, 2) = 1;
313 PERIPHERAL_BITBAND(GPIOC_PDDR, 3) = 1;
314 PERIPHERAL_BITBAND(GPIOC_PDDR, 4) = 1;
315 PERIPHERAL_BITBAND(GPIOD_PDDR, 4) = 1;
316 PERIPHERAL_BITBAND(GPIOD_PDDR, 5) = 1;
317
318 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(1);
319 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(1);
320 PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(1);
321 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1);
322 PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1);
323 PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1);
324 }
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700325#else
326#endif
327#else
328 FTM0->SC &= ~FTM_SC_TOF;
329 FTM0->C0V = 0;
330 FTM0->C1V = 0;
331 FTM0->C2V = 0;
332 FTM0->C3V = 0;
333 FTM0->C4V = 0;
334 FTM0->C5V = 0;
335 FTM0->PWMLOAD = FTM_PWMLOAD_LDOK;
336#endif
James Kuszmaul998d3032018-09-08 15:41:41 -0700337
James Kuszmaul998d3032018-09-08 15:41:41 -0700338}
339
340} // extern "C"
341
342void ConfigurePwmFtm(BigFTM *pwm_ftm) {
343 // Put them all into combine active-high mode, and all the low ones staying on
344 // all the time by default.
345 pwm_ftm->C0SC = FTM_CSC_ELSA;
346 pwm_ftm->C0V = 0;
347 pwm_ftm->C1SC = FTM_CSC_ELSA;
348 pwm_ftm->C1V = 0;
349 pwm_ftm->C2SC = FTM_CSC_ELSA;
350 pwm_ftm->C2V = 0;
351 pwm_ftm->C3SC = FTM_CSC_ELSA;
352 pwm_ftm->C3V = 0;
353 pwm_ftm->C4SC = FTM_CSC_ELSA;
354 pwm_ftm->C4V = 0;
355 pwm_ftm->C5SC = FTM_CSC_ELSA;
356 pwm_ftm->C5V = 0;
357 pwm_ftm->C6SC = FTM_CSC_ELSA;
358 pwm_ftm->C6V = 0;
359 pwm_ftm->C7SC = FTM_CSC_ELSA;
360 pwm_ftm->C7V = 0;
361
362 pwm_ftm->COMBINE = FTM_COMBINE_SYNCEN3 /* Synchronize updates usefully */ |
363 FTM_COMBINE_DTEN3 /* Enable deadtime */ |
364 FTM_COMBINE_COMP3 /* Make them complementary */ |
365 FTM_COMBINE_COMBINE3 /* Combine the channels */ |
366 FTM_COMBINE_SYNCEN2 /* Synchronize updates usefully */ |
367 FTM_COMBINE_DTEN2 /* Enable deadtime */ |
368 FTM_COMBINE_COMP2 /* Make them complementary */ |
369 FTM_COMBINE_COMBINE2 /* Combine the channels */ |
370 FTM_COMBINE_SYNCEN1 /* Synchronize updates usefully */ |
371 FTM_COMBINE_DTEN1 /* Enable deadtime */ |
372 FTM_COMBINE_COMP1 /* Make them complementary */ |
373 FTM_COMBINE_COMBINE1 /* Combine the channels */ |
374 FTM_COMBINE_SYNCEN0 /* Synchronize updates usefully */ |
375 FTM_COMBINE_DTEN0 /* Enable deadtime */ |
376 FTM_COMBINE_COMP0 /* Make them complementary */ |
377 FTM_COMBINE_COMBINE0 /* Combine the channels */;
378 // Safe state for all channels is low.
379 pwm_ftm->POL = 0;
380
381 // Set the deadtime.
382 pwm_ftm->DEADTIME =
383 FTM_DEADTIME_DTPS(0) /* Prescaler of 1 */ | FTM_DEADTIME_DTVAL(9);
384
385 pwm_ftm->CONF =
386 FTM_CONF_BDMMOD(1) /* Set everything to POLn during debug halt */;
387}
388
389// Zeros the encoder. This involves blocking for an arbitrary length of time
390// with interrupts disabled.
391void ZeroMotor() {
392#if 0
393 while (true) {
394 if (PERIPHERAL_BITBAND(GPIOB_PDIR, 11)) {
395 encoder_ftm_->CNT = 0;
396 break;
397 }
398 }
399#else
400 uint32_t scratch;
401 __disable_irq();
402 // Stuff all of this in an inline assembly statement so we can make sure the
403 // compiler doesn't decide sticking constant loads etc in the middle of
404 // the loop is a good idea, because that increases the latency of recognizing
405 // the index pulse edge which makes velocity affect the zeroing accuracy.
406 __asm__ __volatile__(
407 // A label to restart the loop.
408 "0:\n"
409 // Load the current PDIR value for the pin we care about.
410 "ldr %[scratch], [%[pdir_word]]\n"
411 // Terminate the loop if it's non-0.
412 "cbnz %[scratch], 1f\n"
413 // Go back around again.
414 "b 0b\n"
415 // A label to finish the loop.
416 "1:\n"
417 // Reset the count once we're down here. It doesn't actually matter what
418 // value we store because writing anything resets it to CNTIN (ie 0).
419 "str %[scratch], [%[cnt]]\n"
420 : [scratch] "=&l"(scratch)
421 : [pdir_word] "l"(&PERIPHERAL_BITBAND(GPIOB_PDIR, 11)),
422 [cnt] "l"(&FTM1->CNT));
423 __enable_irq();
424#endif
425}
426
427} // namespace
428
429extern "C" int main(void) {
430 // for background about this startup delay, please see these conversations
431 // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
432 // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
433 delay(400);
434
435 // Set all interrupts to the second-lowest priority to start with.
436 for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD);
437
438 // Now set priorities for all the ones we care about. They only have meaning
439 // relative to each other, which means centralizing them here makes it a lot
440 // more manageable.
441 NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0x3);
442 NVIC_SET_SANE_PRIORITY(IRQ_UART0_STATUS, 0xE);
443
444 // Set the LED's pin to output mode.
445 PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1;
446 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1);
447
448#if 0
449 PERIPHERAL_BITBAND(GPIOA_PDDR, 15) = 1;
450 PORTA_PCR15 = PORT_PCR_DSE | PORT_PCR_MUX(1);
451#endif
452
453 // Set up the CAN pins.
454 PORTA_PCR12 = PORT_PCR_DSE | PORT_PCR_MUX(2);
455 PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(2);
456
457 DMA.CR = M_DMA_EMLM;
458
459 PORTB_PCR16 = PORT_PCR_DSE | PORT_PCR_MUX(3);
460 PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_MUX(3);
461 SIM_SCGC4 |= SIM_SCGC4_UART0;
462 teensy::InterruptBufferedUart debug_uart(&UART0, F_CPU);
463 debug_uart.Initialize(115200);
464 global_stdout.store(&debug_uart, ::std::memory_order_release);
465 NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
466
467 AdcInitFet12();
468 MathInit();
469 delay(100);
470 can_init(0, 1);
471
472 MotorControlsImplementation controls;
473
474 delay(100);
475
476 // Index pin
477 PORTB_PCR11 = PORT_PCR_MUX(1);
478 // FTM1_QD_PH{A,B}
479 PORTB_PCR0 = PORT_PCR_MUX(6);
480 PORTB_PCR1 = PORT_PCR_MUX(6);
481
482 // FTM0_CH[0-5]
483 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(4);
484 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(4);
485 PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(4);
486 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4);
487 PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4);
488 PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(4);
489
490 Motor motor(FTM0, FTM1, &controls, {&FTM0->C0V, &FTM0->C2V, &FTM0->C4V});
491 motor.set_encoder_offset(810);
492 motor.set_deadtime_compensation(9);
493 ConfigurePwmFtm(FTM0);
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700494
495 FTM2->CONF = FTM_CONF_GTBEEN;
496 FTM2->MODE = FTM_MODE_WPDIS;
497 FTM2->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN;
498 FTM2->SC = FTM_SC_CLKS(0) /* Disable counting for now */;
499 FTM2->CNTIN = 0;
500 FTM2->CNT = 0;
501 // TODO(Brian): Don't duplicate this.
502 FTM2->MOD = BUS_CLOCK_FREQUENCY / SWITCHING_FREQUENCY;
503 FTM2->OUTINIT = 0;
504 // All of the channels are active high.
505 FTM2->POL = 0;
506 FTM2->SYNCONF = FTM_SYNCONF_HWWRBUF | FTM_SYNCONF_SWWRBUF |
507 FTM_SYNCONF_SWRSTCNT | FTM_SYNCONF_SYNCMODE;
508 // Don't want any intermediate loading points.
509 FTM2->PWMLOAD = 0;
510
511 // Need to set them to some kind of output mode so we can actually change
512 // them.
513 FTM2->C0SC = FTM_CSC_MSA;
514 FTM2->C1SC = FTM_CSC_MSA;
515
516 // This has to happen after messing with SYNCONF, and should happen after
517 // messing with various other things so the values can get flushed out of the
518 // buffers.
519 FTM2->SYNC =
520 FTM_SYNC_SWSYNC /* Flush everything out right now */ |
521 FTM_SYNC_CNTMAX /* Load new values at the end of the cycle */;
522 // Wait for the software synchronization to finish.
523 while (FTM2->SYNC & FTM_SYNC_SWSYNC) {
524 }
525 FTM2->SC = FTM_SC_CLKS(1) /* Use the system clock */ |
526 FTM_SC_PS(0) /* Don't prescale the clock */;
527 // TODO:
528 //FTM2->MODE &= ~FTM_MODE_WPDIS;
529
530 FTM2->EXTTRIG = FTM_EXTTRIG_CH0TRIG | FTM_EXTTRIG_CH1TRIG;
531
532 teensy::AdcDmaSampler adc_dma;
533 // ADC0_Dx0 is 1-0
534 // ADC0_Dx2 is 1-2
535 // ADC0_Dx3 is 2-0
536 // ADC1_Dx0 is 2-0
537 // ADC1_Dx3 is 1-0
538 // Sample 0: 1-2,2-0
539 // Sample 1: 1-2,1-0
540 // Sample 2: 1-0,2-0
541 // Sample 3: 23(SENSE0),18(VIN)
542 adc_dma.set_adc0_samples({V_ADC_ADCH(2) | M_ADC_DIFF,
543 V_ADC_ADCH(2) | M_ADC_DIFF,
544 V_ADC_ADCH(0) | M_ADC_DIFF, V_ADC_ADCH(23)});
545 adc_dma.set_adc1_samples({V_ADC_ADCH(0) | M_ADC_DIFF,
546 V_ADC_ADCH(3) | M_ADC_DIFF,
547 V_ADC_ADCH(0) | M_ADC_DIFF, V_ADC_ADCH(18)});
548 adc_dma.set_ftm_delays({&FTM2->C0V, &FTM2->C1V});
549 adc_dma.set_pdb_input(PDB_IN_FTM2);
550
551 adc_dma.Initialize();
552 FTM0->CONF = FTM_CONF_GTBEEN;
James Kuszmaul998d3032018-09-08 15:41:41 -0700553 motor.Init();
554 global_motor.store(&motor, ::std::memory_order_relaxed);
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700555 global_adc_dma.store(&adc_dma, ::std::memory_order_relaxed);
556
James Kuszmaul998d3032018-09-08 15:41:41 -0700557 // Output triggers to things like the PDBs on initialization.
558 FTM0_EXTTRIG = FTM_EXTTRIG_INITTRIGEN;
559 // Don't let any memory accesses sneak past here, because we actually
560 // need everything to be starting up.
561 __asm__("" :: : "memory");
562
563 // Give everything a chance to get going.
564 delay(100);
565
566 printf("Ram start: %p\n", __bss_ram_start__);
567 printf("Heap start: %p\n", __heap_start__);
568 printf("Heap end: %p\n", __brkval);
569 printf("Stack start: %p\n", __stack_end__);
570
571 printf("Going silent to zero motors...\n");
572 // Give the print a chance to make it out.
573 delay(100);
574 ZeroMotor();
575
576 motor.set_encoder_multiplier(-1);
577 motor.set_encoder_calibration_offset(
578 558 + 1034 + 39 /*big data bemf comp*/ - 14 /*just backwardsbackwards comp*/);
579
580 printf("Zeroed motor!\n");
581 // Give stuff a chance to recover from interrupts-disabled.
582 delay(100);
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700583 adc_dma.Reset();
James Kuszmaul998d3032018-09-08 15:41:41 -0700584 motor.Start();
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700585 // Now poke the GTB to actually start both timers.
586 FTM0->CONF = FTM_CONF_GTBEEN | FTM_CONF_GTBEOUT;
587
James Kuszmaul998d3032018-09-08 15:41:41 -0700588 NVIC_ENABLE_IRQ(IRQ_FTM0);
589 GPIOC_PSOR = 1 << 5;
590
591 constexpr bool dump_full_sample = false;
592 while (true) {
593 if (dump_full_sample) {
594 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(4);
595 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(4);
596 PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(4);
597 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4);
598 PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4);
599 PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(4);
600 motor.Reset();
601 }
602 global_debug_buffer.size.store(0);
603 global_debug_buffer.count.store(0);
604 while (global_debug_buffer.size.load(::std::memory_order_relaxed) <
605 global_debug_buffer.samples.size()) {
606 }
607 if (dump_full_sample) {
608 printf("Dumping data\n");
609 for (size_t i = 0; i < global_debug_buffer.samples.size(); ++i) {
610 const auto &sample = global_debug_buffer.samples[i];
611
612 printf("%u, %d, %d, %d, %u, %u, %u, %u, %d, %d, %d, %d\n", i,
613 sample.currents[0], sample.currents[1], sample.currents[2],
614 sample.commands[0], sample.commands[1], sample.commands[2],
615 sample.position, static_cast<int>(sample.est_omega),
616 sample.commanded_currents[0], sample.commanded_currents[1],
617 sample.commanded_currents[2]);
618 }
619 printf("Done dumping data\n");
620 } else {
621 //const auto &sample = global_debug_buffer.samples.back();
622 const DebugBuffer::Sample sample = global_debug_buffer.samples[0];
623#if 1
624 printf("%" PRIu32
625 ", %d, %d, %d, %u, %u, %u, %u, %d, %d, %d, %d, %d, %d, %d\n",
626 sample.cycles_since_start, sample.currents[0], sample.currents[1],
627 sample.currents[2], sample.commands[0], sample.commands[1],
628 sample.commands[2], sample.position,
629 static_cast<int>(sample.est_omega), sample.commanded_currents[0],
630 sample.commanded_currents[1], sample.commanded_currents[2],
631 sample.total_command, static_cast<int>(sample.driver_request),
632 static_cast<int>(sample.fuse_badness));
633#else
634 printf("%d, %d\n", static_cast<int>(sample.fuse_voltage),
635 sample.fuse_current);
636#endif
637 }
638 }
639
640 return 0;
641}
642
643} // namespace motors
644} // namespace frc971