Skip to content

Commit

Permalink
[example] Adapt STM32G0 DMA ADC example to API change
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-durand committed Mar 17, 2024
1 parent bd095e6 commit d86f513
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
7 changes: 4 additions & 3 deletions examples/nucleo_g070rb/adc_dma/adc_dma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define EXAMPLE_ADCDMA_HPP

#include <modm/platform.hpp>
#include <span>

template<class Adc, class DmaChannel>
class AdcDma
Expand All @@ -31,7 +32,7 @@ class AdcDma
*/
template<class SystemClock>
static void
initialize(uintptr_t destination_ptr, size_t length,
initialize(std::span<uint16_t> buffer,
modm::platform::DmaBase::Priority priority = modm::platform::DmaBase::Priority::Low,
modm::platform::DmaBase::CircularMode circularMode =
modm::platform::DmaBase::CircularMode::Enabled,
Expand All @@ -46,8 +47,8 @@ class AdcDma
modm::platform::DmaBase::MemoryIncrementMode::Increment,
modm::platform::DmaBase::PeripheralIncrementMode::Fixed, priority, circularMode);
Dma::AdcChannel::setPeripheralAddress(Adc::getDataRegisterAddress());
Dma::AdcChannel::setDataLength(length);
Dma::AdcChannel::setMemoryAddress(destination_ptr);
Dma::AdcChannel::setDataLength(buffer.size());
Dma::AdcChannel::setMemoryAddress(reinterpret_cast<uintptr_t>(buffer.data()));

setTransferErrorCallback(transferErrorCallback);
setHalfCompletedConversionCallback(halfCompletedCallback);
Expand Down
15 changes: 5 additions & 10 deletions examples/nucleo_g070rb/adc_dma/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ using namespace Board;
using namespace modm::platform;
using Adc1Dma = AdcDma<Adc1, Dma1::Channel1>;

std::array<uint16_t, 100> adc_results;
std::array<uint16_t, 100> adc_results{};
volatile bool dma_completed = false;

void
Expand All @@ -49,26 +49,21 @@ main()
LedD13::setOutput();
LedD13::reset();

adc_results.fill(0);

Adc1::connect<GpioInputA0::In0>();
Adc1::connect<GpioInputA1::In1>();

const auto a0_channel = Adc1::getPinChannel<GpioInputA0>();
const auto a1_channel = Adc1::getPinChannel<GpioInputA1>();
Adc1::setSampleTime(Adc1::SampleTime::Cycles3_5);
Adc1::initialize<SystemClock, Adc1::ClockMode::Asynchronous>();
Adc1::enableScanMode();
modm::delay(500ms);
// On STM32G0 Event1 means TIM1's channel 4 capture and compare event.
// Each controller has a different trigger mapping, check the reference
// manual for more information on the trigger mapping of your controller.
Adc1::enableRegularConversionExternalTrigger(Adc1::ExternalTriggerPolarity::RisingEdge,
Adc1::RegularConversionExternalTrigger::Event1);
Adc1::addChannel(a0_channel);
Adc1::addChannel(a1_channel);

Adc1::setChannels(Adc1::channelSequenceFromPins<GpioInputA0, GpioInputA1>());
Dma1::enable();
Adc1Dma::initialize<SystemClock>((uintptr_t)(&adc_results[0]), adc_results.size());
Adc1Dma::initialize<SystemClock>(adc_results);
Adc1Dma::setCompletedConversionCallback(completedCallback);
Adc1Dma::startDma();
Adc1::startConversion();
Expand All @@ -83,7 +78,7 @@ main()
dma_completed = false;
MODM_LOG_INFO << "Measurements"
<< "\r" << modm::endl;
for (const uint16_t& sample : adc_results) { MODM_LOG_INFO << sample << ", "; }
for (uint16_t sample : adc_results) { MODM_LOG_INFO << sample << ", "; }
MODM_LOG_INFO << "\r" << modm::endl;
adc_results.fill(0);
timerStart<Timer1>();
Expand Down

0 comments on commit d86f513

Please sign in to comment.