Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

SPI


  • Please log in to reply
11 replies to this topic

#1 Stephen

Stephen

    New Member

  • Members
  • Pip
  • 8 posts

Posted 22 August 2010 - 10:10 AM

Hello,

I'm trying to figure out SPI on the netduino with this code:

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;

namespace NetduinoApplication1 {
    public class Program {
        public static void Main() {
            SPI spi_port = new SPI(new SPI.Configuration(
                Pins.GPIO_NONE,                             // Chip select pin
                false,                                      // false - CS active state is low
                0,                                          // CS setup time in ms
                0,                                          // CS hold time
                false,                                      // false - Clk low on idle
                false,                                      // false - Data valid on falling edge
                100,                                        // Clock rate in kHz
                SPI_Devices.SPI1                            // SPI Module
                ));
            byte[] readBuffer = new byte[2];
            while (true) {                
                spi_port.WriteRead(new byte[] { 0x00, 0x55 }, readBuffer);
                //Thread.Sleep(1);
            }
        }       
    }
}

I have problem with:
1. Clock rate adjustment. What clock speed is supported by Netduino?
2. 20us before the data transfer, the clock line is weakly pulled-up for about 40us. This gave me an extra unwanted clock edge. Is it possible to remove this?

#2 pascal06

pascal06

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts
  • LocationFrance

Posted 22 August 2010 - 11:43 AM

Hello,

I'm trying to figure out SPI on the netduino with this code:

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;

namespace NetduinoApplication1 {
    public class Program {
        public static void Main() {
            SPI spi_port = new SPI(new SPI.Configuration(
                Pins.GPIO_NONE,                             // Chip select pin
                false,                                      // false - CS active state is low
                0,                                          // CS setup time in ms
                0,                                          // CS hold time
                false,                                      // false - Clk low on idle
                false,                                      // false - Data valid on falling edge
                100,                                        // Clock rate in kHz
                SPI_Devices.SPI1                            // SPI Module
                ));
            byte[] readBuffer = new byte[2];
            while (true) {                
                spi_port.WriteRead(new byte[] { 0x00, 0x55 }, readBuffer);
                //Thread.Sleep(1);
            }
        }       
    }
}

I have problem with:
1. Clock rate adjustment. What clock speed is supported by Netduino?
2. 20us before the data transfer, the clock line is weakly pulled-up for about 40us. This gave me an extra unwanted clock edge. Is it possible to remove this?


Hello Stephen,

Currently I use a clock at 1000 kHz with my nokia 3310 LCD with success,

Pascal

#3 Stephen

Stephen

    New Member

  • Members
  • Pip
  • 8 posts

Posted 22 August 2010 - 12:13 PM

Currently I use a clock at 1000 kHz with my nokia 3310 LCD with success,

Thanks Pascal,

My problem is that the hardware generates clock at a rate other than specified.

#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 August 2010 - 02:01 PM

1. Clock rate adjustment. What clock speed is supported by Netduino?


We've used a variety of clock speeds with success (internally, the .NET MF runtime divides the 48MHz clock into a small counter and then multiplies that counter to match your speed as closely as possible). What clock speed are you looking for?

2. 20us before the data transfer, the clock line is weakly pulled-up for about 40us. This gave me an extra unwanted clock edge. Is it possible to remove this?


Hmm, curious. The SPI code comes straight from Atmel and Microsoft, but we can look into this. I'd have to look at some native code SPI samples to see if this is a chip implementation or CLR implementation item. But if you need us to, we can schedule some time to analyze this further. Are you having data communication isssues on SPI because of this?

Chris

#5 pascal06

pascal06

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts
  • LocationFrance

Posted 22 August 2010 - 02:15 PM

We've used a variety of clock speeds with success (internally, the .NET MF runtime divides the 48MHz clock into a small counter and then multiplies that counter to match your speed as closely as possible). What clock speed are you looking for?



Hmm, curious. The SPI code comes straight from Atmel and Microsoft, but we can look into this. I'd have to look at some native code SPI samples to see if this is a chip implementation or CLR implementation item. But if you need us to, we can schedule some time to analyze this further. Are you having data communication isssues on SPI because of this?

Chris


