Skip to content

Commit

Permalink
feat(stm32g4xx): add irq lookup for AnalogToDigitalPinStm based on pe…
Browse files Browse the repository at this point in the history
…ripheral number (#398)

* feat(stm32g4xx): add irq lookup for AnalogToDigitalPinStm based on peripheral number

* Update hal_st/stm32fxxx/AnalogToDigitalPinStm.cpp

Co-authored-by: Richard Peters <richard.peters@philips.com>

* Update hal_st/stm32fxxx/AnalogToDigitalPinStm.cpp

* Apply suggestions from code review

* chore: fix stmf4xx compilation

---------

Co-authored-by: Richard Peters <richard.peters@philips.com>
  • Loading branch information
daantimmer and richardapeters committed Sep 4, 2024
1 parent ecfb4ce commit 7569d4a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
57 changes: 54 additions & 3 deletions hal_st/stm32fxxx/AnalogToDigitalPinStm.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#include "hal_st/stm32fxxx/AnalogToDigitalPinStm.hpp"
#include "generated/stm32fxxx/PeripheralTable.hpp"
#include "hal_st/cortex/InterruptCortex.hpp"
#include "hal_st/stm32fxxx/GpioStm.hpp"
#include "infra/event/EventDispatcher.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/MemoryRange.hpp"
#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdlib>

#include DEVICE_HEADER

#if defined(STM32F7)
extern "C"
{
Expand All @@ -11,8 +23,47 @@ extern "C"

namespace
{
constexpr uint32_t adcChannel[] = {
ADC_CHANNEL_0,
#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
#endif

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

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

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

IRQn_Type LookupIrq(std::size_t index)
{
auto iter = std::find_if(irqMap.begin(), irqMap.end(), [index](const auto& pair)
{
return pair.first == index;
});

if (iter == irqMap.end())
std::abort();

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,
Expand Down Expand Up @@ -126,7 +177,7 @@ namespace hal
#if defined(STM32WB) || defined(STM32G0)
, interruptHandler(ADC1_IRQn, [this]()
#elif defined(STM32G4)
, interruptHandler(ADC1_2_IRQn, [this]()
, interruptHandler(LookupIrq(oneBasedIndex), [this]()
#elif defined(STM32WBA)
, interruptHandler(ADC4_IRQn, [this]()
#elif defined(STM32H5)
Expand Down
7 changes: 6 additions & 1 deletion hal_st/stm32fxxx/AnalogToDigitalPinStm.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#ifndef HAL_ANALOG_TO_DIGITAL_PIN_STM_HPP
#define HAL_ANALOG_TO_DIGITAL_PIN_STM_HPP

#include DEVICE_HEADER
#include "hal/interfaces/AnalogToDigitalPin.hpp"
#include "hal_st/cortex/InterruptCortex.hpp"
#include "hal_st/stm32fxxx/GpioStm.hpp"
#include "infra/util/AutoResetFunction.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/MemoryRange.hpp"
#include <cstddef>
#include <cstdint>

#include DEVICE_HEADER

namespace hal
{
Expand Down

0 comments on commit 7569d4a

Please sign in to comment.