Is there a way to get the a RFM12B radio module work with a Raspberry 2 (Windows 10 IOT) over SPI? Can I connect the RFM12B directly to the Raspberry? Are there any code examples in C#?
-
1I don't think you will find a ready made solution for this one, as win 10 is relatively new, but i am sure if you know how to use SPI in win10, it should not be a problem.– dhruvvyas90Oct 7, 2015 at 15:42
-
Can you recommend a tutorial for SPI on Win 10 IOT?– user2025830Oct 8, 2015 at 6:32
-
I would stick to official guide. This link seems to be a good starting point. ms-iot.github.io/content/en-US/win10/StartCoding.htm– dhruvvyas90Oct 8, 2015 at 6:43
1 Answer
Here is some sample code how to initialise SPI in WinIoT. You first need to enable GPIO, then the SPI.
using Windows.Devices.Enumeration;
using Windows.Devices.Spi;
using Windows.Devices.Gpio;
using DisplayFont;
/* Initialize GPIO, SPI, and the display */
private async void InitAll()
{
try
{
InitGpio(); /* Initialize the GPIO controller and GPIO pins */
await InitSpi(); /* Initialize the SPI controller */
}
/* If initialization fails, display the exception and stop running */
catch (Exception ex)
{
Text_Status.Text = "Exception: " + ex.Message;
if (ex.InnerException != null)
{
Text_Status.Text += "\nInner Exception: " + ex.InnerException.Message;
}
return;
}
Text_Status.Text = "Initialized";
}
/* Initialize the GPIO */
private void InitGpio()
{
try
{
IoController = GpioController.GetDefault(); /* Get the default GPIO controller on the system */
/* Initialize a pin as output for the Data/Command line on the display */
DataCommandPin = IoController.OpenPin(DATA_COMMAND_PIN);
DataCommandPin.Write(GpioPinValue.High);
DataCommandPin.SetDriveMode(GpioPinDriveMode.Output);
/* Initialize a pin as output for the hardware Reset line on the display */
ResetPin = IoController.OpenPin(RESET_PIN);
ResetPin.Write(GpioPinValue.High);
ResetPin.SetDriveMode(GpioPinDriveMode.Output);
}
/* If initialization fails, throw an exception */
catch (Exception ex)
{
throw new Exception("GPIO initialization failed", ex);
}
}
/* Initialize the SPI bus */
private async Task InitSpi()
{
try
{
var settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE); /* Create SPI initialization settings */
settings.ClockFrequency = 10000000; /* Datasheet specifies maximum SPI clock frequency of 10MHz */
settings.Mode = SpiMode.Mode3; /* The display expects an idle-high clock polarity, we use Mode3
* to set the clock polarity and phase to: CPOL = 1, CPHA = 1
*/
string spiAqs = SpiDevice.GetDeviceSelector(SPI_CONTROLLER_NAME); /* Find the selector string for the SPI bus controller */
var devicesInfo = await DeviceInformation.FindAllAsync(spiAqs); /* Find the SPI bus controller device with our selector string */
SpiDisplay = await SpiDevice.FromIdAsync(devicesInfo[0].Id, settings); /* Create an SpiDevice with our bus controller and SPI settings */
}
/* If initialization fails, display the exception and stop running */
catch (Exception ex)
{
throw new Exception("SPI Initialization Failed", ex);
}
}
And in your main code, you will define the PINs
public sealed partial class MainPage : Page
{
/* Important! Uncomment the code below corresponding to your target device */
/* Uncomment for MinnowBoard Max */
//private const string SPI_CONTROLLER_NAME = "SPI0"; /* For MinnowBoard Max, use SPI0 */
//private const Int32 SPI_CHIP_SELECT_LINE = 0; /* Line 0 maps to physical pin number 5 on the MBM */
//private const Int32 DATA_COMMAND_PIN = 3; /* We use GPIO 3 since it's conveniently near the SPI pins */
//private const Int32 RESET_PIN = 4; /* We use GPIO 4 since it's conveniently near the SPI pins */
/* Uncomment for Raspberry Pi 2 */
private const string SPI_CONTROLLER_NAME = "SPI0"; /* For Raspberry Pi 2, use SPI0 */
private const Int32 SPI_CHIP_SELECT_LINE = 0; /* Line 0 maps to physical pin number 24 on the Rpi2 */
private const Int32 DATA_COMMAND_PIN = 22; /* We use GPIO 22 since it's conveniently near the SPI pins */
private const Int32 RESET_PIN = 23; /* We use GPIO 23 since it's conveniently near the SPI pins */
// ... the rest of your code
}