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

Unable to change SPI CPOL with P2 f/w 4.2.2

SPI Netduino Plus 2

  • Please log in to reply
16 replies to this topic

#1 rcross

rcross

    New Member

  • Members
  • Pip
  • 7 posts

Posted 13 January 2013 - 03:33 AM

Hello,

 

A new user here. I have experience with native embedded, mostly AVR/MSP430/PIC, some ARM M3. Now exploring NetMF waters somewhat cautiously :-).

 

I am glad to see SPI timing fixes in 4.2.2, which helps immenselly. However I am still seeing CPOL issue. I am unable to use SPI modes 0 and 1, because CPOL appears to be 1, regardless of Clock_IdleState parameter of SPI.Configuration.

 

Here is my code:

 

public class SPIMain{    private static byte[] sendBuffer;    private static byte[] receiveBuffer;    public static void Main()    {        SPI.Configuration spiConfiguration = new SPI.Configuration(                             Pins.GPIO_PIN_D10,                             false,     // CS active low                             2,         // CS setup time (2us)                             2,         // CS hold time (2us)                             false,     // clock state during idle                             true,      // sample of which edge                             500,       // clock rate in kHz                             SPI_Devices.SPI1);        SPI spi = new SPI(spiConfiguration);        sendBuffer = new byte[10];        receiveBuffer = new byte[10];        sendBuffer[0] = 0xAA;        sendBuffer[1] = 0x55;        while (true)        {            DoubleSend(spi);        }    }    private static void DoubleSend(SPI spi)    {        spi.WriteRead(sendBuffer, 0, 2, receiveBuffer, 0, 2, 0);        spi.WriteRead(sendBuffer, 0, 2, receiveBuffer, 0, 2, 0);    }}

What I am seeing is this:

 

 

Attached File  Netduino_ClockHigh.JPG   69.99KB   26 downloads

 

I would expect SCK to drop low after the last byte and stay low when /CS goes high between the transmissions. SCK is also analog channel in the scope.

 

Am I missing anything here?

 

--rC



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 13 January 2013 - 04:19 AM

Hi rcross, Curious. For diagnostics here...if you set the clock idle state to true, does that reverse things properly? If you swap the clock idle edge? I see that the STM32 core in NETMF toggles CPHA, but it ties CPOL directly to the clock idle state. Chris

#3 rcross

rcross

    New Member

  • Members
  • Pip
  • 7 posts

Posted 13 January 2013 - 05:23 AM

[Edit: fixed the pictures]

 

Hi Chris,

 

thanks for speedy reply. I think your description makes sense from ARM core perspective - NetMF docs would imply that Clock_IdleState determines CPOL, and that CPHA is determined by Clock_Edge. In NetMF definition the two are independent, while SPI spec says that CPHA is relative to CPOL, but I could live with the independt values. However I suspect some layer above ARM core is keeping some residual state when setting these up.

 

For the sake of experiment I tried all four combinations. BTW, the reason I do double transaction in the experiment, is that I see different behavior depending on what the state of SCK line was prior to /CS transition.

 

1. Clock_IdleState = true, Clock_Edge = false. According to NetMF docs this should be mode 2 - CPOL=1, CPHA=0. What I am seeing is mode 1 - CPOL=0, CPHA=1. Also, notice extraneous MOSI pulse while setting first bit for the first transaction:

 

Attached File  Netduino_ClkIdle-TRUE_Edge-FALSE.JPG   69.25KB   38 downloads

 

 

2. Clock_IdleState = true, Clock_Edge = true. According to docs this should be mode 3 - CPOL=1, CPHA=1. What I am seeing is two different behaviors - on first transaction the SCK drops to low just before /CS transition, and it is totally messed up transaction - there are 17 SCK positive edges within /CS assertion window (MOSI is sampled on pos edge). Then it leaves SCK in high state (as Clock_IdleState instructs)  and subsequent send is mode 3 CPOL=1, CPHA=1:

 

Attached File  Netduino_ClkIdle-TRUE_Edge-TRUE.JPG   70.39KB   21 downloads

 

3. Clock_IdleState = false, Clock_Edge = false. According to docs this should be mode 1 - CPOL=0, CPHA=1. What I am seeing is more or less mode 1 - CPOL=0, CPHA=1. There is inexplicable pulsing of MOSI after /CS before first SCK pulse, but it is valid from SPI perspective, yet it smells fishy:

 

Attached File  Netduino_ClkIdle-FALSE_Edge-FALSE.JPG   69.37KB   27 downloads

 

4. Clock_IdleState = false, Clock_Edge = true. According to docs this should be mode 0 - CPOL=0, CPHA=0. What I am seeing is different behavior during first transaction than second. For the first transaction mode is 0 - CPOL=0, CPHA=0, but there is short pulsing of MOSI during setup of the first bit (which is valid, but suspicious), then on the second transaction mode is 3 - CPOL=1, CPHA=1:

 

Attached File  Netduino_ClkIdle-FALSE_Edge-TRUE.JPG   69.61KB   29 downloads

 

Thus I stand corrected - I am not able to achieve just mode 0. Even though the behavior is contradicting documentation for most part.

 

I suppose it would not hurt to start digging through the Netduino and NetMF code at this point.

 

--rC

 

 



#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 13 January 2013 - 05:48 AM

Hi rcross, Thank you very much for the additional data. Just to clarify: you are now able to invert the clock idle signal--but there are some SPI CPOL/CPHA modes you'd like to be able to represent in NETMF? BTW, the extra signaling seem to be setup-related. We've seen those while working on the SPI code. Some of them are related to how NETMF uses SPI generally, and some of them are related to how the STM32 core behaves. In all cases that we've studied, the extra signaling is benign as it happens when the SPI clock is disabled and therefore is not interpreted by the attached device. I think the best thing to do is to bring the current NETMF implementation more in line with the traditional SPI CPOL/CPHA standard. We want to avoid breaking backwards-compatibility with existing NETMF code, but we would prefer to err on the side of technical correctness wherever possible. Chris

#5 rcross

rcross

    New Member

  • Members
  • Pip
  • 7 posts

Posted 13 January 2013 - 06:07 PM

Hi Chris,

 

Yes, I was able to invert the clock. My remaining concerns are twofold -

 

a ) I am not able to implement mode 0 reliably;

b ) incorrect and unstable behavior when switching modes (see case 2 in the post above).

 

I will run experiments with FEZ Panda II to see if it has the same issue. Also, it would be interesting to see what Netduino or Netduino Plus do in this case.

 

I am pondering netMF/Netduino SDK fixes, but I do not have RVDS license and reading the forums I get the impression that GCC toolchain is not a good path due to size of resulting binaries.

 

Do you think GCC toolchain (yagarto or coocox) is a viable option, as in, not just for trying it out, but for compiling the production final firmware?

 

--rC



#6 rcross

rcross

