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