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

[sam] Add SAMx7x DMA + DAC #987

Merged
merged 7 commits into from
Apr 10, 2023
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ git clone --recurse-submodules --jobs 8 https://github.com/modm-io/modm.git

## Microcontrollers

modm can create a HAL for <!--allcount-->3607<!--/allcount--> devices of these vendors:
modm can create a HAL for <!--allcount-->3628<!--/allcount--> devices of these vendors:

- STMicroelectronics STM32: <!--stmcount-->2802<!--/stmcount--> devices.
- STMicroelectronics STM32: <!--stmcount-->2823<!--/stmcount--> devices.
- Microchip SAM: <!--samcount-->416<!--/samcount--> devices.
- Microchip AVR: <!--avrcount-->388<!--/avrcount--> devices.
- Raspberry Pi: <!--rpicount-->1<!--/rpicount--> device.
Expand Down Expand Up @@ -221,7 +221,7 @@ 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>
Expand All @@ -245,7 +245,7 @@ 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>
Expand Down
50 changes: 50 additions & 0 deletions examples/samv71_xplained_ultra/dac/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023, 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>

using namespace Board;

/**
* Simple DAC example
*
* Output sawtooth waveform in free-running mode.
*
* See DMA examples for use of DAC with DMA.
*/

using Out0 = GpioB13;

void waitForTxReady()
{
while (!(Dac::readInterruptFlags() & Dac::Interrupt::TxReadyCh0));
}

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

Dac::connect<Out0::Ch0>();
Dac::initialize<SystemClock, 12_MHz>();

Dac::enableChannel(Dac::Channel::Channel0);

while (true)
{
for (int i = 0; i <= 4095; ++i)
{
waitForTxReady();
Dac::setOutput0(i);
}
}

return 0;
}
10 changes: 10 additions & 0 deletions examples/samv71_xplained_ultra/dac/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:samv71-xplained-ultra</extends>
<options>
<option name="modm:build:build.path">../../../build/samv71_xplained_ultra/dac</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:dac</module>
</modules>
</library>
109 changes: 109 additions & 0 deletions examples/samv71_xplained_ultra/dma/block_transfer/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2023, 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 <cmath>
#include <numbers>

using namespace Board;

/*
* DMA block transfer example
*
* Output sine signal on DAC output 0
*/

