Finish low level parts of the simple receiver code

It does the same motor test code still, but it's ready for real code to
be used instead.

Change-Id: Ie74f0b29f40ff25402adf7cbac0283262bd8f07c
diff --git a/motors/peripheral/adc.cc b/motors/peripheral/adc.cc
index 7232a45..90b1d0d 100644
--- a/motors/peripheral/adc.cc
+++ b/motors/peripheral/adc.cc
@@ -129,6 +129,13 @@
   PORTB_PCR2 = PORT_PCR_MUX(0);
 }
 
+void AdcInitSimple() {
+  AdcInitCommon();
+
+  // ENC_SIN ADC0_SE23
+  // ENC_COS ADC1_SE23
+}
+
 MediumAdcReadings AdcReadMedium(const DisableInterrupts &) {
   MediumAdcReadings r;
 
@@ -248,5 +255,20 @@
   return r;
 }
 
+SimpleAdcReadings AdcReadSimple(const DisableInterrupts &) {
+  SimpleAdcReadings r;
+
+  ADC0_SC1A = 23;
+  ADC1_SC1A = 23;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  r.sin = ADC0_RA;
+  r.cos = ADC1_RA;
+
+  return r;
+}
+
 }  // namespace salsa
 }  // namespace frc971
diff --git a/motors/peripheral/adc.h b/motors/peripheral/adc.h
index d855acf..04caff7 100644
--- a/motors/peripheral/adc.h
+++ b/motors/peripheral/adc.h
@@ -5,6 +5,9 @@
 
 #include "motors/util.h"
 
+// TODO(Brian): Avoid cramming all the code for each specific application into a
+// single file like this.
+
 namespace frc971 {
 namespace salsa {
 
@@ -28,15 +31,21 @@
   uint16_t analog0, analog1, analog2, analog3;
 };
 
+struct SimpleAdcReadings {
+  uint16_t sin, cos;
+};
+
 void AdcInitMedium();
 void AdcInitSmall();
 void AdcInitJoystick();
+void AdcInitSimple();
 
 MediumAdcReadings AdcReadMedium(const DisableInterrupts &);
 SmallAdcReadings AdcReadSmall0(const DisableInterrupts &);
 SmallAdcReadings AdcReadSmall1(const DisableInterrupts &);
 SmallInitReadings AdcReadSmallInit(const DisableInterrupts &);
 JoystickAdcReadings AdcReadJoystick(const DisableInterrupts &);
+SimpleAdcReadings AdcReadSimple(const DisableInterrupts &);
 
 }  // namespace salsa
 }  // namespace frc971
diff --git a/motors/peripheral/can.c b/motors/peripheral/can.c
index b46a8b4..ccd8d7d 100644
--- a/motors/peripheral/can.c
+++ b/motors/peripheral/can.c
@@ -18,8 +18,9 @@
 // The number of message buffers we're actually going to use. The chip only has
 // 16. Using fewer means less for the CAN module (and CPU) to go through looking
 // for actual data.
-// 0 is for sending and 1 is for receiving commands.
-#define NUMBER_MESSAGE_BUFFERS 4
+// 0 and 1 are for receiving.
+// 2-7 are for sending.
+#define NUMBER_MESSAGE_BUFFERS 8
 
 #if NUMBER_MESSAGE_BUFFERS > 16
 #error Only have 16 message buffers on this part.
@@ -53,21 +54,19 @@
       CAN_MCR_SRXDIS /* Don't want to see our own frames at all. */ |
       CAN_MCR_IRMQ /* Use individual masks for each filter. */ |
       CAN_MCR_LPRIOEN /* Let us prioritize TX mailboxes. */ |
+      (0 << 12) /* !AEN to avoid complicated abort semantics. */ |
       (0 << 8) /* No need to pack IDs tightly, so it's easier not to. */ |
       (NUMBER_MESSAGE_BUFFERS - 1);
 
   // Initialize all the buffers and RX filters we're enabling.
 
-  // Just in case this does anything...
-  CAN0_RXIMRS[2] = 0;
-  CAN0_MESSAGES[2].prio_id = 0;
-  CAN0_MESSAGES[2].control_timestamp =
-      CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_INACTIVE);
-
-  CAN0_RXIMRS[3] = 0;
-  CAN0_MESSAGES[3].prio_id = 0;
-  CAN0_MESSAGES[3].control_timestamp =
-      CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_INACTIVE);
+  for (int i = 2; i < 8; ++i) {
+    // Just in case this does anything...
+    CAN0_RXIMRS[i] = 0;
+    CAN0_MESSAGES[i].prio_id = 0;
+    CAN0_MESSAGES[i].control_timestamp =
+        CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_INACTIVE);
+  }
 
   CAN0_RXIMRS[0] = (1 << 31) /* Want to filter out RTRs. */ |
                    (0 << 30) /* Want to only get standard frames. */ |
@@ -169,6 +168,8 @@
   // Just inactivate the mailbox to start with. Checking if it's done being
   // transmitted doesn't seem to work like the reference manual describes, so
   // just take the brute force approach.
+  // The reference manual says this will either transmit the frame or not, but
+  // there's no way to tell which happened, which is fine for what we're doing.
   message_buffer->control_timestamp =
       CAN_MB_CONTROL_INSERT_CODE(CAN_MB_CODE_TX_INACTIVE);
 
diff --git a/motors/peripheral/can.h b/motors/peripheral/can.h
index 98083c9..061fa05 100644
--- a/motors/peripheral/can.h
+++ b/motors/peripheral/can.h
@@ -14,7 +14,7 @@
 
 void can_init(uint32_t id0, uint32_t id1);
 
-// Mailbox is 2 or 3 for the two send mailboxes.
+// Mailbox is 2-7 (inclusive) for the send mailboxes.
 int can_send(uint32_t can_id, const unsigned char *data, unsigned int length,
              unsigned int mailbox);