SPI communication

Hello,
I have EMX module and Freescale microcontroller. I’d like to make communication between them by 16 bit SPI. And it is desired to set this 16 bit register control like that:
1-2nd bits - command ID (start, stop, error)
3rd bit - positive(1) or negative(0).
4-16 bits - value.
Is it possible to make it in the code?
Thank you.

I don’t see why not – NETMF has full support for SPI functionality. Just make a two-byte array, and set the appropriate values. Bit operations are expensive in NETMF, so keep that in mind when you’re doing bitwise math.

SPI is a pretty loose specification, but most options are supported by NETMF. You’ll have to figure out what clock polarity you want, etc, but once you have it configured, you should be good to go.

@ jay - Here is my code:

//Frequency is 1KHz, chip select is on zero, clock is idle low (false) with clocking on rising edge (true) and with zero for select setup and hold time.
SPI.Configuration MyConfig =
new SPI.Configuration((Cpu.Pin)EMX.Pin.IO26,
false, 0, 0, false, true, 1, SPI.SPI_module.SPI1);
MySPI = new SPI(MyConfig);

byte[] answer = new byte[]{???};// Here is te problem, I want to represent byte number like in C, like int i= 0b010110101110000, but C# doesn’t allow it :frowning:
byte[] measure = new byte[];

MySPI.WriteRead(measure, answer);
Any ideas?

First of all, remember to wrap your code in the [ code ] tags when posting on this forum; it makes it easier to read.

Most programming environments don’t have support for binary literals. Hex is usually preferred.
0b010110101110000 is 0x2D70, so it should be:


byte[] answer = new byte[]{0x2D, 0x70 };

If you want to build it programmatically, you’ll have to do bit shifting and such.


ushort value;
byte signBit;
byte commandID;
byte[] answer = new byte[] { (byte)((value<<8) & 0x1F + (signBit << 6) & 0x01 + (commandID << 7) & 0x01),  (byte)(value & 0xFF)}

@ Ugeen - We have tons of examples on how to use SPI on codeshare. I would use ushort instead of byte for 16bit SPI

Aha, understand, thank you, guys.

Hello again,
I’m trying to connect EMX with Freescale. But neither MISO nor MOSI don’t transfer data. However, chip select pin and clock are ok. How do you think, could it be because of low clock frequency(I’m using 1 KHz)? Or maybe Freescale is not ok?
Here is my config:

 //Frequency is 1KHz, chip select is on zero, clock is idle low (false) with clocking on rising edge (true) and with zero for select setup and hold time.
        SPI.Configuration MyConfig =
             new SPI.Configuration((Cpu.Pin)EMX.Pin.IO26,
             false, 0, 0, false, true, 1, SPI.SPI_module.SPI1);

P.S. What does define

SPI.SPI_module.SPI1

?

@ Ugeen - Certain pins belong to SPI1 or SPI2, etc. by specifying SPI.SPI_module.SPI1 you are indicating which one you using exactly so in this case it will be MISO1, MOSI1 etc.

Can you tell how you connect the pins?

1KHz seems slow for SPI.

@ Architect - My SPI pins are I/O 24-27. It’s SPI1. Our pins(EMX & Freescale) are connected through Freescale TOWER system.

Is the Freescale set up as a slave?

@ Architect - Yes, It is.

Try use higher bus speed value. Check at what speed Freescale SPI works.

Somewhat related to the topic, but just wondering why my SPI.Configuration constructor is different than the original poster’s:

my code is:


using Gadgeteer.Interfaces;

GT.Socket _socket = GT.Socket.GetSocket(6, true, null, null);
ConfigSPI = new SPI.Configuration(false, 0, 0, false, true, 1000);
SPI MySPI = new SPI(_socket, ConfigSPI, SPI.Sharing.Shared, maxO);

Just using the maxO module as an example, not sure if it uses SPI. I know that it has its own interface for writing to it.

Different device - different configuration. :slight_smile:

oh…haha true!

EDIT: question moved to new thread

@ Maelstrom - I would start a new thread.