<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Shiftbrite SPI</title>
		<link>http://forums.leaflabs.com/topic.php?id=1004</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:13:15 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=1004" rel="self" type="application/rss+xml" />

		<item>
			<title>gbulmer on "Shiftbrite SPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1004#post-6138</link>
			<pubDate>Tue, 30 Aug 2011 17:58:19 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">6138@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;asterix - &#60;code&#62;(command)&#38;amp;0b11&#60;/code&#62; uses the binary, or bit-wise, AND operation '&#38;amp;', and 0b11 is a way of writing a binary literal value, in this case decimal 3. 0b is the binary cognate of 0x for writing hexadecimal literal numbers. The result is an unsigned value.&#60;/p&#62;
&#60;p&#62;So it all means binary AND the value of command with binary 011&#60;br /&#62;
i.e. do a bit-wise AND operation between the contents of the variable labelled &#60;code&#62;command&#60;/code&#62; (an int) and the 32-bit unsigned integer&#60;br /&#62;
0000 0000 0000 0000 0000 0000 0000 0011&#60;/p&#62;
&#60;p&#62;HTH
&#60;/p&#62;</description>
		</item>
		<item>
			<title>asterix on "Shiftbrite SPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1004#post-6131</link>
			<pubDate>Tue, 30 Aug 2011 07:43:52 +0000</pubDate>
			<dc:creator>asterix</dc:creator>
			<guid isPermaLink="false">6131@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;hmmm it is spitting an error when I compile using the example sketch posted on the shiftbrite information page I posted earlier:&#60;/p&#62;
&#60;p&#62;libraries\SPI\SPI.cpp:52: error: 'class SPIClass' has no member named 'nssPin'
&#60;/p&#62;</description>
		</item>
		<item>
			<title>asterix on "Shiftbrite SPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1004#post-6130</link>
			<pubDate>Tue, 30 Aug 2011 07:31:47 +0000</pubDate>
			<dc:creator>asterix</dc:creator>
			<guid isPermaLink="false">6130@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks mbolivar. I will try out the port and report back.&#60;br /&#62;
I personally found maples implementation much simpler and easier to implement. Is there any advantage to arduino's SPI lib? Because really, I find it abstracted and annoying.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Shiftbrite SPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1004#post-6124</link>
			<pubDate>Mon, 29 Aug 2011 18:46:29 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">6124@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;gbulmer,&#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;
Very good question! I went digging through the Maple IDE source files, and found this:&#60;br /&#62;
[...]&#60;br /&#62;
but it would be nice to have this in the documentation.
&#60;/p&#62;&#60;/blockquote&#62;
&#60;p&#62;Good point.  I've pushed this to the docs sources; it'll go out in the next release:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;https://github.com/leaflabs/leaflabs-docs/commit/202d0ae14f235c385c4f06b60a459164f09a308b&#34; rel=&#34;nofollow&#34;&#62;https://github.com/leaflabs/leaflabs-docs/commit/202d0ae14f235c385c4f06b60a459164f09a308b&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Shiftbrite SPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1004#post-6123</link>
			<pubDate>Mon, 29 Aug 2011 18:38:40 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">6123@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi asterix,&#60;/p&#62;
&#60;p&#62;I recently made a port for the Arduino SPI lib.  If you've got time, I'd love it if you gave it a try and let me know if it works with your example:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://static.leaflabs.com/mbolivar/SPI.zip&#34; rel=&#34;nofollow&#34;&#62;http://static.leaflabs.com/mbolivar/SPI.zip&#60;/a&#62; &#60;/p&#62;
&#60;p&#62;To try it out, drop it in the &#34;libraries&#34; folder in your sketchbook (create this folder if it doesn't already exist).  Then restart the IDE; you should be able to use it from the Sketch &#38;gt; Import Library menu.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>asterix on "Shiftbrite SPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1004#post-6115</link>
			<pubDate>Mon, 29 Aug 2011 18:11:28 +0000</pubDate>
			<dc:creator>asterix</dc:creator>
			<guid isPermaLink="false">6115@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Gbulmer thank you for such a thorough and informative reply post :)&#60;/p&#62;
