This repository has been archived by the owner on Nov 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sx1280_spi.cpp
272 lines (186 loc) · 6.21 KB
/
sx1280_spi.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
This file is part of SX1280 Linux driver.
Copyright (C) 2020 ReimuNotMoe
This program is based on sx1280-driver from Semtech S.A.,
see LICENSE-SEMTECH.txt for details.
Original maintainer of sx1280-driver: Miguel Luis, Gregory Cristian
and Matthieu Verdy
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "sx1280_spi.hpp"
/*!
* \brief Used to block execution waiting for low state on radio busy pin.
* Essentially used in SPI communications
*/
#define WaitOnBusy( ) while(BUSY.read()){ usleep(10); }
using namespace YukiWorkshop::Drivers::Semtech;
SX1280_SPI::SX1280_SPI(const std::string &__spidev_path, YukiWorkshop::GPIO::Device &__gpio_iface, uint32_t __busy_pin,
uint32_t __rst_pin, uint32_t __nss_pin, int32_t __dio1_pin, int32_t __dio2_pin,
int32_t __dio3_pin, const RadioCallbacks_t &callbacks) :
RadioSpi(__spidev_path, SPI_MODE_0|SPI_NO_CS, 8, 8000000),
RadioGpio(__gpio_iface),
DIOPins{__dio1_pin, __dio2_pin, __dio3_pin},
BUSY(__gpio_iface.line(__busy_pin, GPIO::LineMode::Input, 0, "SX1280 BUSY")),
RadioNss(__gpio_iface.line(__nss_pin, GPIO::LineMode::Output, 1, "SX1280 NSS")),
RadioReset(__gpio_iface.line(__rst_pin, GPIO::LineMode::Output, 1, "SX1280 NRESET")),
SX1280(callbacks) {
}
void SX1280_SPI::SetSpiSpeed(uint32_t spiSpeed) {
RadioSpi.set_max_speed_hz(spiSpeed);
}
void SX1280_SPI::IoIrqInit(const std::function<void()>& irqHandler) {
for (auto it : DIOPins) {
if (it != -1)
RadioGpio.on_event(it, GPIO::LineMode::Input, GPIO::EventMode::RisingEdge,
[this, &irqHandler](GPIO::EventType t, uint64_t){
if (t == GPIO::EventType::RisingEdge)
irqHandler();
});
}
}
void SX1280_SPI::Reset() {
std::lock_guard lg(IOLock);
RadioReset.set_mode(GPIO::LineMode::Output, 1, "SX1280 NRESET (Out)");
usleep(50 * 1000);
RadioReset.write(0);
usleep(50 * 1000);
RadioReset.write(1);
usleep(50 * 1000);
RadioReset.set_mode(GPIO::LineMode::Input, 0, "SX1280 NRESET (In)");
usleep(50 * 1000);
BUSY.read();
}
void SX1280_SPI::Wakeup() {
std::lock_guard lg(IOLock);
// Don't wait for BUSY here
if (Debug)
printf("SX1280: Wakeup\n");
uint8_t buf[2] = {RADIO_GET_STATUS, 0};
RadioNss.write(0);
RadioSpi.write(buf, 2);
RadioNss.write(1);
// Wait for chip to be ready.
WaitOnBusy();
if (Debug)
printf("SX1280: Wakeup done\n");
}
void SX1280_SPI::WriteCommand(RadioCommands_t command, uint8_t *buffer, uint16_t size )
{
std::lock_guard lg(IOLock);
if (Debug)
printf("SX1280: WriteCommand: 0x%02x %u\n", command, size);
WaitOnBusy();
RadioNss.write(0);
RadioSpi.transfer(command, false);
RadioSpi.write(buffer, size);
RadioNss.write(1);
if (Debug)
printf("SX1280: WriteCommand: send done\n");
if (command != RADIO_SET_SLEEP) {
WaitOnBusy();
}
if (Debug)
printf("SX1280: WriteCommand: wait done\n");
}
void SX1280_SPI::ReadCommand(RadioCommands_t command, uint8_t *buffer, uint16_t size )
{
std::lock_guard lg(IOLock);
WaitOnBusy();
RadioNss.write(0);
if( command == RADIO_GET_STATUS ) {
buffer[0] = RadioSpi.transfer(command, false);
RadioSpi.transfer(0, false);
RadioSpi.transfer(0);
} else {
RadioSpi.transfer(command, false);
RadioSpi.transfer(0, false);
RadioSpi.read(buffer, size);
}
RadioNss.write(1);
WaitOnBusy();
}
void SX1280_SPI::WriteRegister(uint16_t address, uint8_t *buffer, uint16_t size ) {
std::lock_guard lg(IOLock);
if (Debug)
printf("SX1280: WriteRegister: 0x%04x %u\n", address, size);
WaitOnBusy();
RadioNss.write(0);
RadioSpi.transfer(RADIO_WRITE_REGISTER, false);
RadioSpi.transfer((address & 0xFF00) >> 8, false);
RadioSpi.transfer(address & 0x00FF, false);
RadioSpi.write(buffer, size);
RadioNss.write(1);
if (Debug)
printf("SX1280: WriteRegister: send done\n");
WaitOnBusy();
if (Debug)
printf("SX1280: WriteRegister: Wait done\n");
}
void SX1280_SPI::WriteRegister(uint16_t address, uint8_t value ) {
WriteRegister(address, &value, 1);
}
void SX1280_SPI::ReadRegister(uint16_t address, uint8_t *buffer, uint16_t size) {
std::lock_guard lg(IOLock);
WaitOnBusy();
RadioNss.write(0);
uint8_t buf[4] = {RADIO_READ_REGISTER, static_cast<uint8_t>((address & 0xFF00) >> 8),
static_cast<uint8_t>(address & 0x00FF), 0};
RadioSpi.write(buf, 4);
// RadioSpi.transfer(RADIO_READ_REGISTER, false);
// RadioSpi.transfer( (address & 0xFF00) >> 8 , false);
// RadioSpi.transfer( address & 0x00FF , false);
// RadioSpi.transfer( 0 , false);
RadioSpi.read(buffer, size);
RadioNss.write(1);
WaitOnBusy();
}
uint8_t SX1280_SPI::ReadRegister(uint16_t address) {
uint8_t data;
ReadRegister( address, &data, 1 );
return data;
}
void SX1280_SPI::WriteBuffer(uint8_t offset, uint8_t *buffer, uint8_t size) {
std::lock_guard lg(IOLock);
WaitOnBusy();
RadioNss.write(0);
RadioSpi.transfer( RADIO_WRITE_BUFFER, false);
RadioSpi.transfer( offset, false);
RadioSpi.write(buffer, size);
RadioNss.write(1);
WaitOnBusy();
}
void SX1280_SPI::ReadBuffer(uint8_t offset, uint8_t *buffer, uint8_t size) {
std::lock_guard lg(IOLock);
WaitOnBusy();
RadioNss.write(0);
RadioSpi.transfer( RADIO_READ_BUFFER, false);
RadioSpi.transfer( offset, false);
RadioSpi.transfer( 0, false);
RadioSpi.read(buffer, size);
RadioNss.write(1);
WaitOnBusy();
}
uint8_t SX1280_SPI::GetDioStatus() {
return 0;
}
void SX1280_SPI::StartIrqHandler(int __prio) {
IrqThread = std::thread([this, __prio](){
sched_param param;
param.sched_priority = __prio;
pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
RadioGpio.run_eventlistener();
});
}
void SX1280_SPI::StopIrqHandler() {
RadioGpio.stop_eventlistener();
IrqThread.join();
}