    New Member

  • Members
  • Pip
  • 7 posts

Posted 13 January 2013 - 06:59 PM

Ran a quick test on PandaII. The mode support is clean - all four modes work as specified.

 

Here is an example of mode 0:

 

Attached File  PandaII_ClkIdle-FALSE_Edge-TRUE.JPG   50.09KB   21 downloads

 

Thus it appears that mapping Clock_IdleSate and Clock_Edge to CPOL and CPHA is a Netduino platform issue and not NetMF issue.

 

On PandaII am also seeing the effect of previous CPOL when changing CPOL for the new mode - the previous CPOL will be the state of SCK when /CS is asserted, but will snap to the new CPOL when /CS is deasserted. I am not sure whether this is desired behavior - on one hand it is doing what it is told to do, on the other hand it would require extra SCK transition to do what it is told to do. I think I could live with this.

 

Couple more things to note:

 

1. USBizi is much slower chip, thus byte boundaries are more distinct.

2. Even being a slower chip it is closer to the specified 500kHz frequency at ~450kHz than NP2, which runs ~350kHz.

3. For the USBizi the whole SPI transaction looks so much cleaner in the LA/scope than NP2. I know it has nothing to do with reliability, but still, it inspires confidence, when observed behavior matches specified behavior.

 

So how about that encouraging nudge towards GCC toolchain?  ;)

 

--rC



#7 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 13 January 2013 - 07:21 PM

So how about that encouraging nudge towards GCC toolchain?  ;)

 

AFAIK Netduino 2 firmware can be compiled with any of the recent GNU-based toolchains, but it requires some changes. For Netduino firmware you'd need size-optimized C RunTime libraries - the latest release of GNU Tools for ARM Embedded Processors has introduced newlib-nano, the resulting image size is very similar to RVCT (used to build the official firmware).

 

Also, there is free 30-days limited RVDS edition available on ARM website...



#8 rcross

rcross

    New Member

  • Members
  • Pip
  • 7 posts

Posted 13 January 2013 - 07:26 PM

Hi CW2,

 

Thanks for the nudge. 

 

Do you know an entry point in the Netduino wiki for the GCC toolchain? I am still searching through various threads and external resources trying to piece the strategy.

 

--rC



#9 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 13 January 2013 - 08:15 PM

Do you know an entry point in the Netduino wiki for the GCC toolchain? I am still searching through various threads and external resources trying to piece the strategy.

 

Hm, I don't think there is any  :(

 

Unfortunately, I have not had a chance yet to play with Netduino Plus 2 source code, so I don't know exactly what needs to be changed in order to get GCC-built firmware working, but you could have a look at my NetduinoGoFirmware repository and there is also patch for GHI's Cerberus (it also uses STM32 micro).

 

