Skip to content

Commit 3d40be4

Browse files
committed
added usesDma
1 parent 367dc71 commit 3d40be4

File tree

8 files changed

+160
-0
lines changed

8 files changed

+160
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2019, Niklas Hauser
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/board.hpp>
13+
#include <modm/driver/pwm/ws2812b.hpp>
14+
#include <modm/ui/led/tables.hpp>
15+
#include <modm/processing/timer.hpp>
16+
17+
using namespace Board;
18+
19+
using Output = Board::D11;
20+
modm::Ws2812b<SpiMaster2, Output, 8*8> leds;
21+
modm::ShortPeriodicTimer tmr{33ms};
22+
23+
int
24+
main()
25+
{
26+
Board::initialize();
27+
LedD13::setOutput();
28+
leds.initialize<Board::SystemClock>();
29+
30+
constexpr uint8_t max = 60;
31+
uint8_t r=0, g=max/3, b=max/3*2;
32+
33+
while (true)
34+
{
35+
for (size_t ii=0; ii < leds.size; ii++)
36+
{
37+
leds.setColor(ii,
38+
{modm::ui::table22_8_256[r*3/2],
39+
modm::ui::table22_8_256[g*3/2],
40+
modm::ui::table22_8_256[b*3/2]});
41+
if (r++ >= max) r = 0;
42+
if (g++ >= max) g = 0;
43+
if (b++ >= max) b = 0;
44+
}
45+
leds.write();
46+
47+
while(not tmr.execute()) ;
48+
LedD13::toggle();
49+
}
50+
51+
return 0;
52+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<library>
3+
<extends>modm:disco-f469ni</extends>
4+
<options>
5+
<option name=":build:build.path">../../../build/stm32f469_discovery/ws2812b</option>
6+
</options>
7+
<modules>
8+
<module>modm:driver:ws2812</module>
9+
<module>modm:platform:spi:2</module>
10+
<module>modm:ui:led</module>
11+
<module>modm:build:scons</module>
12+
</modules>
13+
</library>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2019, Niklas Hauser
3+
* Copyright (c) 2023, Henrik Hose
4+
*
5+
* This file is part of the modm project.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
10+
*/
11+
// ----------------------------------------------------------------------------
12+
13+
#include <modm/board.hpp>
14+
#include <modm/driver/pwm/ws2812b.hpp>
15+
#include <modm/ui/led/tables.hpp>
16+
#include <modm/processing/timer.hpp>
17+
#include <modm/processing.hpp>
18+
19+
using namespace Board;
20+
21+
using Output = Board::D11;
22+
using DmaRx = Dma1::Channel3;
23+
using DmaTx = Dma1::Channel4;
24+
using SpiLed = SpiMaster2_Dma<DmaRx, DmaTx>;
25+
// using SpiLed = SpiMaster2; // for non-dma version
26+
modm::Ws2812b<SpiLed, Output, 8*8> leds;
27+
modm::ShortPeriodicTimer tmr{33ms};
28+
29+
constexpr uint8_t max = 62;
30+
uint8_t r=0, g=max/3, b=max/3*2;
31+
32+
int
33+
main()
34+
{
35+
Board::initialize();
36+
LedD13::setOutput();
37+
Dma1::enable();
38+
leds.initialize<Board::SystemClock>();
39+
40+
constexpr uint8_t max = 60;
41+
uint8_t r=0, g=max/3, b=max/3*2;
42+
43+
while (true)
44+
{
45+
for (size_t ii=0; ii < leds.size; ii++)
46+
{
47+
leds.setColor(ii,
48+
{modm::ui::table22_8_256[r*3/2],
49+
modm::ui::table22_8_256[g*3/2],
50+
modm::ui::table22_8_256[b*3/2]});
51+
if (r++ >= max) r = 0;
52+
if (g++ >= max) g = 0;
53+
if (b++ >= max) b = 0;
54+
}
55+
leds.write();
56+
57+
while(not tmr.execute()) ;
58+
LedD13::toggle();
59+
}
60+
61+
return 0;
62+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<library>
3+
<extends>modm:disco-f469ni</extends>
4+
<options>
5+
<option name=":build:build.path">../../../build/stm32f469_discovery/ws2812b_dma</option>
6+
</options>
7+
<modules>
8+
<module>modm:driver:ws2812</module>
9+
<module>modm:platform:spi:2</module>
10+
<module>modm:platform:dma</module>
11+
<module>modm:ui:led</module>
12+
<module>modm:build:scons</module>
13+
<module>modm:processing:timer</module>
14+
</modules>
15+
</library>

src/modm/architecture/interface/spi_master.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class SpiMaster : public ::modm::PeripheralDriver, public Spi
155155
*/
156156
static modm::ResumableResult<void>
157157
transfer(const uint8_t *tx, uint8_t *rx, std::size_t length);
158+
158159
#endif
159160
};
160161

src/modm/driver/pwm/ws2812b.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,24 @@ class Ws2812b : protected modm::NestedResumable<3>
103103

104104
modm::ResumableResult<void>
105105
write()
106+
requires SpiMaster::usesDma
106107
{
107108
RF_BEGIN();
108109
RF_CALL(SpiMaster::transfer(data, nullptr, length+1));
109110
RF_END_RETURN();
110111
}
112+
113+
modm::ResumableResult<void>
114+
write()
115+
{
116+
RF_BEGIN();
117+
for (const auto value : data) {
118+
while (not SpiMaster::Hal::isTransmitRegisterEmpty()) ;
119+
SpiMaster::Hal::write(value);
120+
}
121+
RF_END_RETURN();
122+
}
123+
111124
};
112125

113126
} // namespace modm

src/modm/platform/spi/stm32/spi_master.hpp.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public:
153153

154154
static modm::ResumableResult<void>
155155
transfer(const uint8_t *tx, uint8_t *rx, std::size_t length);
156+
156157
};
157158

158159
} // namespace platform

src/modm/platform/spi/stm32/spi_master_dma.hpp.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public:
7474
static modm::ResumableResult<void>
7575
transfer(const uint8_t *tx, uint8_t *rx, std::size_t length);
7676

77+
constexpr bool
78+
usesDma(){return true;};
79+
7780
private:
7881
static void
7982
handleDmaTransferError();

0 commit comments

Comments
 (0)