<?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: Serial Problem?</title>
		<link>http://forums.leaflabs.com/topic.php?id=249</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:05:27 +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=249" rel="self" type="application/rss+xml" />

		<item>
			<title>x893 on "Serial Problem?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=249#post-1864</link>
			<pubDate>Thu, 21 Oct 2010 02:48:05 +0000</pubDate>
			<dc:creator>x893</dc:creator>
			<guid isPermaLink="false">1864@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;You're right. I use&#60;br /&#62;
while(SerialX.available())&#60;br /&#62;
{&#60;br /&#62;
  ... SerialX.read();&#60;br /&#62;
}&#60;br /&#62;
not depends from negative or positive. Also easy to check. Send 60 bytes and read all. Send next 60 bytes (w/o read) and print SerialX.available() value
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Serial Problem?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=249#post-1861</link>
			<pubDate>Wed, 20 Oct 2010 18:51:22 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">1861@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I think the code for calculating the number of characters in the buffer is&#60;/p&#62;
&#60;p&#62;&#60;code&#62;return ((rb-&#38;gt;tail - rb-&#38;gt;head) &#38;amp; (rb-&#38;gt;size-1));&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;This should turn the negative value from &#60;code&#62;(rb-&#38;gt;tail - rb-&#38;gt;head)&#60;/code&#62; into a positive one, modulo the buffer size (which is a power of 2).&#60;/p&#62;
&#60;p&#62;(I think this is correct. I checked  with my Mac's calculator in programmer mode, and it looks about right ?-)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Serial Problem?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=249#post-1860</link>
			<pubDate>Wed, 20 Oct 2010 18:37:29 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">1860@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;x893 - Okay, I agree with you. That is the code I see for the usart Serial. &#60;/p&#62;