If you are interested, there are a few hints:

  • Flash memory layout file (aka "scatterfile") is usually correct only for the compiler used to produce the official firmware, i.e. RVDS in case of Secret Labs. Keil MDK uses the same compiler/linker, so scatterfile can be created by simply copying RVDS one. Scatter file for GCC is almost always completely wrong. In all likelihood you will not be able to use NetduinoGo GCC scatterfile directly, but it should not be too hard to modify it (I think Netduino Plus 2 uses additional bootloader section or such),
  • There were a few assembly routines incorrectly generating ARM instructions, which caused fault on Cortex-M4. It was needed to add ".thumb_func" directive there, but I think this was fixed in .NET MF 4.2. The other problem was that the default build configuration forces use of ARM libraries, these linker settings has to be changed so correct Thumb libraries are used (the path can vary depending on the toolchain used),
  • Yagarto does not have longlong support by default, which was causing some problems with string formatting. I solved that by compiling a custom version. But I also noticed there was a fix related to number formatting in .NET MF 4.2, so it could be related.
  • Nonetheless, I switched to different toolchain (GNU Tools for ARM Embedded).

 



#10 rcross

rcross

    New Member

  • Members
  • Pip
  • 7 posts

Posted 13 January 2013 - 10:26 PM

Thank you so much! I'll give GNU Tools for ARM Embedded a try.



#11 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 14 January 2013 - 07:30 AM

Incidentally, there is now Building Netduino Plus 2 Firmware 4.2.1.2 with Yagarto GCC 4.6 wiki page - thanks ziggurat29!

 

(The patch file is available under "Attachments" button, top right corner)



#12 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 14 January 2013 - 02:33 PM

That's a great write up ziggurat29! I have yet to try compiling my own Netduino firmware as it did not seem like a task for just mere mortals. Your tutorial is well written and has removed much of the mystery and confusion. Now I just need to find the time to give it a shot, though one shouldn't expect any radical firmware enhancements from me at anytime in the near future. Probably just a bunch of noob questions :)

 

Thanks!

Steve



#13 rcross

rcross

    New Member

  • Members
  • Pip
  • 7 posts

Posted 15 January 2013 - 04:56 AM

Yes, very useful information. Thank you!

 

Now just need to wait till 4.2.2 source is available..



#14 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 24 January 2013 - 01:52 AM

Yes, very useful information. Thank you!   Now just need to wait till 4.2.2 source is available..
Now posted on downloads page. Includes Netduino 2, Netduino Plus 2, and Shield Base (NETMF) source. Chris

#15 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 25 January 2013 - 06:01 PM

Hi rcross,

 

I tried to replicate what you were seeing, using the same code as the sample you provided. It would appear that I am getting different results than you. I too am using the latest firmware, 4.2.2.0 on the Netduino Plus 2. 

 

Using my Saleae Logic here are the results:

 

Mode 0 - which "seems" to work ok for me. 

Attached File  NP2_SPI-Mode_0.png   656.63KB   5 downloads

 

Mode 1 - again this seems to be ok

Attached File  NP2_SPI-Mode_1.png   651.25KB   5 downloads

 

Mode 2 - this is where things start to get wonky. I've tried playing with the ChipSelect_SetupTime and ChipSelect_HoldTime but haven't been able to find the proper combination, if that's even a possible solution. 

Attached File  NP2_SPI-Mode_2.png   653.47KB   4 downloads

 

Mode 3 - again, this one seems to be considerably off as well. 

Attached File  NP2_SPI-Mode_3.png   672.33KB   3 downloads

 

My knowledge of SPI is limited at best, but I'm hoping to gain a better understanding while I work through some of these issues. Any input you and the rest of the braniacs in this thread can provide would be greatly appreciated. :)

 

Cheers,

Steve



#16 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 26 January 2013 - 06:48 AM

So this is looking a lot better. The timing issues with CPOL and CPHA seem to have been fixed with the latest firmware updates, version 4.2.2.1. 0/

 

Here are some screen shots of the various modes, now correctly being interpretted by the Logic's SPI analyzer. There does seem to be an inconsistent blip on the MISO and sometimes the MOSI lines, however they don't affect anything in the transmission. 

 

Mode 0:

Attached File  NP2_SPI-Mode_0-v4221.png   658.36KB   3 downloads

 

Mode 1:

Attached File  NP2_SPI-Mode_1-v4221.png   659.38KB   3 downloads

 

Mode 2:

Attached File  NP2_SPI-Mode_2-v4221.png   649.97KB   3 downloads

 

Mode 3:

Attached File  NP2_SPI-Mode_3_v4221.png   649.62KB   3 downloads

 

Thanks Chris for quickly looking into these issues and providing a solution so soon!

 

Cheers,

Steve



#17 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 26 January 2013 - 07:03 AM

Thanks for verifying this, Steve.

BTW, the blips seem to be the STM32's way of getting into the proper state (mindset?) for its operations. They should not affect SPI communication in any way since they're outside of the sampling edge.

Here's a link to the new firmware:
Netduino Plus 2 4.2.2.1 firmware

Chris





Also tagged with one or more of these keywords: SPI, Netduino Plus 2

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.