Send And Receive Data With NanoframeWork And NRF24L01_PA_LNA

Things used in this project
Ā
Hardware components
Story
Ā
Hi friends :)
Ā
I want to share another Nanoframework project with you.
Ā
letās start with the commutations Device!
Ā
Today commutations are a very important concept.
Ā
We have many devices and protocols to commutate with other things, for example, Bluetooth or Wi-Fi or wireā¦.
Specially Commutation is important in IoT Projects when we want to control a remote car or lamp or ā¦.

I worked before with esp32 Wi-Fi and simple wire.
Ā
So I decided to work with the new Module that name is NRF24L01_PA_LNA
NRF24L01_PA_LNA is a Wireless Module that works with SPI

I prepared a base Nanoframework library to control NRF24L01_PA_LNA. (AP.NanoFrameWork.NRF24L01PALNA)
Actually, Iām working on it and I will continue developing this library for Nanoframework.
Ā
Ok, letās show you my library usage with a sample project:
Ā
First, we need two āesp32 wroom32ā develop kits. (I have a develop kit v1 and develop kit v4)


we need two NRF24L01_PA_LNA Modules
Ā
Step1: Connect NRF24L01_PA_LNA to esp32 develop kits.
Ā
GND to GND
Ā
VCC to VCC 3.3
Ā
IRQ to GPIO 17
Ā
CE to GPIO 16
Ā
MOSI to MOSI (GPIO 23 or GPIO 13 ??)
Ā
MISO to MISO (GPIO 19 or GPIO 12 ??)
Ā
SS to SS (GPIO 15 or GPIO 5 ??)

When we look at esp32 pinouts, we find out it has 2 interfaces for SPI (VSPI, HSPI), The question here is that which interface must be connected to NRF24L01_PA_LNA ??!!!

To find the answer, letās take a look at the Nanoframework spi samples

Hmm, with this sample I canāt understand which interface Nanoframework spi library is performed?!
Ā
After much R&D I found it; Nanoframework SPI library works with SPI1. So, we need to set PinFunction for SPI1.
I used VSPI. So, I should set PinFunction like this:


Now, we should initial SPI with This Parameter:

Note: this parameter is initialized according to NRF24L01PALNA Document
Ā
Then, with AP.NanoFrameWork.NRF24L01PALNA, we can send and receive data easily:
Ā
Initial:

Note: You can set PA and Data Rate on both sender and receiver
Ā
Sender:
Ā
Initial TX

or

Send Data

Receive:
Ā
Initial RX

Begin Receive

Some parameters are hardcoding in my library, but I will develop it that and make it more flexible


