Skip to content

Commit 080c495

Browse files
committed
add full api for settig edge and source of the trigger from regular and injected, added set offset api
1 parent 464c35f commit 080c495

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

src/modm/platform/adc/stm32f3/adc.hpp.in

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ public:
309309
%% endif
310310
};
311311

312+
enum class ExternalTriggerPolarity
313+
{
314+
NoTriggerDetection = 0x0u,
315+
RisingEdge = 0x1u,
316+
FallingEdge = 0x2u,
317+
RisingAndFallingEdge = 0x3u,
318+
};
319+
312320
enum class Interrupt : uint32_t
313321
{
314322
Ready = ADC_IER_ADRDYIE,
@@ -342,6 +350,57 @@ public:
342350
};
343351
MODM_FLAGS32(InterruptFlag);
344352

353+
/**
354+
* Enum mapping all events on a external trigger converter.
355+
* The source mapped to each event varies on controller family,
356+
* refer to the ADC external trigger section on reference manual
357+
* of your controller for more information
358+
*/
359+
enum class RegularConversionExternalTrigger
360+
{
361+
Event0 = 0x00u,
362+
Event1 = 0x01u,
363+
Event2 = 0x02u,
364+
Event3 = 0x03u,
365+
Event4 = 0x04u,
366+
Event5 = 0x05u,
367+
Event6 = 0x06u,
368+
Event7 = 0x07u,
369+
Event9 = 0x09u,
370+
Event10 = 0x0Au,
371+
Event11 = 0x0Bu,
372+
Event12 = 0x0Cu,
373+
Event13 = 0x0Du,
374+
Event14 = 0x0Eu,
375+
Event15 = 0x0Fu,
376+
%% if target["family"] in ["g4"]
377+
Event16 = 0x10u,
378+
Event17 = 0x11u,
379+
Event18 = 0x12u,
380+
Event19 = 0x13u,
381+
Event20 = 0x14u,
382+
Event21 = 0x15u,
383+
Event22 = 0x16u,
384+
Event23 = 0x17u,
385+
Event24 = 0x18u,
386+
Event25 = 0x19u,
387+
Event26 = 0x1Au,
388+
Event27 = 0x1Bu,
389+
Event28 = 0x1Cu,
390+
Event29 = 0x1Du,
391+
Event30 = 0x1Eu,
392+
Event31 = 0x1Fu,
393+
%% endif
394+
};
395+
396+
enum class OffsetSlot : uint8_t
397+
{
398+
Slot0 = 0,
399+
Slot1 = 1,
400+
Slot2 = 2,
401+
Slot3 = 3,
402+
};
403+
345404
public:
346405
template< class... Signals >
347406
static void
@@ -499,6 +558,18 @@ public:
499558
static inline void
500559
stopConversion();
501560

561+
/**
562+
* enable regular conversions on external trigger.
563+
*
564+
* @param externalTriggerPolarity
565+
* Polarity of the external trigger signal.
566+
* @param regularConversionExternalTrigger
567+
* Regular conversion external trigger source.
568+
*/
569+
static inline void enableRegularConversionExternalTrigger(
570+
ExternalTriggerPolarity externalTriggerPolarity,
571+
RegularConversionExternalTrigger regularConversionExternalTrigger);
572+
502573
/**
503574
* @return If the conversion is finished.
504575
* @pre A conversion should have been started with startConversion()
@@ -539,6 +610,18 @@ public:
539610
static inline bool
540611
setInjectedConversionSequenceLength(uint8_t length);
541612

613+
/**
614+
* enable injected conversions on external trigger.
615+
*
616+
* @param externalTriggerPolarity
617+
* Polarity of the external trigger signal.
618+
* @param regularConversionExternalTrigger
619+
* Regular conversion external trigger source.
620+
*/
621+
static inline void enableInjectedConversionExternalTrigger(
622+
ExternalTriggerPolarity externalTriggerPolarity,
623+
RegularConversionExternalTrigger regularConversionExternalTrigger);
624+
542625
/**
543626
* @return If the injected conversion sequence is finished.
544627
* @pre An injected conversion should have been started with startInjectedConversionSequence()
@@ -585,6 +668,25 @@ public:
585668
}
586669
}
587670

671+
/**
672+
* @arg slot for the offset register (0..3)
673+
* @arg channel channel to which the offset is applied
674+
* @arg offset offset value to be applied to the channel
675+
*/
676+
static inline bool
677+
setChannelOffset(OffsetSlot slot, Channel channel, int16_t offset, bool saturate = false, bool enable = true);
678+
679+
/**
680+
* @arg slot for the offset register (0..3)
681+
* @return offset value applied to the channel
682+
*/
683+
template<class Gpio>
684+
static inline bool
685+
setChannelOffset(OffsetSlot slot, int16_t offset, bool saturate = false, bool enable = true)
686+
{
687+
return setChannelOffset(slot, getPinChannel<Gpio>(), offset, saturate, enable);
688+
}
689+
588690
static inline void
589691
enableInterruptVector(const uint32_t priority, const bool enable = true);
590692

