Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STM32G0/G4 DMA + more devices + DAC DMA #632

Merged
merged 9 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,17 @@ Please [discover modm's peripheral drivers for your specific device][discover].
<td align="center">○</td>
</tr><tr>
<td align="left">DAC</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">✅</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">○</td>
<td align="center">✗</td>
<td align="center">○</td>
Expand All @@ -191,8 +191,8 @@ Please [discover modm's peripheral drivers for your specific device][discover].
<td align="center">✅</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center"></td>
<td align="center"></td>
<td align="center"></td>
<td align="center"></td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
Expand Down
65 changes: 65 additions & 0 deletions examples/nucleo_f446ze/dac_basic/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2021, 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 <modm/board.hpp>

#include <numbers>
#include <cmath>
#include <array>

using namespace Board;

constexpr auto computeSinTable()
{
std::array<uint16_t, 100> data{};
constexpr auto HalfOutput = ((1 << 12) - 1) / 2; // 12 bit dac
for (size_t i = 0; i < data.size(); ++i) {
constexpr auto pi = std::numbers::pi_v<float>;
data[i] = HalfOutput * (1 + sin(i * (2*pi / data.size())));
}
return data;
}

constexpr auto sinTable = computeSinTable();

int
main()
{
Board::initialize();
LedBlue::setOutput();

MODM_LOG_INFO << "DAC Demo" << modm::endl;

// Output ~8 kHz sine / cosine waveforms on gpio A4/A5
Dac::connect<GpioA4::Out1, GpioA5::Out2>();
Dac::initialize();

Dac::enableChannel(Dac::Channel::Channel1);
Dac::enableChannel(Dac::Channel::Channel2);
Dac::enableOutputBuffer(Dac::Channel::Channel1, true);
Dac::enableOutputBuffer(Dac::Channel::Channel2, true);

uint32_t counter = 0;
while (true)
{
constexpr auto size = sinTable.size();
const auto sinIndex = counter % size;
const auto cosIndex = (counter + (size/4)) % size;
counter++;

Dac::setOutput1(sinTable[sinIndex]);
Dac::setOutput2(sinTable[cosIndex]);

LedBlue::toggle();
modm::delay_us(1);
}

return 0;
}
10 changes: 10 additions & 0 deletions examples/nucleo_f446ze/dac_basic/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:nucleo-f446ze</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_f446ze/dac_basic</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:dac</module>
</modules>
</library>
60 changes: 60 additions & 0 deletions examples/nucleo_g474re/dac_basic/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2021, 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 <modm/board.hpp>

#include <numbers>
#include <cmath>
#include <array>

using namespace Board;

constexpr auto computeSinTable()
{
std::array<uint16_t, 100> data{};
constexpr auto HalfOutput = ((1 << 12) - 1) / 2; // 12 bit dac
for (size_t i = 0; i < data.size(); ++i) {
constexpr auto pi = std::numbers::pi_v<float>;
data[i] = HalfOutput * (1 + sin(i * (2*pi / data.size())));
}
return data;
}

constexpr auto sinTable = computeSinTable();

int
main()
{
Board::initialize();
LedD13::setOutput();

MODM_LOG_INFO << "DAC Demo" << modm::endl;

// Output ~8 kHz sine waveform on gpio A6
Dac2::connect<GpioA6::Out1>();
Dac2::initialize<Board::SystemClock>();

Dac2::setMode(Dac2::Channel::Channel1, Dac2::Mode::ExternalWithBuffer);
Dac2::enableChannel(Dac2::Channel::Channel1);

uint32_t counter = 0;
while (true)
{
constexpr auto size = sinTable.size();
const auto index = counter++ % size;

Dac2::setOutput1(sinTable[index]);

LedD13::toggle();
modm::delay_us(1);
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/nucleo_g474re/dac_basic/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library>
<extends>modm:nucleo-g474re</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_g474re/dac_basic</option>
</options>
<modules>
<module>modm:debug</module>
<module>modm:platform:dac:2</module>
<module>modm:build:scons</module>
</modules>
</library>
123 changes: 123 additions & 0 deletions examples/nucleo_g474re/dac_dma/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2021, 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 <modm/board.hpp>

#include <numbers>
#include <cmath>
#include <array>

using namespace Board;

constexpr auto computeSinTable(float frequency = 1.f)
{
std::array<uint16_t, 100> data{};
constexpr auto HalfOutput = ((1 << 12) - 1) / 2; // 12 bit dac
for (size_t i = 0; i < data.size(); ++i) {
constexpr auto pi = std::numbers::pi_v<float>;
data[i] = HalfOutput * (1 + sin(i * (2 * pi * frequency / data.size())));
}
return data;
}

constexpr auto sinTable1 = computeSinTable(1.0f);
constexpr auto sinTable2 = computeSinTable(2.0f);

// DAC1 channel 1 on GpioA4: constantly output 20kHz sine signal in circular mode
// DAC4 channel 1 on GpioB12: switch between 20kHz and 40 kHz sine signal after DMA transfer complete

void setupDac1()
{
using Dac = Dac1Dma;
using DacChannel = Dac::Channel1<Dma1::Channel1>;

Dac::connect<GpioOutputA4::Out1>();
Dac::initialize<Board::SystemClock>();

DacChannel::configure(sinTable1.data(), sinTable1.size(), DmaBase::CircularMode::Enabled);

// trigger source 5: timer 4 for DAC4, see reference manual
DacChannel::setTriggerSource(5);

DacChannel::startDma();
DacChannel::enableDacChannel();
}

void setupDac4()
{
using Dac = Dac4Dma;
using DacChannel = Dac::Channel1<Dma1::Channel2>;

Dac::initialize<Board::SystemClock>();

// Internal output to opamp 4
DacChannel::setMode(DacChannel::Mode::Internal);

DacChannel::configure(sinTable1.data(), sinTable1.size(), DmaBase::CircularMode::Disabled);

// trigger source 5: timer 4 for DAC4, see reference manual
DacChannel::setTriggerSource(5);

// switch between signals when transfer completed
static bool toggleBit = false;
Dma1::Channel1::setTransferCompleteIrqHandler([]() {
DacChannel::stopDma();
toggleBit = !toggleBit;
if (toggleBit) {
DacChannel::setData(sinTable1.data(), sinTable1.size());
} else {
DacChannel::setData(sinTable2.data(), sinTable2.size());
}
DacChannel::startDma();
});

Dma1::Channel1::enableInterruptVector();
Dma1::Channel1::enableInterrupt(Dma1::Interrupt::TransferComplete | Dma1::Interrupt::Error);

// DAC4 channel 1 output via opamp 4 in follower mode connected to output B12
GpioOutputB12::setAnalogInput();
OPAMP4->CSR = OPAMP_CSR_OPAMPxEN | OPAMP_CSR_VPSEL | OPAMP_CSR_VMSEL;

DacChannel::startDma();
DacChannel::enableDacChannel();
}

int main()
{
Board::initialize();
LedD13::setOutput();

MODM_LOG_INFO << "DAC DMA Demo" << modm::endl;

Dma1::enable();

setupDac1();
setupDac4();

// configure timer 4 as trigger for DACs
// 170 MHz / 85 = 2 MHz => 2 Msps DAC output
Timer4::enable();
Timer4::setMode(Timer4::Mode::UpCounter);
Timer4::setPrescaler(1);
Timer4::setOverflow(85 - 1);
Timer4::applyAndReset();
Timer4::start();

// Enable trigger out for timer 4
TIM4->CR2 |= TIM_CR2_MMS_1;

while (true)
{
LedD13::toggle();
modm::delay_ms(500);
}

return 0;
}
14 changes: 14 additions & 0 deletions examples/nucleo_g474re/dac_dma/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<library>
<extends>modm:nucleo-g474re</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_g474re/dac_dma</option>
</options>
<modules>
<module>modm:debug</module>
<module>modm:platform:dac:1</module>
<module>modm:platform:dac:4</module>
<module>modm:platform:dma</module>
<module>modm:platform:timer:4</module>
<module>modm:build:scons</module>
</modules>
</library>
11 changes: 10 additions & 1 deletion src/modm/platform/clock/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ def build(env):
all_peripherals.update(dname + translate(i) for i in d["instance"])
else:
all_peripherals.add(dname)
all_peripherals = sorted(list(all_peripherals))
if dname == "Dma" and d["type"] == "stm32-mux":
all_peripherals.add("Dmamux1")

all_peripherals = sorted(list(all_peripherals))
rcc_map = env.query(":cmsis:device:rcc-map")
rcc_enable = {}
rcc_reset = {}
Expand All @@ -103,6 +105,13 @@ def build(env):
if "Fdcan1" in all_peripherals and per == "FDCAN":
per = "FDCAN1"
nper = "FDCAN"
# DAC
if "Dac1" in all_peripherals and per == "DAC":
per = "DAC1"
nper = "DAC"
if "Dac" in all_peripherals and per == "DAC1":
per = "DAC"
nper = "DAC1"
# Fix ADC vs ADC1
if "Adc1" in all_peripherals and per == "ADC":
per = "ADC1"
Expand Down
Loading