static void Main() { #if TINYCLR_V2_SC20100DEV_MIKROBUS_1 Rfm9XDevice rfm9XDevice = new Rfm9XDevice(SC20100.SpiBus.Spi3, SC20100.GpioPin.PD3, SC20100.GpioPin.PD4); #endif #if TINYCLR_V2_SC20100DEV_MIKROBUS_2 Rfm9XDevice rfm9XDevice = new Rfm9XDevice(SC20100.SpiBus.Spi3, SC20100.GpioPin.PD14, SC20100.GpioPin.PD15); #endif #if TINYCLR_V2_FEZDUINO Rfm9XDevice rfm9XDevice = new Rfm9XDevice(SC20100.SpiBus.Spi6, SC20100.GpioPin.PB1, SC20100.GpioPin.PA15); #endif #if TINYCLR_V2_FEZPORTAL Rfm9XDevice rfm9XDevice = new Rfm9XDevice(SC20100.SpiBus.Spi3, SC20100.GpioPin.PC13, SC20100.GpioPin.PD4); #endif // Put device into LoRa + Sleep mode rfm9XDevice.RegisterWriteByte(0x01, 0b10000000); // 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(0x01, 0b10000101); // RegOpMode set LoRa & RxContinuous while (true) { // Wait until a packet is received, no timeouts in PoC Debug.WriteLine("Receive-Wait"); byte irqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags while ((irqFlags & 0b01000000) == 0) // wait until RxDone cleared { Thread.Sleep(100); irqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags //Debug.Write("."); } Debug.WriteLine(""); Debug.WriteLine($"RegIrqFlags 0X{irqFlags:X2}"); Debug.WriteLine("Receive-Message"); byte currentFifoAddress = rfm9XDevice.RegisterReadByte(0x10); // RegFifiRxCurrent rfm9XDevice.RegisterWriteByte(0x0d, currentFifoAddress); // RegFifoAddrPtr byte numberOfBytes = rfm9XDevice.RegisterReadByte(0x13); // RegRxNbBytes byte[] messageBytes = rfm9XDevice.RegisterRead(0x00, numberOfBytes); // RegFifo rfm9XDevice.RegisterWriteByte(0x0d, 0); rfm9XDevice.RegisterWriteByte(0x12, 0b11111111); // RegIrqFlags clear all the bits string messageText = UTF8Encoding.UTF8.GetString(messageBytes); Debug.WriteLine($"Received {messageBytes.Length} byte message {messageText}"); Debug.WriteLine("Receive-Done"); } }
static void Main() { #if ST_STM32F429I_DISCOVERY int chipSelectPinNumber = PinNumber('C', 2); int resetPinNumber = PinNumber('C', 3); #endif #if ESP32_WROOM_32_LORA_1_CHANNEL int chipSelectPinNumber = Gpio.IO16; #endif #if NETDUINO3_WIFI int chipSelectPinNumber = PinNumber('B', 10); int resetPinNumber = PinNumber('E', 5); #endif try { #if ESP32_WROOM_32_LORA_1_CHANNEL Configuration.SetPinFunction(Gpio.IO12, DeviceFunction.SPI1_MISO); Configuration.SetPinFunction(Gpio.IO13, DeviceFunction.SPI1_MOSI); Configuration.SetPinFunction(Gpio.IO14, DeviceFunction.SPI1_CLOCK); Rfm9XDevice rfm9XDevice = new Rfm9XDevice(SpiBusId, chipSelectPinNumber); #endif #if ST_STM32F429I_DISCOVERY || NETDUINO3_WIFI Rfm9XDevice rfm9XDevice = new Rfm9XDevice(SpiBusId, chipSelectPinNumber, resetPinNumber); #endif Thread.Sleep(500); // Put device into LoRa + Sleep mode rfm9XDevice.RegisterWriteByte(0x01, 0b10000000); // 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(0x01, 0b10000101); // RegOpMode set LoRa & RxContinuous while (true) { // Wait until a packet is received, no timeouts in PoC Debug.WriteLine("Receive-Wait"); byte irqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags while ((irqFlags & 0b01000000) == 0) // wait until RxDone cleared { Thread.Sleep(100); irqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags Debug.Write("."); } Debug.WriteLine(""); Debug.WriteLine($"RegIrqFlags 0X{irqFlags:X2}"); Debug.WriteLine("Receive-Message"); byte currentFifoAddress = rfm9XDevice.RegisterReadByte(0x10); // RegFifiRxCurrent rfm9XDevice.RegisterWriteByte(0x0d, currentFifoAddress); // RegFifoAddrPtr byte numberOfBytes = rfm9XDevice.RegisterReadByte(0x13); // RegRxNbBytes byte[] messageBytes = rfm9XDevice.RegisterRead(0x00, numberOfBytes); // RegFifo rfm9XDevice.RegisterWriteByte(0x0d, 0); rfm9XDevice.RegisterWriteByte(0x12, 0b11111111); // RegIrqFlags clear all the bits // Remove unprintable characters from messages for (int index = 0; index < messageBytes.Length; index++) { if ((messageBytes[index] < 0x20) || (messageBytes[index] > 0x7E)) { messageBytes[index] = 0x20; } } string messageText = UTF8Encoding.UTF8.GetString(messageBytes, 0, messageBytes.Length); Debug.WriteLine($"Received {messageBytes.Length} byte message {messageText}"); Debug.WriteLine("Receive-Done"); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }