blob: e59a96f5c13cd0345d0507cf61ce54e3b2d1d8ea [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
68AdcDmaSampler::AdcDmaSampler() {
69 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
120 static constexpr int kConversionTime =
121 kSfcAdder + 1 /* AverageNum */ * (kBct + kLstAdder + kHscAdder);
122
123 const int ftm_delay =
124 (kConversionTime * 2 /* 2 ADC samples */ + ftm_clock_divider - 1) /
125 ftm_clock_divider;
126 // TODO(Brian): Center on the start of the cycle instead. Need to think
127 // through how to get it started synced correctly though. Specifically,
128 // the second of the four samples should happen on the cycle boundary.
129 *ftm_delays_[0] = 0;
130 *ftm_delays_[1] = ftm_delay;
131 }
132
133 InitializePdbChannel(&PDB0.CH[0]);
134 InitializePdbChannel(&PDB0.CH[1]);
135 PDB0.MOD = 1 /* Doesn't matter */;
136 PDB0.SC = pdb_sc(pdb_input_);
137
138 DMAMUX0.CHCFG[result_dma_channel(0)] = M_DMAMUX_ENBL | DMAMUX_SOURCE_ADC0;
139 DMAMUX0.CHCFG[result_dma_channel(1)] = 0;
140 DMAMUX0.CHCFG[reconfigure_dma_channel(0)] = 0;
141 DMAMUX0.CHCFG[reconfigure_dma_channel(1)] = 0;
142
143 static constexpr ssize_t kResultABOffset =
144 static_cast<ssize_t>(offsetof(KINETIS_ADC_t, RB)) -
145 static_cast<ssize_t>(offsetof(KINETIS_ADC_t, RA));
146 static_assert(kResultABOffset > 0, "Offset is backwards");
147 static constexpr ssize_t kResultOffsetBits =
148 ConstexprLog2(kResultABOffset * 2);
149 static constexpr ssize_t kSC1ABOffset =
150 static_cast<ssize_t>(offsetof(KINETIS_ADC_t, SC1B)) -
151 static_cast<ssize_t>(offsetof(KINETIS_ADC_t, SC1A));
152 static_assert(kSC1ABOffset > 0, "Offset is backwards");
153 static constexpr ssize_t kSC1OffsetBits = ConstexprLog2(kSC1ABOffset * 2);
154 for (int adc = 0; adc < 2; ++adc) {
155 // Make sure we can actually write to all the fields in the DMA channels.
156 DMA.CDNE = result_dma_channel(adc);
157 DMA.CDNE = reconfigure_dma_channel(adc);
158 DMA.CERQ = result_dma_channel(adc);
159 DMA.CERQ = reconfigure_dma_channel(adc);
160
161 ADC(adc)
162 ->SC2 |= ADC_SC2_ADTRG /* Use hardware triggering */ |
163 ADC_SC2_DMAEN /* Enable DMA triggers */;
164
165 int next_result_channel, next_reconfigure_channel;
166 if (adc == 0) {
167 next_result_channel = result_dma_channel(1);
168 next_reconfigure_channel = reconfigure_dma_channel(1);
169 } else {
170 next_result_channel = reconfigure_dma_channel(0);
171#if 0
172 // TODO(Brian): Use this once we're actually doing encoder captures.
173 next_reconfigure_channel = encoder_value_dma_channel();
174#else
175 next_reconfigure_channel = -1;
176#endif
177 }
178 result_dma(adc)->SOFF = kResultABOffset;
179 reconfigure_dma(adc)->SOFF = sizeof(uint32_t);
180 result_dma(adc)->SADDR = &ADC(adc)->RA;
181 reconfigure_dma(adc)->SADDR = &adc_sc1s_[adc][2];
182 result_dma(adc)->ATTR =
183 V_TCD_SMOD(kResultOffsetBits) | V_TCD_SSIZE(TCD_SIZE_16BIT) |
184 V_TCD_DMOD(0) /* No DMOD */ | V_TCD_DSIZE(TCD_SIZE_16BIT);
185 reconfigure_dma(adc)->ATTR =
186 V_TCD_SMOD(0) /* No SMOD */ | V_TCD_SSIZE(TCD_SIZE_32BIT) |
187 V_TCD_DMOD(kSC1OffsetBits) | V_TCD_DSIZE(TCD_SIZE_32BIT);
188 result_dma(adc)->NBYTES = sizeof(uint16_t);
189 reconfigure_dma(adc)->NBYTES = sizeof(uint32_t);
190 result_dma(adc)->SLAST = 0;
191 reconfigure_dma(adc)->SLAST = -(kNumberAdcSamples * sizeof(uint32_t));
192 result_dma(adc)->DADDR = &adc_results_[adc][0];
193 reconfigure_dma(adc)->DADDR = &ADC(adc)->SC1A;
194 result_dma(adc)->DOFF = sizeof(uint16_t);
195 reconfigure_dma(adc)->DOFF = kSC1ABOffset;
196 {
197 uint16_t link = 0;
198 if (next_result_channel != -1) {
199 link = M_TCD_ELINK | V_TCD_LINKCH(next_result_channel);
200 }
201 result_dma(adc)->CITER = result_dma(adc)->BITER =
202 link | 4 /* 4 minor iterations */;
203 }
204 {
205 uint16_t link = 0;
206 if (next_reconfigure_channel != -1) {
207 link = M_TCD_ELINK | V_TCD_LINKCH(next_reconfigure_channel);
208 }
209 reconfigure_dma(adc)->CITER = reconfigure_dma(adc)->BITER =
210 link | 4 /* 4 minor iterations */;
211 }
212 result_dma(adc)->DLASTSGA = -(kNumberAdcSamples * sizeof(uint16_t));
213 reconfigure_dma(adc)->DLASTSGA = 0;
214 {
215 uint16_t link = 0;
216 if (next_result_channel != -1) {
217 link = V_TCD_MAJORLINKCH(next_result_channel) | M_TCD_MAJORELINK;
218 }
219 result_dma(adc)->CSR =
220 V_TCD_BWC(0) /* No extra stalls */ | link |
221 M_TCD_DREQ /* Disable requests after major iteration */;
222 }
223 {
224 uint16_t link = 0;
225 if (next_reconfigure_channel != -1) {
226 link = V_TCD_MAJORLINKCH(next_reconfigure_channel) | M_TCD_MAJORELINK;
227 }
228 reconfigure_dma(adc)->CSR =
229 V_TCD_BWC(0) /* No extra stalls */ | link |
230 M_TCD_DREQ /* Disable requests after major iteration */ |
231 M_TCD_INTMAJOR;
232 }
233 }
234}
235
236void AdcDmaSampler::Reset() {
237 // Disable the PDB. This both prevents weird interference with what we're
238 // trying to do and is part of the process to clear its internal
239 // (unobservable) "locks". If we get out of sync, then the DMA won't read from
240 // the ADC so the ADC's COCO will stay asserted and the PDB will stay "locked"
241 // on that channel. Disabling then re-enabling the PDB is the easiest way to
242 // clear that. See "37.4.1 PDB pre-trigger and trigger outputs" in the
243 // reference manual for details.
244 PDB0.SC = 0;
245
246 // Set the initial ADC sample configs.
247 for (int adc = 0; adc < 2; ++adc) {
248 // Tell the DMA it should go again.
249 DMA.CDNE = result_dma_channel(adc);
250 DMA.CDNE = reconfigure_dma_channel(adc);
251
252 for (int i = 0; i < 2; ++i) {
253 ADC(adc)->SC1[i] = adc_sc1s_[adc][i];
254 }
255 }
256
257 // Re-enable the first DMA channel (which is the only one triggered by
258 // hardware, and disables itself once it's done).
259 DMA.SERQ = result_dma_channel(0);
260
261 // Now re-enable the PDB.
262 PDB0.SC = pdb_sc(pdb_input_);
263 // Both channels' S registers are now 0, empirically, regardless of what they
264 // were before. Reference manual isn't super clear what's supposed to happen.
265}
266
267void AdcDmaSampler::InitializePdbChannel(KINETIS_PDB_CHANNEL_t *channel) {
268 channel->C1 = V_PDB_BB(2) /* Back-to-back enabled for channel 1 */ |
269 V_PDB_TOS(2) /* Bypass 0, and 1 doesn't matter (?) */ |
270 V_PDB_EN(3) /* Enable our two */;
271}
272
273} // namespace teensy
274} // namespace frc971