<?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/6678" />

	<title>Objective Development Forums</title>
	
	<link href="https://forums.obdev.at/index.php" />
	<updated>2012-05-28T23:37:32+02:00</updated>

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

		<entry>
		<author><name><![CDATA[honupata]]></name></author>
		<updated>2012-05-28T23:37:32+02:00</updated>

		<published>2012-05-28T23:37:32+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=6678&amp;p=21721#p21721</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=6678&amp;p=21721#p21721"/>
		<title type="html"><![CDATA[Issue with function call in main]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=6678&amp;p=21721#p21721"><![CDATA[
Hi,<br /><br />I'm trying to do a Led matrix scroller (5*7) with VUSB (hid-data - datastore).<br />1. The Device is recognized by windows without problem as datastore.<br />2. My issue is that when I use <strong class="text-strong">multiple function </strong> from the main I <strong class="text-strong">can't run a hidtool.exe write without error </strong>(error writing data: Communication error with device).<br />3. But with a main with only <strong class="text-strong">one functions</strong> call hidtool.exe write <strong class="text-strong">works fine</strong>.<br /><br />I already spent lot of time to try to fix it. Can someone please tell me why with more than a function in the main it is not working?<br /><br />Thank you  <img class="smilies" src="./../../../images/smilies/icon_biggrin.gif" alt=":D" title="Very Happy" /> <br /><br /><strong class="text-strong">CODE WHICH IS NOT WORKING</strong>  <img class="smilies" src="./../../../images/smilies/icon_confused.gif" alt=":?" title="Confused" /> <br /><div class="codebox"><p>Code: </p><pre><code>// Prototypes<br />void setPixel(uchar pin,  uchar value);<br />void paint(uchar matrix&#91;&#93;, uchar times);<br />void digitalWrite(uchar pin, uchar val);<br />void vusbAccess(void);<br /><br />// Here is the main <br />int main (void)<br />{<br /><br />    // VUSB<br />   wdt_enable(WDTO_1S);<br />   usbInit();<br /><br />   uchar i;<br />    usbDeviceDisconnect();<br />   /* 250 ms disconnect */<br />   _delay_ms(250);<br />    usbDeviceConnect();<br />   sei();<br /><br />   // all outputs except USB data and PD2 = INT0<br />   DDRD = ~USBMASK &amp; ~(1 &lt;&lt; 2); <br />   DDRB  = 0xff;<br />   <br />   for(;;) {/* main event loop */<br />   <br />      // VUSB Poll<br />      vusbAccess();<br />      ....<br />      if (MODE==2) {<br />         uchar i,frame;   // some variables<br />         uchar screen&#91;5&#93;; // buffer for paint<br /><br />         // for each frame<br />         for (frame=0;frame&lt;FRAMES;frame++){<br />            <br />            // Fill screen buffer with frame<br />            for (i=0;i&lt;5;i++){<br />               screen&#91;i&#93; = pgm_read_byte(&amp;animation&#91;frame&#93;&#91;i&#93;);<br />            }<br />            // Now we paint the screen SPEED times<br />            paint(screen, SPEED);<br />         }// end of frame cycle<br />      }// end of ANIMATION mode<br />   } // end of infinite loop<br />   return 0;<br />}<br /><br /><br />// VUSB usbPoll<br />void vusbAccess(void){<br />   wdt_reset();<br />   usbPoll();<br />}<br /><br />// Set a pixel by the pin number (value can be HIGH or LOW)<br />void setPixel(uchar pin, uchar value){<br /><br />   digitalWrite(pin, value);<br />   // Delay for display<br />   _delay_us(600);<br />   digitalWrite(pin,HIGH);<br />}<br /><br />// Refresh paint all the dotmatrix by scanning all rows and cols<br />// Use times for choose the speeds<br />void paint(uchar matrix&#91;&#93;, uchar times){<br />   uchar col,row, i;<br /><br />   for (i=0;i&lt;times;i++){<br />      for (row=0;row&lt;7;row++){<br />         digitalWrite(rows&#91;row&#93;,HIGH);<br />         for(col=0;col&lt;5;col++){<br />            setPixel(cols&#91;col&#93;,!(matrix&#91;col&#93;&amp;1&lt;&lt;row));<br />         }<br />         digitalWrite(rows&#91;row&#93;,LOW);<br />      }<br />      <br />   }<br />}<br /><br />// digitalWrite from Arduino modified for use absolute pins<br />void digitalWrite(uchar pin, uchar val)<br />{<br /><br />   uchar bit = digitalPinToBitMask&#91;pin&#93;;<br />   uchar port = digitalPinToPort&#91;pin&#93;;<br />   volatile uchar *out;<br /><br />    if (port == PB) {<br />       out = &amp;PORTB; <br />   } else <br />   if (port == PD) { <br />        out = &amp;PORTD; <br />   }<br /><br />   if (val == LOW) *out &amp;= ~bit;<br />   else *out |= bit;<br />   <br />   // VUSB Poll<br />   vusbAccess();<br />    <br />}<br /></code></pre></div><br /><br /><strong class="text-strong">CODE WHICH IS WORKING FINE</strong><br /><br /><div class="codebox"><p>Code: </p><pre><code>// Here is the main <br />int main (void)<br />{<br />    // VUSB<br />   wdt_enable(WDTO_1S);<br />   usbInit();<br /><br />    usbDeviceDisconnect();<br />   /* 250 ms disconnect */<br />   _delay_ms(250);<br />    usbDeviceConnect();<br />   sei();<br /><br />   // all outputs except USB data and PD2 = INT0<br />   DDRD = ~USBMASK &amp; ~(1 &lt;&lt; 2); <br />   DDRB  = 0xff;<br /><br />   for(;;) {/* main event loop */<br />   <br />      // VUSB Poll<br />      wdt_reset();<br />      usbPoll();<br />   <br />      uchar i,offset,frame;   // some variables<br />      uchar screen&#91;5&#93;;       // buffer for paint<br />      uchar col,row;   <br />      ...<br />      if (MODE==2) {<br />         // for each frame<br />         for (frame=0;frame&lt;FRAMES;frame++){<br />            // Now we paint the screen SPEED times<br />            for (i=0;i&lt;SPEED;i++){<br />               for (row=0;row&lt;7;row++){<br />                  digitalWrite(rows&#91;row&#93;,HIGH);<br />                  for(col=0;col&lt;5;col++){<br />                     digitalWrite(cols&#91;col&#93;, !(pgm_read_byte(&amp;animation&#91;frame&#93;&#91;col&#93;)&amp;1&lt;&lt;row));<br />                     // Delay for display<br />                     _delay_us(600);<br />                     digitalWrite(cols&#91;col&#93;,HIGH);<br />                  }<br />                  digitalWrite(rows&#91;row&#93;,LOW);<br />               }<br />            }<br />         }// end of frame cycle<br />      }// end of ANIMATION mode<br />   } // end of infinite loop<br />   return 0;<br />}<br /><br />// digitalWrite from Arduino modified for use absolute pins<br />void digitalWrite(uchar pin, uchar val)<br />{<br /><br />   uchar bit = digitalPinToBitMask&#91;pin&#93;;<br />   uchar port = digitalPinToPort&#91;pin&#93;;<br />   volatile uchar *out;<br /><br />    if (port == PB) {<br />       out = &amp;PORTB; <br />   } else <br />   if (port == PD) { <br />        out = &amp;PORTD; <br />   }<br /><br />   if (val == LOW) *out &amp;= ~bit;<br />   else *out |= bit;<br />   <br />   // VUSB Poll<br />   wdt_reset();<br />   usbPoll();<br />}<br /></code></pre></div><br /><br />here is the makefile<br /><br /><div class="codebox"><p>Code: </p><pre><code># Name: Makefile<br /># Project: hid-data example<br /># Author: Christian Starkjohann<br /># Creation Date: 2008-04-07<br /># Tabsize: 4<br /># Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH<br /># License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)<br /># This Revision: $Id$<br /><br />PROGRAM = main<br />DEVICE  = attiny4313<br />F_CPU   = 16000000   # in Hz<br />FUSE_L  = 0xDE<br />FUSE_H  = 0xDB<br /><br />AVRDUDE = avrdude -c usbasp -p $(DEVICE) # edit this line for your programmer<br /><br />AVRSIZE = avr-size<br /><br />CFLAGS  = -Iusbdrv -I. -DDEBUG_LEVEL=0<br />OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o $(PROGRAM).o<br /><br />COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE)<br /><br /><br />all:   flash fuse clean<br /><br /># symbolic targets:<br />help:<br />   @echo &quot;This Makefile has no default rule. Use one of the following:&quot;<br />   @echo &quot;make hex ....... to build $(PROGRAM).hex&quot;<br />   @echo &quot;make program ... to flash fuses and firmware&quot;<br />   @echo &quot;make fuse ...... to flash the fuses&quot;<br />   @echo &quot;make flash ..... to flash the firmware (use this on metaboard)&quot;<br />   @echo &quot;make clean ..... to delete objects and hex file&quot;<br /><br />hex: $(PROGRAM).hex<br /><br />program: flash clean<br /><br /># rule for programming fuse bits:<br />fuse:<br />   @&#91; &quot;$(FUSE_H)&quot; != &quot;&quot; -a &quot;$(FUSE_L)&quot; != &quot;&quot; &#93; || \<br />      { echo &quot;*** Edit Makefile and choose values for FUSE_L and FUSE_H!&quot;; exit 1; }<br />   $(AVRDUDE) -U hfuse:w:$(FUSE_H):m -U lfuse:w:$(FUSE_L):m<br /><br /># rule for uploading firmware:<br />flash: $(PROGRAM).hex<br />   $(AVRDUDE) -U flash:w:$(PROGRAM).hex:i<br /><br /># rule for deleting dependent files (those which can be built by Make):<br />clean:<br />   rm -f $(PROGRAM).hex $(PROGRAM).lst $(PROGRAM).obj $(PROGRAM).cof $(PROGRAM).list $(PROGRAM).map $(PROGRAM).eep.hex $(PROGRAM).elf *.o usbdrv/*.o $(PROGRAM).s usbdrv/usbdrv.s<br /><br /># Generic rule for compiling C files:<br />.c.o:<br />   $(COMPILE) -c $&lt; -o $@<br /><br /># Generic rule for assembling Assembler source files:<br />.S.o:<br />   $(COMPILE) -x assembler-with-cpp -c $&lt; -o $@<br /># &quot;-x assembler-with-cpp&quot; should not be necessary since this is the default<br /># file type for the .S (with capital S) extension. However, upper case<br /># characters are not always preserved on Windows. To ensure WinAVR<br /># compatibility define the file type manually.<br /><br /># Generic rule for compiling C to assembler, used for debugging only.<br />.c.s:<br />   $(COMPILE) -S $&lt; -o $@<br /><br /># file targets:<br /><br /><br />$(PROGRAM).elf: usbdrv $(OBJECTS)   # usbdrv dependency only needed because we copy it<br />   $(COMPILE) -o $(PROGRAM).elf $(OBJECTS)<br /><br />$(PROGRAM).hex: $(PROGRAM).elf<br />   rm -f $(PROGRAM).hex $(PROGRAM).eep.hex<br />   avr-objcopy -j .text -j .data -O ihex $(PROGRAM).elf $(PROGRAM).hex<br />   $(AVRSIZE) *.hex *.elf *.o<br />   <br /><br /># debugging targets:<br /><br />disasm:   $(PROGRAM).elf<br />   avr-objdump -d $(PROGRAM).elf<br /><br />cpp:<br />   $(COMPILE) -E $(PROGRAM).c<br /></code></pre></div><br /><br />And the compile result of the code which is not working<br /><blockquote class="uncited"><div>&gt; &quot;make.exe&quot; program<br />avr-gcc -Wall -Os -DF_CPU=16000000 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=attiny4313 -c usbdrv/usbdrv.c -o usbdrv/usbdrv.o<br />avr-gcc -Wall -Os -DF_CPU=16000000 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=attiny4313 -x assembler-with-cpp -c usbdrv/usbdrvasm.S -o usbdrv/usbdrvasm.o<br />avr-gcc -Wall -Os -DF_CPU=16000000 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=attiny4313 -c main.c -o main.o<br />main.c: In function 'main':<br />main.c:134:8: warning: unused variable 'i' [-Wunused-variable]<br />main.c: In function 'digitalWrite':<br />main.c:243:23: warning: 'out' may be used uninitialized in this function [-Wmaybe-uninitialized]<br />avr-gcc -Wall -Os -DF_CPU=16000000 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=attiny4313 -o main.elf usbdrv/usbdrv.o usbdrv/usbdrvasm.o main.o<br />rm -f main.hex main.eep.hex<br />avr-objcopy -j .text -j .data -O ihex main.elf main.hex<br />avr-size *.hex *.elf *.o<br />   text   data    bss    dec    hexfilename<br />      0   2196      0   2196    894main.hex<br />   2152     44    185   2381    94dmain.elf<br />    623     12      0    635    27bmain.o<br />avrdude -c usbasp -p attiny4313  -U flash:w:main.hex:i<br /><br />avrdude: warning: cannot set sck period. please check for usbasp firmware update.<br />avrdude: AVR device initialized and ready to accept instructions<br /><br />Reading | ################################################## | 100% 0.00s<br /><br />avrdude: Device signature = 0x1e920d<br />avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed<br />         To disable this feature, specify the -D option.<br />avrdude: erasing chip<br />avrdude: warning: cannot set sck period. please check for usbasp firmware update.<br />avrdude: reading input file &quot;main.hex&quot;<br />avrdude: writing flash (2196 bytes):<br /><br />Writing | ################################################## | 100% 1.36s<br /><br />avrdude: 2196 bytes of flash written<br />avrdude: verifying flash memory against main.hex:<br />avrdude: load data flash data from input file main.hex:<br />avrdude: input file main.hex contains 2196 bytes<br />avrdude: reading on-chip flash data:<br /><br />Reading | ################################################## | 100% 0.70s<br /><br />avrdude: verifying ...<br />avrdude: 2196 bytes of flash verified<br /><br />avrdude done.  Thank you.<br /><br />rm -f main.hex main.lst main.obj main.cof main.list main.map main.eep.hex main.elf *.o usbdrv/*.o main.s usbdrv/usbdrv.s<br /><br />&gt; Process Exit Code: 0<br />&gt; Time Taken: 00:03<br /><blockquote class="uncited"><div></div></blockquote></div></blockquote><p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=5786">honupata</a> — Mon May 28, 2012 11:37 pm</p><hr />
]]></content>
	</entry>
	</feed>
