Hi gents,
We have “Cerbuino Bee” but we didn’t find any tutorial or example code to use SPI
http://www.ghielectronics.com/support/dotnet-micro-framework
As simple as that, Our target is to read from an IMU which talks via SPI
Thnx ![]()
Hi gents,
We have “Cerbuino Bee” but we didn’t find any tutorial or example code to use SPI
http://www.ghielectronics.com/support/dotnet-micro-framework
As simple as that, Our target is to read from an IMU which talks via SPI
Thnx ![]()
Have a look at codeshare. There are many examples
@ PetaByte - Start with the official tutorial from GHI
@ RobvanSchelven yea, a lot of examples but not that much for Cerbuino Bee
@ Architect I found those examples for console examples, but I thought this code will conflict with the previous code which was written in Gadgeteer!!
Does it differ from Gadgteer, and will it work in ProgrameStarted(){…}?
Thnx all 
@ PetaByte - It’s just something like this:
private static GTI.SPI _spi;
private static GTI.SPI.Configuration _config;
private void ProgramStarted()
{
_socket = GT.Socket.GetSocket(1, true, null, null);
_config = new GTI.SPI.Configuration(false, 0, 0, false, true, 2000);
_spi = new GTI.SPI(_socket, _config, GTI.SPI.Sharing.Exclusive, _socket, GT.Socket.Pin.Five, null);
@ Justin thnx dude for your continuous support, I will try it soon 
@ PetaByte - Your welcome, just make sure you post some pics of finished goodness 
@ Justin Thnx 
It seems there are some errs because of the name space !!
BTW: Here is all my code which was logging from the GPS to the SD card successfully…
// This code is grapping RAW NMEA data from GPS module,
// and store it into SD card...
// Last modification 1/1/2013, Tue, 8:18PM
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using System;
using System.IO;
using Microsoft.SPOT.IO;
namespace GadgeteerApp2
{
public partial class Program
{
private GT.Timer _timer;
private string _root;
private static GTI.SPI _spi;
private static GTI.SPI.Configuration _config;
void ProgramStarted()
{
_socket = GT.Socket.GetSocket(1, true, null, null);
_config = new GTI.SPI.Configuration(false, 0, 0, false, true, 2000);
_spi = new GTI.SPI(_socket, _config, GTI.SPI.Sharing.Exclusive, _socket, GT.Socket.Pin.Five, null);
GHI.OSHW.Hardware.StorageDev.MountSD();
if (VolumeInfo.GetVolumes()[0].IsFormatted)
{
_root = VolumeInfo.GetVolumes()[0].RootDirectory;
_timer = new GT.Timer(5000);
_timer.Tick += new GT.Timer.TickEventHandler(TimerTick);
_timer.Start();
}
// Serial Reading
usbSerial.Configure(9600,
GT.Interfaces.Serial.SerialParity.None,
GT.Interfaces.Serial.SerialStopBits.One,
8);
usbSerial.SerialLine.Open();
usbSerial.SerialLine.DataReceived += new
GT.Interfaces.Serial.DataReceivedEventHandler(SerialLine_DataReceived);
// ***
}
void TimerTick(GT.Timer timer)
{
string fileName = Path.Combine(_root, "DONETracksLogNMEA11111.txt");
Stream stream;
// GPS Reading
int NumberOfBytesToRead = usbSerial.SerialLine.BytesToRead;
byte[] readInputBuffer = new byte[NumberOfBytesToRead];
usbSerial.SerialLine.Read(readInputBuffer, 0, NumberOfBytesToRead);
// ***********
if(File.Exists(fileName))
{
stream = File.OpenWrite(fileName);
stream.Position = stream.Length; //FM Not To Overwrite
}
else
{
stream = File.Create(fileName);
}
using(var writer = new StreamWriter(stream))
{
// GPS writing to SD
for (int j = 0; j < NumberOfBytesToRead; j++)
{
char a = (char)readInputBuffer[j];
writer.Write(a.ToString());
}
// ***
writer.WriteLine("***");
}
stream.Dispose();
}
void SerialLine_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
{
}
}
}
GTI is an abbreviation for Gadgeteer.Interfaces, so you’ll want a declaration near the top saying:
using GTI = Gadgeteer.Interfaces;
There’s also an error because _socket isn’t declared, maybe add to the other variable declarations in Program:
private static GT.Socket _socket;
RorschachUK
How did you create your project? And what does your designer surface look like, what mainboard and modules?
Your namespace reference issue is because of a missing reference but the normal Gadgeteer project setup should create everything you need (not that I can test it, I’m using my Surface right now). You could add it manually, you’re looking for Gadgeteer interfaces. Edit: yes, its the using statement its missing… but that usually comes in a default project too
Hi all,
@ RorschachUK:
You are right, we added these definitions and (Gadgeteer.SPI) reference, and everything was pretty good and no errs.
private static GT.Socket _socket;
private static GTI.SPI.Configuration _config;
private static GTI.SPI _spi;
NOW WE ARE TRYING TO READ FROM THE SENSOR 
Any Idea???

Thnx all
Or even if you tell us how to how loop-back test 
To do a loop back test you must connect SDO and SDI together.
byte[] BytesWrite=new byte[]{0,1,2,3,4};
byte[] BytesRead=new byte[5];
_spi.WriteRead(BytesWrite, BytesRead);
After the WriteRead the BytesRead should contain the same bytes as BytesWrite…
SPI1 is documented as on the following:
13 PB3 SPI1 SCK
12 PB4 SPI1 MISO, PWM 7
11 PB5 SPI1 MOSI, PWM 6
and also on Gadgeteer socket 1:
Pin 7 SPI1 MOSI PWM 6, CAN2 RX, PB5
Pin 8 SPI1 MISO PWM 7, PB4
Pin 9 SPI1 SCK PWM 8, PB3
So lets step through the foundation concepts here.
_socket = GT.Socket.GetSocket(1, true, null, null);
Is allocating Socket 1
_config = new GTI.SPI.Configuration(false, 0, 0, false, true, 2000);
Is setting up the SPI general configuration
_spi = new GTI.SPI(_socket, _config, GTI.SPI.Sharing.Exclusive, _socket, GT.Socket.Pin.Five, null);
Is using the socket and configuration you just created, in exclusive mode, and is using socket pin 5 as the Chip Select pin.
One thing that really helps you understand this kind of thing is to start re-typing the lines of code in your program rather than just pasting it in from somewhere else - you’ll see that intellisense pops up great information about what the command structure is, what the parameters mean, and what other options you might find if you need it.
Guys, I don’t know how to thank you :))
It was working perfectly!
I used GT.Timer this time, and it was working perfectly, but suddenly I got this output:
[em]The thread ‘’ (0x3) has exited with code 0 (0x0).
[/em]
Unfortunately, somehow after a while the result started to be missed up again!!
Here is my code:
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.Interfaces;
namespace GadgeteerApp3
{
public partial class Program
{
static GT.Socket _socket;
static GTI.SPI.Configuration _config;
static GTI.SPI _spi;
GT.Timer timer = new GT.Timer(1000);
// This method is run when the mainboard is powered up or reset.
public void ProgramStarted()
{
timer.Tick += new GT.Timer.TickEventHandler(spiTimer);
timer.Start();
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
_socket = GT.Socket.GetSocket(1, true, null, null);
_config = new GTI.SPI.Configuration(false, 0, 0, false, true, 2000);
_spi = new GTI.SPI(_socket, _config, GTI.SPI.Sharing.Exclusive, _socket, GT.Socket.Pin.Five, null);
//Timer MyTimer = new Timer(new TimerCallback(spiTimer), null, 5000, 1000);
//Thread.Sleep(Timeout.Infinite);
}
static void spiTimer(GT.Timer timer) {
byte[] BytesWrite = new byte[] { 0, 1, 2, 3, 4 };
byte[] BytesRead = new byte[5];
_spi.WriteRead(BytesWrite, BytesRead);
Debug.Print("**********");
for (int i = 0; i < 5; i++)
Debug.Print(BytesRead[i].ToString());
}
}
}