This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
223 lines (171 loc) · 7.58 KB
/
Program.cs
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
//---------------------------------------------------------------------------------
// Copyright (c) August 2018, devMobile Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//---------------------------------------------------------------------------------
namespace devMobile.IoT.NetMF.Rfm9X.ReceiveInterrupt
{
using System;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
public sealed class Rfm9XDevice
{
private const byte RegisterAddressReadMask = 0X7f;
private const byte RegisterAddressWriteMask = 0x80;
private SPI Rfm9XLoraModem = null;
private OutputPort ResetGpioPin = null;
private InterruptPort InterruptPin = null;
public Rfm9XDevice(Cpu.Pin chipSelect, Cpu.Pin resetPin, Cpu.Pin interruptPin)
{
// Factory reset pin configuration
ResetGpioPin = new OutputPort(Pins.GPIO_PIN_D9, true);
ResetGpioPin.Write(false);
Thread.Sleep(10);
ResetGpioPin.Write(true);
Thread.Sleep(10);
this.Rfm9XLoraModem = new SPI(new SPI.Configuration(chipSelect, false, 0, 0, false, false, 2000, SPI.SPI_module.SPI1));
InterruptPin = new InterruptPort(interruptPin, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);
InterruptPin.OnInterrupt += InterruptPin_OnInterrupt;
Thread.Sleep(100);
}
public Rfm9XDevice(Cpu.Pin chipSelect, Cpu.Pin reset)
{
// Factory reset pin configuration
ResetGpioPin = new OutputPort(Pins.GPIO_PIN_D9, true);
ResetGpioPin.Write(false);
Thread.Sleep(10);
ResetGpioPin.Write(true);
Thread.Sleep(10);
this.Rfm9XLoraModem = new SPI(new SPI.Configuration(chipSelect, false, 0, 0, false, false, 2000, SPI.SPI_module.SPI1));
Thread.Sleep(100);
}
public Byte RegisterReadByte(byte registerAddress)
{
byte[] writeBuffer = new byte[] { registerAddress };
byte[] readBuffer = new byte[1];
Debug.Assert(Rfm9XLoraModem != null);
Rfm9XLoraModem.WriteRead(writeBuffer, readBuffer, 1);
return readBuffer[0];
}
public ushort RegisterReadWord(byte address)
{
byte[] writeBuffer = new byte[] { address &= RegisterAddressReadMask };
byte[] readBuffer = new byte[2];
Debug.Assert(Rfm9XLoraModem != null);
readBuffer[0] = RegisterReadByte(address);
readBuffer[1] = RegisterReadByte(address += 1);
return (ushort)(readBuffer[1] + (readBuffer[0] << 8));
}
public byte[] RegisterRead(byte address, int length)
{
byte[] writeBuffer = new byte[] { address &= RegisterAddressReadMask };
byte[] readBuffer = new byte[length];
Debug.Assert(Rfm9XLoraModem != null);
for (byte index = 0; index < length; index++)
{
readBuffer[index] = RegisterReadByte(address += 1);
}
return readBuffer;
}
public void RegisterWriteByte(byte address, byte value)
{
byte[] writeBuffer = new byte[] { address |= RegisterAddressWriteMask, value };
Debug.Assert(Rfm9XLoraModem != null);
Rfm9XLoraModem.Write(writeBuffer);
}
public void RegisterWriteWord(byte address, ushort value)
{
byte[] valueBytes = BitConverter.GetBytes(value);
byte[] writeBuffer = new byte[] { address |= RegisterAddressWriteMask, valueBytes[0], valueBytes[1] };
Debug.Assert(Rfm9XLoraModem != null);
Rfm9XLoraModem.Write(writeBuffer);
}
public void RegisterWrite(byte address, byte[] bytes)
{
byte[] writeBuffer = new byte[1 + bytes.Length];
Debug.Assert(Rfm9XLoraModem != null);
Array.Copy(bytes, 0, writeBuffer, 1, bytes.Length);
writeBuffer[0] = address |= RegisterAddressWriteMask;
Rfm9XLoraModem.Write(writeBuffer);
}
public void RegisterDump()
{
Debug.Print("---Registers 0x00 thru 0x42---");
for (byte registerIndex = 0; registerIndex <= 0x42; registerIndex++)
{
byte registerValue = this.RegisterReadByte(registerIndex);
Debug.Print("Register 0x" + ByteToHexString(registerIndex) + " - Value 0X" + ByteToHexString(registerValue));
}
}
void InterruptPin_OnInterrupt(uint data1, uint data2, DateTime time)
{
byte IrqFlags = this.RegisterReadByte(0x12); // RegIrqFlags
Debug.Print("RegIrqFlags " + ByteToHexString(IrqFlags));
if ((IrqFlags & 0x40) == 0x40) // TxDone
{
Debug.Print("Receive-Message");
byte currentFifoAddress = this.RegisterReadByte(0x10); // RegFifiRxCurrent
this.RegisterWriteByte(0x0d, currentFifoAddress); // RegFifoAddrPtr
byte numberOfBytes = this.RegisterReadByte(0x13); // RegRxNbBytes
// Allocate buffer for message
byte[] messageBytes = new byte[numberOfBytes];
for (int i = 0; i < numberOfBytes; i++)
{
messageBytes[i] = this.RegisterReadByte(0x00); // RegFifo
}
string messageText = new string(UTF8Encoding.UTF8.GetChars(messageBytes));
Debug.Print("Received " + messageBytes.Length.ToString() + " byte message " + messageText);
}
this.RegisterWriteByte(0x12, 0xff);// RegIrqFlags
}
private static string ByteToHexString(byte singlebyte)
{
string hexString = string.Empty;
// Create a character array for hexidecimal conversion.
const string hexChars = "0123456789ABCDEF";
// Grab the top 4 bits and append the hex equivalent to the return string.
hexString += hexChars[singlebyte >> 4];
// Mask off the upper 4 bits to get the rest of it.
hexString += hexChars[singlebyte & 0x0F];
return hexString;
}
private static string WordToHexString(ushort singleword)
{
string hexString = string.Empty;
byte[] bytes = BitConverter.GetBytes(singleword);
hexString += ByteToHexString(bytes[1]);
hexString += ByteToHexString(bytes[0]);
return hexString;
}
public class Program
{
public static void Main()
{
Rfm9XDevice rfm9XDevice = new Rfm9XDevice(Pins.GPIO_PIN_D10, Pins.GPIO_PIN_D9, Pins.GPIO_PIN_D2);
// Put device into LoRa + Sleep mode
rfm9XDevice.RegisterWriteByte(0x01, 0x80); // RegOpMode
// Set the frequency to 915MHz
byte[] frequencyWriteBytes = { 0xE4, 0xC0, 0x00 }; // RegFrMsb, RegFrMid, RegFrLsb
rfm9XDevice.RegisterWrite(0x06, frequencyWriteBytes);
rfm9XDevice.RegisterWriteByte(0x0F, 0x0); // RegFifoRxBaseAddress
rfm9XDevice.RegisterWriteByte(0x40, 0x0); // RegDioMapping1 0b00000000 DIO0 RxReady & TxReady
rfm9XDevice.RegisterWriteByte(0x01, 0x85); // RegOpMode set LoRa & RxContinuous
Thread.Sleep(Timeout.Infinite);
}
}
}
}