blob: d2dc6c93da04f2cc10d58d049830cfb330f36a71 [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
Philipp Schrader790cb542023-07-05 21:06:52 -070068AdcDmaSampler::AdcDmaSampler(int counts_per_cycle)
69 : counts_per_cycle_(counts_per_cycle) {
Brian Silvermancabadaf2018-09-03 19:36:44 -070070 for (int adc = 0; adc < 2; ++adc) {
71 for (int i = 0; i < 2; ++i) {
72 adc_sc1s_[adc][kNumberAdcSamples + i] = ADC_SC1_ADCH(0x1f);
73 }
74 }
75}
76
77void AdcDmaSampler::set_adc0_samples(
78 const ::std::array<uint32_t, kNumberAdcSamples> &adc0_samples) {
79 for (int i = 0; i < kNumberAdcSamples; ++i) {
80 adc_sc1s_[0][i] = adc0_samples[i] | ADC_SC1_AIEN;
81 }
82}
83
84void AdcDmaSampler::set_adc1_samples(
85 const ::std::array<uint32_t, kNumberAdcSamples> &adc1_samples) {
86 for (int i = 0; i < kNumberAdcSamples; ++i) {
87 adc_sc1s_[1][i] = adc1_samples[i] | ADC_SC1_AIEN;
88 }
89}
90
91void AdcDmaSampler::Initialize() {
92 // TODO(Brian): Put this somewhere better?
93 SIM_SCGC6 |= SIM_SCGC6_DMAMUX | SIM_SCGC6_PDB;
94
95 assert(ftm_delays_[0] != nullptr);
96 assert(ftm_delays_[1] != nullptr);
97 {
98 // See "Figure 33-92. Conversion time equation" for details on this math.
99 // All the math is in bus clock cycles, until being divided into FTM
100 // clock cycles at the end.
101
102 // Divider from bus clock to FTM clock.
103 // TODO(Brian): Make it so this can actually change.
104 const int ftm_clock_divider = 1;
105
106 // Divider from bus clock to ADC clock.
107 // TODO(Brian): Make sure this stays in sync with what adc.cc actually
108 // configures.
109 static constexpr int kAdcClockDivider = 4;
110
111 static constexpr int kSfcAdder = 5 * kAdcClockDivider + 5;
112
113 // 12-bit single-ended is only 20, but waiting a bit too long for some of
114 // the samples doesn't hurt anything.
115 static constexpr int kBct = 30 /* 13-bit differential */ * kAdcClockDivider;
116
117 static constexpr int kLstAdder = 0 * kAdcClockDivider;
118
119 static constexpr int kHscAdder = 2 * kAdcClockDivider;
120
121 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
Philipp Schrader790cb542023-07-05 21:06:52 -0700168 ADC(adc)->SC2 |= ADC_SC2_ADTRG /* Use hardware triggering */ |
169 ADC_SC2_DMAEN /* Enable DMA triggers */;
Brian Silvermancabadaf2018-09-03 19:36:44 -0700170
171 int next_result_channel, next_reconfigure_channel;
172 if (adc == 0) {
173 next_result_channel = result_dma_channel(1);
174 next_reconfigure_channel = reconfigure_dma_channel(1);
175 } else {
176 next_result_channel = reconfigure_dma_channel(0);
177#if 0
178 // TODO(Brian): Use this once we're actually doing encoder captures.
179 next_reconfigure_channel = encoder_value_dma_channel();
180#else
181 next_reconfigure_channel = -1;
182#endif
183 }
184 result_dma(adc)->SOFF = kResultABOffset;
185 reconfigure_dma(adc)->SOFF = sizeof(uint32_t);
186 result_dma(adc)->SADDR = &ADC(adc)->RA;
187 reconfigure_dma(adc)->SADDR = &adc_sc1s_[adc][2];
188 result_dma(adc)->ATTR =
189 V_TCD_SMOD(kResultOffsetBits) | V_TCD_SSIZE(TCD_SIZE_16BIT) |
190 V_TCD_DMOD(0) /* No DMOD */ | V_TCD_DSIZE(TCD_SIZE_16BIT);
191 reconfigure_dma(adc)->ATTR =
192 V_TCD_SMOD(0) /* No SMOD */ | V_TCD_SSIZE(TCD_SIZE_32BIT) |
193 V_TCD_DMOD(kSC1OffsetBits) | V_TCD_DSIZE(TCD_SIZE_32BIT);
194 result_dma(adc)->NBYTES = sizeof(uint16_t);
195 reconfigure_dma(adc)->NBYTES = sizeof(uint32_t);
196 result_dma(adc)->SLAST = 0;
197 reconfigure_dma(adc)->SLAST = -(kNumberAdcSamples * sizeof(uint32_t));
198 result_dma(adc)->DADDR = &adc_results_[adc][0];
199 reconfigure_dma(adc)->DADDR = &ADC(adc)->SC1A;
200 result_dma(adc)->DOFF = sizeof(uint16_t);
201 reconfigure_dma(adc)->DOFF = kSC1ABOffset;
202 {
203 uint16_t link = 0;
204 if (next_result_channel != -1) {
205 link = M_TCD_ELINK | V_TCD_LINKCH(next_result_channel);
206 }
207 result_dma(adc)->CITER = result_dma(adc)->BITER =
208 link | 4 /* 4 minor iterations */;
209 }
210 {
211 uint16_t link = 0;
212 if (next_reconfigure_channel != -1) {
213 link = M_TCD_ELINK | V_TCD_LINKCH(next_reconfigure_channel);
214 }
215 reconfigure_dma(adc)->CITER = reconfigure_dma(adc)->BITER =
216 link | 4 /* 4 minor iterations */;
217 }
218 result_dma(adc)->DLASTSGA = -(kNumberAdcSamples * sizeof(uint16_t));
219 reconfigure_dma(adc)->DLASTSGA = 0;
220 {
221 uint16_t link = 0;
222 if (next_result_channel != -1) {
223 link = V_TCD_MAJORLINKCH(next_result_channel) | M_TCD_MAJORELINK;
224 }
225 result_dma(adc)->CSR =
226 V_TCD_BWC(0) /* No extra stalls */ | link |
227 M_TCD_DREQ /* Disable requests after major iteration */;
228 }
229 {
230 uint16_t link = 0;
231 if (next_reconfigure_channel != -1) {
232 link = V_TCD_MAJORLINKCH(next_reconfigure_channel) | M_TCD_MAJORELINK;
233 }
234 reconfigure_dma(adc)->CSR =
235 V_TCD_BWC(0) /* No extra stalls */ | link |
236 M_TCD_DREQ /* Disable requests after major iteration */ |
237 M_TCD_INTMAJOR;
238 }
239 }
240}
241
242void AdcDmaSampler::Reset() {
243 // Disable the PDB. This both prevents weird interference with what we're
244 // trying to do and is part of the process to clear its internal
245 // (unobservable) "locks". If we get out of sync, then the DMA won't read from
246 // the ADC so the ADC's COCO will stay asserted and the PDB will stay "locked"
247 // on that channel. Disabling then re-enabling the PDB is the easiest way to
248 // clear that. See "37.4.1 PDB pre-trigger and trigger outputs" in the
249 // reference manual for details.
250 PDB0.SC = 0;
251
252 // Set the initial ADC sample configs.
253 for (int adc = 0; adc < 2; ++adc) {
254 // Tell the DMA it should go again.
255 DMA.CDNE = result_dma_channel(adc);
256 DMA.CDNE = reconfigure_dma_channel(adc);
257
258 for (int i = 0; i < 2; ++i) {
259 ADC(adc)->SC1[i] = adc_sc1s_[adc][i];
260 }
261 }
262
263 // Re-enable the first DMA channel (which is the only one triggered by
264 // hardware, and disables itself once it's done).
265 DMA.SERQ = result_dma_channel(0);
266
267 // Now re-enable the PDB.
268 PDB0.SC = pdb_sc(pdb_input_);
269 // Both channels' S registers are now 0, empirically, regardless of what they
270 // were before. Reference manual isn't super clear what's supposed to happen.
271}
272
273void AdcDmaSampler::InitializePdbChannel(KINETIS_PDB_CHANNEL_t *channel) {
274 channel->C1 = V_PDB_BB(2) /* Back-to-back enabled for channel 1 */ |
275 V_PDB_TOS(2) /* Bypass 0, and 1 doesn't matter (?) */ |
276 V_PDB_EN(3) /* Enable our two */;
277}
278
279} // namespace teensy
280} // namespace frc971