diff --git a/examples/nucleo_g474re/adc_sequence_dma/main.cpp b/examples/nucleo_g474re/adc_sequence_dma/main.cpp new file mode 100644 index 0000000000..9a04683e9c --- /dev/null +++ b/examples/nucleo_g474re/adc_sequence_dma/main.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2021, Raphael Lehmann + * Copyright (c) 2024, Christopher Durand + * + * This file is part of the modm project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include + +int main() +{ + Board::initialize(); + + MODM_LOG_INFO << "STM32G4 ADC regular conversion sequence example" << modm::endl; + + // max. ADC clock for STM32G474: 60 MHz + // 170 MHz AHB clock / 4 = 42.5 MHz + Adc1::initialize( + Adc1::ClockMode::SynchronousPrescaler4, + Adc1::ClockSource::SystemClock, + Adc1::Prescaler::Disabled, + Adc1::CalibrationMode::SingleEndedInputsMode, + true); + + Adc1::connect(); + + constexpr auto sampleTime = Adc1::SampleTime::Cycles641; + constexpr std::array sequence{{ + // Perform dummy conversion (ADC errata 2.7.7 and 2.7.8) + {Adc1::getPinChannel(), sampleTime}, + // Read A0, A1, A3, A4 + {Adc1::getPinChannel(), sampleTime}, + {Adc1::getPinChannel(), sampleTime}, + {Adc1::getPinChannel(), sampleTime}, + {Adc1::getPinChannel(), sampleTime} + }}; + Adc1::setChannelSequence(sequence); + Adc1::setDmaMode(Adc1::DmaMode::OneShot); + + Dma1::enable(); + using DmaChannel = Dma1::Channel1; + DmaChannel::configure(Dma1::DataTransferDirection::PeripheralToMemory, Dma1::MemoryDataSize::Bit16, + Dma1::PeripheralDataSize::Bit16, Dma1::MemoryIncrementMode::Increment, + Dma1::PeripheralIncrementMode::Fixed); + DmaChannel::setPeripheralAddress(reinterpret_cast(Adc1::dataRegister())); + constexpr auto request = DmaChannel::RequestMapping::Request; + DmaChannel::setPeripheralRequest(); + + std::array samples{}; + + while (true) + { + DmaChannel::setMemoryAddress(reinterpret_cast(samples.data())); + DmaChannel::setDataLength(samples.size()); + DmaChannel::start(); + Adc1::startConversion(); + + while (!(DmaChannel::getInterruptFlags() & Dma1::InterruptFlags::TransferComplete)); + DmaChannel::stop(); + + const double factor = 3.3 / 4095; + MODM_LOG_INFO.printf("A0: %1.3f V\n", samples[1] * factor); + MODM_LOG_INFO.printf("A1: %1.3f V\n", samples[2] * factor); + MODM_LOG_INFO.printf("A3: %1.3f V\n", samples[3] * factor); + MODM_LOG_INFO.printf("A4: %1.3f V\n", samples[4] * factor); + MODM_LOG_INFO << '\n'; + modm::delay(500ms); + } + + return 0; +} diff --git a/examples/nucleo_g474re/adc_sequence_dma/project.xml b/examples/nucleo_g474re/adc_sequence_dma/project.xml new file mode 100644 index 0000000000..35d6e6add3 --- /dev/null +++ b/examples/nucleo_g474re/adc_sequence_dma/project.xml @@ -0,0 +1,12 @@ + + modm:nucleo-g474re + + + + + modm:debug + modm:platform:adc:1 + modm:platform:dma + modm:build:scons + +