Skip to content

Commit e579250

Browse files
Javaskrlehkingelilol
committed
[examples] nucleo_f401re: Add communication example using dw3110 driver
Co-authored-by: Raphael Lehmann <[email protected]> Co-authored-by: Elias H. <[email protected]>
1 parent 6bf8875 commit e579250

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
class TXThread : public modm::pt::Protothread
27+
{
28+
public:
29+
bool
30+
init()
31+
{
32+
auto ret = RF_CALL_BLOCKING(radio.initialize(
33+
modm::Dw3110::Channel::Channel9, modm::Dw3110::PreambleCode::Code_64Mhz_9,
34+
modm::Dw3110::PreambleLength::Preamble_128,
35+
modm::Dw3110::StartFrameDelimiter::Decawave_8));
36+
RF_CALL_BLOCKING(radio.setEnableLongFrames(true));
37+
return ret;
38+
}
39+
40+
bool
41+
run()
42+
{
43+
PT_BEGIN();
44+
while (true)
45+
{
46+
47+
txdata[txdata.size() - 1]++;
48+
timeout.restart(Button::read() ? 500ms : 10ms);
49+
PT_WAIT_UNTIL(timeout.execute());
50+
if (PT_CALL(radio.transmit(txdata, true)) == MyDw3110_b::Error::None)
51+
{
52+
sentCount++;
53+
} else
54+
{
55+
MODM_LOG_DEBUG << "[TX] Failed to trasmit!" << modm::endl;
56+
}
57+
}
58+
PT_END();
59+
}
60+
61+
size_t
62+
getCount()
63+
{
64+
return sentCount;
65+
}
66+
67+
private:
68+
MyDw3110_b radio{};
69+
std::array<uint8_t, 5> txdata = {0xBA, 0xDE, 0xAF, 0xFE, 0x00};
70+
modm::Timeout timeout{10ms};
71+
size_t sentCount{0};
72+
};
73+
74+
class RXThread : public modm::pt::Protothread
75+
{
76+
public:
77+
bool
78+
init()
79+
{
80+
auto ret = RF_CALL_BLOCKING(radio.initialize(
81+
modm::Dw3110::Channel::Channel9, modm::Dw3110::PreambleCode::Code_64Mhz_9,
82+
modm::Dw3110::PreambleLength::Preamble_128,
83+
modm::Dw3110::StartFrameDelimiter::Decawave_8));
84+
RF_CALL_BLOCKING(radio.setEnableLongFrames(true));
85+
return ret;
86+
}
87+
88+
bool
89+
run()
90+
{
91+
PT_BEGIN();
92+
while (true)
93+
{
94+
while (!PT_CALL(radio.packetReady()))
95+
{
96+
if (!PT_CALL(radio.isReceiving()))
97+
{
98+
// KEEP ON SEPERATE LINE
99+
PT_CALL(radio.startReceive());
100+
}
101+
PT_YIELD();
102+
}
103+
104+
if (PT_CALL(radio.fetchPacket(rxdata, rxlen))) { recvCount++; }
105+
}
106+
PT_END();
107+
}
108+
109+
size_t
110+
getCount()
111+
{
112+
return recvCount;
113+
}
114+
115+
private:
116+
constexpr static size_t RxBufferSize = 1021; // Maximum supported packet size
117+
MyDw3110_a radio{};
118+
size_t rxlen{0}, recvCount{0};
119+
std::array<uint8_t, RxBufferSize> rxdata = {};
120+
};
121+
122+
int
123+
main()
124+
{
125+
Board::initialize();
126+
LedD13::setOutput();
127+
128+
MySpiMaster::initialize<Board::SystemClock, 21_MHz>();
129+
MySpiMaster::connect<GpioA6::Miso, GpioA7::Mosi, GpioA5::Sck>();
130+
131+
// Use the logging streams to print some messages.
132+
// Change MODM_LOG_LEVEL above to enable or disable these messages
133+
MODM_LOG_DEBUG << "debug" << modm::endl;
134+
MODM_LOG_INFO << "info" << modm::endl;
135+
MODM_LOG_WARNING << "warning" << modm::endl;
136+
MODM_LOG_ERROR << "error" << modm::endl;
137+
138+
MODM_LOG_INFO << "Initializing Devices..." << modm::endl;
139+
bool success = true;
140+
TXThread tx;
141+
if (!tx.init())
142+
{
143+
MODM_LOG_ERROR << "Failed to initialize TX Device!" << modm::endl;
144+
success = false;
145+
}
146+
147+
RXThread rx;
148+
if (!rx.init())
149+
{
150+
MODM_LOG_ERROR << "Failed to initialize TR Device!" << modm::endl;
151+
success = false;
152+
}
153+
if (!success)
154+
while (true) { __NOP(); }
155+
156+
modm::PeriodicTimer timer{1000ms};
157+
MODM_LOG_INFO << "Starting ping pong..." << modm::endl;
158+
while (true)
159+
{
160+
rx.run();
161+
tx.run();
162+
if (timer.execute())
163+
{
164+
MODM_LOG_DEBUG << "Sent " << tx.getCount() << ", received " << rx.getCount()
165+
<< ". Diff:" << tx.getCount() - rx.getCount() << modm::endl;
166+
}
167+
}
168+
169+
return 0;
170+
}
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)