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