&#60;p&#62;The usart code uses the code in libmaple/ring_buffer.h&#60;/p&#62;
&#60;p&#62;Would you please check my logic?&#60;br /&#62;
That part of libmaple looks like it contains bug to me in the ring_buffer.h code.&#60;/p&#62;
&#60;p&#62;/libmaple/wirish/comm/HardwareSerial.cpp contains:&#60;br /&#62;
&#60;code&#62;HardwareSerial Serial2(USART2, 2250000UL, GPIOA_BASE, 2, 3,  TIMER2, 3);&#60;/code&#62;&#60;br /&#62;
and defines:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;uint32 HardwareSerial::available(void) {
    return usart_data_available(usart_num);
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;/libmaple/libmaple/usart.h contains:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;static inline uint32 usart_data_available(uint8 usart_num) {
    return rb_full_count(&#38;amp;usart_dev_table[usart_num].rb);
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;ring_buffer.h contains:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;static inline uint32 rb_full_count(ring_buffer *rb) {
    return rb-&#38;gt;tail - rb-&#38;gt;head;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;and&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;typedef struct ring_buffer {
    uint32 head;
    uint32 tail;
    uint8 size;
    uint8 *buf;
} ring_buffer;&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;The size of the buffer is constrained to be a power of 2:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;static inline void rb_init(ring_buffer *rb, uint8 size, uint8 *buf) {
    ASSERT(IS_POWER_OF_TWO(size));
    rb-&#38;gt;head = 0;
    rb-&#38;gt;tail = 0;
    rb-&#38;gt;size = size;
    rb-&#38;gt;buf = buf;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Let's imagine the buffer is 64 bytes long, a power of 2.&#60;br /&#62;
Characters are added, and removed until head = 17 (characters are added to the tail, and removed from the head)&#60;br /&#62;
Some more charactera are added, filling the buffer, and tail is wrapped around to the start of the buffer, and becomes 0.&#60;/p&#62;
&#60;p&#62;rb_full_count(...) will return 17&#60;/p&#62;
&#60;p&#62;Let's repeat the experiment with the buffer size 128, another power of 2.&#60;br /&#62;
A similar sequence of events, but more characters are written before the wrap around.&#60;br /&#62;
So head = 17, and tail = 0&#60;/p&#62;
&#60;p&#62;rb_full_count(...) will still return 17&#60;br /&#62;
so even though the buffer is bigger, the number of characters is the same.&#60;/p&#62;
&#60;p&#62;Do the same thought-experiment where head gets to 3 short of the size of the buffer, and then tail wraps to 0. Even though there are fewer characters, the value from rb_full_count(...) is even bigger.&#60;/p&#62;
&#60;p&#62;Does that seem correct?&#60;/p&#62;
&#60;p&#62;I think this is a bug. It should return the number of characters in the buffer.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>x893 on "Serial Problem?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=249#post-1855</link>
			<pubDate>Wed, 20 Oct 2010 09:11:02 +0000</pubDate>
			<dc:creator>x893</dc:creator>
			<guid isPermaLink="false">1855@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I'm not sure but you can check indetail. I see&#60;br /&#62;
uint32 available()&#60;br /&#62;
{&#60;br /&#62;
  return (uint8)tail - (uint8)head;&#60;br /&#62;
}&#60;br /&#62;
if tail = 5 and head = 250.&#60;br /&#62;
i can absolutly sure that data not available than return 0.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Serial Problem?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=249#post-1853</link>
			<pubDate>Wed, 20 Oct 2010 07:10:21 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">1853@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;x893 - why does &#60;code&#62;SerialUSB.available()&#60;/code&#62; return a negative number when their are bytes available? &#60;/p&#62;
&#60;p&#62;That isn't the definition of SerialUSB.available(), or AFAIK, how it works.&#60;/p&#62;
&#60;p&#62;&#60;a&#62;http://arduino.cc/en/Serial/Available&#60;/a&#62;&#60;br /&#62;
&#60;a&#62;http://leaflabs.com/docs/maple/usb/&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>crenn on "Serial Problem?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=249#post-1852</link>
			<pubDate>Tue, 19 Oct 2010 20:28:30 +0000</pubDate>
			<dc:creator>crenn</dc:creator>
			<guid isPermaLink="false">1852@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;while(SerialUSB.available() &#38;gt; 0){&#60;br /&#62;
is still valid.&#60;br /&#62;
SerialUSB.available() returns type uint32. However I can still see what you mean.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>x893 on "Serial Problem?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=249#post-1850</link>
			<pubDate>Tue, 19 Oct 2010 19:27:43 +0000</pubDate>
			<dc:creator>x893</dc:creator>
			<guid isPermaLink="false">1850@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;You must use&#60;br /&#62;
  while(SerialUSB.available()){&#60;br /&#62;
not a&#60;br /&#62;
  while(SerialUSB.available() &#38;gt; 0){&#60;br /&#62;
because available() return positive or negative if chars available.&#60;br /&#62;
The same for Serial2
&#60;/p&#62;</description>
		</item>
		<item>
			<title>crenn on "Serial Problem?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=249#post-1849</link>
			<pubDate>Tue, 19 Oct 2010 18:12:50 +0000</pubDate>
			<dc:creator>crenn</dc:creator>
			<guid isPermaLink="false">1849@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;That's SerialUSB, all messages sent are less than 255 characters, I can do it a lot better, yes, but currently that's not the issue.&#60;/p&#62;
&#60;p&#62;I'm sending the messages using the Serial Console. If I sending &#34;Testing...&#34; I only expect to see &#34;Testing...&#34; come back with that code!
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Serial Problem?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=249#post-1847</link>
			<pubDate>Tue, 19 Oct 2010 17:38:40 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">1847@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;May I ask, what is printing the &#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;TesTesting&#60;br /&#62;
Testing...&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;is it SerialUSB or Serial2?&#60;/p&#62;
&#60;p&#62;What should it say? I don't understand why that output is an error.&#60;/p&#62;
&#60;p&#62;I think there are the possibility of errors in the while loops&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;while(SerialUSB.available() &#38;gt; 0){
      buffer[i] = SerialUSB.read();
      i++;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;and&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;while(Serial2.available() &#38;gt; 0){
      buffer[i] = Serial2.read();
      i++;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;If there are more than 255 characters available, it is possible that a while runs off the end of the array. I'm not confident that this is causing the error, but I don't understand the pieces of the system, so it may be.&#60;/p&#62;
&#60;p&#62;It'd be easy enough to check when &#60;code&#62;i&#38;gt;=BUFFER_SIZE&#60;/code&#62;, and flush the buffer immediately.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>crenn on "Serial Problem?"</title>
			<link>http://forums.leaflabs.com/topic.php?id=249#post-1825</link>
			<pubDate>Sun, 17 Oct 2010 06:54:49 +0000</pubDate>
			<dc:creator>crenn</dc:creator>
			<guid isPermaLink="false">1825@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;While testing out a bluetooth module for the first time today, I noticed a slight problem. Here's the example output:&#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;&#60;code&#62;TesTesting&#60;br /&#62;
Testing...&#60;/code&#62;&#60;/p&#62;&#60;/blockquote&#62;
&#60;p&#62;It happens sort of randomly and only when I'm using the in built Serial Console. Output length is always the same as the input length, but can be jumbled a little.&#60;/p&#62;
&#60;p&#62;Here's the code I'm currently using:&#60;br /&#62;
&#60;a href=&#34;http://www.crennsmind.com/Code/Maple/Communication/Bluetooth-Bee/Serial-Link.txt&#34; rel=&#34;nofollow&#34;&#62;http://www.crennsmind.com/Code/Maple/Communication/Bluetooth-Bee/Serial-Link.txt&#60;/a&#62;
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
