blob: 539d7b63ad50f845b25312881845fe0ee80ba329 [file] [log] [blame]
Brian Silverman3240e102019-02-16 18:24:24 -08001#include "aos/time/time.h"
2#include "motors/core/kinetis.h"
3#include "motors/core/time.h"
4#include "motors/peripheral/configuration.h"
Brian Silvermand7d01102019-02-24 16:11:21 -08005#include "motors/peripheral/spi.h"
Brian Silverman3240e102019-02-16 18:24:24 -08006#include "motors/peripheral/uart.h"
7#include "motors/print/print.h"
8#include "motors/util.h"
Brian Silvermand7d01102019-02-24 16:11:21 -08009#include "third_party/GSL/include/gsl/gsl"
10#include "y2019/jevois/cobs.h"
11#include "y2019/jevois/spi.h"
12#include "y2019/jevois/uart.h"
13
14using frc971::teensy::InterruptBufferedUart;
15using frc971::teensy::InterruptBufferedSpi;
16
17// All indices here refer to the ports as numbered on the PCB.
Brian Silverman3240e102019-02-16 18:24:24 -080018
19namespace frc971 {
20namespace jevois {
21namespace {
22
Brian Silvermand7d01102019-02-24 16:11:21 -080023// Holds all of our hardware UARTs. There is exactly one global instance for
24// interrupt handlers to access.
Brian Silverman3240e102019-02-16 18:24:24 -080025struct Uarts {
26 Uarts() {
27 DisableInterrupts disable_interrupts;
Brian Silvermand7d01102019-02-24 16:11:21 -080028 global_instance = this;
Brian Silverman3240e102019-02-16 18:24:24 -080029 }
30 ~Uarts() {
31 DisableInterrupts disable_interrupts;
Brian Silvermand7d01102019-02-24 16:11:21 -080032 global_instance = nullptr;
Brian Silverman3240e102019-02-16 18:24:24 -080033 }
Brian Silvermand7d01102019-02-24 16:11:21 -080034 Uarts(const Uarts &) = delete;
35 Uarts &operator=(const Uarts &) = delete;
Brian Silverman3240e102019-02-16 18:24:24 -080036
37 void Initialize(int baud_rate) {
38 cam0.Initialize(baud_rate);
39 cam1.Initialize(baud_rate);
40 cam2.Initialize(baud_rate);
41 cam3.Initialize(baud_rate);
42 cam4.Initialize(baud_rate);
43 }
44
Brian Silvermand7d01102019-02-24 16:11:21 -080045 InterruptBufferedUart cam0{&UART1, F_CPU};
46 InterruptBufferedUart cam1{&UART0, F_CPU};
47 InterruptBufferedUart cam2{&UART2, BUS_CLOCK_FREQUENCY};
48 InterruptBufferedUart cam3{&UART3, BUS_CLOCK_FREQUENCY};
49 InterruptBufferedUart cam4{&UART4, BUS_CLOCK_FREQUENCY};
Brian Silverman3240e102019-02-16 18:24:24 -080050
Brian Silvermand7d01102019-02-24 16:11:21 -080051 static Uarts *global_instance;
Brian Silverman3240e102019-02-16 18:24:24 -080052};
53
Brian Silvermand7d01102019-02-24 16:11:21 -080054Uarts *Uarts::global_instance = nullptr;
55
56// Manages the transmit buffer to a single camera.
57//
58// We have to add delays between sending each byte in order for the camera to
59// successfully receive them.
60struct TransmitBuffer {
61 TransmitBuffer(InterruptBufferedUart *camera_in) : camera(camera_in) {}
62 InterruptBufferedUart *const camera;
63
64 frc971::teensy::UartBuffer<1024> buffer;
65 aos::monotonic_clock::time_point last_send = aos::monotonic_clock::min_time;
66
67 // Sends a byte to the camera if it's time.
68 void Tick(aos::monotonic_clock::time_point now) {
69 if (buffer.empty()) {
70 return;
71 }
72 if (now < last_send + std::chrono::milliseconds(1)) {
73 return;
74 }
75 last_send = now;
76 camera->Write(std::array<char, 1>{{buffer.PopSingle()}});
77 }
78
79 // Queues up another packet to send, only if the previous one has finished.
80 void MaybeWritePacket(const CameraCalibration &calibration) {
81 if (!buffer.empty()) {
82 return;
83 }
84 const auto serialized = UartPackToCamera(calibration);
85 buffer.PushSingle(0);
86 if (buffer.PushSpan(serialized) == static_cast<int>(serialized.size())) {
87 buffer.PushSingle(0);
88 }
89 }
90};
91
92InterruptBufferedSpi *global_spi_instance = nullptr;
93
94// Manages queueing a transfer to send via SPI.
95class SpiQueue {
96 public:
97 SpiQueue() {
98 DisableInterrupts disable_interrupts;
99 global_instance = this;
100 }
101 ~SpiQueue() {
102 DisableInterrupts disable_interrupts;
103 global_instance = nullptr;
104 }
105 SpiQueue(const SpiQueue &) = delete;
106 SpiQueue &operator=(const SpiQueue &) = delete;
107
108 tl::optional<gsl::span<const char, spi_transfer_size()>> Tick() {
109 {
110 DisableInterrupts disable_interrupts;
111 if (waiting_for_enable_ || waiting_for_disable_) {
112 return tl::nullopt;
113 }
114 }
115 const auto now = aos::monotonic_clock::now();
116 if (TransferTimedOut(now)) {
117 printf("SPI timeout with %d left\n", static_cast<int>(to_receive_.size()));
118 WaitForNextTransfer();
119 return tl::nullopt;
120 }
121 {
122 DisableInterrupts disable_interrupts;
123 if (!PERIPHERAL_BITBAND(GPIOA_PDIR, 17) &&
124 cs_deassert_time_ == aos::monotonic_clock::max_time) {
125 cs_deassert_time_ = now;
126 }
127 }
128 if (DeassertHappened(now)) {
129 printf("CS deasserted with %d left\n", static_cast<int>(to_receive_.size()));
130 WaitForNextTransfer();
131 return tl::nullopt;
132 }
133 bool all_done;
134 {
135 DisableInterrupts disable_interrupts;
136 if (received_dummy_) {
137 to_receive_ = to_receive_.subspan(
138 global_spi_instance->Read(to_receive_, &disable_interrupts).size());
139 all_done = to_receive_.empty();
140 } else {
141 std::array<char, 1> dummy_data;
142 if (global_spi_instance->Read(dummy_data, &disable_interrupts).size() >=
143 1) {
144 received_dummy_ = true;
145 }
146 all_done = false;
147 }
148 }
149 if (all_done) {
150 WaitForNextTransfer();
151 return received_transfer_;
152 }
153 return tl::nullopt;
154 }
155
156 void HandleInterrupt() {
157 DisableInterrupts disable_interrupts;
158 if (waiting_for_disable_) {
159 if (!PERIPHERAL_BITBAND(GPIOA_PDIR, 17)) {
160 PORTA_PCR17 =
161 PORT_PCR_MUX(1) | PORT_PCR_IRQC(0xC) /* Interrupt when logic 1 */;
162 // Clear the interrupt flag now that we've reconfigured it.
163 PORTA_ISFR = 1 << 17;
164 waiting_for_disable_ = false;
165 } else {
166 // Clear the interrupt flag. It shouldn't trigger again immediately
167 // because the pin is still asserted.
168 PORTA_ISFR = 1 << 17;
169 }
170 return;
171 }
172 if (waiting_for_enable_) {
173 if (PERIPHERAL_BITBAND(GPIOA_PDIR, 17)) {
174 global_spi_instance->ClearQueues(disable_interrupts);
175 // Tell the SPI peripheral its CS is asserted.
176 PERIPHERAL_BITBAND(GPIOB_PDOR, 17) = 0;
177 // Disable interrupts on the enable pin. We'll re-enable once we finish
178 // the transfer.
179 PORTA_PCR17 = PORT_PCR_MUX(1);
180 // Clear the interrupt flag now that we've reconfigured it.
181 PORTA_ISFR = 1 << 17;
182 if (have_transfer_) {
183 global_spi_instance->Write(transfer_, &disable_interrupts);
184 have_transfer_ = false;
185 } else {
186 printf("Writing dummy SPI frame\n");
187 // If we don't have anything, just write 0s to avoid getting the
188 // hardware confused.
189 global_spi_instance->Write(SpiTransfer{}, &disable_interrupts);
190 }
191 // Queue up a dummy byte at the end. This won't actually be sent,
192 // because the first byte we do send will be garbage, but it will
193 // synchronize our queues so we receive all the useful data bytes.
194 global_spi_instance->Write(std::array<char, 1>(), &disable_interrupts);
195 waiting_for_enable_ = false;
196 receive_start_ = aos::monotonic_clock::now();
197 cs_deassert_time_ = aos::monotonic_clock::max_time;
198 // To make debugging easier.
199 received_transfer_.fill(0);
200 } else {
201 // Clear the interrupt flag. It shouldn't trigger again immediately
202 // because the pin is still asserted.
203 PORTA_ISFR = 1 << 17;
204 }
205 return;
206 }
207 // We shouldn't ever get here. Clear all the flags and hope they don't get
208 // re-asserted immediately.
209 PORTA_ISFR = UINT32_C(0xFFFFFFFF);
210 }
211
212 void UpdateTransfer(const SpiTransfer &transfer, const DisableInterrupts &) {
213 have_transfer_ = true;
214 transfer_ = transfer;
215 }
216
217 // Returns whether a transfer is currently queued. This will be true between a
218 // call to UpdateTransfer and that transfer actually being moved out to the
219 // hardware.
220 bool HaveTransfer(const DisableInterrupts &) const { return have_transfer_; }
221
222 static SpiQueue *global_instance;
223
224 private:
225 void WaitForNextTransfer() {
226 to_receive_ = received_transfer_;
227 received_dummy_ = false;
228 {
229 DisableInterrupts disable_interrupts;
230 waiting_for_enable_ = true;
231 waiting_for_disable_ = true;
232 PORTA_PCR17 =
233 PORT_PCR_MUX(1) | PORT_PCR_IRQC(0x8) /* Interrupt when logic 0 */;
234 // Clear the interrupt flag now that we've reconfigured it.
235 PORTA_ISFR = 1 << 17;
236 }
237 // Tell the SPI peripheral its CS is de-asserted.
238 PERIPHERAL_BITBAND(GPIOB_PDOR, 17) = 1;
239 }
240
241 bool TransferTimedOut(aos::monotonic_clock::time_point now) {
242 DisableInterrupts disable_interrupts;
243 // TODO: Revise this timeout.
244 return now - std::chrono::milliseconds(50) > receive_start_;
245 }
246
247 bool DeassertHappened(aos::monotonic_clock::time_point now) {
248 DisableInterrupts disable_interrupts;
249 return now - std::chrono::microseconds(50) > cs_deassert_time_;
250 }
251
252 bool waiting_for_enable_ = true;
253 bool waiting_for_disable_ = false;
254 bool have_transfer_ = false;
255 SpiTransfer transfer_;
256 bool received_dummy_ = false;
257 SpiTransfer received_transfer_;
258 gsl::span<char> to_receive_ = received_transfer_;
259 aos::monotonic_clock::time_point receive_start_;
260 aos::monotonic_clock::time_point cs_deassert_time_;
261};
262
263SpiQueue *SpiQueue::global_instance = nullptr;
264
265// All methods here must be fully synchronized by the caller.
266class FrameQueue {
267 public:
268 FrameQueue() = default;
269 FrameQueue(const FrameQueue &) = delete;
270 FrameQueue &operator=(const FrameQueue &) = delete;
271
272 void UpdateFrame(int camera, const Frame &frame) {
273 frames_[camera].targets = frame.targets;
274 frames_[camera].capture_time = aos::monotonic_clock::now() - frame.age;
275 const aos::SizedArray<int, 3> old_last_frames = last_frames_;
276 last_frames_.clear();
277 for (int index : old_last_frames) {
278 if (index != camera) {
279 last_frames_.push_back(index);
280 }
281 }
282 }
283
284 // Creates and returns a transfer with all the current information.
285 //
286 // This does not actually record these frames as transferred until
287 // RemoveLatestFrames() is called.
288 SpiTransfer MakeTransfer();
289
290 // Records the frames represented in the result of the latest MakeTransfer()
291 // call as being transferred, so they will not be represented in subsequent
292 // MakeTransfer() calls.
293 void RemoveLatestFrames() {
294 for (int index : last_frames_) {
295 frames_[index].capture_time = aos::monotonic_clock::min_time;
296 }
297 last_frames_.clear();
298 }
299
300 private:
301 struct FrameData {
302 aos::SizedArray<Target, 3> targets;
303 aos::monotonic_clock::time_point capture_time =
304 aos::monotonic_clock::min_time;
305 };
306
307 std::array<FrameData, 5> frames_;
308 // The indices into frames_ which we returned in the last MakeTransfer() call.
309 aos::SizedArray<int, 3> last_frames_;
310};
311
312SpiTransfer FrameQueue::MakeTransfer() {
313 aos::SizedArray<int, 5> oldest_indices;
314 for (size_t i = 0; i < frames_.size(); ++i) {
315 if (frames_[i].capture_time != aos::monotonic_clock::min_time) {
316 oldest_indices.push_back(i);
317 }
318 }
319 std::sort(oldest_indices.begin(), oldest_indices.end(), [this](int a, int b) {
320 return frames_[a].capture_time < frames_[b].capture_time;
321 });
322
323 TeensyToRoborio message;
324 last_frames_.clear();
325 for (int i = 0; i < std::min<int>(oldest_indices.size(), 3); ++i) {
326 const int index = oldest_indices[i];
327 const FrameData &frame = frames_[index];
328 const auto age = aos::monotonic_clock::now() - frame.capture_time;
329 const auto rounded_age = aos::time::round<camera_duration>(age);
330 message.frames.push_back({frame.targets, rounded_age});
331 last_frames_.push_back(index);
332 }
333 return SpiPackToRoborio(message);
334}
Brian Silverman3240e102019-02-16 18:24:24 -0800335
Brian Silverman2294f352019-03-02 16:31:18 -0800336// Manages turning the debug light on and off periodically.
337//
338// It blinks at 1Hz with a variable duty cycle.
339class DebugLight {
340 public:
341 static constexpr aos::monotonic_clock::duration period() {
342 return std::chrono::seconds(1);
343 }
344
345 void set_next_off_time(aos::monotonic_clock::duration next_off_time) {
346 next_off_time_ = next_off_time;
347 }
348
349 void Tick() {
350 const auto now = aos::monotonic_clock::now();
351 if (last_cycle_start_ == aos::monotonic_clock::min_time) {
352 last_cycle_start_ = now;
353 current_off_point_ = last_cycle_start_ + next_off_time_;
354 } else if (now > last_cycle_start_ + period()) {
355 last_cycle_start_ += period();
356 current_off_point_ = last_cycle_start_ + next_off_time_;
357 }
358 if (now > current_off_point_) {
359 GPIOC_PCOR = 1 << 5;
360 } else {
361 GPIOC_PSOR = 1 << 5;
362 }
363 }
364
365 private:
366 aos::monotonic_clock::time_point last_cycle_start_ =
367 aos::monotonic_clock::min_time;
368
369 aos::monotonic_clock::duration next_off_time_ = std::chrono::milliseconds(100);
370 aos::monotonic_clock::time_point current_off_point_ =
371 aos::monotonic_clock::min_time;
372};
373
Brian Silverman3240e102019-02-16 18:24:24 -0800374extern "C" {
375
376void *__stack_chk_guard = (void *)0x67111971;
377void __stack_chk_fail(void) {
378 while (true) {
379 GPIOC_PSOR = (1 << 5);
380 printf("Stack corruption detected\n");
381 delay(1000);
382 GPIOC_PCOR = (1 << 5);
383 delay(1000);
384 }
385}
386
387extern char *__brkval;
388extern uint32_t __bss_ram_start__[];
389extern uint32_t __heap_start__[];
390extern uint32_t __stack_end__[];
391
392void uart0_status_isr(void) {
393 DisableInterrupts disable_interrupts;
Brian Silvermand7d01102019-02-24 16:11:21 -0800394 Uarts::global_instance->cam1.HandleInterrupt(disable_interrupts);
Brian Silverman3240e102019-02-16 18:24:24 -0800395}
396
397void uart1_status_isr(void) {
398 DisableInterrupts disable_interrupts;
Brian Silvermand7d01102019-02-24 16:11:21 -0800399 Uarts::global_instance->cam0.HandleInterrupt(disable_interrupts);
Brian Silverman3240e102019-02-16 18:24:24 -0800400}
401
402void uart2_status_isr(void) {
403 DisableInterrupts disable_interrupts;
Brian Silvermand7d01102019-02-24 16:11:21 -0800404 Uarts::global_instance->cam2.HandleInterrupt(disable_interrupts);
Brian Silverman3240e102019-02-16 18:24:24 -0800405}
406
407void uart3_status_isr(void) {
408 DisableInterrupts disable_interrupts;
Brian Silvermand7d01102019-02-24 16:11:21 -0800409 Uarts::global_instance->cam3.HandleInterrupt(disable_interrupts);
Brian Silverman3240e102019-02-16 18:24:24 -0800410}
411
412void uart4_status_isr(void) {
413 DisableInterrupts disable_interrupts;
Brian Silvermand7d01102019-02-24 16:11:21 -0800414 Uarts::global_instance->cam4.HandleInterrupt(disable_interrupts);
415}
416
417void spi0_isr(void) {
418 DisableInterrupts disable_interrupts;
419 global_spi_instance->HandleInterrupt(disable_interrupts);
420}
421
422void porta_isr(void) {
423 SpiQueue::global_instance->HandleInterrupt();
Brian Silverman3240e102019-02-16 18:24:24 -0800424}
425
426} // extern "C"
427
428// A test program which echos characters back after adding a per-UART offset to
429// them (CAM0 adds 1, CAM1 adds 2, etc).
430__attribute__((unused)) void TestUarts() {
Brian Silvermand7d01102019-02-24 16:11:21 -0800431 Uarts *const uarts = Uarts::global_instance;
Brian Silverman3240e102019-02-16 18:24:24 -0800432 while (true) {
433 {
434 std::array<char, 10> buffer;
435 const auto data = uarts->cam0.Read(buffer);
436 for (int i = 0; i < data.size(); ++i) {
437 data[i] += 1;
438 }
439 uarts->cam0.Write(data);
440 }
441 {
442 std::array<char, 10> buffer;
443 const auto data = uarts->cam1.Read(buffer);
444 for (int i = 0; i < data.size(); ++i) {
445 data[i] += 2;
446 }
447 uarts->cam1.Write(data);
448 }
449 {
450 std::array<char, 10> buffer;
451 const auto data = uarts->cam2.Read(buffer);
452 for (int i = 0; i < data.size(); ++i) {
453 data[i] += 3;
454 }
455 uarts->cam2.Write(data);
456 }
457 {
458 std::array<char, 10> buffer;
459 const auto data = uarts->cam3.Read(buffer);
460 for (int i = 0; i < data.size(); ++i) {
461 data[i] += 4;
462 }
463 uarts->cam3.Write(data);
464 }
465 {
466 std::array<char, 10> buffer;
467 const auto data = uarts->cam4.Read(buffer);
468 for (int i = 0; i < data.size(); ++i) {
469 data[i] += 5;
470 }
471 uarts->cam4.Write(data);
472 }
473 }
474}
475
476// Tests all the I/O pins. Cycles through each one for 1 second. While active,
477// each output is turned on, and each input has its value printed.
478__attribute__((unused)) void TestIo() {
479 // Set SPI0 pins to GPIO.
480 // SPI_OUT
481 PERIPHERAL_BITBAND(GPIOC_PDDR, 6) = 1;
482 PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(1);
483 // SPI_CS
484 PERIPHERAL_BITBAND(GPIOD_PDDR, 0) = 0;
485 PORTD_PCR0 = PORT_PCR_DSE | PORT_PCR_MUX(1);
486 // SPI_IN
487 PERIPHERAL_BITBAND(GPIOC_PDDR, 7) = 0;
488 PORTC_PCR7 = PORT_PCR_DSE | PORT_PCR_MUX(1);
489 // SPI_SCK
490 PERIPHERAL_BITBAND(GPIOD_PDDR, 1) = 0;
491 PORTD_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(1);
492
493 // Set LED pins to GPIO.
494 PERIPHERAL_BITBAND(GPIOC_PDDR, 11) = 1;
495 PORTC_PCR11 = PORT_PCR_DSE | PORT_PCR_MUX(1);
496 PERIPHERAL_BITBAND(GPIOC_PDDR, 10) = 1;
497 PORTC_PCR10 = PORT_PCR_DSE | PORT_PCR_MUX(1);
498 PERIPHERAL_BITBAND(GPIOC_PDDR, 8) = 1;
499 PORTC_PCR8 = PORT_PCR_DSE | PORT_PCR_MUX(1);
500 PERIPHERAL_BITBAND(GPIOC_PDDR, 9) = 1;
501 PORTC_PCR9 = PORT_PCR_DSE | PORT_PCR_MUX(1);
502 PERIPHERAL_BITBAND(GPIOB_PDDR, 18) = 1;
503 PORTB_PCR18 = PORT_PCR_DSE | PORT_PCR_MUX(1);
504 PERIPHERAL_BITBAND(GPIOC_PDDR, 2) = 1;
505 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(1);
506 PERIPHERAL_BITBAND(GPIOD_PDDR, 7) = 1;
507 PORTD_PCR7 = PORT_PCR_DSE | PORT_PCR_MUX(1);
508 PERIPHERAL_BITBAND(GPIOC_PDDR, 1) = 1;
509 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(1);
510 PERIPHERAL_BITBAND(GPIOB_PDDR, 19) = 1;
511 PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(1);
512 PERIPHERAL_BITBAND(GPIOD_PDDR, 5) = 1;
513 PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1);
514
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 Silvermand7d01102019-02-24 16:11:21 -0800612// Does the normal work of transferring data in all directions.
613//
614// https://community.nxp.com/thread/466937#comment-983881 is a post from NXP
615// claiming that it's impossible to queue up the first byte for the slave end of
616// an SPI connection properly. Instead, we just accept there will be a garbage
617// byte and the other end ignores it.
Brian Silverman83693e42019-03-02 15:45:52 -0800618__attribute__((unused)) void TransferData(
619 frc971::motors::PrintingImplementation *printing) {
Brian Silvermand7d01102019-02-24 16:11:21 -0800620 Uarts *const uarts = Uarts::global_instance;
621 std::array<CobsPacketizer<uart_to_teensy_size()>, 5> packetizers;
622 std::array<TransmitBuffer, 5> transmit_buffers{
623 {&uarts->cam0, &uarts->cam1, &uarts->cam2, &uarts->cam3, &uarts->cam4}};
624 FrameQueue frame_queue;
625 aos::monotonic_clock::time_point last_camera_send =
626 aos::monotonic_clock::min_time;
Brian Silverman83693e42019-03-02 15:45:52 -0800627 CameraCommand stdin_camera_command = CameraCommand::kNormal;
628 CameraCommand last_roborio_camera_command = CameraCommand::kNormal;
Brian Silverman2294f352019-03-02 16:31:18 -0800629 DebugLight debug_light;
Brian Silverman83693e42019-03-02 15:45:52 -0800630
Brian Silvermand7d01102019-02-24 16:11:21 -0800631 bool first = true;
632 while (true) {
Brian Silverman2294f352019-03-02 16:31:18 -0800633 debug_light.Tick();
634
Brian Silvermand7d01102019-02-24 16:11:21 -0800635 {
636 const auto received_transfer = SpiQueue::global_instance->Tick();
637 if (received_transfer) {
638 const auto unpacked = SpiUnpackToTeensy(*received_transfer);
Brian Silverman83693e42019-03-02 15:45:52 -0800639 if (unpacked) {
640 last_roborio_camera_command = unpacked->camera_command;
641 } else {
Brian Silvermand7d01102019-02-24 16:11:21 -0800642 printf("UART decode error\n");
643 }
644 }
645 }
646
647 {
648 std::array<char, 20> buffer;
649 packetizers[0].ParseData(uarts->cam0.Read(buffer));
650 packetizers[1].ParseData(uarts->cam1.Read(buffer));
651 packetizers[2].ParseData(uarts->cam2.Read(buffer));
652 packetizers[3].ParseData(uarts->cam3.Read(buffer));
653 packetizers[4].ParseData(uarts->cam4.Read(buffer));
654 }
655 for (size_t i = 0; i < packetizers.size(); ++i) {
656 if (!packetizers[i].received_packet().empty()) {
657 const auto decoded =
658 UartUnpackToTeensy(packetizers[i].received_packet());
659 packetizers[i].clear_received_packet();
660 if (decoded) {
661 printf("got one with %d\n", (int)decoded->targets.size());
662 frame_queue.UpdateFrame(i, *decoded);
663 }
664 }
665 }
666 {
667 bool made_transfer = false;
668 if (!first) {
669 DisableInterrupts disable_interrupts;
670 made_transfer =
671 !SpiQueue::global_instance->HaveTransfer(disable_interrupts);
672 }
673 if (made_transfer) {
674 frame_queue.RemoveLatestFrames();
675 }
676 const auto transfer = frame_queue.MakeTransfer();
677 {
678 DisableInterrupts disable_interrupts;
679 SpiQueue::global_instance->UpdateTransfer(transfer, disable_interrupts);
680 }
681 }
682 {
683 const auto now = aos::monotonic_clock::now();
684 if (last_camera_send + std::chrono::milliseconds(1000) < now) {
685 last_camera_send = now;
686 CameraCalibration calibration{};
687 calibration.teensy_now = aos::monotonic_clock::now();
688 calibration.realtime_now = aos::realtime_clock::min_time;
Brian Silverman83693e42019-03-02 15:45:52 -0800689 if (last_roborio_camera_command != CameraCommand::kNormal) {
690 calibration.camera_command = last_roborio_camera_command;
691 } else {
692 calibration.camera_command = stdin_camera_command;
693 }
Brian Silverman2294f352019-03-02 16:31:18 -0800694 if (calibration.camera_command == CameraCommand::kUsb) {
695 debug_light.set_next_off_time(std::chrono::milliseconds(900));
696 } else if (calibration.camera_command ==
697 CameraCommand::kCameraPassthrough) {
698 debug_light.set_next_off_time(std::chrono::milliseconds(500));
699 } else {
700 debug_light.set_next_off_time(std::chrono::milliseconds(100));
701 }
Brian Silvermand7d01102019-02-24 16:11:21 -0800702 // TODO(Brian): Actually fill out the calibration field.
703 transmit_buffers[0].MaybeWritePacket(calibration);
704 transmit_buffers[1].MaybeWritePacket(calibration);
705 transmit_buffers[2].MaybeWritePacket(calibration);
706 transmit_buffers[3].MaybeWritePacket(calibration);
707 transmit_buffers[4].MaybeWritePacket(calibration);
708 }
709 for (TransmitBuffer &transmit_buffer : transmit_buffers) {
710 transmit_buffer.Tick(now);
711 }
712 }
713
Brian Silverman83693e42019-03-02 15:45:52 -0800714 {
715 const auto stdin_data = printing->ReadStdin();
716 if (!stdin_data.empty()) {
717 switch (stdin_data.back()) {
718 case 'p':
719 printf("Entering passthrough mode\n");
720 stdin_camera_command = CameraCommand::kCameraPassthrough;
721 break;
722 case 'u':
723 printf("Entering USB mode\n");
724 stdin_camera_command = CameraCommand::kUsb;
725 break;
726 case 'n':
727 printf("Entering normal mode\n");
728 stdin_camera_command = CameraCommand::kNormal;
729 break;
730 default:
731 printf("Unrecognized character\n");
732 break;
733 }
734 }
735 }
736
Brian Silvermand7d01102019-02-24 16:11:21 -0800737 first = false;
738 }
739}
740
Brian Silverman3240e102019-02-16 18:24:24 -0800741int Main() {
742 // for background about this startup delay, please see these conversations
743 // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
744 // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
745 delay(400);
746
747 // Set all interrupts to the second-lowest priority to start with.
748 for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD);
749
750 // Now set priorities for all the ones we care about. They only have meaning
751 // relative to each other, which means centralizing them here makes it a lot
752 // more manageable.
Brian Silvermand7d01102019-02-24 16:11:21 -0800753 NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7);
754 NVIC_SET_SANE_PRIORITY(IRQ_UART0_STATUS, 0x3);
755 NVIC_SET_SANE_PRIORITY(IRQ_UART1_STATUS, 0x3);
756 NVIC_SET_SANE_PRIORITY(IRQ_UART2_STATUS, 0x3);
757 NVIC_SET_SANE_PRIORITY(IRQ_UART3_STATUS, 0x3);
758 NVIC_SET_SANE_PRIORITY(IRQ_UART4_STATUS, 0x3);
759 // This one is relatively sensitive to latencies. The buffer is ~4800 clock
760 // cycles long.
761 NVIC_SET_SANE_PRIORITY(IRQ_SPI0, 0x2);
762 NVIC_SET_SANE_PRIORITY(IRQ_PORTA, 0x3);
Brian Silverman3240e102019-02-16 18:24:24 -0800763
764 // Set the LED's pin to output mode.
765 PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1;
766 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1);
767
768 frc971::motors::PrintingParameters printing_parameters;
769 printing_parameters.dedicated_usb = true;
770 const ::std::unique_ptr<frc971::motors::PrintingImplementation> printing =
771 CreatePrinting(printing_parameters);
772 printing->Initialize();
773
774 DMA.CR = M_DMA_EMLM;
775
Brian Silvermand7d01102019-02-24 16:11:21 -0800776 SIM_SCGC1 |= SIM_SCGC1_UART4;
Brian Silverman3240e102019-02-16 18:24:24 -0800777 SIM_SCGC4 |=
778 SIM_SCGC4_UART0 | SIM_SCGC4_UART1 | SIM_SCGC4_UART2 | SIM_SCGC4_UART3;
Brian Silvermand7d01102019-02-24 16:11:21 -0800779 SIM_SCGC6 |= SIM_SCGC6_SPI0;
Brian Silverman3240e102019-02-16 18:24:24 -0800780
781 // SPI0 goes to the roboRIO.
782 // SPI0_PCS0 is SPI_CS.
Brian Silvermand7d01102019-02-24 16:11:21 -0800783 PORTD_PCR0 = PORT_PCR_MUX(2);
Brian Silverman3240e102019-02-16 18:24:24 -0800784 // SPI0_SOUT is SPI_MISO.
785 PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(2);
786 // SPI0_SIN is SPI_MOSI.
787 PORTC_PCR7 = PORT_PCR_DSE | PORT_PCR_MUX(2);
788 // SPI0_SCK is SPI_CLK.
789 PORTD_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(2);
Brian Silvermand7d01102019-02-24 16:11:21 -0800790 // SPI_CS_DRIVE
791 PERIPHERAL_BITBAND(GPIOB_PDDR, 17) = 1;
792 PERIPHERAL_BITBAND(GPIOB_PDOR, 17) = 1;
793 PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_MUX(1);
794 // SPI_CS_IN
795 PERIPHERAL_BITBAND(GPIOA_PDDR, 17) = 0;
796 // Set the filter width.
797 PORTA_DFWR = 31;
798 // Enable the filter.
799 PERIPHERAL_BITBAND(PORTA_DFER, 17) = 1;
800 PORTA_PCR17 =
801 PORT_PCR_MUX(1) | PORT_PCR_IRQC(0xC) /* Interrupt when logic 1 */;
802 // Clear the interrupt flag now that we've reconfigured it.
803 PORTA_ISFR = 1 << 17;
Brian Silverman3240e102019-02-16 18:24:24 -0800804
805 // FTM0_CH0 is LED0 (7 in silkscreen, a beacon channel).
806 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(4);
807 // FTM0_CH1 is LED1 (5 in silkscreen, a beacon channel).
808 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(4);
809 // FTM0_CH7 is LED2 (6 in silkscreen, a beacon channel).
810 PORTD_PCR7 = PORT_PCR_DSE | PORT_PCR_MUX(4);
811 // FTM0_CH5 is LED3 (9 in silkscreen, a vision camera).
812 PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(4);
813
814 // FTM2_CH1 is LED4 (8 in silkscreen, a vision camera).
815 PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(3);
816 // FTM2_CH0 is LED5 (for CAM4).
817 PORTB_PCR18 = PORT_PCR_DSE | PORT_PCR_MUX(3);
818
819 // FTM3_CH4 is LED6 (for CAM2).
820 PORTC_PCR8 = PORT_PCR_DSE | PORT_PCR_MUX(3);
821 // FTM3_CH5 is LED7 (for CAM3).
822 PORTC_PCR9 = PORT_PCR_DSE | PORT_PCR_MUX(3);
823 // FTM3_CH6 is LED8 (for CAM1).
824 PORTC_PCR10 = PORT_PCR_DSE | PORT_PCR_MUX(3);
825 // FTM3_CH7 is LED9 (for CAM0).
826 PORTC_PCR11 = PORT_PCR_DSE | PORT_PCR_MUX(3);
827
828 // This hardware has been deactivated, but keep this comment for now to
829 // document which pins it is on.
830#if 0
831 // This is ODROID_EN.
832 PERIPHERAL_BITBAND(GPIOC_PDDR, 0) = 1;
833 PERIPHERAL_BITBAND(GPIOC_PDOR, 0) = 0;
834 PORTC_PCR0 = PORT_PCR_DSE | PORT_PCR_MUX(1);
835 // This is CAM_EN.
836 PERIPHERAL_BITBAND(GPIOB_PDDR, 0) = 1;
837 PERIPHERAL_BITBAND(GPIOB_PDOR, 0) = 0;
838 PORTB_PCR0 = PORT_PCR_DSE | PORT_PCR_MUX(1);
839#endif
840 // This is 5V_PGOOD.
841 PERIPHERAL_BITBAND(GPIOD_PDDR, 6) = 0;
842 PORTD_PCR6 = PORT_PCR_MUX(1);
843
844 // These go to CAM1.
845 // UART0_RX (peripheral) is UART1_RX (schematic).
Brian Silvermand7d01102019-02-24 16:11:21 -0800846 PORTA_PCR15 = PORT_PCR_DSE | PORT_PCR_MUX(3) | PORT_PCR_PE /* Do a pull */ |
847 0 /* !PS to pull down */;
Brian Silverman3240e102019-02-16 18:24:24 -0800848 // UART0_TX (peripheral) is UART1_TX (schematic).
849 PORTA_PCR14 = PORT_PCR_DSE | PORT_PCR_MUX(3);
850
851 // These go to CAM0.
852 // UART1_RX (peripheral) is UART0_RX (schematic).
Brian Silvermand7d01102019-02-24 16:11:21 -0800853 PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(3) | PORT_PCR_PE /* Do a pull */ |
854 0 /* !PS to pull down */;
Brian Silverman3240e102019-02-16 18:24:24 -0800855 // UART1_TX (peripheral) is UART0_TX (schematic).
856 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(3);
857
858 // These go to CAM2.
859 // UART2_RX
Brian Silvermand7d01102019-02-24 16:11:21 -0800860 PORTD_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(3) | PORT_PCR_PE /* Do a pull */ |
861 0 /* !PS to pull down */;
Brian Silverman3240e102019-02-16 18:24:24 -0800862 // UART2_TX
863 PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(3);
864
865 // These go to CAM3.
866 // UART3_RX
Brian Silvermand7d01102019-02-24 16:11:21 -0800867 PORTB_PCR10 = PORT_PCR_DSE | PORT_PCR_MUX(3) | PORT_PCR_PE /* Do a pull */ |
868 0 /* !PS to pull down */;
Brian Silverman3240e102019-02-16 18:24:24 -0800869 // UART3_TX
870 PORTB_PCR11 = PORT_PCR_DSE | PORT_PCR_MUX(3);
871
872 // These go to CAM4.
873 // UART4_RX
Brian Silvermand7d01102019-02-24 16:11:21 -0800874 PORTE_PCR25 = PORT_PCR_DSE | PORT_PCR_MUX(3) | PORT_PCR_PE /* Do a pull */ |
875 0 /* !PS to pull down */;
Brian Silverman3240e102019-02-16 18:24:24 -0800876 // UART4_TX
877 PORTE_PCR24 = PORT_PCR_DSE | PORT_PCR_MUX(3);
878
879 Uarts uarts;
Brian Silvermand7d01102019-02-24 16:11:21 -0800880 InterruptBufferedSpi spi0{&SPI0, BUS_CLOCK_FREQUENCY};
881 global_spi_instance = &spi0;
882 SpiQueue spi_queue;
Brian Silverman3240e102019-02-16 18:24:24 -0800883
884 // Give everything a chance to get going.
885 delay(100);
886
887 printf("Ram start: %p\n", __bss_ram_start__);
888 printf("Heap start: %p\n", __heap_start__);
889 printf("Heap end: %p\n", __brkval);
890 printf("Stack start: %p\n", __stack_end__);
891
892 uarts.Initialize(115200);
893 NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
894 NVIC_ENABLE_IRQ(IRQ_UART1_STATUS);
895 NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
896 NVIC_ENABLE_IRQ(IRQ_UART3_STATUS);
897 NVIC_ENABLE_IRQ(IRQ_UART4_STATUS);
Brian Silvermand7d01102019-02-24 16:11:21 -0800898 spi0.Initialize();
899 NVIC_ENABLE_IRQ(IRQ_SPI0);
900 NVIC_ENABLE_IRQ(IRQ_PORTA);
901
Brian Silverman83693e42019-03-02 15:45:52 -0800902 TransferData(printing.get());
Brian Silverman3240e102019-02-16 18:24:24 -0800903
904 while (true) {
905 }
906}
907
908extern "C" {
909
910int main(void) {
911 return Main();
912}
913
914} // extern "C"
915
916} // namespace
917} // namespace jevois
918} // namespace frc971