Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 1 | #include <inttypes.h> |
| 2 | #include <stdio.h> |
| 3 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 4 | #include <optional> |
| 5 | |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 6 | #include "aos/time/time.h" |
| 7 | #include "motors/core/kinetis.h" |
| 8 | #include "motors/core/time.h" |
| 9 | #include "motors/peripheral/configuration.h" |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 10 | #include "motors/peripheral/spi.h" |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 11 | #include "motors/peripheral/uart.h" |
| 12 | #include "motors/print/print.h" |
| 13 | #include "motors/util.h" |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 14 | #include "third_party/GSL/include/gsl/gsl" |
| 15 | #include "y2019/jevois/cobs.h" |
| 16 | #include "y2019/jevois/spi.h" |
| 17 | #include "y2019/jevois/uart.h" |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 18 | #include "y2019/vision/constants.h" |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 19 | |
| 20 | using frc971::teensy::InterruptBufferedUart; |
| 21 | using frc971::teensy::InterruptBufferedSpi; |
| 22 | |
| 23 | // All indices here refer to the ports as numbered on the PCB. |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 24 | |
| 25 | namespace frc971 { |
| 26 | namespace jevois { |
| 27 | namespace { |
| 28 | |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 29 | // Holds all of our hardware UARTs. There is exactly one global instance for |
| 30 | // interrupt handlers to access. |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 31 | struct Uarts { |
| 32 | Uarts() { |
| 33 | DisableInterrupts disable_interrupts; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 34 | global_instance = this; |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 35 | } |
| 36 | ~Uarts() { |
| 37 | DisableInterrupts disable_interrupts; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 38 | global_instance = nullptr; |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 39 | } |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 40 | Uarts(const Uarts &) = delete; |
| 41 | Uarts &operator=(const Uarts &) = delete; |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 42 | |
| 43 | void Initialize(int baud_rate) { |
| 44 | cam0.Initialize(baud_rate); |
| 45 | cam1.Initialize(baud_rate); |
| 46 | cam2.Initialize(baud_rate); |
| 47 | cam3.Initialize(baud_rate); |
| 48 | cam4.Initialize(baud_rate); |
| 49 | } |
| 50 | |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 51 | InterruptBufferedUart cam0{&UART1, F_CPU}; |
| 52 | InterruptBufferedUart cam1{&UART0, F_CPU}; |
| 53 | InterruptBufferedUart cam2{&UART2, BUS_CLOCK_FREQUENCY}; |
| 54 | InterruptBufferedUart cam3{&UART3, BUS_CLOCK_FREQUENCY}; |
| 55 | InterruptBufferedUart cam4{&UART4, BUS_CLOCK_FREQUENCY}; |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 56 | |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 57 | static Uarts *global_instance; |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 60 | Uarts *Uarts::global_instance = nullptr; |
| 61 | |
| 62 | // Manages the transmit buffer to a single camera. |
| 63 | // |
| 64 | // We have to add delays between sending each byte in order for the camera to |
| 65 | // successfully receive them. |
| 66 | struct TransmitBuffer { |
| 67 | TransmitBuffer(InterruptBufferedUart *camera_in) : camera(camera_in) {} |
| 68 | InterruptBufferedUart *const camera; |
| 69 | |
| 70 | frc971::teensy::UartBuffer<1024> buffer; |
| 71 | aos::monotonic_clock::time_point last_send = aos::monotonic_clock::min_time; |
| 72 | |
| 73 | // Sends a byte to the camera if it's time. |
| 74 | void Tick(aos::monotonic_clock::time_point now) { |
| 75 | if (buffer.empty()) { |
| 76 | return; |
| 77 | } |
| 78 | if (now < last_send + std::chrono::milliseconds(1)) { |
| 79 | return; |
| 80 | } |
| 81 | last_send = now; |
| 82 | camera->Write(std::array<char, 1>{{buffer.PopSingle()}}); |
| 83 | } |
| 84 | |
| 85 | // Queues up another packet to send, only if the previous one has finished. |
| 86 | void MaybeWritePacket(const CameraCalibration &calibration) { |
| 87 | if (!buffer.empty()) { |
| 88 | return; |
| 89 | } |
| 90 | const auto serialized = UartPackToCamera(calibration); |
| 91 | buffer.PushSingle(0); |
| 92 | if (buffer.PushSpan(serialized) == static_cast<int>(serialized.size())) { |
| 93 | buffer.PushSingle(0); |
| 94 | } |
| 95 | } |
Brian Silverman | bac7754 | 2019-03-03 13:57:00 -0800 | [diff] [blame] | 96 | |
| 97 | void FillAs() { |
| 98 | while (!buffer.full()) { |
| 99 | buffer.PushSingle('a'); |
| 100 | } |
| 101 | } |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | InterruptBufferedSpi *global_spi_instance = nullptr; |
| 105 | |
| 106 | // Manages queueing a transfer to send via SPI. |
| 107 | class SpiQueue { |
| 108 | public: |
| 109 | SpiQueue() { |
| 110 | DisableInterrupts disable_interrupts; |
| 111 | global_instance = this; |
| 112 | } |
| 113 | ~SpiQueue() { |
| 114 | DisableInterrupts disable_interrupts; |
| 115 | global_instance = nullptr; |
| 116 | } |
| 117 | SpiQueue(const SpiQueue &) = delete; |
| 118 | SpiQueue &operator=(const SpiQueue &) = delete; |
| 119 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 120 | std::optional<gsl::span<const char, spi_transfer_size()>> Tick() { |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 121 | { |
| 122 | DisableInterrupts disable_interrupts; |
| 123 | if (waiting_for_enable_ || waiting_for_disable_) { |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 124 | return std::nullopt; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | const auto now = aos::monotonic_clock::now(); |
| 128 | if (TransferTimedOut(now)) { |
| 129 | printf("SPI timeout with %d left\n", static_cast<int>(to_receive_.size())); |
| 130 | WaitForNextTransfer(); |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 131 | return std::nullopt; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 132 | } |
| 133 | { |
| 134 | DisableInterrupts disable_interrupts; |
| 135 | if (!PERIPHERAL_BITBAND(GPIOA_PDIR, 17) && |
| 136 | cs_deassert_time_ == aos::monotonic_clock::max_time) { |
| 137 | cs_deassert_time_ = now; |
| 138 | } |
| 139 | } |
| 140 | if (DeassertHappened(now)) { |
| 141 | printf("CS deasserted with %d left\n", static_cast<int>(to_receive_.size())); |
| 142 | WaitForNextTransfer(); |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 143 | return std::nullopt; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 144 | } |
| 145 | bool all_done; |
| 146 | { |
| 147 | DisableInterrupts disable_interrupts; |
| 148 | if (received_dummy_) { |
| 149 | to_receive_ = to_receive_.subspan( |
| 150 | global_spi_instance->Read(to_receive_, &disable_interrupts).size()); |
| 151 | all_done = to_receive_.empty(); |
| 152 | } else { |
| 153 | std::array<char, 1> dummy_data; |
| 154 | if (global_spi_instance->Read(dummy_data, &disable_interrupts).size() >= |
| 155 | 1) { |
| 156 | received_dummy_ = true; |
| 157 | } |
| 158 | all_done = false; |
| 159 | } |
| 160 | } |
| 161 | if (all_done) { |
| 162 | WaitForNextTransfer(); |
| 163 | return received_transfer_; |
| 164 | } |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 165 | return std::nullopt; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void HandleInterrupt() { |
| 169 | DisableInterrupts disable_interrupts; |
| 170 | if (waiting_for_disable_) { |
| 171 | if (!PERIPHERAL_BITBAND(GPIOA_PDIR, 17)) { |
| 172 | PORTA_PCR17 = |
| 173 | PORT_PCR_MUX(1) | PORT_PCR_IRQC(0xC) /* Interrupt when logic 1 */; |
| 174 | // Clear the interrupt flag now that we've reconfigured it. |
| 175 | PORTA_ISFR = 1 << 17; |
| 176 | waiting_for_disable_ = false; |
| 177 | } else { |
| 178 | // Clear the interrupt flag. It shouldn't trigger again immediately |
| 179 | // because the pin is still asserted. |
| 180 | PORTA_ISFR = 1 << 17; |
| 181 | } |
| 182 | return; |
| 183 | } |
| 184 | if (waiting_for_enable_) { |
| 185 | if (PERIPHERAL_BITBAND(GPIOA_PDIR, 17)) { |
| 186 | global_spi_instance->ClearQueues(disable_interrupts); |
| 187 | // Tell the SPI peripheral its CS is asserted. |
| 188 | PERIPHERAL_BITBAND(GPIOB_PDOR, 17) = 0; |
| 189 | // Disable interrupts on the enable pin. We'll re-enable once we finish |
| 190 | // the transfer. |
| 191 | PORTA_PCR17 = PORT_PCR_MUX(1); |
| 192 | // Clear the interrupt flag now that we've reconfigured it. |
| 193 | PORTA_ISFR = 1 << 17; |
| 194 | if (have_transfer_) { |
| 195 | global_spi_instance->Write(transfer_, &disable_interrupts); |
| 196 | have_transfer_ = false; |
| 197 | } else { |
| 198 | printf("Writing dummy SPI frame\n"); |
| 199 | // If we don't have anything, just write 0s to avoid getting the |
| 200 | // hardware confused. |
| 201 | global_spi_instance->Write(SpiTransfer{}, &disable_interrupts); |
| 202 | } |
| 203 | // Queue up a dummy byte at the end. This won't actually be sent, |
| 204 | // because the first byte we do send will be garbage, but it will |
| 205 | // synchronize our queues so we receive all the useful data bytes. |
| 206 | global_spi_instance->Write(std::array<char, 1>(), &disable_interrupts); |
| 207 | waiting_for_enable_ = false; |
| 208 | receive_start_ = aos::monotonic_clock::now(); |
| 209 | cs_deassert_time_ = aos::monotonic_clock::max_time; |
| 210 | // To make debugging easier. |
| 211 | received_transfer_.fill(0); |
| 212 | } else { |
| 213 | // Clear the interrupt flag. It shouldn't trigger again immediately |
| 214 | // because the pin is still asserted. |
| 215 | PORTA_ISFR = 1 << 17; |
| 216 | } |
| 217 | return; |
| 218 | } |
| 219 | // We shouldn't ever get here. Clear all the flags and hope they don't get |
| 220 | // re-asserted immediately. |
| 221 | PORTA_ISFR = UINT32_C(0xFFFFFFFF); |
| 222 | } |
| 223 | |
| 224 | void UpdateTransfer(const SpiTransfer &transfer, const DisableInterrupts &) { |
| 225 | have_transfer_ = true; |
| 226 | transfer_ = transfer; |
| 227 | } |
| 228 | |
| 229 | // Returns whether a transfer is currently queued. This will be true between a |
| 230 | // call to UpdateTransfer and that transfer actually being moved out to the |
| 231 | // hardware. |
| 232 | bool HaveTransfer(const DisableInterrupts &) const { return have_transfer_; } |
| 233 | |
| 234 | static SpiQueue *global_instance; |
| 235 | |
| 236 | private: |
| 237 | void WaitForNextTransfer() { |
| 238 | to_receive_ = received_transfer_; |
| 239 | received_dummy_ = false; |
| 240 | { |
| 241 | DisableInterrupts disable_interrupts; |
| 242 | waiting_for_enable_ = true; |
| 243 | waiting_for_disable_ = true; |
| 244 | PORTA_PCR17 = |
| 245 | PORT_PCR_MUX(1) | PORT_PCR_IRQC(0x8) /* Interrupt when logic 0 */; |
| 246 | // Clear the interrupt flag now that we've reconfigured it. |
| 247 | PORTA_ISFR = 1 << 17; |
| 248 | } |
| 249 | // Tell the SPI peripheral its CS is de-asserted. |
| 250 | PERIPHERAL_BITBAND(GPIOB_PDOR, 17) = 1; |
| 251 | } |
| 252 | |
| 253 | bool TransferTimedOut(aos::monotonic_clock::time_point now) { |
| 254 | DisableInterrupts disable_interrupts; |
| 255 | // TODO: Revise this timeout. |
| 256 | return now - std::chrono::milliseconds(50) > receive_start_; |
| 257 | } |
| 258 | |
| 259 | bool DeassertHappened(aos::monotonic_clock::time_point now) { |
| 260 | DisableInterrupts disable_interrupts; |
| 261 | return now - std::chrono::microseconds(50) > cs_deassert_time_; |
| 262 | } |
| 263 | |
| 264 | bool waiting_for_enable_ = true; |
| 265 | bool waiting_for_disable_ = false; |
| 266 | bool have_transfer_ = false; |
| 267 | SpiTransfer transfer_; |
| 268 | bool received_dummy_ = false; |
| 269 | SpiTransfer received_transfer_; |
| 270 | gsl::span<char> to_receive_ = received_transfer_; |
| 271 | aos::monotonic_clock::time_point receive_start_; |
| 272 | aos::monotonic_clock::time_point cs_deassert_time_; |
| 273 | }; |
| 274 | |
| 275 | SpiQueue *SpiQueue::global_instance = nullptr; |
| 276 | |
| 277 | // All methods here must be fully synchronized by the caller. |
| 278 | class FrameQueue { |
| 279 | public: |
| 280 | FrameQueue() = default; |
| 281 | FrameQueue(const FrameQueue &) = delete; |
| 282 | FrameQueue &operator=(const FrameQueue &) = delete; |
| 283 | |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 284 | void UpdateFrame(int camera, const CameraFrame &frame) { |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 285 | frames_[camera].targets = frame.targets; |
| 286 | frames_[camera].capture_time = aos::monotonic_clock::now() - frame.age; |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 287 | frames_[camera].camera_index = camera; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 288 | const aos::SizedArray<int, 3> old_last_frames = last_frames_; |
| 289 | last_frames_.clear(); |
| 290 | for (int index : old_last_frames) { |
| 291 | if (index != camera) { |
| 292 | last_frames_.push_back(index); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // Creates and returns a transfer with all the current information. |
| 298 | // |
| 299 | // This does not actually record these frames as transferred until |
| 300 | // RemoveLatestFrames() is called. |
| 301 | SpiTransfer MakeTransfer(); |
| 302 | |
| 303 | // Records the frames represented in the result of the latest MakeTransfer() |
| 304 | // call as being transferred, so they will not be represented in subsequent |
| 305 | // MakeTransfer() calls. |
| 306 | void RemoveLatestFrames() { |
| 307 | for (int index : last_frames_) { |
| 308 | frames_[index].capture_time = aos::monotonic_clock::min_time; |
| 309 | } |
| 310 | last_frames_.clear(); |
| 311 | } |
| 312 | |
Brian Silverman | b5db4ec | 2019-04-03 21:29:30 -0700 | [diff] [blame] | 313 | bool HaveLatestFrames() const { return !last_frames_.empty(); } |
| 314 | |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 315 | private: |
| 316 | struct FrameData { |
| 317 | aos::SizedArray<Target, 3> targets; |
| 318 | aos::monotonic_clock::time_point capture_time = |
| 319 | aos::monotonic_clock::min_time; |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 320 | int camera_index; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 321 | }; |
| 322 | |
| 323 | std::array<FrameData, 5> frames_; |
| 324 | // The indices into frames_ which we returned in the last MakeTransfer() call. |
| 325 | aos::SizedArray<int, 3> last_frames_; |
| 326 | }; |
| 327 | |
| 328 | SpiTransfer FrameQueue::MakeTransfer() { |
| 329 | aos::SizedArray<int, 5> oldest_indices; |
| 330 | for (size_t i = 0; i < frames_.size(); ++i) { |
| 331 | if (frames_[i].capture_time != aos::monotonic_clock::min_time) { |
| 332 | oldest_indices.push_back(i); |
| 333 | } |
| 334 | } |
| 335 | std::sort(oldest_indices.begin(), oldest_indices.end(), [this](int a, int b) { |
| 336 | return frames_[a].capture_time < frames_[b].capture_time; |
| 337 | }); |
| 338 | |
| 339 | TeensyToRoborio message; |
| 340 | last_frames_.clear(); |
| 341 | for (int i = 0; i < std::min<int>(oldest_indices.size(), 3); ++i) { |
| 342 | const int index = oldest_indices[i]; |
| 343 | const FrameData &frame = frames_[index]; |
| 344 | const auto age = aos::monotonic_clock::now() - frame.capture_time; |
| 345 | const auto rounded_age = aos::time::round<camera_duration>(age); |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 346 | message.frames.push_back({frame.targets, rounded_age, frame.camera_index}); |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 347 | last_frames_.push_back(index); |
| 348 | } |
| 349 | return SpiPackToRoborio(message); |
| 350 | } |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 351 | |
Brian Silverman | 2294f35 | 2019-03-02 16:31:18 -0800 | [diff] [blame] | 352 | // Manages turning the debug light on and off periodically. |
| 353 | // |
| 354 | // It blinks at 1Hz with a variable duty cycle. |
| 355 | class DebugLight { |
| 356 | public: |
| 357 | static constexpr aos::monotonic_clock::duration period() { |
| 358 | return std::chrono::seconds(1); |
| 359 | } |
| 360 | |
| 361 | void set_next_off_time(aos::monotonic_clock::duration next_off_time) { |
| 362 | next_off_time_ = next_off_time; |
| 363 | } |
| 364 | |
Brian Silverman | c2fb02a | 2019-03-03 18:02:00 -0800 | [diff] [blame] | 365 | bool Tick(aos::monotonic_clock::time_point now) { |
Brian Silverman | 2294f35 | 2019-03-02 16:31:18 -0800 | [diff] [blame] | 366 | if (last_cycle_start_ == aos::monotonic_clock::min_time) { |
| 367 | last_cycle_start_ = now; |
| 368 | current_off_point_ = last_cycle_start_ + next_off_time_; |
| 369 | } else if (now > last_cycle_start_ + period()) { |
| 370 | last_cycle_start_ += period(); |
| 371 | current_off_point_ = last_cycle_start_ + next_off_time_; |
| 372 | } |
Brian Silverman | c2fb02a | 2019-03-03 18:02:00 -0800 | [diff] [blame] | 373 | return now > current_off_point_; |
Brian Silverman | 2294f35 | 2019-03-02 16:31:18 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | private: |
| 377 | aos::monotonic_clock::time_point last_cycle_start_ = |
| 378 | aos::monotonic_clock::min_time; |
| 379 | |
Brian Silverman | bac7754 | 2019-03-03 13:57:00 -0800 | [diff] [blame] | 380 | aos::monotonic_clock::duration next_off_time_ = |
| 381 | std::chrono::milliseconds(100); |
Brian Silverman | 2294f35 | 2019-03-02 16:31:18 -0800 | [diff] [blame] | 382 | aos::monotonic_clock::time_point current_off_point_ = |
| 383 | aos::monotonic_clock::min_time; |
| 384 | }; |
| 385 | |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 386 | // Returns an identifier for the processor we're running on. |
| 387 | uint32_t ProcessorIdentifier() { |
| 388 | uint32_t r = 0; |
| 389 | r |= SIM_UIDH << 24; |
| 390 | r |= SIM_UIDMH << 16; |
| 391 | r |= SIM_UIDML << 8; |
| 392 | r |= SIM_UIDL << 0; |
| 393 | return r; |
| 394 | } |
| 395 | |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 396 | extern "C" { |
| 397 | |
| 398 | void *__stack_chk_guard = (void *)0x67111971; |
| 399 | void __stack_chk_fail(void) { |
| 400 | while (true) { |
| 401 | GPIOC_PSOR = (1 << 5); |
| 402 | printf("Stack corruption detected\n"); |
| 403 | delay(1000); |
| 404 | GPIOC_PCOR = (1 << 5); |
| 405 | delay(1000); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | extern char *__brkval; |
| 410 | extern uint32_t __bss_ram_start__[]; |
| 411 | extern uint32_t __heap_start__[]; |
| 412 | extern uint32_t __stack_end__[]; |
| 413 | |
| 414 | void uart0_status_isr(void) { |
| 415 | DisableInterrupts disable_interrupts; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 416 | Uarts::global_instance->cam1.HandleInterrupt(disable_interrupts); |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void uart1_status_isr(void) { |
| 420 | DisableInterrupts disable_interrupts; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 421 | Uarts::global_instance->cam0.HandleInterrupt(disable_interrupts); |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void uart2_status_isr(void) { |
| 425 | DisableInterrupts disable_interrupts; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 426 | Uarts::global_instance->cam2.HandleInterrupt(disable_interrupts); |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void uart3_status_isr(void) { |
| 430 | DisableInterrupts disable_interrupts; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 431 | Uarts::global_instance->cam3.HandleInterrupt(disable_interrupts); |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void uart4_status_isr(void) { |
| 435 | DisableInterrupts disable_interrupts; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 436 | Uarts::global_instance->cam4.HandleInterrupt(disable_interrupts); |
| 437 | } |
| 438 | |
| 439 | void spi0_isr(void) { |
| 440 | DisableInterrupts disable_interrupts; |
| 441 | global_spi_instance->HandleInterrupt(disable_interrupts); |
| 442 | } |
| 443 | |
| 444 | void porta_isr(void) { |
| 445 | SpiQueue::global_instance->HandleInterrupt(); |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | } // extern "C" |
| 449 | |
| 450 | // A test program which echos characters back after adding a per-UART offset to |
| 451 | // them (CAM0 adds 1, CAM1 adds 2, etc). |
| 452 | __attribute__((unused)) void TestUarts() { |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 453 | Uarts *const uarts = Uarts::global_instance; |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 454 | while (true) { |
| 455 | { |
| 456 | std::array<char, 10> buffer; |
| 457 | const auto data = uarts->cam0.Read(buffer); |
| 458 | for (int i = 0; i < data.size(); ++i) { |
| 459 | data[i] += 1; |
| 460 | } |
| 461 | uarts->cam0.Write(data); |
| 462 | } |
| 463 | { |
| 464 | std::array<char, 10> buffer; |
| 465 | const auto data = uarts->cam1.Read(buffer); |
| 466 | for (int i = 0; i < data.size(); ++i) { |
| 467 | data[i] += 2; |
| 468 | } |
| 469 | uarts->cam1.Write(data); |
| 470 | } |
| 471 | { |
| 472 | std::array<char, 10> buffer; |
| 473 | const auto data = uarts->cam2.Read(buffer); |
| 474 | for (int i = 0; i < data.size(); ++i) { |
| 475 | data[i] += 3; |
| 476 | } |
| 477 | uarts->cam2.Write(data); |
| 478 | } |
| 479 | { |
| 480 | std::array<char, 10> buffer; |
| 481 | const auto data = uarts->cam3.Read(buffer); |
| 482 | for (int i = 0; i < data.size(); ++i) { |
| 483 | data[i] += 4; |
| 484 | } |
| 485 | uarts->cam3.Write(data); |
| 486 | } |
| 487 | { |
| 488 | std::array<char, 10> buffer; |
| 489 | const auto data = uarts->cam4.Read(buffer); |
| 490 | for (int i = 0; i < data.size(); ++i) { |
| 491 | data[i] += 5; |
| 492 | } |
| 493 | uarts->cam4.Write(data); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | // Tests all the I/O pins. Cycles through each one for 1 second. While active, |
| 499 | // each output is turned on, and each input has its value printed. |
| 500 | __attribute__((unused)) void TestIo() { |
| 501 | // Set SPI0 pins to GPIO. |
| 502 | // SPI_OUT |
| 503 | PERIPHERAL_BITBAND(GPIOC_PDDR, 6) = 1; |
| 504 | PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 505 | // SPI_CS |
| 506 | PERIPHERAL_BITBAND(GPIOD_PDDR, 0) = 0; |
| 507 | PORTD_PCR0 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 508 | // SPI_IN |
| 509 | PERIPHERAL_BITBAND(GPIOC_PDDR, 7) = 0; |
| 510 | PORTC_PCR7 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 511 | // SPI_SCK |
| 512 | PERIPHERAL_BITBAND(GPIOD_PDDR, 1) = 0; |
| 513 | PORTD_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 514 | |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 515 | auto next = aos::monotonic_clock::now(); |
| 516 | static constexpr auto kTick = std::chrono::seconds(1); |
| 517 | while (true) { |
| 518 | printf("SPI_MISO\n"); |
| 519 | PERIPHERAL_BITBAND(GPIOC_PDOR, 6) = 1; |
| 520 | while (aos::monotonic_clock::now() < next + kTick) { |
| 521 | } |
| 522 | PERIPHERAL_BITBAND(GPIOC_PDOR, 6) = 0; |
| 523 | next += kTick; |
| 524 | |
| 525 | while (aos::monotonic_clock::now() < next + kTick) { |
| 526 | printf("SPI_CS %d\n", (int)PERIPHERAL_BITBAND(GPIOD_PDIR, 0)); |
| 527 | } |
| 528 | next += kTick; |
| 529 | |
| 530 | while (aos::monotonic_clock::now() < next + kTick) { |
| 531 | printf("SPI_MOSI %d\n", (int)PERIPHERAL_BITBAND(GPIOC_PDIR, 7)); |
| 532 | } |
| 533 | next += kTick; |
| 534 | |
| 535 | while (aos::monotonic_clock::now() < next + kTick) { |
| 536 | printf("SPI_CLK %d\n", (int)PERIPHERAL_BITBAND(GPIOD_PDIR, 1)); |
| 537 | } |
| 538 | next += kTick; |
| 539 | |
| 540 | printf("CAM0\n"); |
| 541 | PERIPHERAL_BITBAND(GPIOC_PDOR, 11) = 1; |
| 542 | while (aos::monotonic_clock::now() < next + kTick) { |
| 543 | } |
| 544 | PERIPHERAL_BITBAND(GPIOC_PDOR, 11) = 0; |
| 545 | next += kTick; |
| 546 | |
| 547 | printf("CAM1\n"); |
| 548 | PERIPHERAL_BITBAND(GPIOC_PDOR, 10) = 1; |
| 549 | while (aos::monotonic_clock::now() < next + kTick) { |
| 550 | } |
| 551 | PERIPHERAL_BITBAND(GPIOC_PDOR, 10) = 0; |
| 552 | next += kTick; |
| 553 | |
| 554 | printf("CAM2\n"); |
| 555 | PERIPHERAL_BITBAND(GPIOC_PDOR, 8) = 1; |
| 556 | while (aos::monotonic_clock::now() < next + kTick) { |
| 557 | } |
| 558 | PERIPHERAL_BITBAND(GPIOC_PDOR, 8) = 0; |
| 559 | next += kTick; |
| 560 | |
| 561 | printf("CAM3\n"); |
| 562 | PERIPHERAL_BITBAND(GPIOC_PDOR, 9) = 1; |
| 563 | while (aos::monotonic_clock::now() < next + kTick) { |
| 564 | } |
| 565 | PERIPHERAL_BITBAND(GPIOC_PDOR, 9) = 0; |
| 566 | next += kTick; |
| 567 | |
| 568 | printf("CAM4\n"); |
| 569 | PERIPHERAL_BITBAND(GPIOB_PDOR, 18) = 1; |
| 570 | while (aos::monotonic_clock::now() < next + kTick) { |
| 571 | } |
| 572 | PERIPHERAL_BITBAND(GPIOB_PDOR, 18) = 0; |
| 573 | next += kTick; |
| 574 | |
| 575 | printf("CAM5\n"); |
| 576 | PERIPHERAL_BITBAND(GPIOC_PDOR, 2) = 1; |
| 577 | while (aos::monotonic_clock::now() < next + kTick) { |
| 578 | } |
| 579 | PERIPHERAL_BITBAND(GPIOC_PDOR, 2) = 0; |
| 580 | next += kTick; |
| 581 | |
| 582 | printf("CAM6\n"); |
| 583 | PERIPHERAL_BITBAND(GPIOD_PDOR, 7) = 1; |
| 584 | while (aos::monotonic_clock::now() < next + kTick) { |
| 585 | } |
| 586 | PERIPHERAL_BITBAND(GPIOD_PDOR, 7) = 0; |
| 587 | next += kTick; |
| 588 | |
| 589 | printf("CAM7\n"); |
| 590 | PERIPHERAL_BITBAND(GPIOC_PDOR, 1) = 1; |
| 591 | while (aos::monotonic_clock::now() < next + kTick) { |
| 592 | } |
| 593 | PERIPHERAL_BITBAND(GPIOC_PDOR, 1) = 0; |
| 594 | next += kTick; |
| 595 | |
| 596 | printf("CAM8\n"); |
| 597 | PERIPHERAL_BITBAND(GPIOB_PDOR, 19) = 1; |
| 598 | while (aos::monotonic_clock::now() < next + kTick) { |
| 599 | } |
| 600 | PERIPHERAL_BITBAND(GPIOB_PDOR, 19) = 0; |
| 601 | next += kTick; |
| 602 | |
| 603 | printf("CAM9\n"); |
| 604 | PERIPHERAL_BITBAND(GPIOD_PDOR, 5) = 1; |
| 605 | while (aos::monotonic_clock::now() < next + kTick) { |
| 606 | } |
| 607 | PERIPHERAL_BITBAND(GPIOD_PDOR, 5) = 0; |
| 608 | next += kTick; |
| 609 | } |
| 610 | } |
| 611 | |
Brian Silverman | c2fb02a | 2019-03-03 18:02:00 -0800 | [diff] [blame] | 612 | struct LightRingState { |
| 613 | DebugLight debug_light; |
| 614 | aos::monotonic_clock::time_point last_frame = aos::monotonic_clock::max_time; |
| 615 | |
| 616 | bool Tick(aos::monotonic_clock::time_point now) { |
| 617 | if (last_frame == aos::monotonic_clock::max_time) { |
| 618 | last_frame = now; |
| 619 | } |
| 620 | if (now > last_frame + std::chrono::seconds(1)) { |
| 621 | debug_light.set_next_off_time(std::chrono::milliseconds(500)); |
| 622 | } else { |
| 623 | debug_light.set_next_off_time(std::chrono::seconds(0)); |
| 624 | } |
| 625 | return debug_light.Tick(now); |
| 626 | } |
| 627 | }; |
| 628 | |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 629 | // Does the normal work of transferring data in all directions. |
| 630 | // |
| 631 | // https://community.nxp.com/thread/466937#comment-983881 is a post from NXP |
| 632 | // claiming that it's impossible to queue up the first byte for the slave end of |
| 633 | // an SPI connection properly. Instead, we just accept there will be a garbage |
| 634 | // byte and the other end ignores it. |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 635 | __attribute__((unused)) void TransferData( |
| 636 | frc971::motors::PrintingImplementation *printing) { |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 637 | Uarts *const uarts = Uarts::global_instance; |
| 638 | std::array<CobsPacketizer<uart_to_teensy_size()>, 5> packetizers; |
| 639 | std::array<TransmitBuffer, 5> transmit_buffers{ |
| 640 | {&uarts->cam0, &uarts->cam1, &uarts->cam2, &uarts->cam3, &uarts->cam4}}; |
Brian Silverman | c2fb02a | 2019-03-03 18:02:00 -0800 | [diff] [blame] | 641 | std::array<LightRingState, 5> light_rings; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 642 | FrameQueue frame_queue; |
| 643 | aos::monotonic_clock::time_point last_camera_send = |
| 644 | aos::monotonic_clock::min_time; |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 645 | CameraCommand stdin_camera_command = CameraCommand::kNormal; |
| 646 | CameraCommand last_roborio_camera_command = CameraCommand::kNormal; |
Brian Silverman | c2fb02a | 2019-03-03 18:02:00 -0800 | [diff] [blame] | 647 | DebugLight teensy_debug_light; |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 648 | |
Austin Schuh | f7d60c4 | 2019-03-03 20:44:02 -0800 | [diff] [blame] | 649 | bool verbose = false; |
| 650 | |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 651 | bool first = true; |
| 652 | while (true) { |
Brian Silverman | c2fb02a | 2019-03-03 18:02:00 -0800 | [diff] [blame] | 653 | { |
| 654 | const auto now = aos::monotonic_clock::now(); |
| 655 | PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = !teensy_debug_light.Tick(now); |
| 656 | PERIPHERAL_BITBAND(GPIOC_PDOR, 11) = light_rings[0].Tick(now); |
| 657 | PERIPHERAL_BITBAND(GPIOC_PDOR, 10) = light_rings[1].Tick(now); |
| 658 | PERIPHERAL_BITBAND(GPIOC_PDOR, 8) = light_rings[2].Tick(now); |
| 659 | PERIPHERAL_BITBAND(GPIOC_PDOR, 9) = light_rings[3].Tick(now); |
| 660 | PERIPHERAL_BITBAND(GPIOB_PDOR, 18) = light_rings[4].Tick(now); |
| 661 | } |
Brian Silverman | 2294f35 | 2019-03-02 16:31:18 -0800 | [diff] [blame] | 662 | |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 663 | { |
| 664 | const auto received_transfer = SpiQueue::global_instance->Tick(); |
| 665 | if (received_transfer) { |
| 666 | const auto unpacked = SpiUnpackToTeensy(*received_transfer); |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 667 | if (unpacked) { |
| 668 | last_roborio_camera_command = unpacked->camera_command; |
| 669 | } else { |
Brian Silverman | c2fb02a | 2019-03-03 18:02:00 -0800 | [diff] [blame] | 670 | printf("SPI decode error\n"); |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | { |
| 676 | std::array<char, 20> buffer; |
| 677 | packetizers[0].ParseData(uarts->cam0.Read(buffer)); |
| 678 | packetizers[1].ParseData(uarts->cam1.Read(buffer)); |
| 679 | packetizers[2].ParseData(uarts->cam2.Read(buffer)); |
| 680 | packetizers[3].ParseData(uarts->cam3.Read(buffer)); |
| 681 | packetizers[4].ParseData(uarts->cam4.Read(buffer)); |
| 682 | } |
| 683 | for (size_t i = 0; i < packetizers.size(); ++i) { |
| 684 | if (!packetizers[i].received_packet().empty()) { |
| 685 | const auto decoded = |
| 686 | UartUnpackToTeensy(packetizers[i].received_packet()); |
| 687 | packetizers[i].clear_received_packet(); |
| 688 | if (decoded) { |
Austin Schuh | f7d60c4 | 2019-03-03 20:44:02 -0800 | [diff] [blame] | 689 | if (verbose) { |
Brian Silverman | 177f066 | 2019-03-09 15:45:02 -0800 | [diff] [blame] | 690 | printf("uart frame cam %d, %d targets\n", static_cast<int>(i), |
| 691 | static_cast<int>(decoded->targets.size())); |
Austin Schuh | f7d60c4 | 2019-03-03 20:44:02 -0800 | [diff] [blame] | 692 | } |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 693 | frame_queue.UpdateFrame(i, *decoded); |
Brian Silverman | c2fb02a | 2019-03-03 18:02:00 -0800 | [diff] [blame] | 694 | light_rings[i].last_frame = aos::monotonic_clock::now(); |
| 695 | } else { |
| 696 | printf("UART decode error\n"); |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | } |
| 700 | { |
| 701 | bool made_transfer = false; |
Brian Silverman | b5db4ec | 2019-04-03 21:29:30 -0700 | [diff] [blame] | 702 | const bool have_old_frames = frame_queue.HaveLatestFrames(); |
| 703 | { |
| 704 | const auto new_transfer = frame_queue.MakeTransfer(); |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 705 | DisableInterrupts disable_interrupts; |
Brian Silverman | b5db4ec | 2019-04-03 21:29:30 -0700 | [diff] [blame] | 706 | if (!first) { |
| 707 | made_transfer = |
| 708 | !SpiQueue::global_instance->HaveTransfer(disable_interrupts); |
| 709 | } |
| 710 | // If we made a transfer just now, then new_transfer might contain |
| 711 | // duplicate targets, in which case don't use it. |
| 712 | if (!have_old_frames || !made_transfer) { |
| 713 | SpiQueue::global_instance->UpdateTransfer(new_transfer, |
| 714 | disable_interrupts); |
| 715 | } |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 716 | } |
Brian Silverman | b5db4ec | 2019-04-03 21:29:30 -0700 | [diff] [blame] | 717 | // If we made a transfer, then make sure we aren't remembering any |
| 718 | // in-flight frames. |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 719 | if (made_transfer) { |
| 720 | frame_queue.RemoveLatestFrames(); |
| 721 | } |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 722 | } |
| 723 | { |
| 724 | const auto now = aos::monotonic_clock::now(); |
Brian Silverman | bac7754 | 2019-03-03 13:57:00 -0800 | [diff] [blame] | 725 | CameraCommand current_camera_command = CameraCommand::kNormal; |
| 726 | if (last_roborio_camera_command != CameraCommand::kNormal) { |
| 727 | current_camera_command = last_roborio_camera_command; |
| 728 | } else { |
| 729 | current_camera_command = stdin_camera_command; |
| 730 | } |
| 731 | if (current_camera_command == CameraCommand::kUsb) { |
Brian Silverman | c2fb02a | 2019-03-03 18:02:00 -0800 | [diff] [blame] | 732 | teensy_debug_light.set_next_off_time(std::chrono::milliseconds(900)); |
Brian Silverman | bac7754 | 2019-03-03 13:57:00 -0800 | [diff] [blame] | 733 | } else if (current_camera_command == CameraCommand::kCameraPassthrough) { |
Brian Silverman | c2fb02a | 2019-03-03 18:02:00 -0800 | [diff] [blame] | 734 | teensy_debug_light.set_next_off_time(std::chrono::milliseconds(500)); |
Brian Silverman | bac7754 | 2019-03-03 13:57:00 -0800 | [diff] [blame] | 735 | } else { |
Brian Silverman | c2fb02a | 2019-03-03 18:02:00 -0800 | [diff] [blame] | 736 | teensy_debug_light.set_next_off_time(std::chrono::milliseconds(100)); |
Brian Silverman | bac7754 | 2019-03-03 13:57:00 -0800 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | if (current_camera_command == CameraCommand::kAs) { |
| 740 | for (size_t i = 0; i < transmit_buffers.size(); ++i) { |
| 741 | transmit_buffers[i].FillAs(); |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 742 | } |
Brian Silverman | bac7754 | 2019-03-03 13:57:00 -0800 | [diff] [blame] | 743 | } else { |
| 744 | if (last_camera_send + std::chrono::milliseconds(1000) < now) { |
| 745 | last_camera_send = now; |
| 746 | CameraCalibration calibration{}; |
| 747 | calibration.teensy_now = aos::monotonic_clock::now(); |
| 748 | calibration.realtime_now = aos::realtime_clock::min_time; |
| 749 | calibration.camera_command = current_camera_command; |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 750 | |
| 751 | for (int i = 0; i < 5; ++i) { |
| 752 | const y2019::vision::CameraCalibration *const constants = |
James Kuszmaul | e2c71ea | 2019-03-04 08:14:21 -0800 | [diff] [blame] | 753 | y2019::vision::GetCamera(y2019::vision::CameraSerialNumbers( |
| 754 | ProcessorIdentifier())[i]); |
Alex Perry | f3e46be | 2019-03-03 17:26:14 -0800 | [diff] [blame] | 755 | calibration.calibration(0, 0) = constants->intrinsics.mount_angle; |
| 756 | calibration.calibration(0, 1) = constants->intrinsics.focal_length; |
| 757 | calibration.calibration(0, 2) = constants->intrinsics.barrel_mount; |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 758 | transmit_buffers[i].MaybeWritePacket(calibration); |
| 759 | } |
Brian Silverman | 2294f35 | 2019-03-02 16:31:18 -0800 | [diff] [blame] | 760 | } |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 761 | } |
| 762 | for (TransmitBuffer &transmit_buffer : transmit_buffers) { |
| 763 | transmit_buffer.Tick(now); |
| 764 | } |
| 765 | } |
| 766 | |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 767 | { |
| 768 | const auto stdin_data = printing->ReadStdin(); |
| 769 | if (!stdin_data.empty()) { |
| 770 | switch (stdin_data.back()) { |
| 771 | case 'p': |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 772 | printf("Sending passthrough mode\n"); |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 773 | stdin_camera_command = CameraCommand::kCameraPassthrough; |
| 774 | break; |
| 775 | case 'u': |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 776 | printf("Sending USB mode\n"); |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 777 | stdin_camera_command = CameraCommand::kUsb; |
| 778 | break; |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 779 | case 'l': |
| 780 | printf("Log mode\n"); |
| 781 | stdin_camera_command = CameraCommand::kLog; |
| 782 | break; |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 783 | case 'n': |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 784 | printf("Sending normal mode\n"); |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 785 | stdin_camera_command = CameraCommand::kNormal; |
| 786 | break; |
Brian Silverman | bac7754 | 2019-03-03 13:57:00 -0800 | [diff] [blame] | 787 | case 'a': |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 788 | printf("Sending all 'a's\n"); |
Brian Silverman | bac7754 | 2019-03-03 13:57:00 -0800 | [diff] [blame] | 789 | stdin_camera_command = CameraCommand::kAs; |
| 790 | break; |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 791 | case 'c': |
| 792 | printf("This UART board is 0x%" PRIx32 "\n", ProcessorIdentifier()); |
| 793 | for (int i = 0; i < 5; ++i) { |
James Kuszmaul | e2c71ea | 2019-03-04 08:14:21 -0800 | [diff] [blame] | 794 | printf( |
| 795 | "Camera slot %d's serial number is %d\n", i, |
| 796 | y2019::vision::CameraSerialNumbers(ProcessorIdentifier())[i]); |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 797 | } |
| 798 | break; |
Austin Schuh | f7d60c4 | 2019-03-03 20:44:02 -0800 | [diff] [blame] | 799 | case 'v': |
| 800 | printf("Toggling verbose mode\n"); |
| 801 | verbose = !verbose; |
| 802 | break; |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 803 | case 'h': |
| 804 | printf("UART board commands:\n"); |
| 805 | printf(" p: Send passthrough mode\n"); |
| 806 | printf(" u: Send USB mode\n"); |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 807 | printf(" l: Send Log mode\n"); |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 808 | printf(" n: Send normal mode\n"); |
| 809 | printf(" a: Send all-'a' mode\n"); |
| 810 | printf(" c: Dump camera configuration\n"); |
Austin Schuh | f7d60c4 | 2019-03-03 20:44:02 -0800 | [diff] [blame] | 811 | printf(" v: Toggle verbose print\n"); |
Brian Silverman | a498bbb | 2019-03-03 17:18:04 -0800 | [diff] [blame] | 812 | break; |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 813 | default: |
| 814 | printf("Unrecognized character\n"); |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 820 | first = false; |
| 821 | } |
| 822 | } |
| 823 | |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 824 | int Main() { |
| 825 | // for background about this startup delay, please see these conversations |
| 826 | // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980 |
| 827 | // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273 |
| 828 | delay(400); |
| 829 | |
| 830 | // Set all interrupts to the second-lowest priority to start with. |
| 831 | for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD); |
| 832 | |
| 833 | // Now set priorities for all the ones we care about. They only have meaning |
| 834 | // relative to each other, which means centralizing them here makes it a lot |
| 835 | // more manageable. |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 836 | NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7); |
| 837 | NVIC_SET_SANE_PRIORITY(IRQ_UART0_STATUS, 0x3); |
| 838 | NVIC_SET_SANE_PRIORITY(IRQ_UART1_STATUS, 0x3); |
| 839 | NVIC_SET_SANE_PRIORITY(IRQ_UART2_STATUS, 0x3); |
| 840 | NVIC_SET_SANE_PRIORITY(IRQ_UART3_STATUS, 0x3); |
| 841 | NVIC_SET_SANE_PRIORITY(IRQ_UART4_STATUS, 0x3); |
| 842 | // This one is relatively sensitive to latencies. The buffer is ~4800 clock |
| 843 | // cycles long. |
| 844 | NVIC_SET_SANE_PRIORITY(IRQ_SPI0, 0x2); |
| 845 | NVIC_SET_SANE_PRIORITY(IRQ_PORTA, 0x3); |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 846 | |
| 847 | // Set the LED's pin to output mode. |
| 848 | PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1; |
| 849 | PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 850 | |
| 851 | frc971::motors::PrintingParameters printing_parameters; |
| 852 | printing_parameters.dedicated_usb = true; |
| 853 | const ::std::unique_ptr<frc971::motors::PrintingImplementation> printing = |
| 854 | CreatePrinting(printing_parameters); |
| 855 | printing->Initialize(); |
| 856 | |
| 857 | DMA.CR = M_DMA_EMLM; |
| 858 | |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 859 | SIM_SCGC1 |= SIM_SCGC1_UART4; |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 860 | SIM_SCGC4 |= |
| 861 | SIM_SCGC4_UART0 | SIM_SCGC4_UART1 | SIM_SCGC4_UART2 | SIM_SCGC4_UART3; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 862 | SIM_SCGC6 |= SIM_SCGC6_SPI0; |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 863 | |
| 864 | // SPI0 goes to the roboRIO. |
| 865 | // SPI0_PCS0 is SPI_CS. |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 866 | PORTD_PCR0 = PORT_PCR_MUX(2); |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 867 | // SPI0_SOUT is SPI_MISO. |
| 868 | PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 869 | // SPI0_SIN is SPI_MOSI. |
| 870 | PORTC_PCR7 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 871 | // SPI0_SCK is SPI_CLK. |
| 872 | PORTD_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 873 | // SPI_CS_DRIVE |
| 874 | PERIPHERAL_BITBAND(GPIOB_PDDR, 17) = 1; |
| 875 | PERIPHERAL_BITBAND(GPIOB_PDOR, 17) = 1; |
| 876 | PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 877 | // SPI_CS_IN |
| 878 | PERIPHERAL_BITBAND(GPIOA_PDDR, 17) = 0; |
| 879 | // Set the filter width. |
| 880 | PORTA_DFWR = 31; |
| 881 | // Enable the filter. |
| 882 | PERIPHERAL_BITBAND(PORTA_DFER, 17) = 1; |
| 883 | PORTA_PCR17 = |
| 884 | PORT_PCR_MUX(1) | PORT_PCR_IRQC(0xC) /* Interrupt when logic 1 */; |
| 885 | // Clear the interrupt flag now that we've reconfigured it. |
| 886 | PORTA_ISFR = 1 << 17; |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 887 | |
Brian Silverman | 177f066 | 2019-03-09 15:45:02 -0800 | [diff] [blame] | 888 | // For now, we have no need to dim the LEDs, so we're just going to set them |
| 889 | // all to GPIO mode for simplicity of programming. |
| 890 | #if 0 |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 891 | // FTM0_CH0 is LED0 (7 in silkscreen, a beacon channel). |
| 892 | PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 893 | // FTM0_CH1 is LED1 (5 in silkscreen, a beacon channel). |
| 894 | PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 895 | // FTM0_CH7 is LED2 (6 in silkscreen, a beacon channel). |
| 896 | PORTD_PCR7 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 897 | // FTM0_CH5 is LED3 (9 in silkscreen, a vision camera). |
| 898 | PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 899 | |
| 900 | // FTM2_CH1 is LED4 (8 in silkscreen, a vision camera). |
| 901 | PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 902 | // FTM2_CH0 is LED5 (for CAM4). |
| 903 | PORTB_PCR18 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 904 | |
| 905 | // FTM3_CH4 is LED6 (for CAM2). |
| 906 | PORTC_PCR8 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 907 | // FTM3_CH5 is LED7 (for CAM3). |
| 908 | PORTC_PCR9 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 909 | // FTM3_CH6 is LED8 (for CAM1). |
| 910 | PORTC_PCR10 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 911 | // FTM3_CH7 is LED9 (for CAM0). |
| 912 | PORTC_PCR11 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
Brian Silverman | 177f066 | 2019-03-09 15:45:02 -0800 | [diff] [blame] | 913 | #else |
| 914 | // Set all the LED pins to GPIO. |
| 915 | PERIPHERAL_BITBAND(GPIOC_PDDR, 11) = 1; |
| 916 | PORTC_PCR11 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 917 | PERIPHERAL_BITBAND(GPIOC_PDDR, 10) = 1; |
| 918 | PORTC_PCR10 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 919 | PERIPHERAL_BITBAND(GPIOC_PDDR, 8) = 1; |
| 920 | PORTC_PCR8 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 921 | PERIPHERAL_BITBAND(GPIOC_PDDR, 9) = 1; |
| 922 | PORTC_PCR9 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 923 | PERIPHERAL_BITBAND(GPIOB_PDDR, 18) = 1; |
| 924 | PORTB_PCR18 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 925 | PERIPHERAL_BITBAND(GPIOC_PDDR, 2) = 1; |
| 926 | PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 927 | PERIPHERAL_BITBAND(GPIOD_PDDR, 7) = 1; |
| 928 | PORTD_PCR7 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 929 | PERIPHERAL_BITBAND(GPIOC_PDDR, 1) = 1; |
| 930 | PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 931 | PERIPHERAL_BITBAND(GPIOB_PDDR, 19) = 1; |
| 932 | PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 933 | PERIPHERAL_BITBAND(GPIOD_PDDR, 5) = 1; |
| 934 | PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 935 | #endif |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 936 | |
| 937 | // This hardware has been deactivated, but keep this comment for now to |
| 938 | // document which pins it is on. |
| 939 | #if 0 |
| 940 | // This is ODROID_EN. |
| 941 | PERIPHERAL_BITBAND(GPIOC_PDDR, 0) = 1; |
| 942 | PERIPHERAL_BITBAND(GPIOC_PDOR, 0) = 0; |
| 943 | PORTC_PCR0 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 944 | // This is CAM_EN. |
| 945 | PERIPHERAL_BITBAND(GPIOB_PDDR, 0) = 1; |
| 946 | PERIPHERAL_BITBAND(GPIOB_PDOR, 0) = 0; |
| 947 | PORTB_PCR0 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 948 | #endif |
| 949 | // This is 5V_PGOOD. |
| 950 | PERIPHERAL_BITBAND(GPIOD_PDDR, 6) = 0; |
| 951 | PORTD_PCR6 = PORT_PCR_MUX(1); |
| 952 | |
| 953 | // These go to CAM1. |
Brian Silverman | 30adf34 | 2019-03-09 18:27:56 -0800 | [diff] [blame] | 954 | // UART0_RX (peripheral) is UART1_RX (schematic) is UART1_TX_RAW (label TX). |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 955 | PORTA_PCR15 = PORT_PCR_DSE | PORT_PCR_MUX(3) | PORT_PCR_PE /* Do a pull */ | |
| 956 | 0 /* !PS to pull down */; |
Brian Silverman | 30adf34 | 2019-03-09 18:27:56 -0800 | [diff] [blame] | 957 | // UART0_TX (peripheral) is UART1_TX (schematic) is UART1_RX_RAW (label RX). |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 958 | PORTA_PCR14 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 959 | |
| 960 | // These go to CAM0. |
Brian Silverman | 30adf34 | 2019-03-09 18:27:56 -0800 | [diff] [blame] | 961 | // UART1_RX (peripheral) is UART0_RX (schematic) is UART0_TX_RAW (label TX). |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 962 | PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(3) | PORT_PCR_PE /* Do a pull */ | |
| 963 | 0 /* !PS to pull down */; |
Brian Silverman | 30adf34 | 2019-03-09 18:27:56 -0800 | [diff] [blame] | 964 | // UART1_TX (peripheral) is UART0_TX (schematic) is UART0_RX_RAW (label RX). |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 965 | PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 966 | |
| 967 | // These go to CAM2. |
Brian Silverman | 30adf34 | 2019-03-09 18:27:56 -0800 | [diff] [blame] | 968 | // UART2_RX is UART2_TX_RAW (label TX). |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 969 | PORTD_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(3) | PORT_PCR_PE /* Do a pull */ | |
| 970 | 0 /* !PS to pull down */; |
Brian Silverman | 30adf34 | 2019-03-09 18:27:56 -0800 | [diff] [blame] | 971 | // UART2_TX is UART2_RX_RAW (label RX). |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 972 | PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 973 | |
| 974 | // These go to CAM3. |
Brian Silverman | 30adf34 | 2019-03-09 18:27:56 -0800 | [diff] [blame] | 975 | // UART3_RX is UART3_TX_RAW (label TX). |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 976 | PORTB_PCR10 = PORT_PCR_DSE | PORT_PCR_MUX(3) | PORT_PCR_PE /* Do a pull */ | |
| 977 | 0 /* !PS to pull down */; |
Brian Silverman | 30adf34 | 2019-03-09 18:27:56 -0800 | [diff] [blame] | 978 | // UART3_TX is UART3_RX_RAW (label RX). |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 979 | PORTB_PCR11 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 980 | |
| 981 | // These go to CAM4. |
Brian Silverman | 30adf34 | 2019-03-09 18:27:56 -0800 | [diff] [blame] | 982 | // UART4_RX is UART4_TX_RAW (label TX). |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 983 | PORTE_PCR25 = PORT_PCR_DSE | PORT_PCR_MUX(3) | PORT_PCR_PE /* Do a pull */ | |
| 984 | 0 /* !PS to pull down */; |
Brian Silverman | 30adf34 | 2019-03-09 18:27:56 -0800 | [diff] [blame] | 985 | // UART4_TX is UART4_RX_RAW (label RX). |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 986 | PORTE_PCR24 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 987 | |
| 988 | Uarts uarts; |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 989 | InterruptBufferedSpi spi0{&SPI0, BUS_CLOCK_FREQUENCY}; |
| 990 | global_spi_instance = &spi0; |
| 991 | SpiQueue spi_queue; |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 992 | |
| 993 | // Give everything a chance to get going. |
| 994 | delay(100); |
| 995 | |
| 996 | printf("Ram start: %p\n", __bss_ram_start__); |
| 997 | printf("Heap start: %p\n", __heap_start__); |
| 998 | printf("Heap end: %p\n", __brkval); |
| 999 | printf("Stack start: %p\n", __stack_end__); |
| 1000 | |
| 1001 | uarts.Initialize(115200); |
| 1002 | NVIC_ENABLE_IRQ(IRQ_UART0_STATUS); |
| 1003 | NVIC_ENABLE_IRQ(IRQ_UART1_STATUS); |
| 1004 | NVIC_ENABLE_IRQ(IRQ_UART2_STATUS); |
| 1005 | NVIC_ENABLE_IRQ(IRQ_UART3_STATUS); |
| 1006 | NVIC_ENABLE_IRQ(IRQ_UART4_STATUS); |
Brian Silverman | d7d0110 | 2019-02-24 16:11:21 -0800 | [diff] [blame] | 1007 | spi0.Initialize(); |
| 1008 | NVIC_ENABLE_IRQ(IRQ_SPI0); |
| 1009 | NVIC_ENABLE_IRQ(IRQ_PORTA); |
| 1010 | |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 1011 | TransferData(printing.get()); |
Brian Silverman | 3240e10 | 2019-02-16 18:24:24 -0800 | [diff] [blame] | 1012 | |
| 1013 | while (true) { |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | extern "C" { |
| 1018 | |
| 1019 | int main(void) { |
| 1020 | return Main(); |
| 1021 | } |
| 1022 | |
| 1023 | } // extern "C" |
| 1024 | |
| 1025 | } // namespace |
| 1026 | } // namespace jevois |
| 1027 | } // namespace frc971 |