Reorganize and extend Kinetis hardware access macros
This adds support for the K22 MCUs on fet12v2. It also adds support for
some additional pieces of hardware the code will use. It also makes the
new (hermetic) compiler happy with all of it.
Change-Id: I4b9b26d71fd57d11e16737ef7a6480643dbd0ca5
diff --git a/motors/peripheral/adc.cc b/motors/peripheral/adc.cc
index 756e166..6325f9f 100644
--- a/motors/peripheral/adc.cc
+++ b/motors/peripheral/adc.cc
@@ -22,12 +22,14 @@
ADC##n##_##PM##G = variable; \
} while (0);
-#define ADC_INIT_SINGLE(n) \
+#define ADC_INIT_SINGLE(n, maybe_muxsel) \
do { \
- ADC##n##_CFG1 = ADC_CFG1_ADIV(2) /* Divide clock by 4 to get 15MHz. */ | \
- ADC_CFG1_MODE(1) /* 12 bit mode. */ | \
- ADC_CFG1_ADICLK(0) /* Use the bus clock (60MHz). */; \
- ADC##n##_CFG2 = ADC_CFG2_MUXSEL /* Use the b channels. */ | \
+ ADC##n##_CFG1 = \
+ ADC_CFG1_ADIV(2) /* Divide clock by 4 to get 15MHz. */ | \
+ 0 /* !ADLSMP to sample faster */ | \
+ ADC_CFG1_MODE(1) /* 12 bit single-ended or 13-bit differential. */ | \
+ ADC_CFG1_ADICLK(0) /* Use the bus clock (60MHz). */; \
+ ADC##n##_CFG2 = (maybe_muxsel) | \
ADC_CFG2_ADHSC /* Support higher ADC clock speeds. */; \
ADC##n##_SC1A = 0; /* Clear SC1A's COCO flag. */ \
ADC##n##_SC2 = ADC_SC2_BASE; \
@@ -46,12 +48,12 @@
} // namespace
-void AdcInitCommon() {
+void AdcInitCommon(AdcChannels adc0_channels, AdcChannels adc1_channels) {
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);
+ ADC_INIT_SINGLE(0, (adc0_channels == AdcChannels::kB) ? ADC_CFG2_MUXSEL : 0);
+ ADC_INIT_SINGLE(1, (adc1_channels == AdcChannels::kB) ? ADC_CFG2_MUXSEL : 0);
}
} // namespace motors
diff --git a/motors/peripheral/adc.h b/motors/peripheral/adc.h
index 9ca5506..c22a856 100644
--- a/motors/peripheral/adc.h
+++ b/motors/peripheral/adc.h
@@ -8,7 +8,12 @@
namespace frc971 {
namespace motors {
-void AdcInitCommon();
+enum class AdcChannels {
+ kA,
+ kB,
+};
+void AdcInitCommon(AdcChannels adc0_channels = AdcChannels::kB,
+ AdcChannels adc1_channels = AdcChannels::kB);
} // namespace motors
} // namespace frc971