&#60;p&#62;Sending the resulting byte via SerialUSB is fixed (just a bug in my code).&#60;/p&#62;
&#60;p&#62;I managed to isolate a big part of the problem which was me connecting the shiftbrite V+ and ground rail directly to a 9v source. I patch it into a neighbouring arduinos VCC / Ground and Im getting somewhere (slap).&#60;/p&#62;
&#60;p&#62;For learning sake, can you explain briefly what happens in your example code when you prefix &#38;amp;0b11 to command :(command)&#38;amp;0b11 ??&#60;/p&#62;
&#60;p&#62;I'll post up my code shortly once I've had a play. I'll actually be sending video wirelessly using Xbee's to maple &#38;gt; shiftBrites. The arduino clock cycle of 16mhz is too slow for high data transfers which is why I'm playing with the maple.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Shiftbrite SPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1004#post-6105</link>
			<pubDate>Sun, 28 Aug 2011 16:55:02 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">6105@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I apologise for the weird backquote and html markup nonsense, instead of code outline, but this dreadful forum software isn't capable of converting a post with code tags back and forth. Frankly I got sick of trying to fight it. I've mentioned some of the many failings of the forum software before, but I would much prefer LeafLabs to get Oak shipped rather than fix it.&#60;/p&#62;
&#60;p&#62;Also, please note, I haven't compiled and tested any of my posted code.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Shiftbrite SPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1004#post-6104</link>
			<pubDate>Sun, 28 Aug 2011 16:48:54 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">6104@http://forums.leaflabs.com/</guid>
			<description>&#60;blockquote&#62;&#60;p&#62;`// reworked to 7 as the first bite is insignificant, and the second should be 0 so that command mode is 0;&#38;lt;/p&#38;gt;&#38;lt;br /&#38;gt;&#60;br /&#62;
