@@ -211,7 +211,6 @@ modm::platform::Adc{{ id }}::configureChannel(Channel channel,
211211
212212}
213213
214-
215214bool
216215modm::platform::Adc{{ id }}::setChannel(Channel channel,
217216 SampleTime sampleTime)
@@ -224,6 +223,42 @@ modm::platform::Adc{{ id }}::setChannel(Channel channel,
224223 return true;
225224}
226225
226+ bool
227+ modm::platform::Adc{{ id }}::setChannelSequence(std::span<const SequenceChannel> sequence)
228+ {
229+ if (sequence.size() > 16 || sequence.size() == 0) {
230+ return false;
231+ }
232+
233+ ADC{{ id }}->SQR1 = sequence.size() - 1;
234+
235+ for (const auto [i, config] : modm::enumerate(sequence)) {
236+ const auto [channel, sampleTime] = config;
237+ if (!configureChannel(channel, sampleTime)) {
238+ return false;
239+ }
240+
241+ if (i < 4) {
242+ const auto shift = (i + 1) * 6;
243+ const auto mask = 0b111111u << shift;
244+ ADC{{ id }}->SQR1 = (ADC{{ id }}->SQR1 & ~mask) | (std::to_underlying(channel) << shift);
245+ } else if (i < 9) {
246+ const auto shift = (i - 4) * 6;
247+ const auto mask = 0b111111u << shift;
248+ ADC{{ id }}->SQR2 = (ADC{{ id }}->SQR2 & ~mask) | (std::to_underlying(channel) << shift);
249+ } else if (i < 14) {
250+ const auto shift = (i - 9) * 6;
251+ const auto mask = 0b111111u << shift;
252+ ADC{{ id }}->SQR3 = (ADC{{ id }}->SQR3 & ~mask) | (std::to_underlying(channel) << shift);
253+ } else {
254+ const auto shift = (i - 14) * 6;
255+ const auto mask = 0b111111u << shift;
256+ ADC{{ id }}->SQR4 = (ADC{{ id }}->SQR4 & ~mask) | (std::to_underlying(channel) << shift);
257+ }
258+ }
259+ return true;
260+ }
261+
227262void
228263modm::platform::Adc{{ id }}::setFreeRunningMode(const bool enable)
229264{
@@ -239,17 +274,34 @@ modm::platform::Adc{{ id }}::startConversion()
239274{
240275 // TODO: maybe add more interrupt flags
241276 acknowledgeInterruptFlags(InterruptFlag::EndOfRegularConversion |
242- InterruptFlag::EndOfSampling | InterruptFlag::Overrun);
277+ InterruptFlag::EndOfSampling | InterruptFlag::Overrun |
278+ InterruptFlag::EndOfRegularSequenceOfConversions);
243279 // starts single conversion for the regular group
244280 ADC{{ id }}->CR |= ADC_CR_ADSTART;
245281}
246282
283+ void
284+ modm::platform::Adc{{ id }}::stopConversion()
285+ {
286+ ADC{{ id }}->CR |= ADC_CR_ADSTP | ADC_CR_JADSTP;
287+ while ((ADC{{ id }}->CR & (ADC_CR_ADSTP | ADC_CR_JADSTP)) != 0);
288+
289+ acknowledgeInterruptFlags(InterruptFlag::EndOfRegularConversion |
290+ InterruptFlag::EndOfSampling | InterruptFlag::Overrun);
291+ }
292+
247293bool
248294modm::platform::Adc{{ id }}::isConversionFinished()
249295{
250296 return static_cast<bool>(getInterruptFlags() & InterruptFlag::EndOfRegularConversion);
251297}
252298
299+ bool
300+ modm::platform::Adc{{ id }}::isConversionSequenceFinished()
301+ {
302+ return static_cast<bool>(getInterruptFlags() & InterruptFlag::EndOfRegularSequenceOfConversions);
303+ }
304+
253305void
254306modm::platform::Adc{{ id }}::startInjectedConversionSequence()
255307{
0 commit comments