Pull the medium-specific pieces out of the common code

Change-Id: I9cd1824a6bf535e285553fbe5f79f6d72919f048
diff --git a/motors/peripheral/adc.cc b/motors/peripheral/adc.cc
index ba2b247..0f5d947 100644
--- a/motors/peripheral/adc.cc
+++ b/motors/peripheral/adc.cc
@@ -4,6 +4,7 @@
 
 namespace frc971 {
 namespace salsa {
+namespace {
 
 #define ADC_SC2_BASE (ADC_SC2_REFSEL(0) /* Use the external reference pins. */)
 
@@ -43,39 +44,79 @@
     ADC##n##_SC3 = 0 /* Disable hardware averaging. */;                      \
   } while (0)
 
-void AdcInit() {
+void AdcInitCommon() {
   SIM_SCGC3 |= SIM_SCGC3_ADC1;
   SIM_SCGC6 |= SIM_SCGC6_ADC0;
   // TODO(Brian): Mess with SIM_SOPT7 to reconfigure ADC trigger input source?
   ADC_INIT_SINGLE(0);
   ADC_INIT_SINGLE(1);
+}
 
-  // M_CH2V/M1_CH2F ADC0_SE14
+}  // namespace
+
+void AdcInitMedium() {
+  AdcInitCommon();
+
+  // M_CH2V ADC0_SE14
   PORTC_PCR0 = PORT_PCR_MUX(0);
 
-  // M_CH0V/M1_CH0F ADC0_SE13
+  // M_CH0V ADC0_SE13
   PORTB_PCR3 = PORT_PCR_MUX(0);
 
-  // M_CH1V/M1_CH1F ADC0_SE12
+  // M_CH1V ADC0_SE12
   PORTB_PCR2 = PORT_PCR_MUX(0);
 
-  // M0_CH0F/M_CH0F ADC1_SE14
+  // M_CH0F ADC1_SE14
   PORTB_PCR10 = PORT_PCR_MUX(0);
 
-  // M0_CH1F/M_CH1F ADC1_SE15
+  // M_CH1F ADC1_SE15
   PORTB_PCR11 = PORT_PCR_MUX(0);
 
-  // WHEEL_ABS/M_VREF ADC0_SE18
+  // M_VREF ADC0_SE18
   PORTE_PCR25 = PORT_PCR_MUX(0);
 
-  // VIN/VIN ADC1_SE5B
+  // VIN ADC1_SE5B
   PORTC_PCR9 = PORT_PCR_MUX(0);
 
-  // M0_CH2F/M_CH2F ADC1_SE17
+  // M_CH2F ADC1_SE17
   PORTA_PCR17 = PORT_PCR_MUX(0);
 }
 
-MediumAdcReadings AdcReadMedium() {
+void AdcInitSmall() {
+  AdcInitCommon();
+
+  // M0_CH0F ADC1_SE17
+  PORTA_PCR17 = PORT_PCR_MUX(0);
+
+  // M0_CH1F ADC1_SE14
+  PORTB_PCR10 = PORT_PCR_MUX(0);
+
+  // M0_CH2F ADC1_SE15
+  PORTB_PCR11 = PORT_PCR_MUX(0);
+
+  // M0_ABS ADC0_SE5b
+  PORTD_PCR1 = PORT_PCR_MUX(0);
+
+  // M1_CH0F ADC0_SE13
+  PORTB_PCR3 = PORT_PCR_MUX(0);
+
+  // M1_CH1F ADC0_SE12
+  PORTB_PCR2 = PORT_PCR_MUX(0);
+
+  // M1_CH2F ADC0_SE14
+  PORTC_PCR0 = PORT_PCR_MUX(0);
+
+  // M1_ABS ADC0_SE17
+  PORTE_PCR24 = PORT_PCR_MUX(0);
+
+  // WHEEL_ABS ADC0_SE18
+  PORTE_PCR25 = PORT_PCR_MUX(0);
+
+  // VIN ADC1_SE5B
+  PORTC_PCR9 = PORT_PCR_MUX(0);
+}
+
+MediumAdcReadings AdcReadMedium(const DisableInterrupts &) {
   MediumAdcReadings r;
 
   ADC1_SC1A = 14;
@@ -114,5 +155,62 @@
   return r;
 }
 
+SmallAdcReadings AdcReadSmall0(const DisableInterrupts &) {
+  SmallAdcReadings r;
+
+  ADC1_SC1A = 17;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC1_SC1A = 14;
+  r.currents[0] = ADC1_RA;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC1_SC1A = 15;
+  r.currents[1] = ADC1_RA;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  r.currents[2] = ADC1_RA;
+
+  return r;
+}
+
+SmallAdcReadings AdcReadSmall1(const DisableInterrupts &) {
+  SmallAdcReadings r;
+
+  ADC0_SC1A = 13;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC0_SC1A = 12;
+  r.currents[0] = ADC0_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC0_SC1A = 14;
+  r.currents[1] = ADC0_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  r.currents[2] = ADC0_RA;
+
+  return r;
+}
+
+SmallInitReadings AdcReadSmallInit(const DisableInterrupts &) {
+  SmallInitReadings r;
+
+  ADC0_SC1A = 5;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC0_SC1A = 17;
+  r.motor0_abs = ADC0_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC0_SC1A = 18;
+  r.motor1_abs = ADC0_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  r.wheel_abs = ADC0_RA;
+
+  return r;
+}
+
 }  // namespace salsa
 }  // namespace frc971
diff --git a/motors/peripheral/adc.h b/motors/peripheral/adc.h
index 4aee769..f9ec22a 100644
--- a/motors/peripheral/adc.h
+++ b/motors/peripheral/adc.h
@@ -3,6 +3,8 @@
 
 #include <stdint.h>
 
+#include "motors/util.h"
+
 namespace frc971 {
 namespace salsa {
 
@@ -12,9 +14,23 @@
   uint16_t input_voltage;
 };
 
-void AdcInit();
+struct SmallAdcReadings {
+  uint16_t currents[3];
+};
 
-MediumAdcReadings AdcReadMedium();
+struct SmallInitReadings {
+  uint16_t motor0_abs;
+  uint16_t motor1_abs;
+  uint16_t wheel_abs;
+};
+
+void AdcInitMedium();
+void AdcInitSmall();
+
+MediumAdcReadings AdcReadMedium(const DisableInterrupts &);
+SmallAdcReadings AdcReadSmall0(const DisableInterrupts &);
+SmallAdcReadings AdcReadSmall1(const DisableInterrupts &);
+SmallInitReadings AdcReadSmallInit(const DisableInterrupts &);
 
 }  // namespace salsa
 }  // namespace frc971
diff --git a/motors/peripheral/can.c b/motors/peripheral/can.c
index 7187ac9..0e731e2 100644
--- a/motors/peripheral/can.c
+++ b/motors/peripheral/can.c
@@ -33,8 +33,6 @@
 
 void can_init(void) {
   printf("can_init\n");
-  PORTB_PCR18 = PORT_PCR_DSE | PORT_PCR_MUX(2);
-  PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(2);
 
   SIM_SCGC6 |= SIM_SCGC6_FLEXCAN0;
 
@@ -175,7 +173,7 @@
 void can_receive_command(unsigned char *data, int *length) {
   if (0) {
     static int i = 0;
-    if (i++ == 13) {
+    if (i++ == 10000) {
       printf("IFLAG1=%" PRIx32 " ESR=%" PRIx32 " ESR1=%" PRIx32 "\n",
              CAN0_IFLAG1, CAN0_ECR, CAN0_ESR1);
       i = 0;