Tx
var gpioController = new GpioController();
var p1 = Configuration.GetFunctionPin(DeviceFunction.SPI1_CLOCK);
if (p1 != 18)
{
Configuration.SetPinFunction(18, DeviceFunction.SPI1_CLOCK);
Configuration.SetPinFunction(19, DeviceFunction.SPI1_MISO);
Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI);
}
var irq = gpioController.OpenPin(17);
irq.SetPinMode(PinMode.InputPullUp);
irq.ValueChanged += Irq_ValueChanged;
var ce = gpioController.OpenPin(16);
ce.SetPinMode(PinMode.Output);
var csn = gpioController.OpenPin(5, PinMode.Output);
Thread.Sleep(200);
SpiDevice spiDevice;
SpiConnectionSettings connectionSettings;
SpiBusInfo spiBusInfo = SpiDevice.GetBusInfo(1);
Debug.WriteLine($"{nameof(spiBusInfo.ChipSelectLineCount)}: {spiBusInfo.ChipSelectLineCount}");
Debug.WriteLine($"{nameof(spiBusInfo.MaxClockFrequency)}: {spiBusInfo.MaxClockFrequency}");
Debug.WriteLine($"{nameof(spiBusInfo.MinClockFrequency)}: {spiBusInfo.MinClockFrequency}");
Debug.WriteLine($"{nameof(spiBusInfo.SupportedDataBitLengths)}: ");
foreach (var data in spiBusInfo.SupportedDataBitLengths)
{
Debug.WriteLine($" {data}");
}
connectionSettings = new SpiConnectionSettings(1, -1);
connectionSettings.ClockFrequency = 4_000_000;
connectionSettings.DataBitLength = 8;
connectionSettings.DataFlow = DataFlow.MsbFirst;
connectionSettings.Mode = SpiMode.Mode0;
// Then you create your SPI device by passing your settings
spiDevice = SpiDevice.Create(connectionSettings);
Thread.Sleep(50);
NRFActions nrf24 = new NRFActions(spiDevice, csn, ce);
////Read Status
////..................
nrf24.InitialTXMode(NRFActions.PAState.Min, NRFActions.DataRate.d1Mbps);
Thread.Sleep(100);
while (true)
{
nrf24.SendData("Hi My Name Is Alireza Paridar.)");
Thread.Sleep(1000);
}
RX:
var gpioController = new GpioController();
var irq = gpioController.OpenPin(17);
irq.SetPinMode(PinMode.InputPullUp);
irq.ValueChanged += Irq_ValueChanged;
var p1 = Configuration.GetFunctionPin(DeviceFunction.SPI1_CLOCK);
if (p1 != 18)
{
Configuration.SetPinFunction(18, DeviceFunction.SPI1_CLOCK);
Configuration.SetPinFunction(19, DeviceFunction.SPI1_MISO);
Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI);
}
var ce = gpioController.OpenPin(16);
ce.SetPinMode(PinMode.Output);
// var csn = gpioController.OpenPin(15, PinMode.Output);
var csn = gpioController.OpenPin(5, PinMode.Output);
Thread.Sleep(200);
SpiDevice spiDevice;
SpiConnectionSettings connectionSettings;
Debug.WriteLine("Hello from sample for System.Device.Spi!");
// You can get the values of SpiBus
SpiBusInfo spiBusInfo = SpiDevice.GetBusInfo(1);
Debug.WriteLine($"{nameof(spiBusInfo.ChipSelectLineCount)}: {spiBusInfo.ChipSelectLineCount}");
Debug.WriteLine($"{nameof(spiBusInfo.MaxClockFrequency)}: {spiBusInfo.MaxClockFrequency}");
Debug.WriteLine($"{nameof(spiBusInfo.MinClockFrequency)}: {spiBusInfo.MinClockFrequency}");
Debug.WriteLine($"{nameof(spiBusInfo.SupportedDataBitLengths)}: ");
foreach (var data in spiBusInfo.SupportedDataBitLengths)
{
Debug.WriteLine($" {data}");
}
connectionSettings = new SpiConnectionSettings(1, -1);
connectionSettings.ClockFrequency = 4_000_000;//96000;//10_000_000;
connectionSettings.DataBitLength = 8;
connectionSettings.DataFlow = DataFlow.MsbFirst;
connectionSettings.Mode = SpiMode.Mode0;
// Then you create your SPI device by passing your settings
spiDevice = SpiDevice.Create(connectionSettings);
Thread.Sleep(50);
NRFActions nrf24 = new NRFActions(spiDevice, csn, ce);
nrf24.InitialRXMode(NRFActions.PAState.Min, NRFActions.DataRate.d1Mbps);
while (true)
{
Thread.Sleep(1000);
var buf = nrf24.ReciveData();
if(buf==null || buf.Length <= 0)
{
continue;
}
string res = "";
for (int i = 0; i < buf.Length; i++)
{
res += $"{buf[i].ToString()},";
}
Debug.WriteLine(res);
Debug.WriteLine("");
string txt = System.Text.Encoding.UTF8.GetString(buf, 0, buf.Length);
Debug.WriteLine(txt);
Debug.WriteLine("------------------");
}
I Have many ideas to create many projects with this Module, and Iām working on them. I will share the results when I finish them.
Ā
If you want, you can join me on GitHub to develop this library.
Ā
My GitHub (github.com/AlirezaP)
Ā
AlirezaP/AP.NanoFrameWork.NRF24L01PALNA (github.com)
Ā
NanoFrameWork makes it easy to deal with the hardware. Forget low-level c or c++ code, just focus on the Ideas ;)
Schematics

Code
