Issue with function call in main

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
honupata
Posts: 14
Joined: Sat Sep 24, 2011 1:53 am

Issue with function call in main

Post by honupata » Mon May 28, 2012 11:37 pm

Hi,

I'm trying to do a Led matrix scroller (5*7) with VUSB (hid-data - datastore).
1. The Device is recognized by windows without problem as datastore.
2. My issue is that when I use multiple function from the main I can't run a hidtool.exe write without error (error writing data: Communication error with device).
3. But with a main with only one functions call hidtool.exe write works fine.

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?

Thank you :D

CODE WHICH IS NOT WORKING :?

Code: Select all

// Prototypes
void setPixel(uchar pin,  uchar value);
void paint(uchar matrix[], uchar times);
void digitalWrite(uchar pin, uchar val);
void vusbAccess(void);

// Here is the main
int main (void)
{

    // VUSB
   wdt_enable(WDTO_1S);
   usbInit();

   uchar i;
    usbDeviceDisconnect();
   /* 250 ms disconnect */
   _delay_ms(250);
    usbDeviceConnect();
   sei();

   // all outputs except USB data and PD2 = INT0
   DDRD = ~USBMASK & ~(1 << 2);
   DDRB  = 0xff;
   
   for(;;) {/* main event loop */
   
      // VUSB Poll
      vusbAccess();
      ....
      if (MODE==2) {
         uchar i,frame;   // some variables
         uchar screen[5]; // buffer for paint

         // for each frame
         for (frame=0;frame<FRAMES;frame++){
            
            // Fill screen buffer with frame
            for (i=0;i<5;i++){
               screen[i] = pgm_read_byte(&animation[frame][i]);
            }
            // Now we paint the screen SPEED times
            paint(screen, SPEED);
         }// end of frame cycle
      }// end of ANIMATION mode
   } // end of infinite loop
   return 0;
}


// VUSB usbPoll
void vusbAccess(void){
   wdt_reset();
   usbPoll();
}

// Set a pixel by the pin number (value can be HIGH or LOW)
void setPixel(uchar pin, uchar value){

   digitalWrite(pin, value);
   // Delay for display
   _delay_us(600);
   digitalWrite(pin,HIGH);
}

// Refresh paint all the dotmatrix by scanning all rows and cols
// Use times for choose the speeds
void paint(uchar matrix[], uchar times){
   uchar col,row, i;

   for (i=0;i<times;i++){
      for (row=0;row<7;row++){
         digitalWrite(rows[row],HIGH);
         for(col=0;col<5;col++){
            setPixel(cols[col],!(matrix[col]&1<<row));
         }
         digitalWrite(rows[row],LOW);
      }
      
   }
}

// digitalWrite from Arduino modified for use absolute pins
void digitalWrite(uchar pin, uchar val)
{

   uchar bit = digitalPinToBitMask[pin];
   uchar port = digitalPinToPort[pin];
   volatile uchar *out;

    if (port == PB) {
       out = &PORTB;
   } else
   if (port == PD) {
        out = &PORTD;
   }

   if (val == LOW) *out &= ~bit;
   else *out |= bit;
   
   // VUSB Poll
   vusbAccess();
   
}


CODE WHICH IS WORKING FINE

Code: Select all

// Here is the main 
int main (void)
{
    // VUSB
   wdt_enable(WDTO_1S);
   usbInit();

    usbDeviceDisconnect();
   /* 250 ms disconnect */
   _delay_ms(250);
    usbDeviceConnect();
   sei();

   // all outputs except USB data and PD2 = INT0
   DDRD = ~USBMASK & ~(1 << 2);
   DDRB  = 0xff;

   for(;;) {/* main event loop */
   
      // VUSB Poll
      wdt_reset();
      usbPoll();
   
      uchar i,offset,frame;   // some variables
      uchar screen[5];       // buffer for paint
      uchar col,row;   
      ...
      if (MODE==2) {
         // for each frame
         for (frame=0;frame<FRAMES;frame++){
            // Now we paint the screen SPEED times
            for (i=0;i<SPEED;i++){
               for (row=0;row<7;row++){
                  digitalWrite(rows[row],HIGH);
                  for(col=0;col<5;col++){
                     digitalWrite(cols[col], !(pgm_read_byte(&animation[frame][col])&1<<row));
                     // Delay for display
                     _delay_us(600);
                     digitalWrite(cols[col],HIGH);
                  }
                  digitalWrite(rows[row],LOW);
               }
            }
         }// end of frame cycle
      }// end of ANIMATION mode
   } // end of infinite loop
   return 0;
}

