Page 1 of 1

Getting V-USB to compile under Atmel Studio 6

Posted: Tue Nov 27, 2012 11:50 pm
by winelight
This is what I did to get V-USB to compile on Atmel Studio 6. Credit to the many people who've answered the various questions people have raised before here and in other forums, but I couldn't find all the answers, or all of them in the same place, which is why I made this list.

Of course I don't know if this is the best way, or even the right way to go about it, but it gets rid of the errors for me. It seems a shame that you have to define the include paths and the CPU frequency twice: if there's some global way to do it, I can't find it.

    Download and unzip the latest version of the V-USB source code. Take a note of which example you are going to start with, because you need to get to the files in the /firmware folder for that example.

    Make a new project in Atmel Studio 6 with File / New / Project. Select "GCC C Executable project" and your target processor, and give it a sensible name.

    Copy and paste the code from the main.c that came with V-USB for your chosen example into the main.c that Atmel Studio creates (it will be called yourpojectname.c and should be open on the screen in front of you).

    Go to the Project menu. (The name of your project should appear on that menu with Properties after it. If it doesn't, it's because you currently have a file open that is not part of your project.) Select Project / (Name of your project) Properties / Toolchain tab / AVR/GNU C Compiler / Directories / Include path and add the path to where where the firmware/usbdrv folder is for your chosen example.

    You will need to do the same for the assembler. So… AVR/GNU Assembler / General / Include paths.

    Now you will get errors like "variable 'usbDescriptorDevice' must be const in order to be put into read-only section by means of '__attribute__((progmem))". So go and add const to the start of every line there is a complaint about. Changes may be necessary in usbdrv.h, usbdrv.c and your main C program.

    Copy usbconfig.h from the example project to your own project folder. At some point you will need to go in and check / change this file, but let's at least get things to compile.

    You will now get errors "undefined reference to `usbMsgPtr'", and similar. Go to the "solution explorer" pane in the top right of your screen, right-click on the name of your project, then select Add / Existing item. Navigate to usbdrv.c (which will be in the firmware/usbdrv folder for your chosen example) and add it.

    While you're there, also add:
    usbdrvasm.asm
    usbdrvasm.S
    usbportability.h

    Now you will get a lot more const error messages generated by usbdrv.c so go and fix those.

    Next you get a F_CPU not defined error. This is because normally it is defined in the makefile. So go to Project / Properties / AVR/GNU C Compiler / Symbols / Defined symbols -D and add F_CPU=12000000 or whatever frequency you are using.

    As before you need to do this for assembler too. So back to Project / (Your project name) Properties / Toolchain / Assembler / General / Assembler flags and add -DF_CPU=12000000 because if you don't do this second step, you'll get an error message "USB_CFG_CLOCK_KHZ is not one of the supported non-crc-rates!"

Bingo.

(edit)

Fuses
One final note. If you are using Atmel Studio 6 then are you are likely using another utility to program your ATMega and set the fuses. With the default V-USB that you downloaded, this is all done for you by the makefile. So, you have another step to do.

Work out the fuse settings (you can probably use the default value you find in the V-USB makefile, but check to be sure) and program this using your ATMega utility (probably avrdude, or a GUI for it).

Beware: the fuses are inverted.

So SPIEN (enable serial programming) is actually set to 0, not 1, to "program" the fuse and enable serial programming. But if you use an online fuse calculator, or a GUI to set the fuses, they probably invert the logic again: so for SPIEN you check the checkbox to enable programming, which sets the fuse to 0, which because the fuses are inverted actually does enable the serial programming.

Re: Getting V-USB to compile under Atmel Studio 6

Posted: Mon Sep 07, 2015 3:53 am
by swulf
So here is my experience under Atmel Studio 6 (Version: 6.2.1563 - Service Pack 2) and Windows 7 64 bit.

'Download and unzip the latest version of the V-USB source code. Take a note of which example you are going to start with, because you need to get to the files in the /firmware folder for that example.'

OK. I'm trying to build PowerSwitch so I get this here.

