-
Notifications
You must be signed in to change notification settings - Fork 1
/
rf.cpp
282 lines (231 loc) · 12.2 KB
/
rf.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
271
272
273
274
275
276
277
278
#include "hal.h"
#include "rf.h"
#include "gps.h"
#include "timesync.h"
#ifdef WITH_FLASHLOG // log own track to unused Flash pages (STM32 only)
#include "flashlog.h"
#endif
// ---------------------------------------------------------------------------------------------------------------------------------
static uint16_t InfoParmIdx = 0; // the round-robin index to info records in info packets
static int ReadInfo(OGN1_Packet &Packet)
{ Packet.clrInfo();
uint8_t ParmIdx;
for( ParmIdx=InfoParmIdx; ; )
{ const char *Parm = Parameters.InfoParmValue(ParmIdx);
if(Parm)
{ // printf("Parm[%d]=%s\n", ParmIdx, Parm);
if(Parm[0])
{ int Add=Packet.addInfo(Parm, ParmIdx); if(Add==0) break; }
}
ParmIdx++; if(ParmIdx>=Parameters.InfoParmNum) ParmIdx=0;
if(ParmIdx==InfoParmIdx) break;
}
InfoParmIdx = ParmIdx;
Packet.setInfoCheck();
return Packet.Info.DataChars; } // zero => no info parameters were stored
// ---------------------------------------------------------------------------------------------------------------------------------
const uint32_t OGN1_SYNC = 0x0AF3656C; // OGN SYNC
const uint32_t OGN2_SYNC = 0xF56D3738; // OGNv2 SYNC
#ifdef WITH_OGN1
static const uint32_t OGN_SYNC = OGN1_SYNC;
#endif
#ifdef WITH_OGN2
static const uint32_t OGN_SYNC = OGN2_SYNC;
#endif
static RFM_TRX TRX; // radio transceiver
int16_t RF_Temp; // [0.1degC] temperature of the RF chip: uncalibrated
uint16_t RF_VCC; // [0.01V]
static uint32_t RF_SlotTime; // [sec] UTC time which belongs to the current time slot (0.3sec late by GPS UTC)
FreqPlan RF_FreqPlan; // frequency hopping pattern calculator
FIFO<OGN_TxPacket<OGN_Packet>, 4> RF_TxFIFO; // buffer for transmitted packets
uint16_t TX_Credit =0; // counts transmitted packets vs. time to avoid using more than 1% of the time
// uint32_t RX_Random=0x12345678; // Random number from LSB of RSSI readouts
static uint8_t Transmit(const uint8_t *PacketByte)
{ if(PacketByte==0) return 0; // if no packet to send: simply return
#ifdef WITH_LED_TX
LED_TX_Flash(20);
#endif
TRX.WritePacket(PacketByte); // write packet into FIFO
TRX.Transmit(); // trigger the transmission
vTaskDelay(6);
return 1; }
static uint32_t IdleUntil(TickType_t End)
{ uint32_t Count=0;
for( ; ; )
{ int32_t Left = End-xTaskGetTickCount();
if(Left<=0) break;
vTaskDelay(1); }
return Count; }
static void TimeSlot(uint32_t SlotLen, const uint8_t *PacketByte, uint32_t TxTime=0)
{ TickType_t Start = xTaskGetTickCount(); // when the slot started
TickType_t End = Start + SlotLen; // when should it end
uint32_t MaxTxTime = SlotLen-8; // time limit when transmision could start
if( (TxTime==0) || (TxTime>=MaxTxTime) ) TxTime = GPS_Random%MaxTxTime; // if TxTime out of limits, setup a random TxTime
TickType_t Tx = Start + TxTime; // Tx = the moment to start transmission
IdleUntil(Tx); // listen until this time comes
if( (TX_Credit) && (PacketByte) ) // when packet to transmit is given and there is still TX $
TX_Credit-=Transmit(PacketByte); // attempt to transmit the packet
IdleUntil(End); // listen till the end of the time-slot
}
static void SetFreqPlan(void) // set the RF TRX according to the selected frequency hopping plan
{ TRX.setBaseFrequency(RF_FreqPlan.BaseFreq); // set the base frequency (recalculate to RFM69 internal synth. units)
TRX.setChannelSpacing(RF_FreqPlan.ChanSepar); // set the channel separation
TRX.setFrequencyCorrection(Parameters.RFchipFreqCorr); // set the fine correction (to counter the Xtal error)
}
static uint8_t StartRFchip(void)
{ // TRX.RESET(1); // RESET active
// vTaskDelay(10); // wait 10ms
// TRX.RESET(0); // RESET released
// vTaskDelay(10); // wait 10ms
TRX.SoftReset();
vTaskDelay(2); // wait 2ms
SetFreqPlan(); // set TRX base frequency and channel separation after the frequency hop$
TRX.Configure(0, OGN_SYNC); // setup RF chip parameters and set to channel #0
TRX.WriteTxPower(Parameters.RFchipTxPower); // [dBm]
uint8_t Version = TRX.ReadVersion();
#ifdef DEBUG_PRINT
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
Format_String(CONS_UART_Write, "StartRFchip() v");
Format_Hex(CONS_UART_Write, Version);
CONS_UART_Write(' ');
Format_UnsDec(CONS_UART_Write, RF_FreqPlan.BaseFreq, 4, 3);
CONS_UART_Write('+');
Format_UnsDec(CONS_UART_Write, (uint16_t)RF_FreqPlan.Channels, 2);
CONS_UART_Write('x');
Format_UnsDec(CONS_UART_Write, RF_FreqPlan.ChanSepar, 4, 3);
Format_String(CONS_UART_Write, "kHz\n");
TRX.RegDump(CONS_UART_Write);
xSemaphoreGive(CONS_Mutex);
#endif
return Version; } // read the RF chip version and return it
extern "C"
void vTaskRF(void* pvParameters)
{
vTaskDelay(500); // put some initial delay not to start all devices at once
#ifdef WITH_FLASHLOG
uint16_t kB = FlashLog_OpenForWrite();
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
Format_String(CONS_UART_Write, "TaskRF: ");
Format_UnsDec(CONS_UART_Write, kB);
Format_String(CONS_UART_Write, "KB FlashLog\n");
xSemaphoreGive(CONS_Mutex);
#endif
RF_TxFIFO.Clear();
#ifdef USE_BLOCK_SPI
TRX.TransferBlock = RFM_TransferBlock;
#else
TRX.Select = RFM_Select;
TRX.Deselect = RFM_Deselect;
TRX.TransferByte = RFM_TransferByte;
#endif
TRX.DIO0_isOn = RFM_IRQ_isOn; // dummy call
TRX.RESET = RFM_RESET; // dummy call
RF_FreqPlan.setPlan(Parameters.FreqPlan); // 1 = Europe/Africa:868MHz, 5 = Europe/Africe:433MHz
vTaskDelay(5);
for( ; ; )
{ uint8_t ChipVersion = StartRFchip();
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
Format_String(CONS_UART_Write, "TaskRF: ");
CONS_UART_Write('v'); Format_Hex(CONS_UART_Write, ChipVersion);
CONS_UART_Write(' '); Format_SignDec(CONS_UART_Write, TRX.ReadChipTemp(), 2, 1);
Format_String(CONS_UART_Write,"degC, ");
Format_UnsDec(CONS_UART_Write, TRX.ReadBatVolt(), 3, 2);
Format_String(CONS_UART_Write,"V\n");
xSemaphoreGive(CONS_Mutex);
if( (ChipVersion!=0x00) && (ChipVersion!=0xFF) ) break; // only break the endless loop then an RF chip is detected
vTaskDelay(1000);
}
TX_Credit = 0; // count slots and packets transmitted: to keep the rule of 1% transmitter duty cycle
OGN_TxPacket<OGN_Packet> TxPosPacket;
OGN_TxPacket<OGN_Packet> TxStatPacket;
TxPosPacket.Packet.HeaderWord=0;
TxPosPacket.Packet.Header.Address=Parameters.Address;
TxPosPacket.Packet.Header.AddrType=Parameters.AddrType;
TxPosPacket.Packet.calcAddrParity();
TxStatPacket.Packet.HeaderWord = TxPosPacket.Packet.HeaderWord;
for( ; ; )
{ do
{ vTaskDelay(1); }
while(TimeSync_msTime()<290); // wait till 300ms after PPS
SetFreqPlan();
RF_Temp = TRX.ReadChipTemp();
RF_VCC = TRX.ReadBatVolt();
StartRFchip();
uint16_t MCU_Vref = ADC_Read_MCU_Vref(); // internal reference voltage: 1.2V
uint16_t MCU_Vtemp = ADC_Read_MCU_Vtemp(); // internal temperature
uint16_t Vsupply = ADC_Read_Vsupply(); // battery voltage
uint16_t Vbutton = ADC_Read_Vbutton(); // power-on/off button
static uint8_t PwrButton = 0;
uint16_t Vthresh = Vsupply-Vsupply/16;
if(Vsupply>MCU_Vref/2) // if on battery power
{ uint8_t Pressed = Vbutton>Vthresh; // if button pressed
PwrButton = (PwrButton<<1) | Pressed;
if( ((PwrButton&0xF0)==0xF0) && ((PwrButton&0x03)==0) ) Power_On(0); // if pressed for more than 5 seconds
} else PwrButton=0;
uint16_t MCU_VCC = ( ((uint32_t)120<<12)+(MCU_Vref>>1))/MCU_Vref; // [0.01V]
int16_t MCU_Temp = 250 + ( ( ( (int32_t)1430 - ((int32_t)1200*(int32_t)MCU_Vtemp+(MCU_Vref>>1))/MCU_Vref )*(int32_t)37 +8 )>>4); // [0.1degC]
#ifdef DEBUG_PRINT
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
Format_String(CONS_UART_Write, "ADC: Vref=");
Format_UnsDec(CONS_UART_Write, MCU_Vref, 4);
Format_String(CONS_UART_Write,", Vtemp=");
Format_UnsDec(CONS_UART_Write, MCU_Vtemp, 4);
Format_String(CONS_UART_Write,", Vsupply=");
Format_UnsDec(CONS_UART_Write, Vsupply, 4);
Format_String(CONS_UART_Write,", Vbutton=");
Format_UnsDec(CONS_UART_Write, Vbutton, 4);
Format_String(CONS_UART_Write,", Vthresh=");
Format_UnsDec(CONS_UART_Write, Vthresh, 4);
Format_String(CONS_UART_Write,", VCC=");
Format_UnsDec(CONS_UART_Write, MCU_VCC, 4, 2);
Format_String(CONS_UART_Write,", Temp=");
Format_SignDec(CONS_UART_Write, MCU_Temp, 3, 1);
Format_String(CONS_UART_Write,", PwrButon=");
Format_Hex(CONS_UART_Write, PwrButton);
Format_String(CONS_UART_Write, "\n");
xSemaphoreGive(CONS_Mutex);
#endif
GPS_Position *Position = GPS_getPosition();
if( Position && Position->hasGPS && Position->isValid() )
{ TxPosPacket.Packet.Position.AcftType=Parameters.AcftType;
Position->Encode(TxPosPacket.Packet);
#ifdef WITH_FLASHLOG
bool Written=FlashLog_Process(TxPosPacket.Packet, Position->getUnixTime());
#endif
TxPosPacket.Packet.Whiten();
TxPosPacket.calcFEC();
OGN_TxPacket<OGN_Packet> *TxPkt=RF_TxFIFO.getWrite();
*TxPkt = TxPosPacket;
RF_TxFIFO.Write(); }
if((GPS_Random&0x1F000)==0)
{ TxStatPacket.Packet.Header.NonPos=1;
TxStatPacket.Packet.calcAddrParity();
ReadInfo(TxStatPacket.Packet);
TxStatPacket.Packet.Whiten();
TxStatPacket.calcFEC();
OGN_TxPacket<OGN_Packet> *TxPkt=RF_TxFIFO.getWrite();
*TxPkt = TxStatPacket;
RF_TxFIFO.Write(); }
RF_SlotTime = TimeSync_Time();
uint8_t TxChan = RF_FreqPlan.getChannel(RF_SlotTime, 0, 1); // tranmsit channel
TRX.setChannel(TxChan);
TX_Credit+=2; if(TX_Credit>7200) TX_Credit=7200; // count the transmission credit
XorShift32(GPS_Random);
uint32_t TxTime = (GPS_Random&0x3F)+1; TxTime*=6; TxTime+=50; // random transmission time: (1..64)*6+50 [ms]
const uint8_t *TxPktData0=0;
const uint8_t *TxPktData1=0;
const OGN_TxPacket<OGN_Packet> *TxPkt0 = RF_TxFIFO.getRead(0); // get 1st packet from TxFIFO
const OGN_TxPacket<OGN_Packet> *TxPkt1 = RF_TxFIFO.getRead(1); // get 2nd packet from TxFIFO
if(TxPkt0) TxPktData0=TxPkt0->Byte(); // if 1st is not NULL then get its data
if(TxPkt1) TxPktData1=TxPkt1->Byte(); // if 2nd if not NULL then get its data
else TxPktData1=TxPktData0; // but if NULL then take copy of the 1st packet
TimeSlot(800-TimeSync_msTime(), TxPktData0, TxTime); // run the 1st Time-Slot till 0.800sec
TxChan = RF_FreqPlan.getChannel(RF_SlotTime, 1, 1);
TRX.setChannel(TxChan);
XorShift32(GPS_Random);
TxTime = (GPS_Random&0x3F)+1; TxTime*=6;
TimeSlot(1250-TimeSync_msTime(), TxPktData1, TxTime); // run the 2nd Time-Slot
if(TxPkt0) RF_TxFIFO.Read();
if(TxPkt1) RF_TxFIFO.Read();
}
}