Skip to content

Commit

Permalink
add external triggers for regular conversions in adc.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
victorandrehc committed Mar 17, 2023
1 parent b8e772c commit 1372cd3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 30 deletions.
67 changes: 50 additions & 17 deletions src/modm/platform/adc/stm32/adc.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,42 @@ public:
%% endif
};

%% if target["family"] != "f1"
enum class ExternalTriggerPolarity
{
NoTriggerDetection = 0x0u,
RisingEdge = 0x1u,
FallingEdge = 0x2u,
RisingAndFallingEdge = 0x3u,
};
%%endif

/**
* Enum mapping all events on a external trigger converter.
* The source mapped to each event varies on controller family,
* refer to the ADC external trigger section on reference manual
* of your controller for more information
*/
enum class RegularConversionExternalTrigger
{
Event0 = 0x0u,
Event1 = 0x1u,
Event2 = 0x2u,
Event3 = 0x3u,
Event4 = 0x4u,
Event5 = 0x5u,
Event6 = 0x6u,
Event7 = 0x7u,
Event8 = 0x8u,
Event9 = 0x9u,
Event10 = 0xAu,
Event11 = 0xBu,
Event12 = 0xCu,
Event13 = 0xDu,
Event14 = 0xEu,
Event15 = 0xFu,
};

/**
* Possible interrupts.
*
Expand Down Expand Up @@ -174,15 +210,6 @@ public:
%% endif
};
MODM_FLAGS32(InterruptFlag);
%% if target["family"] not in ["f1","f3"]
enum class EndOfConversionFlag
{
///EOC bit is set at the end of the sequence of regular conversions
SequenceConversions = 0u,
///EOC bit is set at the end of each regular conversion
EveryConversion = 1u,
};
%%endif

public:
// start inherited documentation
Expand Down Expand Up @@ -386,6 +413,20 @@ public:
static inline uintptr_t
getDataRegisterPointer();

%% if target["family"] in ["f1","f3"]
static inline void
enableRegularConversionExternalTrigger(
RegularConversionExternalTrigger regularConversionExternalTrigger);

%%endif
%%if target["family"] not in ["f1","f3"]
static inline void
enableRegularConversionExternalTrigger(
ExternalTriggerPolarity externalTriggerPolarity,
RegularConversionExternalTrigger regularConversionExternalTrigger);

%%endif

%% if target["family"] not in ["f3"]
/**
* Enable Dma mode for the ADC
Expand Down Expand Up @@ -423,14 +464,6 @@ public:
static inline void
disableDMASelection();

/**
* set end of conversion selection
* @param endOfConversionFlag Flag stating the desired behavior
* for the end of selection flag
*/
static inline void
setEndOfConversionSelection(EndOfConversionFlag endOfConversionFlag);

%%endif
/**
* Enables scan mode
Expand Down
32 changes: 19 additions & 13 deletions src/modm/platform/adc/stm32/adc_impl.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@ modm::platform::Adc{{ id }}::getDataRegisterPointer()
{
return (uintptr_t) &(ADC{{ per }}->DR);
}
%% if target["family"] in ["f1","f3"]
void
modm::platform::Adc{{ id }}::enableRegularConversionExternalTrigger(
RegularConversionExternalTrigger regularConversionExternalTrigger)
{
const auto externalTrigger = (static_cast<uint32_t>(regularConversionExternalTrigger) << ADC_CR2_EXTSEL_Pos);
ADC{{ per }}->CR2 |= externalTrigger;
}
%%else
void
modm::platform::Adc{{ id }}::enableRegularConversionExternalTrigger(
ExternalTriggerPolarity externalTriggerPolarity,
RegularConversionExternalTrigger regularConversionExternalTrigger)
{
const auto polarity = (static_cast<uint32_t>(externalTriggerPolarity) << ADC_CR2_EXTEN_Pos);
const auto externalTrigger = (static_cast<uint32_t>(regularConversionExternalTrigger) << ADC_CR2_EXTSEL_Pos);
ADC{{ per }}->CR2 |= ( polarity | externalTrigger);
}
%%endif

%% if target["family"] not in ["f3"]
void
Expand Down Expand Up @@ -292,19 +311,6 @@ modm::platform::Adc{{ id }}::disableDMASelection()
ADC{{ per }}->CR2 &= ~ADC_CR2_DDS;
}

void
modm::platform::Adc{{ id }}::setEndOfConversionSelection(EndOfConversionFlag endOfConversionFlag)
{
if (endOfConversionFlag == EndOfConversionFlag::EveryConversion)
{
ADC{{ per }}->CR2 |= ADC_CR2_EOCS;
}
else
{
ADC{{ per }}->CR2 &= ~ADC_CR2_EOCS;
}
}

%%endif

void
Expand Down

0 comments on commit 1372cd3

Please sign in to comment.