This repository was archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmain.cpp
More file actions
170 lines (153 loc) · 5.15 KB
/
Copy pathmain.cpp
File metadata and controls
170 lines (153 loc) · 5.15 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* This is an Example application for one STM32 in combination with a Decawave DM1000 Chip
*
* Connections are the following:
*
* USART: STM32F4&F103 Pin
* TXD -> PA2
* RXD -> PA3 (not used in this example)
*
* DM1000 -> SPI-Master-2
* SPI: STM32F4 Pin STM32F107
* Miso -> PC02 PB14
* Mosi -> PB15 PB15
* Clock -> PB10 PB13
*
* RES -> PA08 PA00 (not used in this example)
* Chipselect -> PE08 PB12
* IRQ -> PD10 PB08(not used in this example)
*
*
* What this program does is:
* 1) Sends out a DSTW-Ranging Frame on Broadcast
* 2) Waits for an Answer and Computes the Range between the both
*
* For 1 Ranging there will be 4 messages but this technique is more precise than SSTW
*
*
* Init -> Resp |Payload{init DSTWR}
* Resp -> Init |Payload{receive and transmittime of Resp}
* Init -> Resp |Payload{receive and transmittime of Init}
* Resp -> Init |Payload{Time of flight of second ranging round}
*/
#include <xpcc/architecture/platform.hpp> // include for platform
#include <xpcc/debug/logger.hpp> // include for DW1000 Error/Debug Messaging
#include <xpcc/driver/radio/dw1000/dw1000.hpp> // DW1000 Driver
#include <xpcc/communication/Frame802154/Frame802154.hpp> // MAC Frame for IEEE 802.15.4
#include <xpcc/positioning/ranging.hpp> // ranging operations for DW1000
#include <xpcc/processing/timer.hpp> // Timer and Timeouts
//-------------------------Namespaces and renaming----------------------------------
// change things up for each individual board
using namespace Board;
using SPI = SpiMaster2;
using SPISCK = GpioOutputB10;
using SPIMISO = GpioInputC2;
using SPIMOSI = GpioOutputB15;
using RES = GpioOutputA8;
using CS = GpioOutputE8;
using IRQ = GpioInputD10;
using dwm = xpcc::Dw1000 < SPI, CS, RES, IRQ >;
using ranging = xpcc::Ranging < dwm >;
//------------------------------LOGGER----------------------------------------------
#undef XPCC_LOG_LEVEL
#define XPCC_LOG_LEVEL xpcc::log::DISABLED
xpcc::IODeviceWrapper< Usart2, xpcc::IOBuffer::BlockIfFull > loggerDevice;
// Set all four logger streams to use the UART
xpcc::log::Logger xpcc::log::debug(loggerDevice);
xpcc::log::Logger xpcc::log::info(loggerDevice);
xpcc::log::Logger xpcc::log::warning(loggerDevice);
xpcc::log::Logger xpcc::log::error(loggerDevice);
//------------------------------DWM Config------------------------------------------------
static xpcc::dw1000::config_t config =
{
2, /* Channel number. */
xpcc::dw1000::PRF_64M, /* Pulse repetition frequency. */
xpcc::dw1000::PLEN_128, /* Preamble length. Used in TX only. */
xpcc::dw1000::PAC8, /* Preamble acquisition chunk size. Used in RX only. */
9, /* TX preamble code. Used in TX only. */
9, /* RX preamble code. Used in RX only. */
0, /* 0 to use standard SFD, 1 to use non-standard SFD. */
xpcc::dw1000::BR_6M8, /* Data rate. */
xpcc::dw1000::PHRMODE_STD, /* PHY header mode. */
(129 + 8 - 8) /* SFD timeout (preamble length + 1 + SFD length - PAC size). Used in RX only. */
};
static constexpr uint16_t hostaddress= 0xAAAA;
xpcc::Frame802154 receiveframe;
xpcc::ShortTimeout timeout;
uint8_t buffer[256];
bool isrx = false;
uint32_t length;
//-------------------------------MAIN-----------------------------------------------------
int
main()
{
//setup USART
GpioOutputA2::connect(Usart2::Tx);
GpioInputA3::connect(Usart2::Rx, Gpio::InputType::PullUp);
Usart2::initialize<Board::systemClock, 115200>(12);
//initialize the board
Board::initialize();
XPCC_LOG_INFO << "GENERIC DSTW RESP v1.1"<< xpcc::endl;
//activate the CS on the DW1000
CS::setOutput(xpcc::Gpio::High);
//setup SPI
SPIMOSI::connect(SPI::Mosi);
SPIMISO::connect(SPI::Miso);
SPISCK::connect(SPI::Sck);
SPI::initialize<Board::systemClock,2500000,xpcc::Tolerance::DontCare>();
SPI::setDataMode(SPI::DataMode::Mode0);
//Init with the config
while (!(dwm::init(config)))
{
Board::Leds::toggle();
xpcc::delayMilliseconds(100);
}
/* Apply antenna delay value.*/
dwm::setRXAntennaDelay(dwm::standardAntennaDelay);
dwm::setTXAntennaDelay(dwm::standardAntennaDelay);
dwm::hostaddress = hostaddress;
while (true)
{
isrx = false;
dwm::rxEnable();
Leds::toggle();
while(not((isrx = dwm::checkForRX()) || dwm::checkForRXError()))
{}
Leds::toggle();
if (isrx)
{
length = dwm::rxlength();
dwm::readrx(length, buffer);
receiveframe.loadFrame(length, buffer);
if (receiveframe.getDestinationAddress16() == dwm::hostaddress || receiveframe.getDestinationAddress16() == 0xFFFF)
{
Leds::toggle();
if(ranging::IsRangingFrame(receiveframe))
{
ranging::sendAnswer(receiveframe);
timeout.restart(5);
while(not(dwm::isFrameSent() || timeout.isExpired()))
{}
}
Leds::toggle();
}
else
{
Leds::toggle();
xpcc::delayMilliseconds(100);
Leds::toggle();
}
}
else
{
Leds::toggle();
xpcc::delayMilliseconds(50);
Leds::toggle();
xpcc::delayMilliseconds(50);
Leds::toggle();
xpcc::delayMilliseconds(50);
Leds::toggle();
}
dwm::trxdisable();
dwm::rxreset();
}
}