# Make LUFA for Arduino UNO / atmega16u2

I have an Arduino Uno r3 board which has an atmega16u2 chip that normally contains the usb to serial firmware that allows the board to communicate with the IDE.

I have been learning how to flash that chip with different firmwares that let it act as other types of USB devices ( Keyboard / Mouse specifically ).

I have found some hex files that I can use online, but I am trying to learn how to build my own version of the keyboard and mouse hex files.

Here are the variables that I've set in the make file:

MCU = atmega16u2
ARCH = AVR8
BOARD = UNO
F_CPU = 16000000

the LUFA_PATH is set like this (I did not change it):

LUFA_PATH = ../../../..

When I save the makefile like this and try to run make all an error occurs:

../../../../LUFA/Drivers/Board/Joystick.h:119:31: error: Board/Joystick.h: No such file or directory

../../../../Common/Common.h: No such file or directory

../../../../LUFA/Drivers/Board/Buttons.h:135:30: error: Board/Buttons.h: No such file or directory

The weird part is that I can travel to the path that it states and those files do actually exist and are located there.

How do I need to set my LUFA makefile parameters in order to build for the atmega16u2 that is on the Arduino UNO rev3?

EDIT: These are the only steps that I've taken, which led to these errors.

• Travel to dir: C:\LUFA-111009\Demos\Device\ClassDriver\KeyboardMouse

• Open makefile in this directory.

• change the MCU, Board, and F_CPU speed to the values located above. (Arch is already set correctly)

• save make file

• Open cmd in this dir.

• Type "make all"

this process results in the above errors.

EDIT 2: Ok, I made blank Buttons.h, and Joystick.h and put them in the KeyboardMouse/Board/ folder. that got rid of the file not found errors but still gives me this:

KeyboardMouse.c: In function 'SetupHardware':
KeyboardMouse.c:111: warning: implicit declaration of function 'clock_prescale_s
et'
KeyboardMouse.c:111: error: 'clock_div_1' undeclared (first use in this function
)
KeyboardMouse.c:111: error: (Each undeclared identifier is reported only once
KeyboardMouse.c:111: error: for each function it appears in.)
KeyboardMouse.c:114: warning: implicit declaration of function 'Joystick_Init'
KeyboardMouse.c: In function 'CALLBACK_HID_Device_CreateHIDReport':
KeyboardMouse.c:174: warning: implicit declaration of function 'Joystick_GetStat
us'
KeyboardMouse.c:175: warning: implicit declaration of function 'Buttons_GetStatu
s'
KeyboardMouse.c:183: error: 'BUTTONS_BUTTON1' undeclared (first use in this func
tion)
KeyboardMouse.c:188: error: 'JOY_UP' undeclared (first use in this function)
KeyboardMouse.c:190: error: 'JOY_DOWN' undeclared (first use in this function)
KeyboardMouse.c:193: error: 'JOY_LEFT' undeclared (first use in this function)
KeyboardMouse.c:195: error: 'JOY_RIGHT' undeclared (first use in this function)
KeyboardMouse.c:198: error: 'JOY_PRESS' undeclared (first use in this function)
make: *** [KeyboardMouse.o] Error 1

## migrated from stackoverflow.comJan 3 '12 at 21:56

This question came from our site for professional and enthusiast programmers.

• have you created Board folder and added Joystick.h and Buttons.h in KeyboardMouse folder? If you specify unsupported (out of the box) board you need to specify Joystick.h and Buttons.h in the Board folder under project's folder – Mihailo Jan 4 '12 at 9:31
• ../../ etc is a relative path rather than an absolute one. Your test of verifying that the files exist in that location would only be valid if you attempted the test from the same working directory as the compiler attempts to access them from. Absolute paths would be easier to debug by getting rid of this mystery, though would result in a less portable project structure. – Chris Stratton Jan 4 '12 at 17:04
• Yes, when I tested it I started from the directory that I am calling make from. – FoamyGuy Jan 4 '12 at 17:12
• I've updated my answer there, you should copy buttons.h and joystick.h from USBKEY to overcome most of these issues. This is only to make you going, you still need to make your board specific changes, I hope you have enough info in my edited answer – Mihailo Jan 5 '12 at 9:46

