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