Regarding clock speed, when I try 1000 Khz, I see 550.66 Khz on my oscilloscope ...

Stephen, did you see the same difference ?

Pascal

#6 pascal06

pascal06

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts
  • LocationFrance

Posted 22 August 2010 - 02:27 PM

Regarding clock speed, when I try 1000 Khz, I see 550.66 Khz on my oscilloscope ...

Stephen, did you see the same difference ?

Pascal


Also, just to check the netduino clock, I try this code (come from Expert .NET Micro Framework book) :

Cpu.GlitchFilterTime = new TimeSpan(0, 0, 0, 0, 100); //100 ms
float systemClock = Cpu.SystemClock / 1000000.0f;
Debug.Print("System Clock: " + systemClock.ToString("F6") + " MHz");
float slowClock = Cpu.SlowClock / 1000000.0f;
Debug.Print("Slow Clock: " + slowClock.ToString("F6") + " MHz");
float glitchFilterTimeMs = Cpu.GlitchFilterTime.Ticks /
(float)TimeSpan.TicksPerMillisecond;
Debug.Print("Glitch Filter Time: " + glitchFilterTimeMs.ToString("F1") +
" ms");

It works fine on emulator, but it raise a exception at first line on netduino :

"An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.SPOT.Hardware.dll"

Pascal

#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 August 2010 - 02:34 PM

Cpu.GlitchFilterTime = new TimeSpan(0, 0, 0, 0, 100); //100 ms
...

It works fine on emulator, but it raise a exception at first line on netduino :

"An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.SPOT.Hardware.dll"


Microsoft built two GlitchFilers. One is more resource-hungry and supports user-defined GlitchFilterTimes. The other is less resource-hungry and does not support user-defined GlitchFilterTimes. In the case of the AT91SAM7X512 microcontroller on the Netduino, .NET Micro Framework uses the latter.

Basically, this less resource-hungry GlitchFilter samples a pin multiple times to make sure that the signal change wasn't an electrical glitch. Crude but effective--and very fast.

Chris

#8 Stephen

Stephen

    New Member

  • Members
  • Pip
  • 8 posts

Posted 22 August 2010 - 02:47 PM

Regarding clock speed, when I try 1000 Khz, I see 550.66 Khz on my oscilloscope ...


It seems that there is minimum SPI clock speed of about 200KHz.

Desired setting -> Frequency measured on oscilloscope
6000kHz -> 6.00MHz
1000kHz -> 1.00MHz
300kHz -> 300kHz
150kHz -> 743kHz
100kHz -> 215kHz
10kHz -> 251kHz

#9 Stephen

Stephen

    New Member

  • Members
  • Pip
  • 8 posts

Posted 22 August 2010 - 03:28 PM

Are you having data communication isssues on SPI because of this?


I have not tried connecting the SPI to a device yet. I think the extra clock edge will cause communication issues for devices that do not have the Chip Select line connected.

#10 pascal06

pascal06

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts
  • LocationFrance

Posted 22 August 2010 - 04:50 PM

It seems that there is minimum SPI clock speed of about 200KHz.

Desired setting -> Frequency measured on oscilloscope
6000kHz -> 6.00MHz
1000kHz -> 1.00MHz
300kHz -> 300kHz
150kHz -> 743kHz
100kHz -> 215kHz
10kHz -> 251kHz


It's my mistake, I need to multiply by 2,
You're right for all results,
I have the same results now (approximatly),

Pascal

#11 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 August 2010 - 05:02 PM

It's my mistake, I need to multiply by 2,
You're right for all results,
I have the same results now (approximatly),


Okay, just to make sure I understand this right... The SPI clock is working properly for you now, correct?

If not, I want to make sure I get a bug report files so we can investigate...

#12 pascal06

pascal06

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts
  • LocationFrance

Posted 22 August 2010 - 05:16 PM

Okay, just to make sure I understand this right... The SPI clock is working properly for you now, correct?

If not, I want to make sure I get a bug report files so we can investigate...


My SPI works because I use 1 Mhz clock. Because netduino is the master clock, I decided to use that freqency. If the slave doesn't support freqency above 150khz, we will probably have a issue.
But it's not my case.

Pascal




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.