From 5dcdf1d832b0d17e2459f40d00762f95a8ac4fde Mon Sep 17 00:00:00 2001 From: Raphael Lehmann Date: Wed, 27 Oct 2021 11:41:04 +0200 Subject: [PATCH] [exmaples] Add SPI DMA exmaples for Nucleo-F439ZI and -F767ZI --- examples/nucleo_f439zi/spi_dma/main.cpp | 83 ++++++++++++++++++++++ examples/nucleo_f439zi/spi_dma/project.xml | 14 ++++ examples/nucleo_f767zi/spi_dma/main.cpp | 83 ++++++++++++++++++++++ examples/nucleo_f767zi/spi_dma/project.xml | 14 ++++ 4 files changed, 194 insertions(+) create mode 100644 examples/nucleo_f439zi/spi_dma/main.cpp create mode 100644 examples/nucleo_f439zi/spi_dma/project.xml create mode 100644 examples/nucleo_f767zi/spi_dma/main.cpp create mode 100644 examples/nucleo_f767zi/spi_dma/project.xml diff --git a/examples/nucleo_f439zi/spi_dma/main.cpp b/examples/nucleo_f439zi/spi_dma/main.cpp new file mode 100644 index 0000000000..d548a15c2d --- /dev/null +++ b/examples/nucleo_f439zi/spi_dma/main.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2021, Raphael Lehmann + * + * 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 + +using Mosi = GpioOutputB5; +using Miso = GpioInputB4; +using Sck = GpioOutputB3; +using DmaRx = Dma2::Channel0; +using DmaTx = Dma2::Channel3; +using Spi = SpiMaster1_Dma; + +class SpiThread : public modm::pt::Protothread +{ +public: + + bool + update() + { + PT_BEGIN(); + + // Enable DMA controller + Dma2::enable(); + + // Enable and initialize SPI + Spi::connect(); + Spi::initialize(); + + while (true) + { + MODM_LOG_INFO << "sendBuffer adress: " << modm::hex << sendBuffer << modm::endl; + MODM_LOG_INFO << "receiveBuffer adress: " << modm::hex << receiveBuffer << modm::endl; + MODM_LOG_INFO << "Info: 0x20000000 is start of SRAM1" << modm::endl; + + MODM_LOG_INFO << "Before first transfer" << modm::endl; + + // send out 12 bytes, don't care about response + PT_CALL(Spi::transfer(sendBuffer, nullptr, 12)); + + MODM_LOG_INFO << "After first transfer" << modm::endl; + + // send out 12 bytes, read in 12 bytes + PT_CALL(Spi::transfer(sendBuffer, receiveBuffer, 12)); + + MODM_LOG_INFO << "After second transfer" << modm::endl << modm::endl; + + timeout.restart(500ms); + PT_WAIT_UNTIL(timeout.isExpired()); + } + + PT_END(); + } + +private: + uint8_t sendBuffer[13] { "data to send" }; + uint8_t receiveBuffer[13]; + modm::ShortTimeout timeout; +}; + +SpiThread spiThread; + +int main() +{ + Board::initialize(); + + MODM_LOG_INFO << "Hello from SPI-DMA example on Nucleo-F439ZI!" << modm::endl; + + while (true) + { + spiThread.update(); + } + + return 0; +} diff --git a/examples/nucleo_f439zi/spi_dma/project.xml b/examples/nucleo_f439zi/spi_dma/project.xml new file mode 100644 index 0000000000..b8ca489c20 --- /dev/null +++ b/examples/nucleo_f439zi/spi_dma/project.xml @@ -0,0 +1,14 @@ + + modm:nucleo-f439zi + + + + + modm:platform:gpio + modm:platform:dma + modm:platform:spi:1 + modm:processing:protothread + modm:processing:timer + modm:build:scons + + diff --git a/examples/nucleo_f767zi/spi_dma/main.cpp b/examples/nucleo_f767zi/spi_dma/main.cpp new file mode 100644 index 0000000000..fb85e05bd7 --- /dev/null +++ b/examples/nucleo_f767zi/spi_dma/main.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2021, Raphael Lehmann + * + * 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 + +using Mosi = GpioOutputB5; +using Miso = GpioInputB4; +using Sck = GpioOutputB3; +using DmaRx = Dma2::Channel0; +using DmaTx = Dma2::Channel3; +using Spi = SpiMaster1_Dma; + +class SpiThread : public modm::pt::Protothread +{ +public: + + bool + update() + { + PT_BEGIN(); + + // Enable DMA controller + Dma2::enable(); + + // Enable and initialize SPI + Spi::connect(); + Spi::initialize(); + + while (true) + { + MODM_LOG_INFO << "sendBuffer adress: " << modm::hex << sendBuffer << modm::endl; + MODM_LOG_INFO << "receiveBuffer adress: " << modm::hex << receiveBuffer << modm::endl; + MODM_LOG_INFO << "Info: 0x20000000 is start of SRAM1" << modm::endl; + + MODM_LOG_INFO << "Before first transfer" << modm::endl; + + // send out 12 bytes, don't care about response + PT_CALL(Spi::transfer(sendBuffer, nullptr, 12)); + + MODM_LOG_INFO << "After first transfer" << modm::endl; + + // send out 12 bytes, read in 12 bytes + PT_CALL(Spi::transfer(sendBuffer, receiveBuffer, 12)); + + MODM_LOG_INFO << "After second transfer" << modm::endl << modm::endl; + + timeout.restart(500ms); + PT_WAIT_UNTIL(timeout.isExpired()); + } + + PT_END(); + } + +private: + uint8_t sendBuffer[13] { "data to send" }; + uint8_t receiveBuffer[13]; + modm::ShortTimeout timeout; +}; + +SpiThread spiThread; + +int main() +{ + Board::initialize(); + + MODM_LOG_INFO << "Hello from SPI-DMA example on Nucleo-F767ZI!" << modm::endl; + + while (true) + { + spiThread.update(); + } + + return 0; +} diff --git a/examples/nucleo_f767zi/spi_dma/project.xml b/examples/nucleo_f767zi/spi_dma/project.xml new file mode 100644 index 0000000000..0f59fd175c --- /dev/null +++ b/examples/nucleo_f767zi/spi_dma/project.xml @@ -0,0 +1,14 @@ + + modm:nucleo-f767zi + + + + + modm:platform:gpio + modm:platform:dma + modm:platform:spi:1 + modm:processing:protothread + modm:processing:timer + modm:build:scons + +