<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
	<link rel="self" type="application/atom+xml" href="https://forums.obdev.at/app.php/feed/topic/8795" />

	<title>Objective Development Forums</title>
	
	<link href="https://forums.obdev.at/index.php" />
	<updated>2014-01-18T07:54:37+02:00</updated>

	<author><name><![CDATA[Objective Development Forums]]></name></author>
	<id>https://forums.obdev.at/app.php/feed/topic/8795</id>

		<entry>
		<author><name><![CDATA[stf92]]></name></author>
		<updated>2014-01-18T07:00:08+02:00</updated>

		<published>2014-01-18T07:00:08+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26453#p26453</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26453#p26453"/>
		<title type="html"><![CDATA[Re: static uchar usbIsReset;   /* = 0; USB bus is in reset p]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26453#p26453"><![CDATA[
Fantastic! Thanks  cpldcpu.<p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=20156">stf92</a> — Sat Jan 18, 2014 7:00 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[cpldcpu]]></name></author>
		<updated>2014-01-18T07:54:37+02:00 </updated>

		<published>2014-01-18T06:38:19+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26451#p26451</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26451#p26451"/>
		<title type="html"><![CDATA[Re: static uchar usbIsReset;   /* = 0; USB bus is in reset p]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26451#p26451"><![CDATA[
The AVR-LIBC erases the the bss section before entering main. You can actually see it when disassembling:<br /><br />This is just an arbitrary example:<br /><br />&gt;avr-objdump -d -S main.bin &gt;main.lss<br /><br /><div class="codebox"><p>Code: </p><pre><code>000017fa &lt;__do_clear_bss&gt;:<br />    17fa:   20 e0          ldi   r18, 0x00   ; 0<br />    17fc:   a0 e6          ldi   r26, 0x60   ; 96<br />    17fe:   b0 e0          ldi   r27, 0x00   ; 0<br />    1800:   01 c0          rjmp   .+2         ; 0x1804 &lt;.do_clear_bss_start&gt;<br /><br />00001802 &lt;.do_clear_bss_loop&gt;:<br />    1802:   1d 92          st   X+, r1<br /><br />00001804 &lt;.do_clear_bss_start&gt;:<br />    1804:   ac 38          cpi   r26, 0x8C   ; 140<br />    1806:   b2 07          cpc   r27, r18<br />    1808:   e1 f7          brne   .-8         ; 0x1802 &lt;.do_clear_bss_loop&gt;<br /></code></pre></div><p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=20062">cpldcpu</a> — Sat Jan 18, 2014 6:38 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[stf92]]></name></author>
		<updated>2014-01-18T04:08:06+02:00</updated>

		<published>2014-01-18T04:08:06+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26450#p26450</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26450#p26450"/>
		<title type="html"><![CDATA[Re: static uchar usbIsReset;   /* = 0; USB bus is in reset p]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26450#p26450"><![CDATA[
The why: <blockquote class="uncited"><div>For a program running from the ROM of a microcontroller there likely is no loader, and the task of zeroing BSS would fall to the program's runtime startup routine (crt0 in the old days). According to the avr-libc manual, you have the option of placing uninitialized variables in the .bss section (which should be all zero) or the .noinit section, which is a .bss subset that does not get zeroed on startup. </div></blockquote><br />This quotation is from linuxquestions.org. I did not use that option. Do <div class="codebox"><p>Code: </p><pre><code> avr-gcc -I. -c -S -mmcu=&lt;avr device&gt; usbdrv.c</code></pre></div>. It outputs usbdrv.o and usbdrv.s. The latter is an assembler source, the C source file translated into assembler. Looking into it, you'll see <div class="codebox"><p>Code: </p><pre><code> <br />.global __do_copy_data<br />.global __do_clear_bss<br /></code></pre></div><br />The global variable under discussion is declared in usbdrv.c:<div class="codebox"><p>Code: </p><pre><code>volatile char      usbDeviceId;</code></pre></div><br />This variable is used, but not declared, in usbdrvasm.S. But gcc (or avr-gcc, it's the same) considers all undeclared variables in an assembler source as externals. So, in usbdrv.s, it looks like this:<div class="codebox"><p>Code: </p><pre><code>       .comm   usbDeviceId,1,1<br /></code></pre></div><br />Now, if common variables are taken as globals by gcc, we have that usbDeviceId debuts as zero in the executable! The code to clear the .bss section CANNOT be seen in the assembler sources generated by gcc. It lies in some library and put ahead of the programmer's code by the linker. What do you know?<p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=20156">stf92</a> — Sat Jan 18, 2014 4:08 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[stf92]]></name></author>
		<updated>2014-01-17T07:25:32+02:00</updated>

		<published>2014-01-17T07:25:32+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26441#p26441</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26441#p26441"/>
		<title type="html"><![CDATA[Re: static uchar usbIsReset;   /* = 0; USB bus is in reset p]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26441#p26441"><![CDATA[
<blockquote><div><cite>blargg wrote:</cite>I couldn't find that variable in several of the 2008 versions. From the comment, it looks more like it's just trying to emphasize that static objects in C are initialized to 0 if no initializer is present. In other versions there's a local static for calling a user-defined reset handler only once, and the variable is used to keep track of whether it's been called, so maybe this is for that purpose.</div></blockquote><br /><br />Thanks blargg. Let's change usbIsReset by another variable: usbDeviceId, which may be present in newer versions. This variable is so declared in usbdrv.c:<div class="codebox"><p>Code: </p><pre><code>volatile char      usbDeviceId;<br /></code></pre></div><br />And my question is now another one: this variable needs to be zero when control first enters the external interrupt service routine, but it is set to zero nowhere in either usbdrv.c or usbdrvasm.S. It is no other thing than the device address, which must be zero when starting enumeration. How can the declaration above make the variable initial value zero? In C, all uninitialized global variables are zero when the program is loaded (from disk). But here, after turning on power, the RAM is in an aleatory state! <br /><br />The sources I am using are not among the versions listed in this site. I am, however, giving you a link,  <!-- m --><a class="postlink" href="http://www.filedropper.com/takefuji-213">http://www.filedropper.com/takefuji-213</a><!-- m -->, coded in 2004 by Christian Starkjohann, except the app, da.c,  which by its very simplicity is easy to analyze, in case you care to skim it.<p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=20156">stf92</a> — Fri Jan 17, 2014 7:25 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[blargg]]></name></author>
		<updated>2014-01-14T06:57:23+02:00</updated>

		<published>2014-01-14T06:57:23+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26428#p26428</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26428#p26428"/>
		<title type="html"><![CDATA[Re: static uchar usbIsReset;   /* = 0; USB bus is in reset p]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26428#p26428"><![CDATA[
I couldn't find that variable in several of the 2008 versions. From the comment, it looks more like it's just trying to emphasize that static objects in C are initialized to 0 if no initializer is present. In other versions there's a local static for calling a user-defined reset handler only once, and the variable is used to keep track of whether it's been called, so maybe this is for that purpose.<p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=20076">blargg</a> — Tue Jan 14, 2014 6:57 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[stf92]]></name></author>
		<updated>2014-01-13T14:02:16+02:00</updated>

		<published>2014-01-13T14:02:16+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26422#p26422</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26422#p26422"/>
		<title type="html"><![CDATA[static uchar usbIsReset;   /* = 0; USB bus is in reset phase]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=8795&amp;p=26422#p26422"><![CDATA[
Hi: This is the declaration of usbIsReset, in usbdrv.c (This is a version from 2008, so perhaps this variable no longer exists):<br /><div class="codebox"><p>Code: </p><pre><code>static uchar    usbIsReset;             /* = 0; USB bus is in reset phase */</code></pre></div><br />I think the comment is wrong. usbIsReset = 0 when out of reset. Am I right? Another question is this: what is it used for? Only to set usbDeviceId and usbNewDeviceId to zero? And by the way: these two variables are actually the address field of the package, are they not?<p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=20156">stf92</a> — Mon Jan 13, 2014 2:02 pm</p><hr />
]]></content>
	</entry>
	</feed>