// digitalWrite from Arduino modified for use absolute pins
void digitalWrite(uchar pin, uchar val)
{

   uchar bit = digitalPinToBitMask[pin];
   uchar port = digitalPinToPort[pin];
   volatile uchar *out;

    if (port == PB) {
       out = &PORTB;
   } else
   if (port == PD) {
        out = &PORTD;
   }

   if (val == LOW) *out &= ~bit;
   else *out |= bit;
   
   // VUSB Poll
   wdt_reset();
   usbPoll();
}


here is the makefile

Code: Select all

# Name: Makefile
# Project: hid-data example
# Author: Christian Starkjohann
# Creation Date: 2008-04-07
# Tabsize: 4
# Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
# License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
# This Revision: $Id$

PROGRAM = main
DEVICE  = attiny4313
F_CPU   = 16000000   # in Hz
FUSE_L  = 0xDE
FUSE_H  = 0xDB

AVRDUDE = avrdude -c usbasp -p $(DEVICE) # edit this line for your programmer

AVRSIZE = avr-size

CFLAGS  = -Iusbdrv -I. -DDEBUG_LEVEL=0
OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o $(PROGRAM).o

COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE)


all:   flash fuse clean

# symbolic targets:
help:
   @echo "This Makefile has no default rule. Use one of the following:"
   @echo "make hex ....... to build $(PROGRAM).hex"
   @echo "make program ... to flash fuses and firmware"
   @echo "make fuse ...... to flash the fuses"
   @echo "make flash ..... to flash the firmware (use this on metaboard)"
   @echo "make clean ..... to delete objects and hex file"

hex: $(PROGRAM).hex

program: flash clean

# rule for programming fuse bits:
fuse:
   @[ "$(FUSE_H)" != "" -a "$(FUSE_L)" != "" ] || \
      { echo "*** Edit Makefile and choose values for FUSE_L and FUSE_H!"; exit 1; }
   $(AVRDUDE) -U hfuse:w:$(FUSE_H):m -U lfuse:w:$(FUSE_L):m

# rule for uploading firmware:
flash: $(PROGRAM).hex
   $(AVRDUDE) -U flash:w:$(PROGRAM).hex:i

# rule for deleting dependent files (those which can be built by Make):
clean:
   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

# Generic rule for compiling C files:
.c.o:
   $(COMPILE) -c $< -o $@

# Generic rule for assembling Assembler source files:
.S.o:
   $(COMPILE) -x assembler-with-cpp -c $< -o $@
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.

# Generic rule for compiling C to assembler, used for debugging only.
.c.s:
   $(COMPILE) -S $< -o $@

# file targets:


$(PROGRAM).elf: usbdrv $(OBJECTS)   # usbdrv dependency only needed because we copy it
   $(COMPILE) -o $(PROGRAM).elf $(OBJECTS)

$(PROGRAM).hex: $(PROGRAM).elf
   rm -f $(PROGRAM).hex $(PROGRAM).eep.hex
   avr-objcopy -j .text -j .data -O ihex $(PROGRAM).elf $(PROGRAM).hex
   $(AVRSIZE) *.hex *.elf *.o
   

# debugging targets:

disasm:   $(PROGRAM).elf
   avr-objdump -d $(PROGRAM).elf

cpp:
   $(COMPILE) -E $(PROGRAM).c


And the compile result of the code which is not working
> "make.exe" program
avr-gcc -Wall -Os -DF_CPU=16000000 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=attiny4313 -c usbdrv/usbdrv.c -o usbdrv/usbdrv.o
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
avr-gcc -Wall -Os -DF_CPU=16000000 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=attiny4313 -c main.c -o main.o
main.c: In function 'main':
main.c:134:8: warning: unused variable 'i' [-Wunused-variable]
main.c: In function 'digitalWrite':
main.c:243:23: warning: 'out' may be used uninitialized in this function [-Wmaybe-uninitialized]
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
rm -f main.hex main.eep.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size *.hex *.elf *.o
text data bss dec hex filename
0 2196 0 2196 894 main.hex
2152 44 185 2381 94d main.elf
623 12 0 635 27b main.o
avrdude -c usbasp -p attiny4313 -U flash:w:main.hex:i

avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e920d
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "main.hex"
avrdude: writing flash (2196 bytes):

Writing | ################################################## | 100% 1.36s

avrdude: 2196 bytes of flash written
avrdude: verifying flash memory against main.hex:
avrdude: load data flash data from input file main.hex:
avrdude: input file main.hex contains 2196 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.70s

avrdude: verifying ...
avrdude: 2196 bytes of flash verified

avrdude done. Thank you.

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

> Process Exit Code: 0
> Time Taken: 00:03

Post Reply