'Make a new project in Atmel Studio 6 with File / New / Project. Select "GCC C Executable project" and your target processor, and give it a sensible name.'

OK.

'Copy and paste the code from the main.c that came with V-USB for your chosen example into the main.c that Atmel Studio creates (it will be called yourpojectname.c and should be open on the screen in front of you).'

OK.

'Go to the Project menu. (The name of your project should appear on that menu with Properties after it. If it doesn't, it's because you currently have a file open that is not part of your project.) Select Project / (Name of your project) Properties / Toolchain tab / AVR/GNU C Compiler / Directories / Include path and add the path to where where the firmware/usbdrv folder is for your chosen example.'

So at this point I diverged. I had tried several times keeping stuff in other directories and pointing to it, and it never worked. What I did here was to copy everything from the downloaded ZIP archive ('PowerSwitch.2012-12-08.zip' in my case) into the so-called Atmel project directory 'C:\Users\<Username>\Documents\Atmel Studio\6.2\PSwitch3\PSwitch3' in my case. For PowerSwitch, this ended up containing the following files:

  • Debug {directory}
  • PSwitch3.c
  • PSwitch3.cproj
  • asmcommon.inc
  • Changelog.txt
  • checksize
  • CommercialLicense.txt
  • License.txt
  • main.c
  • main.hex
  • Mskefile
  • oddebug.c
  • oddebug.h
  • Readme.txt
  • usbconfig.h
  • usbconfig-prototype.h
  • usbdrv.c
  • usbdrv.h
  • usbdrvasm.asm
  • usbdrvasm.S
  • usbdrvasm12.inc
  • usbdrvasm15.inc
  • usbdrvasm16.inc
  • usbdrvasm18-crc.inc
  • usbdrvasm20.inc
  • usbdrvasm128.inc
  • usbdrvasm165.inc
  • USB-ID-FAQ.txt
  • USB-IDs-for-free.txt
  • usbportability.h

and the directory Debug contains the following:

  • makedep.mk
  • Makefile
  • usbdrv.d
  • usbdrv.o
  • PSwitch3.d
  • PSwitch3.map
  • PSwitch3.o


' You will need to do the same for the assembler. So… AVR/GNU Assembler / General / Include paths.


Now you will get errors like "variable 'usbDescriptorDevice' must be const in order to be put into read-only section by means of '__attribute__((progmem))". So go and add const to the start of every line there is a complaint about. Changes may be necessary in usbdrv.h, usbdrv.c and your main C program.


Copy usbconfig.h from the example project to your own project folder. At some point you will need to go in and check / change this file, but let's at least get things to compile.'

I did not do any of this.

' You will now get errors "undefined reference to `usbMsgPtr'", and similar. Go to the "solution explorer" pane in the top right of your screen, right-click on the name of your project, then select Add / Existing item. Navigate to usbdrv.c (which will be in the firmware/usbdrv folder for your chosen example) and add it.


While you're there, also add:
usbdrvasm.asm
usbdrvasm.S
usbportability.h


Now you will get a lot more const error messages generated by usbdrv.c so go and fix those.'

Nope. I did add the following files in the Solution Explorer (upper right of the Atmel Studio window):

  • usbdrv.c
  • usbdrvasm.asm
  • usbdrvasm.S
  • usbportability.h


'Next you get a F_CPU not defined error. This is because normally it is defined in the makefile. So go to Project / Properties / AVR/GNU C Compiler / Symbols / Defined symbols -D and add F_CPU=12000000 or whatever frequency you are using.'


Nope. Now if I hit F7 it will compile with the error 'USB_CFG_CLOCK_KHZ is not one of the supported non-crc-rates!'. So let's try and fix that by the suggested method:

'So back to Project / (Your project name) Properties / Toolchain / Assembler / General / Assembler flags and add -DF_CPU=12000000 because if you don't do this second step, you'll get an error message "USB_CFG_CLOCK_KHZ is not one of the supported non-crc-rates!"'

Now F7 will result in a successful compile:

'Build succeeded.
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped =========='