-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[stm32] H7 DMA and fixes for DAC DMA
- Loading branch information
Showing
18 changed files
with
454 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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 sinTable = computeSinTable(1.0f); | ||
|
||
// DAC1 channel 1 on GpioA4: constantly output sine signal in circular mode | ||
|
||
void setupDac() | ||
{ | ||
using Dac = DacDma; | ||
using DacChannel = Dac::Channel1<Dma1::Channel5>; | ||
|
||
Dac::connect<GpioOutputA4::Out1>(); | ||
Dac::initialize(); | ||
|
||
DacChannel::configure(sinTable.data(), sinTable.size(), DmaBase::CircularMode::Enabled); | ||
|
||
// trigger source 5: timer 4, see reference manual | ||
DacChannel::setTriggerSource(5); | ||
|
||
DacChannel::startDma(); | ||
DacChannel::enableDacChannel(); | ||
} | ||
|
||
int main() | ||
{ | ||
Board::initialize(); | ||
LedGreen::setOutput(); | ||
|
||
MODM_LOG_INFO << "DAC DMA Demo" << modm::endl; | ||
|
||
Dma1::enable(); | ||
setupDac(); | ||
|
||
// configure timer 4 as trigger for DACs | ||
// 90 MHz / 90 = 1 MHz => 1 Msps DAC output | ||
Timer4::enable(); | ||
Timer4::setMode(Timer4::Mode::UpCounter); | ||
Timer4::setPrescaler(1); | ||
Timer4::setOverflow(90 - 1); | ||
Timer4::applyAndReset(); | ||
Timer4::start(); | ||
|
||
// Enable trigger out for timer 4 | ||
TIM4->CR2 |= TIM_CR2_MMS_1; | ||
|
||
while (true) | ||
{ | ||
LedGreen::toggle(); | ||
modm::delay_ms(500); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<library> | ||
<extends>modm:nucleo-f446ze</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../build/nucleo_f446ze/dac_dma</option> | ||
</options> | ||
<modules> | ||
<module>modm:debug</module> | ||
<module>modm:platform:dac</module> | ||
<module>modm:platform:dma</module> | ||
<module>modm:platform:timer:4</module> | ||
<module>modm:build:scons</module> | ||
</modules> | ||
</library> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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 sinTable = computeSinTable(1.0f); | ||
|
||
// DAC1 channel 1 on GpioA4: constantly output sine signal in circular mode | ||
|
||
void setupDac() | ||
{ | ||
using Dac = Dac1Dma; | ||
using DacChannel = Dac::Channel1<Dma1::Channel1>; | ||
|
||
Dac::connect<GpioOutputA4::Out1>(); | ||
Dac::initialize(); | ||
|
||
DacChannel::configure(sinTable.data(), sinTable.size(), DmaBase::CircularMode::Enabled); | ||
|
||
// trigger source 3: timer 4, see reference manual | ||
DacChannel::setTriggerSource(3); | ||
|
||
DacChannel::startDma(); | ||
DacChannel::enableDacChannel(); | ||
} | ||
|
||
int main() | ||
{ | ||
Board::initialize(); | ||
LedGreen::setOutput(); | ||
|
||
MODM_LOG_INFO << "DAC DMA Demo" << modm::endl; | ||
|
||
Dma1::enable(); | ||
setupDac(); | ||
|
||
// configure timer 4 as trigger for DACs | ||
// 275 MHz / 275 = 1 MHz => 1 Msps DAC output | ||
Timer4::enable(); | ||
Timer4::setMode(Timer4::Mode::UpCounter); | ||
Timer4::setPrescaler(1); | ||
Timer4::setOverflow(275 - 1); | ||
Timer4::applyAndReset(); | ||
Timer4::start(); | ||
|
||
// Enable trigger out for timer 4 | ||
TIM4->CR2 |= TIM_CR2_MMS_1; | ||
|
||
while (true) | ||
{ | ||
LedGreen::toggle(); | ||
modm::delay_ms(500); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<library> | ||
<extends>modm:nucleo-h723zg</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../build/nucleo_h723zg/dac_dma</option> | ||
</options> | ||
<modules> | ||
<module>modm:debug</module> | ||
<module>modm:platform:dac:1</module> | ||
<module>modm:platform:dma</module> | ||
<module>modm:platform:timer:4</module> | ||
<module>modm:build:scons</module> | ||
</modules> | ||
</library> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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 sinTable = computeSinTable(1.0f); | ||
|
||
// DAC1 channel 1 on GpioA4: constantly output sine signal in circular mode | ||
|
||
void setupDac() | ||
{ | ||
using Dac = Dac1Dma; | ||
using DacChannel = Dac::Channel1<Dma1::Channel3>; | ||
|
||
Dac::connect<GpioOutputA4::Out1>(); | ||
Dac::initialize(); | ||
|
||
DacChannel::configure(sinTable.data(), sinTable.size(), DmaBase::CircularMode::Enabled); | ||
|
||
// trigger source 5: timer 4, see reference manual | ||
DacChannel::setTriggerSource(5); | ||
|
||
DacChannel::startDma(); | ||
DacChannel::enableDacChannel(); | ||
} | ||
|
||
int main() | ||
{ | ||
Board::initialize(); | ||
LedGreen::setOutput(); | ||
|
||
MODM_LOG_INFO << "DAC DMA Demo" << modm::endl; | ||
|
||
Dma1::enable(); | ||
setupDac(); | ||
|
||
// configure timer 4 as trigger for DACs | ||
// 48 MHz / 48 = 1 MHz => 1 Msps DAC output | ||
Timer4::enable(); | ||
Timer4::setMode(Timer4::Mode::UpCounter); | ||
Timer4::setPrescaler(1); | ||
Timer4::setOverflow(48 - 1); | ||
Timer4::applyAndReset(); | ||
Timer4::start(); | ||
|
||
// Enable trigger out for timer 4 | ||
TIM4->CR2 |= TIM_CR2_MMS_1; | ||
|
||
while (true) | ||
{ | ||
LedGreen::toggle(); | ||
modm::delay_ms(500); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<library> | ||
<extends>modm:nucleo-l476rg</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../build/nucleo_l476rg/dac_dma</option> | ||
</options> | ||
<modules> | ||
<module>modm:debug</module> | ||
<module>modm:platform:dac:1</module> | ||
<module>modm:platform:dma</module> | ||
<module>modm:platform:timer:4</module> | ||
<module>modm:build:scons</module> | ||
</modules> | ||
</library> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* 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 sinTable = computeSinTable(1.0f); | ||
|
||
// DAC1 channel 1 on GpioA4: constantly output sine signal in circular mode | ||
|
||
void setupDac() | ||
{ | ||
using Dac = DacDma; | ||
using DacChannel = Dac::Channel1<Dma1::Channel5>; | ||
|
||
Dac::connect<GpioOutputA4::Out1>(); | ||
Dac::initialize(); | ||
|
||
DacChannel::configure(sinTable.data(), sinTable.size(), DmaBase::CircularMode::Enabled); | ||
|
||
// trigger source 5: timer 4, see reference manual | ||
DacChannel::setTriggerSource(5); | ||
|
||
DacChannel::startDma(); | ||
DacChannel::enableDacChannel(); | ||
} | ||
|
||
int main() | ||
{ | ||
Board::initialize(); | ||
LedJ5::setOutput(); | ||
LedJ13::setOutput(true); | ||
|
||
MODM_LOG_INFO << "DAC DMA Demo" << modm::endl; | ||
|
||
Dma1::enable(); | ||
setupDac(); | ||
|
||
// configure timer 4 as trigger for DACs | ||
// 108 MHz / 108 = 1 MHz => 1 Msps DAC output | ||
Timer4::enable(); | ||
Timer4::setMode(Timer4::Mode::UpCounter); | ||
Timer4::setPrescaler(1); | ||
Timer4::setOverflow(108 - 1); | ||
Timer4::applyAndReset(); | ||
Timer4::start(); | ||
|
||
// Enable trigger out for timer 4 | ||
TIM4->CR2 |= TIM_CR2_MMS_1; | ||
|
||
//GpioOutputA4::setOutput(true); | ||
while (true) | ||
{ | ||
LedJ5::toggle(); | ||
LedJ13::toggle(); | ||
modm::delay_ms(500); | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.