1.4.1 Code cleanup

This commit is contained in:
Raphaël Assénat 2013-04-26 12:23:38 +00:00
parent e4e9555583
commit 4caa7ea958
6 changed files with 68 additions and 53 deletions

View File

@ -1,3 +1,10 @@
--- v1.4.1 (April 26, 2013)
- Reduced declared max current from 120mA to 100mA. This makes
it possible to connect the adapter to my keyboard USB port but
is still a conservative estimate for the worst combination
of hypothetical non-standard power hungry controllers.
- Some code cleanups
--- v1.4 (April 20, 2013)
- Support for Atmega168. Use Makefile.atmega168.
- New USB VID/PID

View File

@ -46,19 +46,33 @@ $(HEXFILE): $(ELFFILE)
flash: $(HEXFILE)
$(AVRDUDE) -Uflash:w:$(HEXFILE) -B 1.0
# Extended fuse byte (Datasheet Table 28-5)
#
# - - - - - BOOTSZ1 BOOTSZ0 BOOTRST
# 0 0 0 0 0 0 0 1
#
EFUSE=0x01
# Fuse high byte (Datasheet Table 28-6)
#
# RSTDISBL DWEN SPIEN WDTON EESAVE BODLEVEL2 BODLEVEL1 BODLEVEL0
# 1 1 0 1 1 1 1 1
# 1 1 0 1 0 1 0 1
#
HFUSE=0xd5
# Fuse low byte (Datasheet Table 28-7)
#
# CKDIV8 CKOUT SUT1 SUT0 CKSEL3 CKSEL2 CKSEL1 CKSEL0
# 1 1 0 1 0 1 1 1
#
# Full swing crystal oscillator, BOD enabled
LFUSE=0xDF
# Full swing crystal oscillator
# 0.4 - 20 MHz : CKSEL3..1 011
#
# Crystal Oscillator, BOD enabled (Table 9-6)
# CKSEL0 : 1
# SUT1..0 : 01
#
LFUSE=0xD7
fuse:
$(AVRDUDE) -e -Uefuse:w:$(EFUSE):m -Uhfuse:w:$(HFUSE):m -Ulfuse:w:$(LFUSE):m -B 20.0 -v

View File

@ -13,7 +13,6 @@
#include <avr/pgmspace.h>
#include <string.h>
#include "gamepad.h"
#include "leds.h"
#include "fournsnes.h"
#define GAMEPAD_BYTES 8 /* 2 byte per snes controller * 4 controllers */

18
leds.h
View File

@ -1,18 +0,0 @@
#ifndef _leds_h__
#define _leds_h__
#ifndef PORTD
#include <avr/io.h>
#endif
#define LED_OFF()
#define LED_ON();
#define LED_TOGGLE();
//#define LED_OFF() do { PORTD |= 0x20; } while(0)
//#define LED_ON() do { PORTD &= ~0x20; } while(0)
//#define LED_TOGGLE() do { PORTD ^= 0x20; } while(0)
#endif

71
main.c
View File

@ -18,7 +18,6 @@
#include "fournsnes.h"
#include "leds.h"
#include "devdesc.h"
static uchar *rt_usbHidReportDescriptor=NULL;
@ -101,32 +100,40 @@ static Gamepad *curGamepad;
static void hardwareInit(void)
{
uchar i, j;
/* PORTB
*
* Bit Description Direction Level/pu
* 0 Jumpers common Out 0
* 1 JP1 In 1
* 2 JP2 In 1
* 3 MOSI In 1
* 4 MISO In 1
* 5 SCK In 1
* 6 -
* 7 -
*/
DDRB = 0x01;
PORTB = 0xFE;
// init port C as input with pullup
DDRC = 0x00;
PORTC = 0xff;
/* 1101 1000 bin: activate pull-ups except on USB lines
*
* USB signals are on bit 0 and 2.
*
* Bit 1 is connected with bit 0 (rev.C pcb error), so the pullup
* is not enabled.
* */
PORTD = 0xf8;
/* Usb pin are init as outputs */
/*
* For port D, activate pull-ups on all lines except the D+, D- and bit 1.
*
* For historical reasons (a mistake on an old PCB), bit 1
* is now always connected with bit 0. So bit 1 configured
* as an input without pullup.
*
* Usb pin are init as output, low. (device reset). They are released
* later when usbReset() is called.
*/
PORTD = 0xf8;
DDRD = 0x01 | 0x04;
j = 0;
while(--j){ /* USB Reset by device only required on Watchdog Reset */
i = 0;
while(--i); /* delay >10ms for USB reset */
}
DDRD = 0x00; /* 0000 0000 bin: remove USB reset condition */
/* Configure timers */
#if defined(AT168_COMPATIBLE)
TCCR2A= (1<<WGM21);
TCCR2B=(1<<CS22)|(1<<CS21)|(1<<CS20);
@ -137,6 +144,19 @@ static void hardwareInit(void)
#endif
}
static void usbReset(void)
{
/* [...] a single ended zero or SE0 can be used to signify a device
reset if held for more than 10mS. A SE0 is generated by holding
both th D- and D+ low (< 0.3V).
*/
PORTD &= ~(0x01 | 0x04); // Set D+ and D- to 0
DDRD |= 0x01 | 0x04;
_delay_ms(15);
DDRD &= ~(0x01 | 0x04);
}
#if defined(AT168_COMPATIBLE)
#define mustPollControllers() (TIFR2 & (1<<OCF2A))
@ -213,17 +233,11 @@ uchar usbFunctionSetup(uchar data[8])
int main(void)
{
char must_report = 0; /* bitfield */
uchar idleCounters[MAX_REPORTS];
int i;
unsigned char run_mode;
memset(idleCounters, 0, MAX_REPORTS);
DDRB |= 0x01;
DDRB &= ~0x06;
PORTB |= 0x06; /* enable pull up on DB1 and DB2 */
PORTB &= ~0x01; /* Set DB0 to low */
hardwareInit();
_delay_ms(10); /* let pins settle */
run_mode = (PINB & 0x06)>>1;
@ -264,8 +278,7 @@ int main(void)
// patch the config descriptor with the HID report descriptor size
my_usbDescriptorConfiguration[25] = rt_usbHidReportDescriptorSize;
//wdt_enable(WDTO_2S);
hardwareInit();
usbReset();
curGamepad->init();
usbInit();

View File

@ -5,7 +5,7 @@
* Tabsize: 4
* Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
* License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt)
* This Revision: $Id: usbconfig.h,v 1.9 2013-04-20 17:24:53 cvs Exp $
* This Revision: $Id: usbconfig.h,v 1.10 2013-04-26 12:23:38 cvs Exp $
*/
#ifndef __usbconfig_h_included__
@ -89,7 +89,7 @@ rename it to "usbconfig.h". Then edit it accordingly.
/* Define this to 1 if the device has its own power supply. Set it to 0 if the
* device is powered from the USB bus.
*/
#define USB_CFG_MAX_BUS_POWER 120
#define USB_CFG_MAX_BUS_POWER 100
/* Set this variable to the maximum USB bus power consumption of your device.
* The value is in milliamperes. [It will be divided by two since USB
* communicates power requirements in units of 2 mA.]