Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 1 | // This file has the main for the Teensy on the simple receiver board. |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | #include <stdio.h> |
| 5 | #include <atomic> |
| 6 | #include <cmath> |
| 7 | |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 8 | #include "motors/core/kinetis.h" |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 9 | #include "motors/core/time.h" |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 10 | #include "motors/peripheral/adc.h" |
| 11 | #include "motors/peripheral/can.h" |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 12 | #include "motors/peripheral/configuration.h" |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 13 | #include "motors/usb/cdc.h" |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 14 | #include "motors/usb/usb.h" |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 15 | #include "motors/util.h" |
| 16 | |
| 17 | namespace frc971 { |
| 18 | namespace motors { |
| 19 | namespace { |
| 20 | |
| 21 | ::std::atomic<teensy::AcmTty *> global_stdout{nullptr}; |
| 22 | |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 23 | // Last width we received on each channel. |
| 24 | uint16_t pwm_input_widths[5]; |
| 25 | // When we received a pulse on each channel in milliseconds. |
| 26 | uint32_t pwm_input_times[5]; |
| 27 | |
| 28 | // Returns the most recently captured value for the specified input channel |
| 29 | // scaled from -1 to 1, or 0 if it was captured over 100ms ago. |
| 30 | float convert_input_width(int channel) { |
| 31 | uint16_t width; |
| 32 | { |
| 33 | DisableInterrupts disable_interrupts; |
| 34 | if (time_after(millis(), time_add(pwm_input_times[channel], 100))) { |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | width = pwm_input_widths[channel]; |
| 39 | } |
| 40 | |
| 41 | // Values measured with a channel mapped to a button. |
| 42 | static constexpr uint16_t kMinWidth = 4133; |
| 43 | static constexpr uint16_t kMaxWidth = 7177; |
| 44 | if (width < kMinWidth) { |
| 45 | width = kMinWidth; |
| 46 | } else if (width > kMaxWidth) { |
| 47 | width = kMaxWidth; |
| 48 | } |
| 49 | return (static_cast<float>(2 * (width - kMinWidth)) / |
| 50 | static_cast<float>(kMaxWidth - kMinWidth)) - |
| 51 | 1.0f; |
| 52 | } |
| 53 | |
| 54 | // Sends a SET_RPM command to the specified VESC. |
| 55 | // Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN |
| 56 | // bandwidth. |
| 57 | void vesc_set_rpm(int vesc_id, float rpm) { |
| 58 | const int32_t rpm_int = rpm; |
| 59 | uint32_t id = CAN_EFF_FLAG; |
| 60 | id |= vesc_id; |
| 61 | id |= (0x03 /* SET_RPM */) << 8; |
| 62 | uint8_t data[4] = { |
| 63 | static_cast<uint8_t>((rpm_int >> 24) & 0xFF), |
| 64 | static_cast<uint8_t>((rpm_int >> 16) & 0xFF), |
| 65 | static_cast<uint8_t>((rpm_int >> 8) & 0xFF), |
| 66 | static_cast<uint8_t>((rpm_int >> 0) & 0xFF), |
| 67 | }; |
| 68 | can_send(id, data, sizeof(data), 2 + vesc_id); |
| 69 | } |
| 70 | |
| 71 | // Sends a SET_CURRENT command to the specified VESC. |
| 72 | // current is in amps. |
| 73 | // Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN |
| 74 | // bandwidth. |
| 75 | void vesc_set_current(int vesc_id, float current) { |
Brian Silverman | 4d1e527 | 2018-03-26 03:18:42 -0400 | [diff] [blame^] | 76 | const int32_t current_int = current * 1000.0f; |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 77 | uint32_t id = CAN_EFF_FLAG; |
| 78 | id |= vesc_id; |
| 79 | id |= (0x01 /* SET_CURRENT */) << 8; |
| 80 | uint8_t data[4] = { |
| 81 | static_cast<uint8_t>((current_int >> 24) & 0xFF), |
| 82 | static_cast<uint8_t>((current_int >> 16) & 0xFF), |
| 83 | static_cast<uint8_t>((current_int >> 8) & 0xFF), |
| 84 | static_cast<uint8_t>((current_int >> 0) & 0xFF), |
| 85 | }; |
| 86 | can_send(id, data, sizeof(data), 2 + vesc_id); |
| 87 | } |
| 88 | |
Brian Silverman | 4d1e527 | 2018-03-26 03:18:42 -0400 | [diff] [blame^] | 89 | // Sends a SET_DUTY command to the specified VESC. |
| 90 | // duty is from -1 to 1. |
| 91 | // Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN |
| 92 | // bandwidth. |
| 93 | void vesc_set_duty(int vesc_id, float duty) { |
| 94 | const int32_t duty_int = duty * 100000.0f; |
| 95 | uint32_t id = CAN_EFF_FLAG; |
| 96 | id |= vesc_id; |
| 97 | id |= (0x00 /* SET_DUTY */) << 8; |
| 98 | uint8_t data[4] = { |
| 99 | static_cast<uint8_t>((duty_int >> 24) & 0xFF), |
| 100 | static_cast<uint8_t>((duty_int >> 16) & 0xFF), |
| 101 | static_cast<uint8_t>((duty_int >> 8) & 0xFF), |
| 102 | static_cast<uint8_t>((duty_int >> 0) & 0xFF), |
| 103 | }; |
| 104 | can_send(id, data, sizeof(data), 2 + vesc_id); |
| 105 | } |
| 106 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 107 | void DoVescTest() { |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 108 | uint32_t time = micros(); |
| 109 | while (true) { |
| 110 | for (int i = 0; i < 6; ++i) { |
| 111 | const uint32_t end = time_add(time, 500000); |
| 112 | while (true) { |
| 113 | const bool done = time_after(micros(), end); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 114 | float current; |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 115 | if (done) { |
| 116 | current = -6; |
| 117 | } else { |
| 118 | current = 6; |
| 119 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 120 | vesc_set_current(i, current); |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 121 | if (done) { |
| 122 | break; |
| 123 | } |
| 124 | delay(5); |
| 125 | } |
| 126 | time = end; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 131 | void DoReceiverTest2() { |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 132 | static constexpr float kMaxRpm = 10000.0f; |
| 133 | while (true) { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 134 | const bool flip = convert_input_width(2) > 0; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 135 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 136 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 137 | const float value = convert_input_width(0); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 138 | |
| 139 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 140 | float rpm = ::std::min(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 141 | if (flip) { |
| 142 | rpm *= -1.0f; |
| 143 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 144 | vesc_set_rpm(0, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 148 | float rpm = ::std::max(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 149 | if (flip) { |
| 150 | rpm *= -1.0f; |
| 151 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 152 | vesc_set_rpm(1, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 156 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 157 | const float value = convert_input_width(1); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 158 | |
| 159 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 160 | float rpm = ::std::min(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 161 | if (flip) { |
| 162 | rpm *= -1.0f; |
| 163 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 164 | vesc_set_rpm(2, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 168 | float rpm = ::std::max(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 169 | if (flip) { |
| 170 | rpm *= -1.0f; |
| 171 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 172 | vesc_set_rpm(3, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 176 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 177 | const float value = convert_input_width(4); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 178 | |
| 179 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 180 | float rpm = ::std::min(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 181 | if (flip) { |
| 182 | rpm *= -1.0f; |
| 183 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 184 | vesc_set_rpm(4, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 188 | float rpm = ::std::max(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 189 | if (flip) { |
| 190 | rpm *= -1.0f; |
| 191 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 192 | vesc_set_rpm(5, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 195 | // Give the CAN frames a chance to go out. |
| 196 | delay(5); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | void SetupPwmFtm(BigFTM *ftm) { |
| 201 | ftm->MODE = FTM_MODE_WPDIS; |
| 202 | ftm->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN; |
| 203 | ftm->SC = FTM_SC_CLKS(0) /* Disable counting for now */; |
| 204 | |
| 205 | // Can't change MOD according to the reference manual ("The Dual Edge Capture |
| 206 | // mode must be used with ... the FTM counter in Free running counter."). |
| 207 | ftm->MOD = 0xFFFF; |
| 208 | |
| 209 | // Capturing rising edge. |
| 210 | ftm->C0SC = FTM_CSC_MSA | FTM_CSC_ELSA; |
| 211 | // Capturing falling edge. |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 212 | ftm->C1SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 213 | |
| 214 | // Capturing rising edge. |
| 215 | ftm->C2SC = FTM_CSC_MSA | FTM_CSC_ELSA; |
| 216 | // Capturing falling edge. |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 217 | ftm->C3SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 218 | |
| 219 | // Capturing rising edge. |
| 220 | ftm->C4SC = FTM_CSC_MSA | FTM_CSC_ELSA; |
| 221 | // Capturing falling edge. |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 222 | ftm->C5SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 223 | |
| 224 | // Capturing rising edge. |
| 225 | ftm->C6SC = FTM_CSC_MSA | FTM_CSC_ELSA; |
| 226 | // Capturing falling edge. |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 227 | ftm->C7SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 228 | |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 229 | (void)ftm->STATUS; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 230 | ftm->STATUS = 0x00; |
| 231 | |
| 232 | ftm->COMBINE = FTM_COMBINE_DECAP3 | FTM_COMBINE_DECAPEN3 | |
| 233 | FTM_COMBINE_DECAP2 | FTM_COMBINE_DECAPEN2 | |
| 234 | FTM_COMBINE_DECAP1 | FTM_COMBINE_DECAPEN1 | |
| 235 | FTM_COMBINE_DECAP0 | FTM_COMBINE_DECAPEN0; |
| 236 | |
| 237 | // 34.95ms max period before it starts wrapping and being weird. |
| 238 | ftm->SC = FTM_SC_CLKS(1) /* Use the system clock */ | |
| 239 | FTM_SC_PS(4) /* Prescaler=32 */; |
| 240 | |
| 241 | ftm->MODE &= ~FTM_MODE_WPDIS; |
| 242 | } |
| 243 | |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 244 | extern "C" void ftm0_isr() { |
| 245 | while (true) { |
| 246 | const uint32_t status = FTM0->STATUS; |
| 247 | if (status == 0) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | if (status & (1 << 1)) { |
| 252 | const uint32_t start = FTM0->C0V; |
| 253 | const uint32_t end = FTM0->C1V; |
| 254 | pwm_input_widths[0] = (end - start) & 0xFFFF; |
| 255 | pwm_input_times[0] = millis(); |
| 256 | } |
| 257 | if (status & (1 << 7)) { |
| 258 | const uint32_t start = FTM0->C6V; |
| 259 | const uint32_t end = FTM0->C7V; |
| 260 | pwm_input_widths[1] = (end - start) & 0xFFFF; |
| 261 | pwm_input_times[1] = millis(); |
| 262 | } |
| 263 | if (status & (1 << 5)) { |
| 264 | const uint32_t start = FTM0->C4V; |
| 265 | const uint32_t end = FTM0->C5V; |
| 266 | pwm_input_widths[2] = (end - start) & 0xFFFF; |
| 267 | pwm_input_times[2] = millis(); |
| 268 | } |
| 269 | if (status & (1 << 3)) { |
| 270 | const uint32_t start = FTM0->C2V; |
| 271 | const uint32_t end = FTM0->C3V; |
| 272 | pwm_input_widths[4] = (end - start) & 0xFFFF; |
| 273 | pwm_input_times[4] = millis(); |
| 274 | } |
| 275 | |
| 276 | FTM0->STATUS = 0; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | extern "C" void ftm3_isr() { |
| 281 | while (true) { |
| 282 | const uint32_t status = FTM3->STATUS; |
| 283 | if (status == 0) { |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | if (status & (1 << 3)) { |
| 288 | const uint32_t start = FTM3->C2V; |
| 289 | const uint32_t end = FTM3->C3V; |
| 290 | pwm_input_widths[3] = (end - start) & 0xFFFF; |
| 291 | pwm_input_times[3] = millis(); |
| 292 | } |
| 293 | |
| 294 | FTM3->STATUS = 0; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | float ConvertEncoderChannel(uint16_t reading) { |
| 299 | // Theoretical values based on the datasheet are 931 and 2917. |
| 300 | // With these values, the magnitude ranges from 0.99-1.03, which works fine |
| 301 | // (the encoder's output appears to get less accurate in one quadrant for some |
| 302 | // reason, hence the variation). |
| 303 | static constexpr uint16_t kMin = 802, kMax = 3088; |
| 304 | if (reading < kMin) { |
| 305 | reading = kMin; |
| 306 | } else if (reading > kMax) { |
| 307 | reading = kMax; |
| 308 | } |
| 309 | return (static_cast<float>(2 * (reading - kMin)) / |
| 310 | static_cast<float>(kMax - kMin)) - |
| 311 | 1.0f; |
| 312 | } |
| 313 | |
| 314 | struct EncoderReading { |
| 315 | EncoderReading(const salsa::SimpleAdcReadings &adc_readings) { |
| 316 | const float sin = ConvertEncoderChannel(adc_readings.sin); |
| 317 | const float cos = ConvertEncoderChannel(adc_readings.cos); |
| 318 | |
| 319 | const float magnitude = ::std::sqrt(sin * sin + cos * cos); |
| 320 | const float magnitude_error = ::std::abs(magnitude - 1.0f); |
| 321 | valid = magnitude_error < 0.15f; |
| 322 | |
| 323 | angle = ::std::atan2(sin, cos); |
| 324 | } |
| 325 | |
| 326 | // Angle in radians, in [-pi, pi]. |
| 327 | float angle; |
| 328 | |
| 329 | bool valid; |
| 330 | }; |
| 331 | |
| 332 | extern "C" void pit3_isr() { |
| 333 | PIT_TFLG3 = 1; |
| 334 | |
| 335 | salsa::SimpleAdcReadings adc_readings; |
| 336 | { |
| 337 | DisableInterrupts disable_interrupts; |
| 338 | adc_readings = salsa::AdcReadSimple(disable_interrupts); |
| 339 | } |
| 340 | |
| 341 | EncoderReading encoder(adc_readings); |
| 342 | |
| 343 | printf("TODO(Austin): 1kHz loop %d %d %d %d %d ADC %" PRIu16 " %" PRIu16 |
| 344 | " enc %d/1000 %s from %d\n", |
| 345 | (int)(convert_input_width(0) * 1000), |
| 346 | (int)(convert_input_width(1) * 1000), |
| 347 | (int)(convert_input_width(2) * 1000), |
| 348 | (int)(convert_input_width(3) * 1000), |
| 349 | (int)(convert_input_width(4) * 1000), adc_readings.sin, |
| 350 | adc_readings.cos, (int)(encoder.angle * 1000), |
| 351 | encoder.valid ? "T" : "f", |
| 352 | (int)(::std::sqrt(ConvertEncoderChannel(adc_readings.sin) * |
| 353 | ConvertEncoderChannel(adc_readings.sin) + |
| 354 | ConvertEncoderChannel(adc_readings.cos) * |
| 355 | ConvertEncoderChannel(adc_readings.cos)) * |
| 356 | 1000)); |
| 357 | } |
| 358 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 359 | } // namespace |
| 360 | |
| 361 | extern "C" { |
| 362 | |
| 363 | void *__stack_chk_guard = (void *)0x67111971; |
| 364 | |
| 365 | int _write(int /*file*/, char *ptr, int len) { |
| 366 | teensy::AcmTty *const tty = global_stdout.load(::std::memory_order_acquire); |
| 367 | if (tty != nullptr) { |
| 368 | return tty->Write(ptr, len); |
| 369 | } |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | void __stack_chk_fail(void); |
| 374 | |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 375 | } // extern "C" |
| 376 | |
| 377 | extern "C" int main(void) { |
| 378 | // for background about this startup delay, please see these conversations |
| 379 | // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980 |
| 380 | // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273 |
| 381 | delay(400); |
| 382 | |
| 383 | // Set all interrupts to the second-lowest priority to start with. |
| 384 | for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD); |
| 385 | |
| 386 | // Now set priorities for all the ones we care about. They only have meaning |
| 387 | // relative to each other, which means centralizing them here makes it a lot |
| 388 | // more manageable. |
| 389 | NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7); |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 390 | NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0xa); |
| 391 | NVIC_SET_SANE_PRIORITY(IRQ_FTM3, 0xa); |
| 392 | NVIC_SET_SANE_PRIORITY(IRQ_PIT_CH3, 0x5); |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 393 | |
| 394 | // Builtin LED. |
| 395 | PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 1; |
| 396 | PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(1); |
| 397 | PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1; |
| 398 | |
| 399 | // Set up the CAN pins. |
| 400 | PORTA_PCR12 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 401 | PORTA_PCR13 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 402 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 403 | // PWM_IN0 |
| 404 | // FTM0_CH0 |
| 405 | PORTC_PCR1 = PORT_PCR_MUX(4); |
| 406 | |
| 407 | // PWM_IN1 |
| 408 | // FTM0_CH6 |
| 409 | PORTD_PCR6 = PORT_PCR_MUX(4); |
| 410 | |
| 411 | // PWM_IN2 |
| 412 | // FTM0_CH4 |
| 413 | PORTD_PCR4 = PORT_PCR_MUX(4); |
| 414 | |
| 415 | // PWM_IN3 |
| 416 | // FTM3_CH2 |
| 417 | PORTD_PCR2 = PORT_PCR_MUX(4); |
| 418 | |
| 419 | // PWM_IN4 |
| 420 | // FTM0_CH2 |
| 421 | PORTC_PCR3 = PORT_PCR_MUX(4); |
| 422 | |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 423 | delay(100); |
| 424 | |
| 425 | teensy::UsbDevice usb_device(0, 0x16c0, 0x0492); |
| 426 | usb_device.SetManufacturer("Seems Reasonable LLC"); |
| 427 | usb_device.SetProduct("Simple Receiver Board"); |
| 428 | |
| 429 | teensy::AcmTty tty0(&usb_device); |
| 430 | global_stdout.store(&tty0, ::std::memory_order_release); |
| 431 | usb_device.Initialize(); |
| 432 | |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 433 | SIM_SCGC6 |= SIM_SCGC6_PIT; |
| 434 | // Workaround for errata e7914. |
| 435 | (void)PIT_MCR; |
| 436 | PIT_MCR = 0; |
| 437 | PIT_LDVAL3 = (BUS_CLOCK_FREQUENCY / 1000) - 1; |
| 438 | PIT_TCTRL3 = PIT_TCTRL_TIE | PIT_TCTRL_TEN; |
| 439 | |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 440 | can_init(0, 1); |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 441 | salsa::AdcInitSimple(); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 442 | SetupPwmFtm(FTM0); |
| 443 | SetupPwmFtm(FTM3); |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 444 | |
| 445 | // Leave the LEDs on for a bit longer. |
| 446 | delay(300); |
| 447 | printf("Done starting up\n"); |
| 448 | |
| 449 | // Done starting up, now turn the LED off. |
| 450 | PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 0; |
| 451 | |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 452 | NVIC_ENABLE_IRQ(IRQ_FTM0); |
| 453 | NVIC_ENABLE_IRQ(IRQ_FTM3); |
| 454 | NVIC_ENABLE_IRQ(IRQ_PIT_CH3); |
| 455 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 456 | DoReceiverTest2(); |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | void __stack_chk_fail(void) { |
| 462 | while (true) { |
| 463 | GPIOC_PSOR = (1 << 5); |
| 464 | printf("Stack corruption detected\n"); |
| 465 | delay(1000); |
| 466 | GPIOC_PCOR = (1 << 5); |
| 467 | delay(1000); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | } // namespace motors |
| 472 | } // namespace frc971 |