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 + InvalidOperationException


  • Please log in to reply
7 replies to this topic

#1 demonGeek

demonGeek

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationCanada

Posted 11 March 2011 - 07:27 PM

I just want to instantiate an SPI object but I keep getting this:

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

Here's the code:

SPI.Configuration spiConfig = new SPI.Configuration(Pins.GPIO_PIN_D8, false, 250, 250, true, true, 1000, SPI.SPI_module.SPI1);
SPI spi = new SPI(spiConfig);

What am I doing wrong?

 
EDIT:
I've been looking at this for a while now and I'm beginning to wonder if this error might indicate that there's a problem with the connection to the SPI device (or the device itself). The device is this flash memory board connected as follows:

CS# <-----> D8
SCK <-----> D13
SDI <-----> D12
SDO <-----> D11


I've also tried (unsuccessfully) connecting the BUSY# pin on the board to D7 and then specifying that in the other constructor overload:

SPI.Configuration spiConfig = new SPI.Configuration(Pins.GPIO_PIN_D8, false, 250, 250, true, true, 1000, SPI.SPI_module.SPI1, Pins.GPIO_PIN_D7, false);
SPI spi = new SPI(spiConfig);

[N+ firmware v4.1.1.0 ALPHA7]

#2 demonGeek

demonGeek

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationCanada

Posted 16 March 2011 - 12:50 AM

**bump** Anyone have any ideas? I'm completely stuck on this one - I don't know if it's the code, the connections or the device itself. Any thoughts would be appreciated.

#3 GeoAdams

GeoAdams

    New Member

  • Members
  • Pip
  • 7 posts
  • LocationCincinnati

Posted 17 March 2011 - 01:30 AM

Hello Here is what I have in a class by itself that works for SPI configuration for the MCP2515 can controller private SPI.Configuration SPIConfig; private SPI CANSPI; Then later in the class on the creation of the CANPORT object SPIConfig = new SPI.Configuration(Pins.GPIO_PIN_D10, false, 0, 0, true, true, 6000, SPI_Devices.SPI1); CANSPI = new SPI(SPIConfig); This code works as I can see data being written to the controller using a spi bus monitor This is the only way I can get the spi to work. The code will not work if the SPI.Configuration is placed like this SPI.Configuration SPIConfig = new SPI.Configuration. It creates the exception as you described above. I don't understand why Hope this helps George

#4 demonGeek

demonGeek

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationCanada

Posted 17 March 2011 - 04:31 AM

This code works as I can see data being written to the controller using a spi bus monitor

This is the only way I can get the spi to work. The code will not work if the SPI.Configuration is placed like this SPI.Configuration SPIConfig = new SPI.Configuration. It creates the exception as you described above. I don't understand why

Hope this helps

George


Hi George

You're right, that makes no sense but I tried it anyway: still doesn't work for me though.

I think this exception is a catch-all for just about any unexpected condition and as such isn't very useful.

If you have SPI working with that code then I think I can rule out my code which leaves either the connections or the device itself. Unfortunately I don't have another SPI device to test with at the moment, I think I'll have to order something and then test against that.

Thanks for your help.

- Adam

#5 GeoAdams

GeoAdams

    New Member

  • Members
  • Pip
  • 7 posts
  • LocationCincinnati

Posted 17 March 2011 - 09:48 AM

Adam Here is another idea to try. Since spi is basically a shift register, what you write you should be able to read. You can test the spi controller and your code by connecting MOSI and MISO (11 and 12)on the Netduino+ together. This removes the slave device from the loop. The clock line and the device won't matter, but disconnect them from the device anyway. Try this code: public static void SPI_Test() { var SPIConfig = new SPI.Configuration(Pins.GPIO_PIN_D10, false, 0, 0, true, true, 6000, SPI_Devices.SPI1); var SPI = new SPI(SPIConfig); //connecting MOSI MISO together will allow transmitted data to be read back for testing byte[] WriteBuff = new byte[6] {0x00, 0x01, 0x02, 0x03, 0x55, 0xAA}; byte[] ReadBuff = new byte[6]; SPI.WriteRead(WriteBuff, ReadBuff); } Basically you create an array for writing, write it to the spi controller and then read it back. The ReadBuff should be the same as the WriteBuff. If this doesn't work, it is not the spi device causing the problem. This is the method I used for developing the code for my canbus controller before I got the hardware. Note the spi declarations are of type var. This is different than the code I posted earlier. Initially this was the only way I could get spi to work. I think something the variable declarations and intializers are not right in the framework code. Hope this helps George

