-
Notifications
You must be signed in to change notification settings - Fork 0
/
OGNRadio.cpp
160 lines (124 loc) · 4.4 KB
/
OGNRadio.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
/*
OGN Tracker Client
Copyright (C) <2015> <Mike Roberts>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "OGNRadio.h"
#define SCK 13
#define CS 10
#define MISO 12
#define MOSI 11
SPISettings SPISet(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE0);
OGNRadio::OGNRadio(void)
{
pinMode(SCK, OUTPUT); digitalWrite(SCK, HIGH);
pinMode(MOSI, OUTPUT); digitalWrite(MOSI,HIGH);
pinMode(CS,OUTPUT); digitalWrite(CS,HIGH);
pinMode(MISO, INPUT);
SPI.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV4);
SPI.setBitOrder(MSBFIRST);
}
void OGNRadio::Initialise(uint8_t Power)
{
WriteRegister(REG_OPMODE, RF_DATAMODUL_MODULATIONTYPE_FSK | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY);
WriteRegister(REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONSHAPING_10 | RF_DATAMODUL_MODULATIONTYPE_FSK );
WriteRegister(REG_IRQFLAGS1,0x00);
WriteRegister(REG_IRQFLAGS2,0x00);
WriteRegister(REG_BITRATEMSB, RF_BITRATEMSB_100000);
WriteRegister(REG_BITRATELSB, RF_BITRATELSB_100000);
WriteRegister(REG_FDEVMSB,RF_FDEVMSB_50000);
WriteRegister(REG_FDEVLSB,RF_FDEVLSB_50000);
// Frequenct 868.2Hhz FRF = 14224589 = D90CCD
WriteRegister(REG_FRFMSB, 0xD9);
WriteRegister(REG_FRFMID, 0x0C);
WriteRegister(REG_FRFLSB, 0xCD);
// Frequenct 868.4Hhz FRF = 14227866 = D9199A
//WriteRegister(REG_FRFMSB, 0xD9);
//WriteRegister(REG_FRFMID, 0x19);
//WriteRegister(REG_FRFLSB, 0x9A);
WriteRegister(REG_PACKETCONFIG1, RF_PACKET1_FORMAT_FIXED | RF_PACKET1_DCFREE_OFF | RF_PACKET1_CRC_OFF | RF_PACKET1_CRCAUTOCLEAR_ON | RF_PACKET1_ADRSFILTERING_OFF);
WriteRegister(REG_FIFOTHRESH, RF_FIFOTHRESH_TXSTART_FIFONOTEMPTY | 63);
WriteRegister(REG_PAYLOADLENGTH, 64);
WriteRegister(REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_1BIT | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF);
WriteRegister(REG_AUTOMODES, 0);
SetTxPower(Power);
}
void OGNRadio::SendPacket(uint8_t *Packet, uint16_t Size, uint16_t freq)
{
uint8_t i;
if(freq == F8682)
{
// Frequenct 868.2Hhz FRF = 14224589 = D90CCD
WriteRegister(REG_FRFMSB, 0xD9);
WriteRegister(REG_FRFMID, 0x0C);
WriteRegister(REG_FRFLSB, 0xCD);
}
else
{
// Frequenct 868.4Hhz FRF = 14227866 = D9199A
WriteRegister(REG_FRFMSB, 0xD9);
WriteRegister(REG_FRFMID, 0x19);
WriteRegister(REG_FRFLSB, 0x9A);
}
WriteRegister(REG_PREAMBLEMSB, 0x00);
WriteRegister(REG_PREAMBLELSB, 0x80);
WriteRegister(REG_IRQFLAGS1,0x00);
WriteRegister(REG_IRQFLAGS2,0x00);
for(i=0;i<Size;i++)
{
WriteRegister(REG_FIFO,Packet[i]);
}
WriteRegister(REG_OPMODE , RF_DATAMODUL_MODULATIONTYPE_FSK | RF_OPMODE_LISTEN_OFF | RF_OPMODE_TRANSMITTER);
do {} while( (ReadRegister(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT ) != RF_IRQFLAGS2_PACKETSENT);
WriteRegister(REG_OPMODE, RF_DATAMODUL_MODULATIONTYPE_FSK | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY);
}
void OGNRadio::SetTxPower(uint8_t Power)
{
if(Power <4 ) Power = 4;
if(Power >20) Power = 20;
Power += 11;
WriteRegister(REG_PALEVEL, RF_PALEVEL_PA0_OFF | RF_PALEVEL_PA1_ON | RF_PALEVEL_PA2_ON | (Power & 0x1F));
WriteRegister(REG_OCP, RF_OCP_OFF);
WriteRegister(REG_TESTPA1,0x5D);
WriteRegister(REG_TESTPA2,0x7C);
}
void OGNRadio::StartRecieve(void)
{
}
void OGNRadio::EndRecieve(void)
{
}
void OGNRadio::CheckRecieve(void)
{
}
void OGNRadio::WriteRegister(uint8_t Register, uint8_t Data)
{
SPI.beginTransaction(SPISet);
digitalWrite(CS,LOW);
SPI.transfer(Register | 0x80);
SPI.transfer(Data);
digitalWrite(CS,HIGH);
SPI.endTransaction();
}
uint8_t OGNRadio::ReadRegister(uint8_t Register)
{
uint8_t result;
SPI.beginTransaction(SPISet);
digitalWrite(CS,LOW);
SPI.transfer(Register & 0x7F);
result = SPI.transfer(0);
digitalWrite(CS,HIGH);
SPI.endTransaction();
return result;
}