&#38;lt;p&#38;gt;spi.write((cmode &#38;lt;&#38;lt; 7 &#124; b &#38;gt;&#38;gt;4));&#38;lt;br /&#38;gt;&#60;br /&#62;
spi.write((b &#38;lt;&#38;lt; 4 &#124; r &#38;gt;&#38;gt;6));&#38;lt;br /&#38;gt;&#60;br /&#62;
spi.write((r &#38;lt;&#38;lt; 2 &#124; g &#38;gt;&#38;gt; 8));&#38;lt;br /&#38;gt;&#60;br /&#62;
spi.write(g); `&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;You probably don't want to do this.&#60;br /&#62;
As I read the example at &#60;a href=&#34;http://docs.macetech.com/doku.php/shiftbrite&#34; rel=&#34;nofollow&#34;&#62;http://docs.macetech.com/doku.php/shiftbrite&#60;/a&#62; the top two bits are command, so I would try to be consistent and allow it to be two bits. If cmode it is 0, then set it to 0. I wasted time trying to figure out whether you had made a mistake. Hence:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;int cmode = 0;

spi.write((cmode &#38;lt;&#38;lt; 6)  &#124; (b &#38;gt;&#38;gt;4));
spi.write((b &#38;lt;&#38;lt; 4 &#124; r &#38;gt;&#38;gt;6));
spi.write((r &#38;lt;&#38;lt; 2 &#124; g &#38;gt;&#38;gt; 8));
spi.write(g);&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Also, for clarity, because int's are 32 bits, and to be explicit, you might consider trimming the bits to be the ones required:&#60;br /&#62;
&#60;code&#62;spi.write( (cmode &#38;lt;&#38;lt; 6)&#38;amp;0b11000000  &#124; (b &#38;gt;&#38;gt;4)&#38;amp;0b00111111);&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;I might write some macros or inline functions to do this stuff:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;#define MAKE_SHIFTBRIGHT_DATA(command, red, green, blue)  (((command)&#38;amp;0b11)&#38;lt;&#38;lt;30 \
                               &#124; ((blue)&#38;amp;0x3FF)&#38;lt;&#38;lt;20 &#124; ((red)&#38;amp;0x3FF)&#38;lt;&#38;lt;10 &#124; ((green)&#38;amp;0x3FF)
#define SHIFTBRIGHT_BYTE0(val) ((unsigned char)((val)&#38;gt;&#38;gt;24)&#38;amp;0xFF)
#define SHIFTBRIGHT_BYTE1(val) ((unsigned char)((val)&#38;gt;&#38;gt;16)&#38;amp;0xFF)
#define SHIFTBRIGHT_BYTE2(val) ((unsigned char)((val)&#38;gt;&#38;gt;8)&#38;amp;0xFF)
#define SHIFTBRIGHT_BYTE3(val) ((unsigned char)((val)&#38;gt;&#38;gt;0)&#38;amp;0xFF)&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;and do:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;uint32 val = MAKE_SHIFTBRIGHT_DATA(cmode, r, g, b);
spi.write(SHIFTBRIGHT_BYTE0(val));
spi.write(SHIFTBRIGHT_BYTE1(val));
spi.write(SHIFTBRIGHT_BYTE2(val));
spi.write(SHIFTBRIGHT_BYTE3(val));&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;This has the small advantage that it is easy to print out the values over USB that are also being written via SPI.&#60;/p&#62;
&#60;p&#62;1. when you writre &#34;shiftleft or shiftright functions&#34;, so I assume you mean the left and right shift operators, '&#38;lt;&#38;lt;' and '&#38;gt;&#38;gt;'.&#60;br /&#62;
The shift operators yield the same type as their left operand, so if the operand is a 16 bit signed int, the result will be a 16 bit signed int. An 'int' is 32 bits on an ARM with gcc, not 16 bits.&#60;/p&#62;
&#60;p&#62;The type the shift operators yield can not override the result type, which is truncated (high bits are thrown away)&#60;br /&#62;
So:&#60;br /&#62;
&#60;code&#62;1023 &#38;lt;&#38;lt; 4;&#60;/code&#62;&#60;br /&#62;
is binary 000000000000000000011111111110000 (I hope I counted the 32 bits correctly there :-)&#60;br /&#62;
when this is assigned to an 'int8' yields binary 11110000, or answer a)&#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;I've tried SerialUSB sending the result but I get a blank line.&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;With these sort of things 'the devil is in the detail' so it would be helpful if you gave the code that is being used.&#60;/p&#62;
&#60;p&#62;I assume the code is trying to print the int8 variable &#60;code&#62;rshifted&#60;/code&#62;, which has a decimal value of 240.&#60;br /&#62;
There is no ASCII value, so the terminal program may have no visible glyph to print for that code, and uses space in lieu of one.&#60;br /&#62;
instead, cast the value to be an &#60;code&#62;(unsigned int)&#60;/code&#62; and see what it prints.&#60;/p&#62;
&#60;p&#62;2. Please provide the code for the whole program. What happens in any particular case depends on how the peripheral is setup.&#60;/p&#62;
&#60;p&#62;Assuming everything is simple and vanilla, then SPI write will send 8 bits. It is irrelevant what is contained in the 8 bits, so when sending 1, it will send eight bits, seven 0's and one 1.&#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;3. In SPI's begin(frequency, bitOrder, mode); what exactly is the mode?&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;Very good question! I went digging through the Maple IDE source files, and found this:&#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;`/**&#38;lt;br /&#38;gt;&#60;br /&#62;
 * @brief SPI mode configuration.&#38;lt;br /&#38;gt;&#60;br /&#62;
 *&#38;lt;br /&#38;gt;&#60;br /&#62;
 * Determines a combination of clock polarity (CPOL), which determines&#38;lt;br /&#38;gt;&#60;br /&#62;
 * idle state of the clock line, and clock phase (CPHA), which&#38;lt;br /&#38;gt;&#60;br /&#62;
 * determines which clock edge triggers data capture.&#38;lt;br /&#38;gt;&#60;br /&#62;
 */&#38;lt;br /&#38;gt;&#60;br /&#62;
typedef enum spi_mode {&#38;lt;br /&#38;gt;&#60;br /&#62;
    SPI_MODE_0,  /**&#38;lt; Clock line idles low (0), data capture on first&#38;lt;br /&#38;gt;&#60;br /&#62;
                    clock transition. */&#38;lt;br /&#38;gt;&#60;br /&#62;
    SPI_MODE_1,  /**&#38;lt; Clock line idles low (0), data capture on second&#38;lt;br /&#38;gt;&#60;br /&#62;
                    clock transition */&#38;lt;br /&#38;gt;&#60;br /&#62;
    SPI_MODE_2,  /**&#38;lt; Clock line idles high (1), data capture on first&#38;lt;br /&#38;gt;&#60;br /&#62;
                    clock transition. */&#38;lt;br /&#38;gt;&#60;br /&#62;
    SPI_MODE_3   /**&#38;lt; Clock line idles high (1), data capture on&#38;lt;br /&#38;gt;&#60;br /&#62;
                    second clock transition. */&#38;lt;br /&#38;gt;&#60;br /&#62;
} spi_mode;`&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;but it would be nice to have this in the documentation.&#60;/p&#62;
&#60;p&#62;The linked page you gave doesn't give explicit information to be sure which mode is correct, but the datasheet for the Allegro A6281 will likely say what it expects.&#60;br /&#62;
I tried to work it out by reading the ATmega to understand what mode the setup puts its SPI peripheral into.&#60;br /&#62;
The linked code has:&#60;br /&#62;
&#60;code&#62;SPCR = (1&#38;lt;&#38;lt;SPE)&#124;(1&#38;lt;&#38;lt;MSTR)&#124;(0&#38;lt;&#38;lt;SPR1)&#124;(0&#38;lt;&#38;lt;SPR0);&#60;/code&#62;&#60;br /&#62;
SPE bit enables the SPI peripheral (not relevant)&#60;br /&#62;
MSTR bit sets it in master mode (I assume your set up doesn't change this)&#60;br /&#62;
SPR1 and SPR2 set the clock rate (clock / 4, or 4MHz). I assume your setup code does the same frequency&#60;br /&#62;
So CPOL = 0 which means the clock signal is 'idle low'&#60;br /&#62;
and CPCHA = 0, which means data is setup on the trailing edge of the clock, and sampled on the leading edge.&#60;br /&#62;
This looks like SPI_MODE_0, but you could try 0 and 1 (or even all 4), and see if there are any differences in results.&#60;br /&#62;
This is covered on page 679 of the RM0008 STM32F manual.&#60;/p&#62;
&#60;p&#62;(full disclosure: I am not a member of LeafLabs staff.)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>asterix on "Shiftbrite SPI"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1004#post-6100</link>
			<pubDate>Sun, 28 Aug 2011 01:36:43 +0000</pubDate>
			<dc:creator>asterix</dc:creator>
			<guid isPermaLink="false">6100@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I need help sending 3 10bit integers and one 2bit command mode using SPI.&#60;/p&#62;
&#60;p&#62;I'm trying to send 10bit color values (0-1024) to an array of shiftbrite led modules.&#60;br /&#62;
There is an arduino sketch that uses the arduino SPI class to send data:&#60;br /&#62;
&#60;a href=&#34;http://docs.macetech.com/doku.php/shiftbrite&#34; rel=&#34;nofollow&#34;&#62;http://docs.macetech.com/doku.php/shiftbrite&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;I've unsuccessfully tried reworking a similar example of the sketch using maples hardwareSPI class, something like this:&#60;/p&#62;
&#60;p&#62;////////////////////////////////////////////////&#60;br /&#62;
int r = 900;&#60;br /&#62;
int g = 100;&#60;br /&#62;
int b = 100;&#60;br /&#62;
int cmode = 1;&#60;/p&#62;
&#60;p&#62;  for (int h = 0;h&#38;lt;11;h++)&#60;br /&#62;
  {&#60;br /&#62;
// reworked to 7 as the first bite is insignificant, and the second should be 0 so that command mode is 0;    &#60;/p&#62;
&#60;p&#62;   spi.write((cmode &#38;lt;&#38;lt; 7 &#124; b &#38;gt;&#38;gt;4));&#60;br /&#62;
   spi.write((b &#38;lt;&#38;lt; 4 &#124; r &#38;gt;&#38;gt;6));&#60;br /&#62;
   spi.write((r &#38;lt;&#38;lt; 2 &#124; g &#38;gt;&#38;gt; 8));&#60;br /&#62;
   spi.write(g);&#60;/p&#62;
&#60;p&#62;   }&#60;/p&#62;
&#60;p&#62;   delayMicroseconds(1);&#60;br /&#62;
   digitalWrite(latchpin,HIGH); // latch data into registers&#60;br /&#62;
   delayMicroseconds(1);&#60;br /&#62;
   digitalWrite(latchpin,LOW);&#60;/p&#62;
&#60;p&#62;    delay(1000);&#60;br /&#62;
///////////////////////////////////////////////////&#60;/p&#62;
&#60;p&#62;I've also tried using a software SPI solution using shiftOut():&#60;/p&#62;
&#60;p&#62;///////////////////////////////////////////////////&#60;/p&#62;
&#60;p&#62;int r = 900;&#60;br /&#62;
int g = 100;&#60;br /&#62;
int b = 100;&#60;br /&#62;
int cmode = 1&#60;br /&#62;
for (int h = 0;h&#38;lt;11;h++)&#60;br /&#62;
  {&#60;br /&#62;
     shiftOut(dataPin, clockPin, MSBFIRST, c &#38;lt;&#38;lt; 6 &#124; b&#38;gt;&#38;gt;4);&#60;br /&#62;
     shiftOut(dataPin, clockPin, MSBFIRST, b &#38;lt;&#38;lt; 4 &#124; r &#38;gt;&#38;gt; 6);&#60;br /&#62;
     shiftOut(dataPin, clockPin, MSBFIRST, r &#38;lt;&#38;lt; 2 &#124; g &#38;gt;&#38;gt; 8);&#60;br /&#62;
     shiftOut(dataPin, clockPin, MSBFIRST, g);&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;////////////////////////////////////////////////////&#60;/p&#62;
&#60;p&#62;Data IS being sent out to the shiftbrights through the SPI MOSI as modding the values gives me varied results. But I'm not seeing what I should (colors are different on each module, some are blank, sometimes colors change).&#60;/p&#62;
&#60;p&#62;1. I have a hunch that the shiftleft and shiftright functions aren't outputting an 8bit integer (uint8)that can be sent as a clean byte; as a standard integer is uint16 (2bytes). Example:&#60;/p&#62;
&#60;p&#62;int r = 1023 // binary: 00000011 11111111);&#60;br /&#62;
int8 rshifted = (r &#38;lt;&#38;lt; 4); binary 00111111 11110000, or uint16 16368);&#60;/p&#62;
&#60;p&#62;when I convert rshifted to a byte, will binary result be:&#60;/p&#62;
&#60;p&#62;a) 11110000 (the last 8 bits)&#60;br /&#62;
b) 00111111 (the first 8 bits) ;&#60;br /&#62;
c) some other value like 255 or null?&#60;/p&#62;
&#60;p&#62;I've tried SerialUSB sending the result but I get a blank line.&#60;/p&#62;
&#60;p&#62;2. What happens when I send less than 8 bits through the SPI using MSB first? If I send&#60;br /&#62;
spi.write(1) (binary 00000001); does it send the 0's or just the MSB (1);&#60;/p&#62;
&#60;p&#62;3. In SPI's begin(frequency, bitOrder, mode); what exactly is the mode?
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
