Skip to content

Commit

Permalink
added usesDma
Browse files Browse the repository at this point in the history
  • Loading branch information
hshose committed May 26, 2023
1 parent 367dc71 commit 63fd13c
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
61 changes: 61 additions & 0 deletions examples/stm32f469_discovery/ws2812b_dma/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2019, Niklas Hauser
*
* 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 <modm/driver/pwm/ws2812b.hpp>
#include <modm/ui/led/tables.hpp>
#include <modm/processing/timer.hpp>
#include <modm/processing.hpp>

using namespace Board;

using Output = Board::D11;
using DmaRx = Dma1::Channel3;
using DmaTx = Dma1::Channel4;
using SpiLed = SpiMaster2_Dma<DmaRx, DmaTx>;
// using SpiLed = SpiMaster2; // for non-dma version
modm::Ws2812b<SpiLed, Output, 8*8> leds;
modm::ShortPeriodicTimer tmr{33ms};

constexpr uint8_t max = 62;
uint8_t r=0, g=max/3, b=max/3*2;

int
main()
{
Board::initialize();
LedD13::setOutput();
Dma1::enable();
leds.initialize<Board::SystemClock>();

constexpr uint8_t max = 60;
uint8_t r=0, g=max/3, b=max/3*2;

while (true)
{
for (size_t ii=0; ii < leds.size; ii++)
{
leds.setColor(ii,
{modm::ui::table22_8_256[r*3/2],
modm::ui::table22_8_256[g*3/2],
modm::ui::table22_8_256[b*3/2]});
if (r++ >= max) r = 0;
if (g++ >= max) g = 0;
if (b++ >= max) b = 0;
}
leds.write();

while(not tmr.execute()) ;
LedD13::toggle();
}

return 0;
}
15 changes: 15 additions & 0 deletions examples/stm32f469_discovery/ws2812b_dma/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version='1.0' encoding='UTF-8'?>
<library>
<extends>modm:disco-f469ni</extends>
<options>
<option name=":build:build.path">../../../build/stm32f469_discovery/ws2812b_dma</option>
</options>
<modules>
<module>modm:driver:ws2812</module>
<module>modm:platform:spi:2</module>
<module>modm:platform:dma</module>
<module>modm:ui:led</module>
<module>modm:build:scons</module>
<module>modm:processing:timer</module>
</modules>
</library>
1 change: 1 addition & 0 deletions src/modm/architecture/interface/spi_master.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class SpiMaster : public ::modm::PeripheralDriver, public Spi
*/
static modm::ResumableResult<void>
transfer(const uint8_t *tx, uint8_t *rx, std::size_t length);

#endif
};

Expand Down
13 changes: 13 additions & 0 deletions src/modm/driver/pwm/ws2812b.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,24 @@ class Ws2812b : protected modm::NestedResumable<3>

modm::ResumableResult<void>
write()
requires SpiMaster::usesDma
{
RF_BEGIN();
RF_CALL(SpiMaster::transfer(data, nullptr, length+1));
RF_END_RETURN();
}

modm::ResumableResult<void>
write()
{
RF_BEGIN();
for (const auto value : data) {
while (not SpiMaster::Hal::isTransmitRegisterEmpty()) ;
SpiMaster::Hal::write(value);
}
RF_END_RETURN();
}

};

} // namespace modm
1 change: 1 addition & 0 deletions src/modm/platform/spi/stm32/spi_master.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public:

static modm::ResumableResult<void>
transfer(const uint8_t *tx, uint8_t *rx, std::size_t length);

};

} // namespace platform
Expand Down
3 changes: 3 additions & 0 deletions src/modm/platform/spi/stm32/spi_master_dma.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public:
static modm::ResumableResult<void>
transfer(const uint8_t *tx, uint8_t *rx, std::size_t length);

constexpr bool
usesDma(){return true;};

private:
static void
handleDmaTransferError();
Expand Down

0 comments on commit 63fd13c

Please sign in to comment.