Skip to content

Commit

Permalink
feat: add support for internal ADC channels (#487)
Browse files Browse the repository at this point in the history
* Allow manual configuration of ADC channel

This allows usage of internal ADC channels.

* Add ADC IRQ lookup for STM32H5

* Update hal_st/stm32fxxx/GpioStm.cpp

---------

Co-authored-by: Richard Peters <[email protected]>
  • Loading branch information
fabiangottstein and richardapeters authored Jan 28, 2025
1 parent 1757a74 commit 30a6f4e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 38 deletions.
53 changes: 16 additions & 37 deletions hal_st/stm32fxxx/AnalogToDigitalPinStm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,34 @@ extern "C"

namespace
{
#if defined(STM32G4) || defined(STM32H5)
constexpr std::array irqMap{
#if defined(STM32G4)
constexpr std::array irqMap
{
#if defined(ADC1)
std::make_pair(1, IRQn_Type::ADC1_2_IRQn), // only ADC1 or ADC2 can be configured to use the single interrupt vector
#endif
#if defined(ADC2)
std::make_pair(2, IRQn_Type::ADC1_2_IRQn), // only ADC1 or ADC2 can be configured to use the single interrupt vector
std::make_pair(2, IRQn_Type::ADC1_2_IRQn), // only ADC1 or ADC2 can be configured to use the single interrupt vector
#endif
#else
#if defined(ADC1)
std::make_pair(1, IRQn_Type::ADC1_IRQn),
#endif
#if defined(ADC2)
std::make_pair(2, IRQn_Type::ADC2_IRQn),
#endif
#endif

#if defined(ADC3)
std::make_pair(3, IRQn_Type::ADC3_IRQn),
std::make_pair(3, IRQn_Type::ADC3_IRQn),
#endif

#if defined(ADC4)
std::make_pair(4, IRQn_Type::ADC4_IRQn),
std::make_pair(4, IRQn_Type::ADC4_IRQn),
#endif

#if defined(ADC5)
std::make_pair(5, IRQn_Type::ADC5_IRQn),
std::make_pair(5, IRQn_Type::ADC5_IRQn),
#endif
};

Expand All @@ -59,35 +67,6 @@ namespace
return iter->second;
}
#endif

constexpr std::array adcChannel{
// STM32F4x header defines ADC_CHANNEL_0 as 0x0u, all others are cast to uint32_t
// all other device headers are consistent with all their channel types
static_cast<decltype(ADC_CHANNEL_1)>(ADC_CHANNEL_0),
ADC_CHANNEL_1,
ADC_CHANNEL_2,
ADC_CHANNEL_3,
ADC_CHANNEL_4,
ADC_CHANNEL_5,
ADC_CHANNEL_6,
ADC_CHANNEL_7,
ADC_CHANNEL_8,
ADC_CHANNEL_9,
ADC_CHANNEL_10,
ADC_CHANNEL_11,
ADC_CHANNEL_12,
ADC_CHANNEL_13,
#ifdef ADC_CHANNEL_18
ADC_CHANNEL_14,
ADC_CHANNEL_15,
ADC_CHANNEL_16,
ADC_CHANNEL_17,
ADC_CHANNEL_18,
#endif
#ifdef ADC_CHANNEL_19
ADC_CHANNEL_19,
#endif
};
}

namespace hal
Expand Down Expand Up @@ -181,7 +160,7 @@ namespace hal
#elif defined(STM32WBA)
, interruptHandler(ADC4_IRQn, [this]()
#elif defined(STM32H5)
, interruptHandler(ADC2_IRQn, [this]()
, interruptHandler(LookupIrq(oneBasedIndex), [this]()
#else
, interruptHandler(ADC_IRQn, [this]()
#endif
Expand Down Expand Up @@ -241,7 +220,7 @@ namespace hal

uint32_t AdcStm::Channel(const hal::AnalogPinStm& pin) const
{
return adcChannel[pin.AdcChannel(index + 1)];
return pin.AdcChannel(index + 1);
}

ADC_HandleTypeDef& AdcStm::Handle()
Expand Down
38 changes: 37 additions & 1 deletion hal_st/stm32fxxx/GpioStm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ namespace hal
GPIO_MODE_AF_PP,
GPIO_MODE_AF_OD
};

constexpr std::array adcChannel{
// STM32F4x header defines ADC_CHANNEL_0 as 0x0u, all others are cast to uint32_t
// all other device headers are consistent with all their channel types
static_cast<decltype(ADC_CHANNEL_1)>(ADC_CHANNEL_0),
ADC_CHANNEL_1,
ADC_CHANNEL_2,
ADC_CHANNEL_3,
ADC_CHANNEL_4,
ADC_CHANNEL_5,
ADC_CHANNEL_6,
ADC_CHANNEL_7,
ADC_CHANNEL_8,
ADC_CHANNEL_9,
ADC_CHANNEL_10,
ADC_CHANNEL_11,
ADC_CHANNEL_12,
ADC_CHANNEL_13,
#ifdef ADC_CHANNEL_18
ADC_CHANNEL_14,
ADC_CHANNEL_15,
ADC_CHANNEL_16,
ADC_CHANNEL_17,
ADC_CHANNEL_18,
#endif
#ifdef ADC_CHANNEL_19
ADC_CHANNEL_19,
#endif
};
}

DummyPinStm dummyPinStm;
Expand Down Expand Up @@ -284,14 +313,21 @@ namespace hal
pin.ConfigAnalog();
}

AnalogPinStm::AnalogPinStm(uint32_t channel)
: pin(dummyPinStm)
, channel(channel)
{}

AnalogPinStm::~AnalogPinStm()
{
pin.ResetConfig();
}

uint32_t AnalogPinStm::AdcChannel(uint8_t adc) const
{
return pin.AdcChannel(adc);
if (channel != 0xffffffff)
return channel;
return adcChannel[pin.AdcChannel(adc)];
}

uint32_t AnalogPinStm::DacChannel(uint8_t dac) const
Expand Down
2 changes: 2 additions & 0 deletions hal_st/stm32fxxx/GpioStm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ namespace hal
{
public:
AnalogPinStm(GpioPinStm& pin);
AnalogPinStm(uint32_t channel);
AnalogPinStm(const AnalogPinStm& other) = delete;
AnalogPinStm& operator=(const AnalogPinStm& other) = delete;
~AnalogPinStm();
Expand All @@ -206,6 +207,7 @@ namespace hal

private:
GpioPinStm& pin;
uint32_t channel{ 0xffffffff };
};

class MultiGpioPinStm
Expand Down

0 comments on commit 30a6f4e

Please sign in to comment.