#6 demonGeek

demonGeek

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationCanada

Posted 17 March 2011 - 07:02 PM

Here is another idea to try. Since spi is basically a shift register, what you write you should be able to read. You can test the spi controller
and your code by connecting MOSI and MISO (11 and 12)on the Netduino+ together. This removes the slave device from the loop. The clock line and the device
won't matter, but disconnect them from the device anyway. Try this code:


George

Thank you very much! Your loopback idea put me on the right track to solve the problem.

I had been playing with a motor controller last week which was using a static singleton class that locked D12 before my SPI code ran. When I first tried your code it didn't work so out of desperation I created a completely new project and suddenly the code started working. After that it was just a process of elimination to find the offending code.

- Adam

#7 GeoAdams

GeoAdams

    New Member

  • Members
  • Pip
  • 7 posts
  • LocationCincinnati

Posted 17 March 2011 - 09:44 PM

Adam Glad to here you have the spi bus operational George

#8 msakms

msakms

    New Member

  • Members
  • Pip
  • 4 posts
  • LocationEgypt

Posted 27 April 2013 - 06:47 PM

I'm facing a similar problem when trying to write a driver for a GLCD.

using System;using System.Threading;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware.Netduino;namespace Shield{    class LM6059    {	    byte[] lcd_cmds = new byte[] { 0xA2, 0xA0, 0xC8, 0x40, 0x2C, 0x2E, 0x2F, 0x26, 0x81, 0x1A, 0xAF};	    public byte[] Pages = { 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7 };	    byte Urow = 0x10;	    byte Lrow = 0x00;	    public SPI.Configuration GLCD;	    public SPI SPI_GLCD;   	    public OutputPort RES = new OutputPort(Pins.GPIO_PIN_D1, false);	    public OutputPort A0 = new OutputPort(Pins.GPIO_PIN_D2, false);	    public LM6059()	    {		    Init_LCD();	    }	    public void Init_LCD()	    {	    GLCD = new SPI.Configuration(Pins.GPIO_PIN_D0, false, 0, 0, true, true, 5, SPI.SPI_module.SPI1);	    SPI_GLCD= new SPI(GLCD);  <<<<< Code breaks here		    RES.Write(false);		    RES.Write(true);		    A0.Write(false);		    SPI_GLCD.Write(lcd_cmds);		    for (int k = 0; k < 8; k++)		    {			    A0.Write(false);			    byte[] wrtbfr = Utility.ExtractRangeFromArray(Pages, k, 1);			    SPI_GLCD.Write(wrtbfr);			    gotoColumn(0, 0);			    clearScreen();		    }	    }	    public void gotoColumn(byte row, byte column)	    {		    row += Urow;		    column += Lrow;		    A0.Write(false);		    SPI_GLCD.Write(new byte[] { row });		    SPI_GLCD.Write(new byte[] { column });	    }	    public void clearScreen()	    {		    A0.Write(true);		    for (int j = 0; j < 128; j++)		    {			    SPI_GLCD.Write(new byte[] { 0x00 });		    }	    }	    public void goto_page(byte page)	    {		    A0.Write(false);		    SPI_GLCD.Write(new byte[] { Pages[page] });	    }    }}

I get an error message at the code line  "[color=#ff0000;]SPI_GLCD= new SPI(GLCD);[/color] " shown above saying: "An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.SPOT.Hardware.dll"

 

The loopback suggestion works fine & I've checked my wirings.

 

I've spent a couple of days looking for any hints to get it going without any progress. Any guidance will be much appreciated & many thanks in advance.






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.