As per my comment, you have specified UNO board, but it doesn't have Keyboard/Joystick required definitions for the board (I'm not sure if UNO has any buttons to be defined there, it only has Leds, and leds are defined in Led.h - check out \LUFA-111009\LUFA\Drivers\Board\AVR8\UNO)

So what you could do is to create Board folder under your KeyboardMouse and make empty files Joystick.h and Buttons.h there. This should get you going further. Errors you are seeing are due to the following code in \LUFA\Drivers\Board\Buttons.h

#if (BOARD == BOARD_NONE)
#error The Board Buttons driver cannot be used if the makefile BOARD option is not set.
#elif (BOARD == BOARD_USBKEY)
#include "AVR8/USBKEY/Buttons.h"
....
#else
#include "Board/Buttons.h" <------ THIS IS EXECUTED SINCE UNO DOES NOT HAVE BUTTONS
#endif

../../../../LUFA/Drivers/Board/Joystick.h:119:31: error: Board/Joystick.h: No such file or directory

means that your folder structure and your LUFA configuration is correct, but you're missing file Buttons.h in your KeyboardMouse/Board/ folder. Got it?

Try what I've suggested and see how far you get. You can see how to define buttons in other Board's folders, for example in LUFA\Drivers\Board\AVR8\USBKEY\

EDIT Btw, I forgot to mention, error about common.h should go away hopefully after fixing this since that ........\ is coming from a file in a different location in folder structure thus confusion.

EDIT OK, so here's the link on how to build custom board drivers: http://www.fourwalledcubicle.com/files/LUFA/Doc/111009/html/page_writing_board_drivers.html

What you need to do is to copy files Buttons.h and Joystick.h LUFA\CodeTemplates\DriverStubs\ (or try copying Buttons.h and Joystick.h from USBKEY better, I think you still would need to specify a value for each definition otherwise) This should get rid of undefined errors. You have TODO sections in the files that you need to update.

OK, so I think I should also mention how this is supposed to be used before going further. These drivers/definitions are meant to be used in a specific manner in your code, and LUFA is unifying the approach for you. As far as I can tell, buttons are used in a following manner:

if (Buttons_GetStatus() & BUTTONS_BUTTON1){ ... do something when button 1 pressed....

This way, if you have several boards with at least one button, your code should theoretically stay the same across the boards. Similar stands for LEDs, you can use them like:

....

I hope you get the picture. In order to use these library functions you have to define buttons/joystick/led specifics in their respective header files. So for example - in buttons.h you need to specify any custom header files you need, add port masks for buttons (on which pin of which port they are connected), specify port initialization and how to read the status of the buttons. You can find all of that in the USBKEY's buttons.h - e.g. it's importing common.h, defines BUTTONS_BUTTON1 like pin 2 of a port, initializes PortE with this button (so button is pin 2 on port E), and in Buttons_GetStatus it reads the status of the button.

I could go on and on in the same manner for joystick as well, but I hope you get the picture. Joystick is more involved but it's like having 4 buttons of which 0, 1 or 2 can be active at any time.

BTW, this is only useful if you have any buttons on your board. For example, I made keyboard driver without any buttons (I had to remove buttons specific code though). I used Ir Diode to read remote control codes and make the board act as keyboard. So you don't really need the buttons, nor the joystick (of course, it completely depends on what you're doing).

• Ok, I made blank .h files in my Board folder and it got a little bit farther along, but still doesn't make it all the way. I checked out the Buttons.h inside the USBKEY folder. But I don't really understand what is going on inside of there. – FoamyGuy Jan 4 '12 at 16:56
• This is a good answer. After reading this, I was able to just set dummy values for those JOY_ and BUTTON_ defines, as I'm not going to be using them. But what I don't know is how to actually make the Keyboard example work using actual keyboard keys, not Joystick signals like in the example. Any advice? – trusktr Jun 25 '14 at 2:32

