Tutorial - How to change firmware on 8u2

Hi,
This is the next step : how to make a joytick emulator with the arduino-uno.

First step
Be sure to be able to upload the initial firwmare arduino-serial: we need this firmware to upload the Atmega328.

How it works
The Atmega328 sends to 8u2 a byte on serial communication that represents the state of the joystick.

Point of attention
It's important to load first the atmega328 program and then the 8u2 firmware.
Once the firmware of 8u2 is upload with joystick program, atmega328 can't be loaded.
In order to modify atmega328 program, the original firmware of 8u2 need to be re-loaded.

Atmega328 program

/*
Program test for arduino uno
8u2 firmware-> Usb Joystick
ant.b
6 oct 2010
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1285962838
 */

void setup() 
{ 
  Serial.begin(9600); 
} 

void loop() 
{ 
send_byte(0,0,0,0,0,0);
send_byte(1,0,0,0,0,0);
send_byte(0,1,0,0,0,0);
send_byte(0,0,1,0,0,0);
send_byte(0,0,0,1,0,0);
send_byte(0,0,0,0,1,0);
send_byte(0,0,0,0,0,1);
} 

void send_byte(boolean JOY_UP, boolean JOY_DOWN,
                boolean JOY_LEFT, boolean JOY_RIGHT,
                boolean JOY_PRESS, boolean BUTTONS_BUTTON1)
{ 
  byte byte_to_send;// use a 'byte' not a 'int'-> a int will send 3 bytes via serial
  byte_to_send= JOY_UP*32 + JOY_DOWN *16  +
                JOY_LEFT*8 + JOY_RIGHT*4 +
                JOY_PRESS*2 +  BUTTONS_BUTTON1*1;
  Serial.print(byte_to_send);  
  delay(1000);//delay 1s before send another byte
}

8u2 program

  • copy the folder LUFA_100807\Demos\Device\ClassDriver\Joystick to LUFA_100807\Projects\Arduino-Joystick
  • copy the following files from *arduino-0021\hardware\arduino\firmwares\arduino-usbserial* to LUFA_100807\Projects\Arduino-Joystick
  • folder 'Lib'
  • folder 'Board'
  • file makefile

Changes to make
In makefile
Set TARGET = Joystick
In Joystick.h
Delete #include <LUFA/Drivers/Board/Joystick.h>
Delete #include <LUFA/Drivers/Board/Buttons.h>
In Joystick.c
Add this on the begining:

#include <LUFA/Drivers/Peripheral/Serial.c>
uint8_t temp;
const uint8_t JOY_UP= 0b100000;
const uint8_t JOY_DOWN = 0b010000;
const uint8_t JOY_LEFT= 0b001000;
const uint8_t JOY_RIGHT= 0b000100;
const uint8_t JOY_PRESS= 0b000010;
const uint8_t BUTTONS_BUTTON1= 0b000001;

Add this in void SetupHardware(void)
 Serial_Init(9600, false);

I delete clock_prescale_set(clock_div_1); because this line can't compile. I don't know if it is usefull ??
I also delete
Joystick_Init();

  • Buttons_Init();*
    I add this in for( ;;; )
while (Serial_IsCharReceived())
                {
            temp=Serial_RxByte();
            Serial_TxByte(temp);// Sendback the same byte juste for information
            }

And finally replace the content of CALLBACK_HID_Device_CreateHIDReport by that

USB_JoystickReport_Data_t* JoystickReport = (USB_JoystickReport_Data_t*)ReportData;

uint8_t JoyStatus_LCL    =temp; //Joystick_GetStatus();

      if (JoyStatus_LCL & JOY_UP)
             JoystickReport->Y = -100;
        else if (JoyStatus_LCL & JOY_DOWN)
              JoystickReport->Y =  100;

       if (JoyStatus_LCL & JOY_LEFT)
              JoystickReport->X = -100;
       else if (JoyStatus_LCL & JOY_RIGHT)
              JoystickReport->X =  100;

       if (JoyStatus_LCL & JOY_PRESS)
              JoystickReport->Button  = (1 << 1);

       if (JoyStatus_LCL & BUTTONS_BUTTON1)
               JoystickReport->Button |= (1 << 0);

      *ReportSize = sizeof(USB_JoystickReport_Data_t);
        return false;

Then compile and upload this program on 8u2

Verify
You can verify that it is working by unplug and replug the usb connection.

The joystick is visible in configuration pannel / gamepad.

Comment : I have had some difficulties with Flip : I often had the error 'Address is out of range'. I don't really now why?? Does someone have an idea?