@@ -302,6 +302,25 @@ modm::platform::Adc{{ id }}::isConversionSequenceFinished()
302
302
return static_cast<bool>(getInterruptFlags() & InterruptFlag::EndOfRegularSequenceOfConversions);
303
303
}
304
304
305
+ void
306
+ modm::platform::Adc{{ id }}::enableRegularConversionExternalTrigger(
307
+ ExternalTriggerPolarity externalTriggerPolarity,
308
+ RegularConversionExternalTrigger regularConversionExternalTrigger)
309
+ {
310
+ const auto polarity = (static_cast<uint32_t>(externalTriggerPolarity) << ADC_CFGR_EXTEN_Pos);
311
+ const auto externalTrigger = (static_cast<uint32_t>(regularConversionExternalTrigger) << ADC_CFGR_EXTSEL_Pos);
312
+ const auto mask = ADC_CFGR_EXTEN_Msk | ADC_CFGR_EXTSEL_Msk;
313
+ ADC{{ id }}->CFGR = (ADC{{ id }}->CFGR & ~mask) | polarity | externalTrigger;
314
+ }
315
+
316
+ void
317
+ modm::platform::Adc{{ id }}::disableRegularConversionExternalTrigger()
318
+ {
319
+ // Disable regular conversions external trigger by clearing the bits
320
+ // for polarity and external trigger source.
321
+ ADC{{ id }}->CFGR &= ~(ADC_CFGR_EXTEN_Msk | ADC_CFGR_EXTSEL_Msk);
322
+ }
323
+
305
324
void
306
325
modm::platform::Adc{{ id }}::startInjectedConversionSequence()
307
326
{
@@ -351,6 +370,26 @@ modm::platform::Adc{{ id }}::setInjectedConversionSequenceLength(uint8_t length)
351
370
return true;
352
371
}
353
372
373
+ void
374
+ modm::platform::Adc{{ id }}::enableInjectedConversionExternalTrigger(
375
+ ExternalTriggerPolarity externalTriggerPolarity,
376
+ RegularConversionExternalTrigger regularConversionExternalTrigger)
377
+ {
378
+ const auto polarity = (static_cast<uint32_t>(externalTriggerPolarity) << ADC_JSQR_JEXTEN_Pos);
379
+ const auto externalTrigger =
380
+ (static_cast<uint32_t>(regularConversionExternalTrigger) << ADC_JSQR_JEXTSEL_Pos);
381
+ const auto mask = ADC_JSQR_JEXTEN_Msk | ADC_JSQR_JEXTSEL_Msk;
382
+ ADC{{ id }}->JSQR = (ADC{{ id }}->JSQR & ~mask) | polarity | externalTrigger;
383
+ }
384
+
385
+ void
386
+ modm::platform::Adc{{ id }}::disableInjectedConversionExternalTrigger()
387
+ {
388
+ // Disable injected conversions external trigger by clearing the bits
389
+ // for polarity and external trigger source.
390
+ ADC{{id}}->JSQR &= ~(ADC_JSQR_JEXTEN_Msk | ADC_JSQR_JEXTSEL_Msk);
391
+ }
392
+
354
393
bool
355
394
modm::platform::Adc{{ id }}::isInjectedConversionFinished()
356
395
{
@@ -410,3 +449,95 @@ modm::platform::Adc{{ id }}::acknowledgeInterruptFlags(const InterruptFlag_t fla
410
449
// Writing a zero is ignored.
411
450
ADC{{ id }}->ISR = flags.value;
412
451
}
452
+
453
+ bool
454
+ modm::platform::Adc{{ id }}::enableChannelOffset(const OffsetSlot slot, const Channel channel, const int16_t offset,
455
+ const bool saturate)
456
+ {
457
+ if ( (ADC{{ id }}->CR & ADC_CR_JADSTART) || (ADC{{ id }}->CR & ADC_CR_ADSTART) ) {
458
+ // ADC is currently converting, cannot set offset
459
+ return false;
460
+ }
461
+
462
+ %% if target["family"] in ["f3", "l4", "l5"] or (target["family"] in ["h7"] and target["name"][0] in ["4", "5", "a", "b"]):
463
+ // F3 or H74x/H75x does not support signed offsets, so signMask is always 0
464
+ if (offset < 0) {
465
+ return false; // F3 ADC offsets must be positive
466
+ }
467
+ const uint32_t signMask = 0u;
468
+
469
+ // F3 or H74x/H75x does not support saturation, so saturateMask is always 0
470
+ if (saturate) {
471
+ return false; // F3 ADC does not support saturation
472
+ }
473
+ const uint32_t saturateMask = 0u;
474
+
475
+ %% elif target["family"] in["h7"]
476
+ %# H7 names the bits differently, but the logic is the same
477
+ const uint32_t signMask = (offset > 0) ? ADC3_OFR1_OFFSETPOS : 0u;
478
+ const uint32_t saturateMask = saturate ? ADC3_OFR1_SATEN : 0u;
479
+ %% else
480
+ const uint32_t signMask = (offset > 0) ? ADC_OFR1_OFFSETPOS : 0u;
481
+ const uint32_t saturateMask = saturate ? ADC_OFR1_SATEN : 0u;
482
+ %% endif
483
+
484
+ %% if target["family"] in ["h7"]
485
+ %% if target["name"][0] in ["4" , "5", "a", "b"]
486
+ %# no specific enable bit for H74x/H75x, so always 0
487
+ const uint32_t enableMask = 0u;
488
+ %% else
489
+ %# H7 uses a different register bit names
490
+ const uint32_t enableMask = ADC3_OFR1_OFFSET1_EN;
491
+ %% endif
492
+ %% else
493
+ const uint32_t enableMask = ADC_OFR1_OFFSET1_EN;
494
+ %% endif
495
+ const uint32_t channelMask = (static_cast<uint32_t>(channel) << ADC_OFR1_OFFSET1_CH_Pos) & ADC_OFR1_OFFSET1_CH_Msk;
496
+ const uint32_t offsetMask = (std::abs(offset) << ADC_OFR1_OFFSET1_Pos) & ADC_OFR1_OFFSET1_Msk;
497
+
498
+ const uint32_t offsetValue = channelMask | offsetMask | saturateMask | enableMask | signMask;
499
+
500
+ switch (slot)
501
+ {
502
+ case OffsetSlot::Slot0: ADC{{id}}->OFR1 = offsetValue; break;
503
+ case OffsetSlot::Slot1: ADC{{id}}->OFR2 = offsetValue; break;
504
+ case OffsetSlot::Slot2: ADC{{id}}->OFR3 = offsetValue; break;
505
+ case OffsetSlot::Slot3: ADC{{id}}->OFR4 = offsetValue; break;
506
+ default:
507
+ return false; // invalid slot
508
+ }
509
+
510
+ return true;
511
+ }
512
+
513
+ bool
514
+ modm::platform::Adc{{ id }}::disableChannelOffset(const OffsetSlot slot)
515
+ {
516
+ if ( (ADC{{ id }}->CR & ADC_CR_JADSTART) || (ADC{{ id }}->CR & ADC_CR_ADSTART) ) {
517
+ // ADC is currently converting, cannot disable offset
518
+ return false;
519
+ }
520
+
521
+ %% if target["family"] in ["h7"]
522
+ %% if target["name"][0] in ["4" , "5", "a", "b"]
523
+ %# no specific enable bit for H74x/H75x, so always 0
524
+ const uint32_t enableMask = ADC_OFR1_OFFSET1_Msk;
525
+ %% else
526
+ %#H7 uses a different register bit names
527
+ const uint32_t enableMask = ADC3_OFR1_OFFSET1_EN;
528
+ %% endif
529
+ %% else
530
+ const uint32_t enableMask = ADC_OFR1_OFFSET1_EN;
531
+ %%endif
532
+
533
+ switch (slot)
534
+ {
535
+ case OffsetSlot::Slot0: ADC{{id}}->OFR1 &= ~enableMask; break;
536
+ case OffsetSlot::Slot1: ADC{{id}}->OFR2 &= ~enableMask; break;
537
+ case OffsetSlot::Slot2: ADC{{id}}->OFR3 &= ~enableMask; break;
538
+ case OffsetSlot::Slot3: ADC{{id}}->OFR4 &= ~enableMask; break;
539
+ default:
540
+ return false; // invalid slot
541
+ }
542
+ return true;
543
+ }
0 commit comments