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

How to write array with SPI


  • Please log in to reply
3 replies to this topic

#1 dragonfly

dragonfly

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationSouth Africa

Posted 14 November 2010 - 02:39 PM

Hello

I'm using SPI to drive a two 8-bit shift registers (74HC595) which in turn drive an LCD display (LM6029). The output loop seems a bit slow because the code needs to call spi.Write once for each output, eg:

           SPI.Configuration config = new SPI.Configuration(
               Pins.GPIO_PIN_D10,
               false,
               0,
               0,
               false,
               true,
               10,
               SPI.SPI_module.SPI1);

            spi = new SPI(config);
     
            ushort[] d = new ushort[1];

            for (int i = 0; i < this.bufferSize; i++)
            {
                d[0] = buffer[i];

                spi.Write(d);
            }

It seems it would be more efficient to be able to write the buffer array with one call to spi.Write and the method seems to be able to support it as it takes an array as the parameter, eg:

       spi.Write(buffer);

Running the latter seems to result in only the last element of the array being output instead of all the elements being output sequentially.

I have compared this to Szymon's code here and he also calls write() once for each output instead of sending an array.

I'm just wondering what the the difference is between calling write with an array with one element in it, compared to calling write with an array of multiple values. I imagine calling write with one value multiple times would behave the same as calling write once but with multiple values.

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 14 November 2010 - 03:31 PM

dragonfly, Hmm...we write SPI arrays frequently without incident, but generally with the 8-bit version. If the 16-bit version isn't writing arrays, we should log that as a bug and check it out... Do you have an oscilloscope or logic analyzer by any chance (so you can verify that arrays are indeed not being written)? Chris

#3 dragonfly

dragonfly

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationSouth Africa

Posted 14 November 2010 - 07:52 PM

Hey Chris. I don't have an oscilloscope or logic analyzer but I think I know whats wrong and it isn't SPI. In typical newb fashion I have misunderstood why this works. It seems the only reason this works is because the CS line (GPIO 10 in the code) is acting to latch the shift registers. What appears to be happening is that the SPI does write the whole data array but the output only latches at the end of the write cycle when the CS line goes low. Calling write() successively for each element latches the shift register on each element. So I guess the actual question is how to toggle the latch pin after each array element is written without having to repeatedly call spi.Write().

#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 14 November 2010 - 08:49 PM

dragonfly, If your device needs to have the CS line deselected in between each value in the array, you'll probably need to send each element of the array individually... This is something that could be tweaked via a firmware modification if there enough devices out there that can only handle single-element transfers... Chris




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.