src/modm/platform/adc/stm32f3/adc_impl.hpp.in

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,17 @@ modm::platform::Adc{{ id }}::isConversionSequenceFinished()
302302
return static_cast<bool>(getInterruptFlags() & InterruptFlag::EndOfRegularSequenceOfConversions);
303303
}
304304

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+
305316
void
306317
modm::platform::Adc{{ id }}::startInjectedConversionSequence()
307318
{
@@ -351,6 +362,19 @@ modm::platform::Adc{{ id }}::setInjectedConversionSequenceLength(uint8_t length)
351362
return true;
352363
}
353364

365+
void
366+
modm::platform::Adc{{ id }}::enableInjectedConversionExternalTrigger(
367+
ExternalTriggerPolarity externalTriggerPolarity,
368+
RegularConversionExternalTrigger regularConversionExternalTrigger)
369+
{
370+
const auto polarity = (static_cast<uint32_t>(externalTriggerPolarity) << ADC_JSQR_JEXTEN_Pos);
371+
const auto externalTrigger =
372+
(static_cast<uint32_t>(regularConversionExternalTrigger) << ADC_JSQR_JEXTSEL_Pos);
373+
const auto mask = ADC_JSQR_JEXTEN_Msk | ADC_JSQR_JEXTSEL_Msk;
374+
ADC{{ id }}->JSQR = (ADC{{ id }}->JSQR & ~mask) | polarity | externalTrigger;
375+
}
376+
377+
354378
bool
355379
modm::platform::Adc{{ id }}::isInjectedConversionFinished()
356380
{
@@ -410,3 +434,38 @@ modm::platform::Adc{{ id }}::acknowledgeInterruptFlags(const InterruptFlag_t fla
410434
// Writing a zero is ignored.
411435
ADC{{ id }}->ISR = flags.value;
412436
}
437+
438+
/**
439+
* @arg slot for the offset register (0..3)
440+
* @arg channel channel to which the offset is applied
441+
* @arg offset offset value to be applied to the channel
442+
* @return true if configuration is successful, false if the ADC is currently converting
443+
*/
444+
bool
445+
modm::platform::Adc{{ id }}::setChannelOffset(OffsetSlot slot, Channel channel, int16_t offset,
446+
bool saturate, bool enable)
447+
{
448+
if ( (ADC{{ id }}->CR & ADC_CR_JADSTART) || (ADC{{ id }}->CR & ADC_CR_ADSTART) ) {
449+
// ADC is currently converting, cannot set offset
450+
return false;
451+
}
452+
453+
const uint32_t channelMask = (static_cast<uint32_t>(channel) << ADC_OFR1_OFFSET1_CH_Pos) & ADC_OFR1_OFFSET1_CH_Msk;
454+
const uint32_t offsetMask = (std::abs(offset) << ADC_OFR1_OFFSET1_Pos) & ADC_OFR1_OFFSET1_Msk;
455+
const uint32_t saturateMask = saturate ? ADC_OFR1_SATEN : 0u;
456+
const uint32_t enableMask = enable ? ADC_OFR1_OFFSET1_EN : 0u;
457+
const uint32_t signMask = (offset > 0) ? ADC_OFR1_OFFSETPOS : 0u;
458+
const uint32_t offsetValue = channelMask | offsetMask | saturateMask | enableMask | signMask;
459+
460+
switch (slot)
461+
{
462+
case OffsetSlot::Slot0: ADC{{id}}->OFR1 = offsetValue; break;
463+
case OffsetSlot::Slot1: ADC{{id}}->OFR2 = offsetValue; break;
464+
case OffsetSlot::Slot2: ADC{{id}}->OFR3 = offsetValue; break;
465+
case OffsetSlot::Slot3: ADC{{id}}->OFR4 = offsetValue; break;
466+
default:
467+
return false; // invalid slot
468+
}
469+
470+
return true;
471+
}

0 commit comments

Comments
 (0)