-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwm_logic.cpp
162 lines (140 loc) · 3.46 KB
/
dwm_logic.cpp
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
#include "dwm_logic.h"
#include "leds.h"
#include "thread_logic.h"
dwDevice_t dwm_device;
dwDevice_t* dwm = &dwm_device;
SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK);
volatile bool sending;
DigitalOut cs(SPI_CS);
InterruptIn sIRQ(PA_0);
DigitalInOut sReset(PA_1);
circularBuffer DWMcb;
uint8_t DWMcb_data[256];
DFrame txFrame;
DFrame rxFrame;
void dwIRQFunction();
void irq_checker();
static void spiWrite(dwDevice_t* dev, const void* header, size_t headerLength,
const void* data, size_t dataLength) {
cs = 0;
spi.lock();
uint8_t* headerP = (uint8_t*) header;
uint8_t* dataP = (uint8_t*) data;
for(size_t i = 0; i<headerLength; ++i) {
spi.write(headerP[i]);
}
for(size_t i = 0; i<dataLength; ++i) {
spi.write(dataP[i]);
}
spi.unlock();
cs = 1;
}
static void spiRead(dwDevice_t* dev, const void *header, size_t headerLength,
void* data, size_t dataLength) {
cs = 0;
spi.lock();
uint8_t* headerP = (uint8_t*) header;
uint8_t* dataP = (uint8_t*) data;
for(size_t i = 0; i<headerLength; ++i) {
spi.write(headerP[i]);
}
for(size_t i = 0; i<dataLength; ++i) {
dataP[i] = spi.write(0);
}
spi.unlock();
cs = 1;
}
static void spiSetSpeed(dwDevice_t* dev, dwSpiSpeed_t speed)
{
if (speed == dwSpiSpeedLow)
spi.frequency(3*1000*1000);
if (speed == dwSpiSpeedHigh)
spi.frequency(20*1000*1000);
}
static void reset(dwDevice_t* dev)
{
sReset.output();
redLed = 1;
sReset = 0;
wait(0.1);
redLed = 0;
sReset.input();
}
static void delayms(dwDevice_t* dev, unsigned int delay)
{
wait(delay * 0.001f);
}
static dwOps_t ops = {
.spiRead = spiRead,
.spiWrite = spiWrite,
.spiSetSpeed = spiSetSpeed,
.delayms = delayms,
.reset = reset
};
void initialiseDWM(void) {
circularBuffer_init(&DWMcb, DWMcb_data, 256);
sIRQ.mode(PullDown);
sIRQ.rise(IRQqueue.event(&dwIRQFunction));
reset(dwm);
dwInit(dwm, &ops); // Init libdw
uint8_t result = dwConfigure(dwm); // Configure the dw1000 chip
if (result == 0) {
dwEnableAllLeds(dwm);
}
dwTime_t delay = {.full = 0};
dwSetAntenaDelay(dwm, delay);
}
void startDWM(){
dwInterruptOnReceived(dwm, true);
dwInterruptOnSent(dwm, true);
dwInterruptOnReceiveTimeout(dwm, true);
dwInterruptOnReceiveFailed(dwm, true);
dwNewConfiguration(dwm);
dwSetDefaults(dwm);
dwEnableMode(dwm, MODE_SHORTDATA_MID_ACCURACY);
dwSetChannel(dwm, CHANNEL_5);
dwSetPreambleCode(dwm, PREAMBLE_CODE_64MHZ_9);
dwCommitConfiguration(dwm);
//dwReceivePermanently(dwm, true);
dwNewReceive(dwm);
dwSetDefaults(dwm);
dwStartReceive(dwm);
wait(0.5f);
IRQqueue.call_every(IRQ_CHECKER_INTERVALL, irq_checker);
}
void sendDWM(uint8_t* data, int length) {
sending = true;
spi.lock();
dwNewTransmit(dwm);
dwSetData(dwm, data, length);
dwStartTransmit(dwm);
spi.unlock();
}
void DWMReceive() {
if(sending)
return;
dwNewReceive(dwm);
dwStartReceive(dwm);
}
uint8_t irq_checker_count = 0;
void irq_checker() {
greenLed = 0;
if(sIRQ.read()) {
irq_checker_count++;
} else {
irq_checker_count = 0;
redLed = 0;
return;
}
if(irq_checker_count < IRQ_CHECKER_THRESHOLD)
return;
redLed = 1;
sending = false;
DWMReceive();
greenLed = 1;
}
void dwIRQFunction(){
greenLed=0;
dwHandleInterrupt(dwm);
greenLed=1;
}