blob: 15d50ebcaadb983ed61ceb9ae0c9ba71b1d9e593 [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
James Kuszmaulef420da2023-12-27 12:02:15 -0800150 static constexpr long kResultABOffset =
151 static_cast<long>(offsetof(KINETIS_ADC_t, RB)) -
152 static_cast<long>(offsetof(KINETIS_ADC_t, RA));
Brian Silvermancabadaf2018-09-03 19:36:44 -0700153 static_assert(kResultABOffset > 0, "Offset is backwards");
James Kuszmaulef420da2023-12-27 12:02:15 -0800154 static constexpr long kResultOffsetBits = ConstexprLog2(kResultABOffset * 2);
155 static constexpr long kSC1ABOffset =
156 static_cast<long>(offsetof(KINETIS_ADC_t, SC1B)) -
157 static_cast<long>(offsetof(KINETIS_ADC_t, SC1A));
Brian Silvermancabadaf2018-09-03 19:36:44 -0700158 static_assert(kSC1ABOffset > 0, "Offset is backwards");
James Kuszmaulef420da2023-12-27 12:02:15 -0800159 static constexpr long kSC1OffsetBits = ConstexprLog2(kSC1ABOffset * 2);
Brian Silvermancabadaf2018-09-03 19:36:44 -0700160 for (int adc = 0; adc < 2; ++adc) {
161 // Make sure we can actually write to all the fields in the DMA channels.
162 DMA.CDNE = result_dma_channel(adc);
163 DMA.CDNE = reconfigure_dma_channel(adc);
164 DMA.CERQ = result_dma_channel(adc);
165 DMA.CERQ = reconfigure_dma_channel(adc);
166
Philipp Schrader790cb542023-07-05 21:06:52 -0700167 ADC(adc)->SC2 |= ADC_SC2_ADTRG /* Use hardware triggering */ |
168 ADC_SC2_DMAEN /* Enable DMA triggers */;
Brian Silvermancabadaf2018-09-03 19:36:44 -0700169
170 int next_result_channel, next_reconfigure_channel;
171 if (adc == 0) {
172 next_result_channel = result_dma_channel(1);
173 next_reconfigure_channel = reconfigure_dma_channel(1);
174 } else {
175 next_result_channel = reconfigure_dma_channel(0);
176#if 0
177 // TODO(Brian): Use this once we're actually doing encoder captures.
178 next_reconfigure_channel = encoder_value_dma_channel();
179#else
180 next_reconfigure_channel = -1;
181#endif
182 }
183 result_dma(adc)->SOFF = kResultABOffset;
184 reconfigure_dma(adc)->SOFF = sizeof(uint32_t);
185 result_dma(adc)->SADDR = &ADC(adc)->RA;
186 reconfigure_dma(adc)->SADDR = &adc_sc1s_[adc][2];
187 result_dma(adc)->ATTR =
188 V_TCD_SMOD(kResultOffsetBits) | V_TCD_SSIZE(TCD_SIZE_16BIT) |
189 V_TCD_DMOD(0) /* No DMOD */ | V_TCD_DSIZE(TCD_SIZE_16BIT);
190 reconfigure_dma(adc)->ATTR =
191 V_TCD_SMOD(0) /* No SMOD */ | V_TCD_SSIZE(TCD_SIZE_32BIT) |
192 V_TCD_DMOD(kSC1OffsetBits) | V_TCD_DSIZE(TCD_SIZE_32BIT);
193 result_dma(adc)->NBYTES = sizeof(uint16_t);
194 reconfigure_dma(adc)->NBYTES = sizeof(uint32_t);
195 result_dma(adc)->SLAST = 0;
196 reconfigure_dma(adc)->SLAST = -(kNumberAdcSamples * sizeof(uint32_t));
197 result_dma(adc)->DADDR = &adc_results_[adc][0];
198 reconfigure_dma(adc)->DADDR = &ADC(adc)->SC1A;
199 result_dma(adc)->DOFF = sizeof(uint16_t);
200 reconfigure_dma(adc)->DOFF = kSC1ABOffset;
201 {
202 uint16_t link = 0;
203 if (next_result_channel != -1) {
204 link = M_TCD_ELINK | V_TCD_LINKCH(next_result_channel);
205 }
James Kuszmaulef420da2023-12-27 12:02:15 -0800206 result_dma(adc)->CITER = link | 4 /* 4 minor iterations */;
207 result_dma(adc)->BITER = link | 4 /* 4 minor iterations */;
Brian Silvermancabadaf2018-09-03 19:36:44 -0700208 }
209 {
210 uint16_t link = 0;
211 if (next_reconfigure_channel != -1) {
212 link = M_TCD_ELINK | V_TCD_LINKCH(next_reconfigure_channel);
213 }
James Kuszmaulef420da2023-12-27 12:02:15 -0800214 reconfigure_dma(adc)->CITER = link | 4 /* 4 minor iterations */;
215 reconfigure_dma(adc)->BITER = link | 4 /* 4 minor iterations */;
Brian Silvermancabadaf2018-09-03 19:36:44 -0700216 }
217 result_dma(adc)->DLASTSGA = -(kNumberAdcSamples * sizeof(uint16_t));
218 reconfigure_dma(adc)->DLASTSGA = 0;
219 {
220 uint16_t link = 0;
221 if (next_result_channel != -1) {
222 link = V_TCD_MAJORLINKCH(next_result_channel) | M_TCD_MAJORELINK;
223 }
224 result_dma(adc)->CSR =
225 V_TCD_BWC(0) /* No extra stalls */ | link |
226 M_TCD_DREQ /* Disable requests after major iteration */;
227 }
228 {
229 uint16_t link = 0;
230 if (next_reconfigure_channel != -1) {
231 link = V_TCD_MAJORLINKCH(next_reconfigure_channel) | M_TCD_MAJORELINK;
232 }
233 reconfigure_dma(adc)->CSR =
234 V_TCD_BWC(0) /* No extra stalls */ | link |
235 M_TCD_DREQ /* Disable requests after major iteration */ |
236 M_TCD_INTMAJOR;
237 }
238 }
239}
240
241void AdcDmaSampler::Reset() {
242 // Disable the PDB. This both prevents weird interference with what we're
243 // trying to do and is part of the process to clear its internal
244 // (unobservable) "locks". If we get out of sync, then the DMA won't read from
245 // the ADC so the ADC's COCO will stay asserted and the PDB will stay "locked"
246 // on that channel. Disabling then re-enabling the PDB is the easiest way to
247 // clear that. See "37.4.1 PDB pre-trigger and trigger outputs" in the
248 // reference manual for details.
249 PDB0.SC = 0;
250
251 // Set the initial ADC sample configs.
252 for (int adc = 0; adc < 2; ++adc) {
253 // Tell the DMA it should go again.
254 DMA.CDNE = result_dma_channel(adc);
255 DMA.CDNE = reconfigure_dma_channel(adc);
256
257 for (int i = 0; i < 2; ++i) {
258 ADC(adc)->SC1[i] = adc_sc1s_[adc][i];
259 }
260 }
261
262 // Re-enable the first DMA channel (which is the only one triggered by
263 // hardware, and disables itself once it's done).
264 DMA.SERQ = result_dma_channel(0);
265
266 // Now re-enable the PDB.
267 PDB0.SC = pdb_sc(pdb_input_);
268 // Both channels' S registers are now 0, empirically, regardless of what they
269 // were before. Reference manual isn't super clear what's supposed to happen.
270}
271
272void AdcDmaSampler::InitializePdbChannel(KINETIS_PDB_CHANNEL_t *channel) {
273 channel->C1 = V_PDB_BB(2) /* Back-to-back enabled for channel 1 */ |
274 V_PDB_TOS(2) /* Bypass 0, and 1 doesn't matter (?) */ |
275 V_PDB_EN(3) /* Enable our two */;
276}
277
278} // namespace teensy
279} // namespace frc971