constexpr auto computeSinTable(float frequency = 1.f)
{
std::array<uint16_t, 50> data{};
constexpr auto HalfOutput = Dac::MaxOutput / 2.f;
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 std::array<uint16_t, 50> sinTable = computeSinTable(1.0f);

using Out0 = GpioB13;

void initializeDac()
{
Dac::connect<Out0::Ch0>();
Dac::initialize<SystemClock, 12_MHz>();

// Max speed mode: DAC runs at DAC clock / 12 and triggers DMA
Dac::setChannelMode(Dac::Channel::Channel0, Dac::Mode::MaxSpeed);

Dac::enableChannel(Dac::Channel::Channel0);
}

BlockTransfer prepareTransfer()
{
BlockTransfer transfer;
transfer.setSourceAddress(sinTable.data());
transfer.setDestinationAddress(Dac::channel0DataRegister());
transfer.setDataLength(sinTable.size());

Dma::ChannelConfig_t config;
Dma::TransferType_t::set(config, Dma::TransferType::Peripheral);
Dma::PeripheralDirection_t::set(config, Dma::PeripheralDirection::MemoryToPeripheral);
// 16 bit data size
Dma::DataWidth_t::set(config, Dma::DataWidth::HalfWord);
// const data in flash, flash is connected to DMA AHB interface 1
Dma::SourceBusInterface_t::set(config, Dma::BusInterface::Bus1);
// peripherals are connected to DMA AHB interface 1
Dma::DestinationBusInterface_t::set(config, Dma::BusInterface::Bus1);
// Auto-increment source address after each sample
Dma::SourceAddressingMode_t::set(config, Dma::AddressingMode::Incrementing);
// Transfer is triggered by DAC channel 0 request
Dma::DmaRequest_t::set(config, DmaRequests::Dacc::Ch0Tx);
transfer.setConfiguration(config);

return transfer;
}

DmaChannel channel = Dma::channel(0);
const auto transfer = prepareTransfer();

int main()
{
Board::initialize();
initializeDac();

Dma::initialize();
const int priority = 5;
Dma::enableInterruptVector(priority);

channel.enableInterrupts();
channel.enableInterruptFlag(DmaChannel::Interrupt::EndOfBlock);

channel.setInterruptHandler([](auto flags) {
if (flags & DmaChannel::Interrupt::EndOfBlock) {
// transfer completed, restart
channel.startTransfer(transfer);
}
});

channel.startTransfer(transfer);

while (true)
{
Led0::toggle();
Led1::toggle();
modm::delay(500ms);

MODM_LOG_INFO << "ping" << modm::endl;
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/samv71_xplained_ultra/dma/block_transfer/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library>
<extends>modm:samv71-xplained-ultra</extends>
<options>
<option name="modm:build:build.path">../../../../build/samv71_xplained_ultra/dma_block_transfer</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:dac</module>
<module>modm:platform:dma</module>
</modules>
</library>
118 changes: 118 additions & 0 deletions examples/samv71_xplained_ultra/dma/linked_list_transfer/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (c) 2023, 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 <cmath>
#include <numbers>

using namespace Board;

/*
* DMA linked list transfer example
*
* Output two alternating signals on DAC output 0
*/

constexpr auto computeSinTable(float frequency = 1.f)
{
std::array<uint16_t, 50> data{};
constexpr auto HalfOutput = Dac::MaxOutput / 2.f;
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 std::array<uint16_t, 50> signal0 = computeSinTable(1.0f);
constexpr std::array<uint16_t, 50> signal1 = computeSinTable(2.0f);

using Out0 = GpioB13;

void initializeDac()
{
Dac::connect<Out0::Ch0>();
Dac::initialize<SystemClock, 12_MHz>();

// Max speed mode: DAC runs at DAC clock / 12 and triggers DMA
Dac::setChannelMode(Dac::Channel::Channel0, Dac::Mode::MaxSpeed);

Dac::enableChannel(Dac::Channel::Channel0);
}

using ListTransfer = LinkedListTransfer<Dma::View2, Dma::View0Src>;

ListTransfer prepareTransfer()
{
ListTransfer transfer;

// Transfer configuration, set by first descriptor.
// It is not overwritten by the second View0 descriptor.
Dma::ChannelConfig_t config;
Dma::TransferType_t::set(config, Dma::TransferType::Peripheral);
Dma::PeripheralDirection_t::set(config, Dma::PeripheralDirection::MemoryToPeripheral);
// 16 bit data size
Dma::DataWidth_t::set(config, Dma::DataWidth::HalfWord);
// const data in flash, flash is connected to DMA AHB interface 1
Dma::SourceBusInterface_t::set(config, Dma::BusInterface::Bus1);
// peripherals are connected to DMA AHB interface 1
Dma::DestinationBusInterface_t::set(config, Dma::BusInterface::Bus1);
// Auto-increment source address after each sample
Dma::SourceAddressingMode_t::set(config, Dma::AddressingMode::Incrementing);
// Transfer is triggered by DAC channel 0 request
Dma::DmaRequest_t::set(config, DmaRequests::Dacc::Ch0Tx);

auto [d0, d1] = transfer.descriptors();

// Configure first descriptor (type 2)
// Set: configuration, source, destination, size
d0.setConfiguration(config);
d0.setSourceAddress(signal0.data());
d0.setDestinationAddress(Dac::channel0DataRegister());
d0.setDataLength(signal0.size());

// Configure first descriptor (type 0)
// Update source and size, other parameters remain unchanged
d1.setSourceAddress(signal1.data());
d1.setDataLength(signal1.size());

// Circular mode
// After the second transfer has finished, the sequence automatically starts
// again with the first descriptor.
transfer.setCircularMode(true);

return transfer;
}

DmaChannel channel = Dma::channel(0);
// Transfer object must remain valid while transfer is active
auto transfer = prepareTransfer();

int main()
{
Board::initialize();
initializeDac();

Dma::initialize();

channel.startTransfer(transfer);

while (true)
{
Led0::toggle();
Led1::toggle();
modm::delay(500ms);

MODM_LOG_INFO << "ping" << modm::endl;
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library>
<extends>modm:samv71-xplained-ultra</extends>
<options>
<option name="modm:build:build.path">../../../../build/samv71_xplained_ultra/dma_linked_list_transfer</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:dac</module>
<module>modm:platform:dma</module>
</modules>
</library>
2 changes: 1 addition & 1 deletion ext/modm-devices
Submodule modm-devices updated 41 files
+4 −4 .github/workflows/tests.yml
+8 −7 README.md
+13 −1 devices/sam/samd10_d11.xml
+57 −1 devices/sam/samd21.xml
+89 −3 devices/sam/samd51_e51_e53_e54.xml
+24 −1 devices/sam/samda1.xml
+61 −1 devices/sam/same70_s70_v70_v71.xml
+25 −1 devices/sam/saml21.xml
+29 −1 devices/sam/saml22.xml
+844 −0 devices/stm32/stm32c0-11_31.xml
+0 −4 devices/stm32/stm32g0-71_81.xml
+16 −52 devices/stm32/stm32g0-b1_c1.xml
+0 −2 devices/stm32/stm32g4-74_84.xml
+1,093 −0 devices/stm32/stm32h5-03.xml
+2,768 −0 devices/stm32/stm32h5-62.xml
+4,193 −0 devices/stm32/stm32h5-63_73.xml
+21 −34 devices/stm32/stm32h7-23_33.xml
+24 −37 devices/stm32/stm32h7-25_35.xml
+21 −34 devices/stm32/stm32h7-30.xml
+5 −5 devices/stm32/stm32h7-42.xml
+5 −5 devices/stm32/stm32h7-43_53.xml
+5 −5 devices/stm32/stm32h7-45_55.xml
+5 −5 devices/stm32/stm32h7-47_57.xml
+5 −5 devices/stm32/stm32h7-50.xml
+2 −4 devices/stm32/stm32l4-31_33_43.xml
+12 −24 devices/stm32/stm32l5-52_62.xml
+2,505 −0 devices/stm32/stm32u5-35_45.xml
+393 −480 devices/stm32/stm32u5-75_85.xml
+24 −104 devices/stm32/stm32wb-10_15.xml
+84 −175 devices/stm32/stm32wb-35_55.xml
+1 −1 modm_devices/__init__.py
+4 −3 tools/generator/Makefile
+2 −2 tools/generator/dfg/input/cmsis_header.py
+24 −4 tools/generator/dfg/sam/sam_device_tree.py
+62 −7 tools/generator/dfg/stm32/stm.py
+1 −0 tools/generator/dfg/stm32/stm_device_tree.py
+1 −1 tools/generator/dfg/stm32/stm_dmamux_requests.py
+27 −0 tools/generator/dfg/stm32/stm_groups.py
+1 −1 tools/generator/dfg/stm32/stm_header.py
+1 −1 tools/generator/raw-data-extractor/extract-stm32.py
+1,176 −0 tools/generator/raw-data-extractor/patches/stm32.patch
2 changes: 1 addition & 1 deletion ext/st/stm32
Submodule stm32 updated 85 files
+6 −4 README.md
+24 −0 patches/c0.patch
+6,702 −0 stm32c0xx/Include/stm32c011xx.h
+6,863 −0 stm32c0xx/Include/stm32c031xx.h
+212 −0 stm32c0xx/Include/stm32c0xx.h
+105 −0 stm32c0xx/Include/system_stm32c0xx.h
+45 −0 stm32c0xx/Release_Notes.html
+15 −17 stm32f0xx/Include/stm32f030x6.h
+15 −17 stm32f0xx/Include/stm32f030x8.h
+16 −18 stm32f0xx/Include/stm32f030xc.h
+18 −20 stm32f0xx/Include/stm32f031x6.h
+14 −16 stm32f0xx/Include/stm32f038xx.h
+25 −25 stm32f0xx/Include/stm32f042x6.h
+25 −25 stm32f0xx/Include/stm32f048xx.h
+22 −22 stm32f0xx/Include/stm32f051x8.h
+22 −22 stm32f0xx/Include/stm32f058xx.h
+14 −16 stm32f0xx/Include/stm32f070x6.h
+18 −20 stm32f0xx/Include/stm32f070xb.h
+23 −23 stm32f0xx/Include/stm32f071xb.h
+29 −29 stm32f0xx/Include/stm32f072xb.h
+25 −25 stm32f0xx/Include/stm32f078xx.h
+23 −23 stm32f0xx/Include/stm32f091xc.h
+29 −29 stm32f0xx/Include/stm32f098xx.h
+11 −17 stm32f0xx/Include/stm32f0xx.h
+6 −8 stm32f0xx/Include/system_stm32f0xx.h
+99 −45 stm32f0xx/Release_Notes.html
+22 −21 stm32f3xx/Include/stm32f301x8.h
+18 −17 stm32f3xx/Include/stm32f302x8.h
+23 −22 stm32f3xx/Include/stm32f302xc.h
+22 −21 stm32f3xx/Include/stm32f302xe.h
+24 −23 stm32f3xx/Include/stm32f303x8.h
+21 −20 stm32f3xx/Include/stm32f303xc.h
+24 −23 stm32f3xx/Include/stm32f303xe.h
+22 −21 stm32f3xx/Include/stm32f318xx.h
+24 −23 stm32f3xx/Include/stm32f328xx.h
+24 −23 stm32f3xx/Include/stm32f334x8.h
+25 −24 stm32f3xx/Include/stm32f358xx.h
+14 −13 stm32f3xx/Include/stm32f373xc.h
+14 −13 stm32f3xx/Include/stm32f378xx.h
+24 −23 stm32f3xx/Include/stm32f398xx.h
+9 −15 stm32f3xx/Include/stm32f3xx.h
+7 −9 stm32f3xx/Include/system_stm32f3xx.h
+42 −26 stm32f3xx/Release_Notes.html
+68 −0 stm32h5xx/Include/partition_stm32h5xx.h
+14,038 −0 stm32h5xx/Include/stm32h503xx.h
+21,478 −0 stm32h5xx/Include/stm32h562xx.h
+23,614 −0 stm32h5xx/Include/stm32h563xx.h
+24,539 −0 stm32h5xx/Include/stm32h573xx.h
+238 −0 stm32h5xx/Include/stm32h5xx.h
+107 −0 stm32h5xx/Include/system_stm32h5xx.h
+47 −0 stm32h5xx/Release_Notes.html
+10 −10 stm32l0xx/Include/stm32l010x4.h
+10 −10 stm32l0xx/Include/stm32l010x6.h
+10 −10 stm32l0xx/Include/stm32l010x8.h
+10 −10 stm32l0xx/Include/stm32l010xb.h
+10 −10 stm32l0xx/Include/stm32l011xx.h
+10 −10 stm32l0xx/Include/stm32l021xx.h
+10 −10 stm32l0xx/Include/stm32l031xx.h
+10 −10 stm32l0xx/Include/stm32l041xx.h
+10 −10 stm32l0xx/Include/stm32l051xx.h
+10 −10 stm32l0xx/Include/stm32l052xx.h
+11 −11 stm32l0xx/Include/stm32l053xx.h
+10 −10 stm32l0xx/Include/stm32l062xx.h
+11 −11 stm32l0xx/Include/stm32l063xx.h
+10 −10 stm32l0xx/Include/stm32l071xx.h
+11 −11 stm32l0xx/Include/stm32l072xx.h
+12 −12 stm32l0xx/Include/stm32l073xx.h
+10 −10 stm32l0xx/Include/stm32l081xx.h
+11 −11 stm32l0xx/Include/stm32l082xx.h
+12 −12 stm32l0xx/Include/stm32l083xx.h
+7 −8 stm32l0xx/Include/stm32l0xx.h
+6 −7 stm32l0xx/Include/system_stm32l0xx.h
+65 −58 stm32l0xx/Release_Notes.html
+4 −0 stm32u5xx/Include/partition_stm32u5xx.h
+21,195 −0 stm32u5xx/Include/stm32u535xx.h
+22,026 −0 stm32u5xx/Include/stm32u545xx.h
+620 −605 stm32u5xx/Include/stm32u575xx.h
+629 −605 stm32u5xx/Include/stm32u585xx.h
+777 −654 stm32u5xx/Include/stm32u595xx.h
+815 −703 stm32u5xx/Include/stm32u599xx.h
+786 −654 stm32u5xx/Include/stm32u5a5xx.h
+824 −703 stm32u5xx/Include/stm32u5a9xx.h
+15 −8 stm32u5xx/Include/stm32u5xx.h
+65 −3 stm32u5xx/Release_Notes.html
+2 −1 update.py
1 change: 1 addition & 0 deletions src/modm/architecture/interface/register.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdint.h>
#include <modm/math/utils/bit_constants.hpp>
#include <modm/architecture/detect.hpp>
#include <modm/architecture/utils.hpp>
#if MODM_HAS_IOSTREAM
#include <modm/io/iostream.hpp>
#endif
Expand Down
1 change: 1 addition & 0 deletions src/modm/board/samv71_xplained_ultra/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct SystemClock
static constexpr uint32_t Usart1 = Mck;
static constexpr uint32_t Spi0 = Mck;
static constexpr uint32_t Twihs0 = Mck;
static constexpr uint32_t Dacc = Mck;
// static constexpr uint32_t Usb = 48_MHz;

static bool inline
Expand Down
Loading