Making Arduino HID firmware for the r3 UNO should be basically the same as for the earlier UNO. Take a look at my blog for example code, firmware, and directions: Arduino Hacking (HID keyboard, mouse, joystick, and MIDI firmware).

Here's the meat of the post:

## Build and install binutils for the AVR

wget ftp://sourceware.org/pub/binutils/releases/binutils-2.21.tar.bz2
tar xjf binutils-2.21.tar.bz2
cd binutils-2.21
mkdir build
cd build
../configure --target=avr
make
sudo make install
cd ../..

## Build and install libgmp (needed for gcc-4.5.3)

wget ftp://ftp.gmplib.org/pub/gmp-5.0.2/gmp-5.0.2.tar.bz2
tar xjf gmp-5.0.2.tar.bz2
sudo apt-get install m4
cd gmp-5.0.2
./configure
make
sudo make install
cd ..

## Install libmpfr and libmpc (needed for gcc-4.5.3)

sudo apt-get install libmpfr-dev
sudo apt-get install libmpc-dev

## Build and install gcc-4.5.3 for AVR

wget ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-4.5.3/gcc-4.5.3.tar.bz2
tar xjf gcc-4.5.3.tar.bz2
cd gcc-4.5.3
mkdir build
cd build
CFLAGS="-g -O2" ../configure --target=avr --with-gcc --with-gnu-as --enable-languages=c,c++ --disable-nls --disable-libssp --with-dwarf2
make
sudo make install
cd ../..

## Build and install avr-libc

tar xjf avr-libc-1.7.1.tar.bz2
cd avr-libc-1.7.1
mkdir build
cd build
CFLAGS=-Os ../configure --build=../config.guess --host=avr
make
sudo make install

## Do a test build of Arduino-usbserial

The Arduino-usbserial firmware is the default firmware that the UNO comes installed with on its atmega8u2. It implements a serial interface over USB. Build this firmware to check that your avr-gcc toolchain and avr-libc are working.

cd Arduino/hardware/arduino/firmwares/arduino-usbserial

edit the makefile and make sure that MCU = atmega8u2 and ARDUINO_PID = 0x0001

make

You should have an newly built Arduino-usbserial.hex file.

• I found your blog a while back and it has helped me out a ton =). I have successfully flashed your keyboard hex file. I was trying to make one that would allow the chip to send both keystrokes and mouse actions, that is why I was trying to compile the KeyboardMouse demo that comes with lufa. I think at one point I even tried to copy the first few settings from your makefile into the default one for the KeyboardMouse demo, but still had no luck. Its been a bit since I was messing with it, I'll give it another go, and see if I can't get it working. – FoamyGuy Jan 10 '12 at 20:46
• Welcome to Electrical Engineering, and thanks for sharing your blog with us! It looks like a very useful resource. However, we believe that answers should be more than links. This not only consolidates the information in one place, it also serves as a great place to summarize a lengthy article, and prevents link-rot if you want to take down or move your blog. Also, please don't sign your posts; your user card in the lower right accomplishes that! – Kevin Vermeer Jan 11 '12 at 19:18
• I tried to quote the relevant parts of your blog here, feel free to add to, cut down, or change the quotation as you see fit. – Kevin Vermeer Jan 11 '12 at 19:19
• @KevinVermeer Thanks for the heads-up and assistance. "More than a link" makes sense. I will do a better job on future posts :-). – Darran Hunt Jan 12 '12 at 20:49
• Tim I have an r3 UNO now, so I'll have a go at building and running basic keyboard firmware on its atemga16u2. It's awesome that its been upgraded, we can probably fit in firmware that provides both serial access and HID features so you can still load sketches with the custom firmware loaded. – Darran Hunt Jan 12 '12 at 20:56