Skip to content

Commit 7d35f6b

Browse files
committed
[example] Add STM32G4 ADC example with conversion sequence and DMA
1 parent c99e134 commit 7d35f6b

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2021, Raphael Lehmann
3+
* Copyright (c) 2024, Christopher Durand
4+
*
5+
* This file is part of the modm project.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
10+
*/
11+
12+
#include <modm/board.hpp>
13+
#include <array>
14+
15+
int main()
16+
{
17+
Board::initialize();
18+
19+
MODM_LOG_INFO << "STM32G4 ADC regular conversion sequence example" << modm::endl;
20+
21+
// max. ADC clock for STM32G474: 60 MHz
22+
// 170 MHz AHB clock / 4 = 42.5 MHz
23+
Adc1::initialize(
24+
Adc1::ClockMode::SynchronousPrescaler4,
25+
Adc1::ClockSource::SystemClock,
26+
Adc1::Prescaler::Disabled,
27+
Adc1::CalibrationMode::SingleEndedInputsMode,
28+
true);
29+
30+
Adc1::connect<Board::A0::In1, Board::A1::In2, Board::A3::In15, Board::A4::In7>();
31+
32+
constexpr auto sampleTime = Adc1::SampleTime::Cycles641;
33+
constexpr std::array<Adc1::SequenceChannel, 5> sequence{{
34+
// Perform dummy conversion (ADC errata 2.7.7 and 2.7.8)
35+
{Adc1::getPinChannel<Board::A0::In1>(), sampleTime},
36+
// Read A0, A1, A3, A4
37+
{Adc1::getPinChannel<Board::A0::In1>(), sampleTime},
38+
{Adc1::getPinChannel<Board::A1::In2>(), sampleTime},
39+
{Adc1::getPinChannel<Board::A3::In15>(), sampleTime},
40+
{Adc1::getPinChannel<Board::A4::In7>(), sampleTime}
41+
}};
42+
Adc1::setChannelSequence(sequence);
43+
Adc1::setDmaMode(Adc1::DmaMode::OneShot);
44+
45+
Dma1::enable();
46+
using DmaChannel = Dma1::Channel1;
47+
DmaChannel::configure(Dma1::DataTransferDirection::PeripheralToMemory, Dma1::MemoryDataSize::Bit16,
48+
Dma1::PeripheralDataSize::Bit16, Dma1::MemoryIncrementMode::Increment,
49+
Dma1::PeripheralIncrementMode::Fixed);
50+
DmaChannel::setPeripheralAddress(reinterpret_cast<uintptr_t>(Adc1::dataRegister()));
51+
constexpr auto request = DmaChannel::RequestMapping<Peripheral::Adc1>::Request;
52+
DmaChannel::setPeripheralRequest<request>();
53+
54+
std::array<uint16_t, 5> samples{};
55+
56+
while (true)
57+
{
58+
DmaChannel::setMemoryAddress(reinterpret_cast<uintptr_t>(samples.data()));
59+
DmaChannel::setDataLength(samples.size());
60+
DmaChannel::start();
61+
Adc1::startConversion();
62+
63+
while (!(DmaChannel::getInterruptFlags() & Dma1::InterruptFlags::TransferComplete));
64+
DmaChannel::stop();
65+
66+
const double factor = 3.3 / 4095;
67+
MODM_LOG_INFO.printf("A0: %1.3f V\n", samples[1] * factor);
68+
MODM_LOG_INFO.printf("A1: %1.3f V\n", samples[2] * factor);
69+
MODM_LOG_INFO.printf("A3: %1.3f V\n", samples[3] * factor);
70+
MODM_LOG_INFO.printf("A4: %1.3f V\n", samples[4] * factor);
71+
MODM_LOG_INFO << '\n';
72+
modm::delay(500ms);
73+
}
74+
75+
return 0;
76+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<library>
2+
<extends>modm:nucleo-g474re</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_g474re/adc_sequence_dma</option>
5+
</options>
6+
<modules>
7+
<module>modm:debug</module>
8+
<module>modm:platform:adc:1</module>
9+
<module>modm:platform:dma</module>
10+
<module>modm:build:scons</module>
11+
</modules>
12+
</library>

0 commit comments

Comments
 (0)