Skip to content

Commit 3c9be48

Browse files
Javaskrlehkingelilol
committed
[examples] nucleo_f401re: Add communication example using dw3110 driver
Co-authored-by: Raphael Lehmann <raphael@rleh.de> Co-authored-by: Elias H. <46895028+kingelilol@users.noreply.github.com>
1 parent f089c21 commit 3c9be48

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright (c) 2024, Elias H.
3+
* Copyright (c) 2024, Raphael Lehmann
4+
* Copyright (c) 2024, Michael Jossen
5+
*
6+
* This file is part of the modm project.
7+
*
8+
* This Source Code Form is subject to the terms of the Mozilla Public
9+
* License, v. 2.0. If a copy of the MPL was not distributed with this
10+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
*/
12+
13+
#include <modm/board.hpp>
14+
#include <modm/debug/logger.hpp>
15+
#include <modm/driver/radio/dw3110/dw3110_phy.hpp>
16+
#include <modm/processing/protothread.hpp>
17+
#include <modm/processing/timer.hpp>
18+
19+
using namespace Board;
20+
using namespace std::chrono_literals;
21+
22+
using MySpiMaster = modm::platform::SpiMaster1;
23+
using MyDw3110_a = modm::Dw3110Phy<MySpiMaster, GpioB6>;
24+
using MyDw3110_b = modm::Dw3110Phy<MySpiMaster, GpioA10>;
25+
26+
constexpr static size_t PacketLength = 1021;
27+
28+
class TXThread : public modm::pt::Protothread
29+
{
30+
public:
31+
bool
32+
init()
33+
{
34+
auto ret = RF_CALL_BLOCKING(radio.initialize(
35+
modm::Dw3110::Channel::Channel9, modm::Dw3110::PreambleCode::Code_64Mhz_9,
36+
modm::Dw3110::PreambleLength::Preamble_128,
37+
modm::Dw3110::StartFrameDelimiter::Decawave_8));
38+
RF_CALL_BLOCKING(radio.setEnableLongFrames(true));
39+
return ret;
40+
}
41+
42+
bool
43+
run()
44+
{
45+
PT_BEGIN();
46+
while (true)
47+
{
48+
49+
txdata[txdata.size() - 1]++;
50+
timeout.restart(Button::read() ? 500ms : 10ms);
51+
PT_WAIT_UNTIL(timeout.execute());
52+
if (PT_CALL(radio.transmit(txdata, true)) == MyDw3110_b::Error::None)
53+
{
54+
sentCount++;
55+
} else
56+
{
57+
MODM_LOG_DEBUG << "[TX] Failed to trasmit!" << modm::endl;
58+
}
59+
}
60+
PT_END();
61+
}
62+
63+
size_t
64+
getCount()
65+
{
66+
return sentCount;
67+
}
68+
69+
private:
70+
MyDw3110_b radio{};
71+
std::array<uint8_t, PacketLength> txdata = {0xBA, 0xDE, 0xAF, 0xFE, 0x00};
72+
modm::Timeout timeout{10ms};
73+
size_t sentCount{0};
74+
};
75+
76+
class RXThread : public modm::pt::Protothread
77+
{
78+
public:
79+
bool
80+
init()
81+
{
82+
auto ret = RF_CALL_BLOCKING(radio.initialize(
83+
modm::Dw3110::Channel::Channel9, modm::Dw3110::PreambleCode::Code_64Mhz_9,
84+
modm::Dw3110::PreambleLength::Preamble_128,
85+
modm::Dw3110::StartFrameDelimiter::Decawave_8));
86+
RF_CALL_BLOCKING(radio.setEnableLongFrames(true));
87+
return ret;
88+
}
89+
90+
bool
91+
run()
92+
{
93+
PT_BEGIN();
94+
while (true)
95+
{
96+
while (!PT_CALL(radio.packetReady()))
97+
{
98+
if (!PT_CALL(radio.isReceiving()))
99+
{
100+
// KEEP ON SEPERATE LINE
101+
PT_CALL(radio.startReceive());
102+
}
103+
PT_YIELD();
104+
}
105+
106+
if (PT_CALL(radio.fetchPacket(rxdata, rxlen))) { recvCount++; }
107+
}
108+
PT_END();
109+
}
110+
111+
size_t
112+
getCount()
113+
{
114+
return recvCount;
115+
}
116+
117+
private:
118+
MyDw3110_a radio{};
119+
size_t rxlen{0}, recvCount{0};
120+
std::array<uint8_t, PacketLength> rxdata = {};
121+
};
122+
123+
int
124+
main()
125+
{
126+
Board::initialize();
127+
LedD13::setOutput();
128+
129+
MySpiMaster::initialize<Board::SystemClock, 21_MHz>();
130+
MySpiMaster::connect<GpioA6::Miso, GpioA7::Mosi, GpioA5::Sck>();
131+
132+
// Use the logging streams to print some messages.
133+
// Change MODM_LOG_LEVEL above to enable or disable these messages
134+
MODM_LOG_DEBUG << "debug" << modm::endl;
135+
MODM_LOG_INFO << "info" << modm::endl;
136+
MODM_LOG_WARNING << "warning" << modm::endl;
137+
MODM_LOG_ERROR << "error" << modm::endl;
138+
139+
MODM_LOG_INFO << "Initializing Devices..." << modm::endl;
140+
bool success = true;
141+
TXThread tx;
142+
if (!tx.init())
143+
{
144+
MODM_LOG_ERROR << "Failed to initialize TX Device!" << modm::endl;
145+
success = false;
146+
}
147+
148+
RXThread rx;
149+
if (!rx.init())
150+
{
151+
MODM_LOG_ERROR << "Failed to initialize TR Device!" << modm::endl;
152+
success = false;
153+
}
154+
if (!success)
155+
while (true) {}
156+
157+
modm::PeriodicTimer timer{1000ms};
158+
MODM_LOG_INFO << "Starting ping pong..." << modm::endl;
159+
while (true)
160+
{
161+
rx.run();
162+
tx.run();
163+
if (timer.execute())
164+
{
165+
MODM_LOG_DEBUG << "Sent " << tx.getCount() << ", received " << rx.getCount()
166+
<< ". Diff:" << tx.getCount() - rx.getCount() << modm::endl;
167+
}
168+
}
169+
170+
return 0;
171+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<library>
2+
<extends>modm:nucleo-f401re</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_f401re/dw3110-communication</option>
5+
</options>
6+
<modules>
7+
<module>modm:build:scons</module>
8+
<module>modm:driver:dw3110</module>
9+
<module>modm:platform:spi:1</module>
10+
<module>modm:processing:protothread</module>
11+
</modules>
12+
</library>

0 commit comments

Comments
 (0)