-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (124 loc) · 4.38 KB
/
main.cpp
File metadata and controls
143 lines (124 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* 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/communication/amnb.hpp>
#include <modm/processing.hpp>
using namespace Board;
using namespace std::chrono_literals;
using namespace modm::amnb;
using Usart1 = BufferedUart<UsartHal1, UartRxBuffer<32>>;
using Usart3 = BufferedUart<UsartHal3, UartRxBuffer<16>>;
using Usart4 = BufferedUart<UsartHal4>;
// ----------------------------------------------------------------------------
Listener listeners[] =
{
{1, [](uint8_t sender, const uint32_t& data)
{
MODM_LOG_INFO << "Node2 and Node3 received Broadcast 1 from '" << sender;
MODM_LOG_INFO << "': " << data << modm::endl;
}
},
{2, [](uint8_t sender)
{
MODM_LOG_INFO << "Node2 and Node3 received Broadcast 2 from '" << sender << "'" << modm::endl;
}
},
};
Action actions[] =
{
{1, [counter=uint8_t{}]() mutable -> Response
{
MODM_LOG_INFO << "Node1 or Node3 received Action 1" << modm::endl;
return counter++;
}
},
{2, [counter=uint8_t{}](const uint32_t& data) mutable -> Response
{
MODM_LOG_INFO << "Node1 or Node3 received Action 2 with argument: " << data << modm::endl;
return ErrorResponse(counter++);
}
},
};
// Two nodes on the same device on different UARTs of course!
DeviceWrapper<Usart1> device1;
DeviceWrapper<Usart3> device2;
DeviceWrapper<Usart4> device3;
Node node1(device1, 1, actions);
Node node2(device2, 2, listeners);
Node node3(device3, 3, actions, listeners);
modm::Fiber fiberNode1t([]{ node1.update_transmit(); });
modm::Fiber fiberNode1r([]{ node1.update_receive(); });
modm::Fiber fiberNode2t([]{ node2.update_transmit(); });
modm::Fiber fiberNode2r([]{ node2.update_receive(); });
modm::Fiber fiberNode3t([]{ node3.update_transmit(); });
modm::Fiber fiberNode3r([]{ node3.update_receive(); });
// You need to connect D1 with D15 and with A0
using PinNode1 = GpioC4; // D1
using PinNode2 = GpioB8; // D15
using PinNode3 = GpioA0; // A0
modm::Fiber fiber_demo([]
{
uint32_t counter{0};
while(true)
{
modm::this_fiber::sleep_for(1s);
node1.broadcast(1, counter++);
node3.broadcast(2);
auto res1 = node1.request<uint8_t>(1, 1);
MODM_LOG_INFO << "Node1 responded with: " << res1.error();
if (res1) { MODM_LOG_INFO << " " << *res1; }
MODM_LOG_INFO << modm::endl;
auto res2 = node1.request<uint8_t, uint8_t>(3, 2, counter);
MODM_LOG_INFO << "Node1 responded with: " << res2.error();
if (res2.hasUserError()) {
MODM_LOG_INFO << " " << *res2.userError();
}
MODM_LOG_INFO << modm::endl;
if (counter % 10 == 0)
{
MODM_LOG_INFO << "Node1t stack=" << fiberNode1t.stack_usage() << "\nNode1r stack=" << fiberNode1r.stack_usage() << modm::endl;
MODM_LOG_INFO << "Node2t stack=" << fiberNode2t.stack_usage() << "\nNode2r stack=" << fiberNode2r.stack_usage() << modm::endl;
MODM_LOG_INFO << "Node3t stack=" << fiberNode3t.stack_usage() << "\nNode3r stack=" << fiberNode3r.stack_usage() << modm::endl;
}
}
});
// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();
LedD13::setOutput();
Usart1::connect<PinNode1::Tx>();
Usart1::initialize<SystemClock, 115200>(Usart1::Parity::Even, Usart1::WordLength::Bit9);
// Use Single-Wire Half-Duplex Mode
PinNode1::configure(Gpio::OutputType::OpenDrain);
PinNode1::configure(Gpio::InputType::PullUp);
USART1->CR1 &= ~USART_CR1_UE;
USART1->CR3 = USART_CR3_HDSEL;
USART1->CR1 |= USART_CR1_UE;
Usart3::connect<PinNode2::Tx>();
Usart3::initialize<SystemClock, 115200>(Usart1::Parity::Even, Usart1::WordLength::Bit9);
// Use Single-Wire Half-Duplex Mode
PinNode2::configure(Gpio::OutputType::OpenDrain);
PinNode2::configure(Gpio::InputType::PullUp);
USART3->CR1 &= ~USART_CR1_UE;
USART3->CR3 = USART_CR3_HDSEL;
USART3->CR1 |= USART_CR1_UE;
Usart4::connect<PinNode3::Tx>();
Usart4::initialize<SystemClock, 115200>(Usart1::Parity::Even, Usart1::WordLength::Bit9);
// Use Single-Wire Half-Duplex Mode
PinNode3::configure(Gpio::OutputType::OpenDrain);
PinNode3::configure(Gpio::InputType::PullUp);
USART4->CR1 &= ~USART_CR1_UE;
USART4->CR3 = USART_CR3_HDSEL;
USART4->CR1 |= USART_CR1_UE;
modm::fiber::Scheduler::run(modm::fiber::Scheduler::AutoWatermark);
return 0;
}