blob: bdd84d6e2242f81ca8619d14d0997c81e24bab67 [file] [log] [blame]
Brian Silvermancabadaf2018-09-03 19:36:44 -07001#include "motors/peripheral/adc_dma.h"
2
3#include <assert.h>
4
5// Design notes:
6// * Want to grab 3 differential-pair captures in rapid succession (aka 3 pairs
7// each containing 2 differential ADC input values)
8// * Use hardware triggering to allow triggering captures on both ADCs at the
9// exact same time
10// * Need to use both A and B channels within each ADC because writing to SC1n
11// while it's doing a capture (like to set up the next one) aborts the capture
12// * Can't use alternate (non-PDB) ADC triggers, because we want to use both A
13// and B channels within each ADC, and only the PDB triggers know how to use
14// two pre-triggers to choose which ADC channel to trigger
15// * Back-to-back connections aren't directly helpful because they're only
16// set up to go ADC0.A, ADC0.B, ADC1.A, ADC1.B, in a loop
17// * Setup:
18// * Set ADC0 and ADC1 for hardware triggering
19// * Write to ADC0_SC1A and ADC1_SC1A (with CPU) with initial values
20// * PDB:
21// * One-shot mode
22// * A output bypassed (so it goes right after the trigger)
23// * Triggered by FTM trigger outputs (twice per cycle)
24// * B output in back-to-back mode
25// * Delays don't matter
26// * A doesn't re-trigger off of B, so PDB+ADC by themselves stop after
27// doing two samples
28// * FTM triggers it at the appropriate points in the cycle (using two
29// otherwise-unused FTM channels)
30// * DMA:
31// * One result triggers off of ADC.COCO (either one)
32// * SOFF moves between RA and RB
33// * 1 minor loop = reading Rn from one ADC
34// * Trigger other result channel or first reconfigure channel after
35// (link on both major and minor completion)
36// * SMOD wraps back to RB after RA
37// * One major iteration is all four samples
38// * Can't use SOFF and SMOD to read all results with one channel because
39// SOFF is too small
40// * Same idea for two reconfigure channels
41// * Use DREQ so disabled after doing all four for CPU to read results
42// * Configure to reset everything after the major iteration so CPU just
43// has to re-enable
44// * Desired sequence:
45// 1. Trigger ADC0.A and ADC1.A
46// 2. [Reading ADC0.RB and ADC1.RB would be OK]
47// 3. Wait for ADC0.A and ADC1.A to finish
48// 4. Trigger ADC0.B and ADC0.B
49// 5. Read ADC0.RA and ADC1.RA
50// 6. Wait for ADC0.B and ADC1.B to finish
51// 7. Trigger ADC0.A and ADC0.A
52// 8. Read ADC0.RB and ADC1.RB
53// 9. Go back to step #3
54
55namespace frc971 {
56namespace teensy {
57namespace {
58
59constexpr uint32_t pdb_sc(int pdb_input) {
60 return V_PDB_LDMOD(0) /* Load immediately after LDOK */ |
61 V_PDB_PRESCALER(0) /* Doesn't matter */ | V_PDB_TRGSEL(pdb_input) |
62 M_PDB_PDBEN /* Enable it */ | V_PDB_MULT(0) /* Doesn't matter */ |
63 M_PDB_LDOK /* Load new values now */;
64}
65
66} // namespace
67
Brian Silvermana1d84822018-09-15 17:18:49 -070068AdcDmaSampler::AdcDmaSampler(int counts_per_cycle) : counts_per_cycle_(counts_per_cycle) {
Brian Silvermancabadaf2018-09-03 19:36:44 -070069 for (int adc = 0; adc < 2; ++adc) {
70 for (int i = 0; i < 2; ++i) {
71 adc_sc1s_[adc][kNumberAdcSamples + i] = ADC_SC1_ADCH(0x1f);
72 }
73 }
74}
75
76void AdcDmaSampler::set_adc0_samples(
77 const ::std::array<uint32_t, kNumberAdcSamples> &adc0_samples) {
78 for (int i = 0; i < kNumberAdcSamples; ++i) {
79 adc_sc1s_[0][i] = adc0_samples[i] | ADC_SC1_AIEN;
80 }
81}
82
83void AdcDmaSampler::set_adc1_samples(
84 const ::std::array<uint32_t, kNumberAdcSamples> &adc1_samples) {
85 for (int i = 0; i < kNumberAdcSamples; ++i) {
86 adc_sc1s_[1][i] = adc1_samples[i] | ADC_SC1_AIEN;
87 }
88}
89
90void AdcDmaSampler::Initialize() {
91 // TODO(Brian): Put this somewhere better?
92 SIM_SCGC6 |= SIM_SCGC6_DMAMUX | SIM_SCGC6_PDB;
93
94 assert(ftm_delays_[0] != nullptr);
95 assert(ftm_delays_[1] != nullptr);
96 {
97 // See "Figure 33-92. Conversion time equation" for details on this math.
98 // All the math is in bus clock cycles, until being divided into FTM
99 // clock cycles at the end.
100
101 // Divider from bus clock to FTM clock.
102 // TODO(Brian): Make it so this can actually change.
103 const int ftm_clock_divider = 1;
104
105 // Divider from bus clock to ADC clock.
106 // TODO(Brian): Make sure this stays in sync with what adc.cc actually
107 // configures.
108 static constexpr int kAdcClockDivider = 4;
109
110 static constexpr int kSfcAdder = 5 * kAdcClockDivider + 5;
111
112 // 12-bit single-ended is only 20, but waiting a bit too long for some of
113 // the samples doesn't hurt anything.
114 static constexpr int kBct = 30 /* 13-bit differential */ * kAdcClockDivider;
115
116 static constexpr int kLstAdder = 0 * kAdcClockDivider;
117
118 static constexpr int kHscAdder = 2 * kAdcClockDivider;
119
Brian Silvermana1d84822018-09-15 17:18:49 -0700120
Brian Silvermancabadaf2018-09-03 19:36:44 -0700121 static constexpr int kConversionTime =
122 kSfcAdder + 1 /* AverageNum */ * (kBct + kLstAdder + kHscAdder);
123
Brian Silvermana1d84822018-09-15 17:18:49 -0700124 // Sampling takes 8 ADCK cycles according to
125 // "33.4.4.5 Sample time and total conversion time". This means we want 0
126 // (the start of the cycle) to be 4 ADCK cycles into the second of our four
127 // samples.
128 const int delay_before_cycle =
129 (kConversionTime -
130 4 /* Clocks before middle of sample */ * kAdcClockDivider +
131 ftm_clock_divider - 1) /
132 ftm_clock_divider;
Brian Silvermancabadaf2018-09-03 19:36:44 -0700133 const int ftm_delay =
134 (kConversionTime * 2 /* 2 ADC samples */ + ftm_clock_divider - 1) /
135 ftm_clock_divider;
Brian Silvermana1d84822018-09-15 17:18:49 -0700136 *ftm_delays_[0] = counts_per_cycle_ - delay_before_cycle;
137 *ftm_delays_[1] = ftm_delay - delay_before_cycle;
Brian Silvermancabadaf2018-09-03 19:36:44 -0700138 }
139
140 InitializePdbChannel(&PDB0.CH[0]);
141 InitializePdbChannel(&PDB0.CH[1]);
142 PDB0.MOD = 1 /* Doesn't matter */;
143 PDB0.SC = pdb_sc(pdb_input_);
144
145 DMAMUX0.CHCFG[result_dma_channel(0)] = M_DMAMUX_ENBL | DMAMUX_SOURCE_ADC0;
146 DMAMUX0.CHCFG[result_dma_channel(1)] = 0;
147 DMAMUX0.CHCFG[reconfigure_dma_channel(0)] = 0;
148 DMAMUX0.CHCFG[reconfigure_dma_channel(1)] = 0;
149
150 static constexpr ssize_t kResultABOffset =
151 static_cast<ssize_t>(offsetof(KINETIS_ADC_t, RB)) -
152 static_cast<ssize_t>(offsetof(KINETIS_ADC_t, RA));
153 static_assert(kResultABOffset > 0, "Offset is backwards");
154 static constexpr ssize_t kResultOffsetBits =
155 ConstexprLog2(kResultABOffset * 2);
156 static constexpr ssize_t kSC1ABOffset =
157 static_cast<ssize_t>(offsetof(KINETIS_ADC_t, SC1B)) -
158 static_cast<ssize_t>(offsetof(KINETIS_ADC_t, SC1A));
159 static_assert(kSC1ABOffset > 0, "Offset is backwards");
160 static constexpr ssize_t kSC1OffsetBits = ConstexprLog2(kSC1ABOffset * 2);
161 for (int adc = 0; adc < 2; ++adc) {
162 // Make sure we can actually write to all the fields in the DMA channels.
163 DMA.CDNE = result_dma_channel(adc);
164 DMA.CDNE = reconfigure_dma_channel(adc);
165 DMA.CERQ = result_dma_channel(adc);
166 DMA.CERQ = reconfigure_dma_channel(adc);
167
168 ADC(adc)
169 ->SC2 |= ADC_SC2_ADTRG /* Use hardware triggering */ |
170 ADC_SC2_DMAEN /* Enable DMA triggers */;
171
172 int next_result_channel, next_reconfigure_channel;
173 if (adc == 0) {
174 next_result_channel = result_dma_channel(1);
175 next_reconfigure_channel = reconfigure_dma_channel(1);
176 } else {
177 next_result_channel = reconfigure_dma_channel(0);
178#if 0
179 // TODO(Brian): Use this once we're actually doing encoder captures.
180 next_reconfigure_channel = encoder_value_dma_channel();
181#else
182 next_reconfigure_channel = -1;
183#endif
184 }
185 result_dma(adc)->SOFF = kResultABOffset;
186 reconfigure_dma(adc)->SOFF = sizeof(uint32_t);
187 result_dma(adc)->SADDR = &ADC(adc)->RA;
188 reconfigure_dma(adc)->SADDR = &adc_sc1s_[adc][2];
189 result_dma(adc)->ATTR =
190 V_TCD_SMOD(kResultOffsetBits) | V_TCD_SSIZE(TCD_SIZE_16BIT) |
191 V_TCD_DMOD(0) /* No DMOD */ | V_TCD_DSIZE(TCD_SIZE_16BIT);
192 reconfigure_dma(adc)->ATTR =
193 V_TCD_SMOD(0) /* No SMOD */ | V_TCD_SSIZE(TCD_SIZE_32BIT) |
194 V_TCD_DMOD(kSC1OffsetBits) | V_TCD_DSIZE(TCD_SIZE_32BIT);
195 result_dma(adc)->NBYTES = sizeof(uint16_t);
196 reconfigure_dma(adc)->NBYTES = sizeof(uint32_t);
197 result_dma(adc)->SLAST = 0;
198 reconfigure_dma(adc)->SLAST = -(kNumberAdcSamples * sizeof(uint32_t));
199 result_dma(adc)->DADDR = &adc_results_[adc][0];
200 reconfigure_dma(adc)->DADDR = &ADC(adc)->SC1A;
201 result_dma(adc)->DOFF = sizeof(uint16_t);
202 reconfigure_dma(adc)->DOFF = kSC1ABOffset;
203 {
204 uint16_t link = 0;
205 if (next_result_channel != -1) {
206 link = M_TCD_ELINK | V_TCD_LINKCH(next_result_channel);
207 }
208 result_dma(adc)->CITER = result_dma(adc)->BITER =
209 link | 4 /* 4 minor iterations */;
210 }
211 {
212 uint16_t link = 0;
213 if (next_reconfigure_channel != -1) {
214 link = M_TCD_ELINK | V_TCD_LINKCH(next_reconfigure_channel);
215 }
216 reconfigure_dma(adc)->CITER = reconfigure_dma(adc)->BITER =
217 link | 4 /* 4 minor iterations */;
218 }
219 result_dma(adc)->DLASTSGA = -(kNumberAdcSamples * sizeof(uint16_t));
220 reconfigure_dma(adc)->DLASTSGA = 0;
221 {
222 uint16_t link = 0;
223 if (next_result_channel != -1) {
224 link = V_TCD_MAJORLINKCH(next_result_channel) | M_TCD_MAJORELINK;
225 }
226 result_dma(adc)->CSR =
227 V_TCD_BWC(0) /* No extra stalls */ | link |
228 M_TCD_DREQ /* Disable requests after major iteration */;
229 }
230 {
231 uint16_t link = 0;
232 if (next_reconfigure_channel != -1) {
233 link = V_TCD_MAJORLINKCH(next_reconfigure_channel) | M_TCD_MAJORELINK;
234 }
235 reconfigure_dma(adc)->CSR =
236 V_TCD_BWC(0) /* No extra stalls */ | link |
237 M_TCD_DREQ /* Disable requests after major iteration */ |
238 M_TCD_INTMAJOR;
239 }
240 }
241}
242
243void AdcDmaSampler::Reset() {
244 // Disable the PDB. This both prevents weird interference with what we're
245 // trying to do and is part of the process to clear its internal
246 // (unobservable) "locks". If we get out of sync, then the DMA won't read from
247 // the ADC so the ADC's COCO will stay asserted and the PDB will stay "locked"
248 // on that channel. Disabling then re-enabling the PDB is the easiest way to
249 // clear that. See "37.4.1 PDB pre-trigger and trigger outputs" in the
250 // reference manual for details.
251 PDB0.SC = 0;
252
253 // Set the initial ADC sample configs.
254 for (int adc = 0; adc < 2; ++adc) {
255 // Tell the DMA it should go again.
256 DMA.CDNE = result_dma_channel(adc);
257 DMA.CDNE = reconfigure_dma_channel(adc);
258
259 for (int i = 0; i < 2; ++i) {
260 ADC(adc)->SC1[i] = adc_sc1s_[adc][i];
261 }
262 }
263
264 // Re-enable the first DMA channel (which is the only one triggered by
265 // hardware, and disables itself once it's done).
266 DMA.SERQ = result_dma_channel(0);
267
268 // Now re-enable the PDB.
269 PDB0.SC = pdb_sc(pdb_input_);
270 // Both channels' S registers are now 0, empirically, regardless of what they
271 // were before. Reference manual isn't super clear what's supposed to happen.
272}
273
274void AdcDmaSampler::InitializePdbChannel(KINETIS_PDB_CHANNEL_t *channel) {
275 channel->C1 = V_PDB_BB(2) /* Back-to-back enabled for channel 1 */ |
276 V_PDB_TOS(2) /* Bypass 0, and 1 doesn't matter (?) */ |
277 V_PDB_EN(3) /* Enable our two */;
278}
279
280} // namespace teensy
281} // namespace frc971