diff --git a/avr/usbload/Makefile b/avr/usbload/Makefile index e882406..ded3f75 100644 --- a/avr/usbload/Makefile +++ b/avr/usbload/Makefile @@ -33,13 +33,13 @@ ifeq ($(DEBUG),1) OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o \ main.o usb_bulk.o uart.o fifo.o sram.o crc.o debug.o \ dump.o timer.o watchdog.o rle.c loader.o info.o shared_memory.o \ - irq.o command.o testing.o + pwm.o irq.o command.o testing.o else LDFLAGS =-Wl,-u CFLAGS =-Iusbdrv -I. -DDEBUG_LEVEL=0 -DNO_DEBUG -DNO_INFO OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o usb_bulk.o \ uart.o fifo.o sram.o crc.o debug.o dump.o timer.o watchdog.o rle.c loader.o \ - info.o shared_memory.o command.o irq.o + pwm.o info.o shared_memory.o command.o irq.o endif COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE) diff --git a/avr/usbload/main.c b/avr/usbload/main.c index 5f711e7..dc38db4 100644 --- a/avr/usbload/main.c +++ b/avr/usbload/main.c @@ -45,6 +45,7 @@ #include "command.h" #include "shared_memory.h" #include "irq.h" +#include "pwm.h" #include "testing.h" @@ -199,6 +200,8 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) req_state = REQ_STATUS_SNES; debug_P(DEBUG_USB, PSTR("USB_MODE_SNES:\n")); ret_len = 0; + pwm_stop(); + /* * ------------------------------------------------------------------------- */ @@ -332,6 +335,7 @@ int main(void) banner(); system_init(); + //pwm_init(); shared_memory_init(); snes_reset_hi(); snes_reset_off(); @@ -339,6 +343,7 @@ int main(void) boot_startup_rom(500); globals_init(); + usbInit(); usb_connect(); diff --git a/avr/usbload/pwm.c b/avr/usbload/pwm.c new file mode 100644 index 0000000..7086c9e --- /dev/null +++ b/avr/usbload/pwm.c @@ -0,0 +1,93 @@ +/* + * ===================================================================================== + * + * ________ .__ __ ________ ____ ________ + * \_____ \ __ __|__| ____ | | __\______ \ _______ _/_ |/ _____/ + * / / \ \| | \ |/ ___\| |/ / | | \_/ __ \ \/ /| / __ \ + * / \_/. \ | / \ \___| < | ` \ ___/\ / | \ |__\ \ + * \_____\ \_/____/|__|\___ >__|_ \/_______ /\___ >\_/ |___|\_____ / + * \__> \/ \/ \/ \/ \/ + * + * www.optixx.org + * + * + * Version: 1.0 + * Created: 07/21/2009 03:32:16 PM + * Author: david@optixx.org + * + * ===================================================================================== + */ +#include +#include +#include +#include + +#include "pwm.h" +#include "debug.h" +#include "info.h" +#include "sram.h" + + +#define PWM_SINE_MAX 255 +uint8_t pwm_sine_table[] = { +0x7f,0x82,0x85,0x88,0x8b,0x8e,0x91,0x94,0x97,0x9b,0x9e,0xa1,0xa4,0xa7,0xaa,0xad, +0xaf,0xb2,0xb5,0xb8,0xbb,0xbe,0xc0,0xc3,0xc6,0xc8,0xcb,0xcd,0xd0,0xd2,0xd4,0xd7, +0xd9,0xdb,0xdd,0xdf,0xe1,0xe3,0xe5,0xe7,0xe9,0xea,0xec,0xee,0xef,0xf0,0xf2,0xf3, +0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfb,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd, +0xfd,0xfd,0xfd,0xfd,0xfd,0xfc,0xfc,0xfb,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4, +0xf3,0xf2,0xf0,0xef,0xee,0xec,0xea,0xe9,0xe7,0xe5,0xe3,0xe1,0xdf,0xdd,0xdb,0xd9, +0xd7,0xd4,0xd2,0xd0,0xcd,0xcb,0xc8,0xc6,0xc3,0xc0,0xbe,0xbb,0xb8,0xb5,0xb2,0xaf, +0xad,0xaa,0xa7,0xa4,0xa1,0x9e,0x9b,0x97,0x94,0x91,0x8e,0x8b,0x88,0x85,0x82,0x7f, +0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,0x4f, +0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,0x25, +0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x14,0x12,0x10,0x0f,0x0e,0x0c,0x0b,0x0a, +0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x02,0x01,0x01,0x01,0x01,0x01,0x01, +0x01,0x01,0x01,0x01,0x02,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b, +0x0c,0x0e,0x0f,0x10,0x12,0x14,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,0x25,0x27, +0x2a,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,0x4f,0x51, +0x54,0x57,0x5a,0x5d,0x60,0x63,0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c,0x7f +}; + + volatile uint8_t pwm_setting; // Einstellungen für die einzelnen PWM-Kanäle + volatile uint8_t pwm_overflow; // Einstellungen für die einzelnen PWM-Kanäle + volatile uint8_t pwm_idx; // Einstellungen für die einzelnen PWM-Kanäle + + ISR(TIMER2_COMPA_vect) { + static uint8_t pwm_cnt=0; + OCR2A += (uint16_t)T_PWM; + + if (pwm_setting> pwm_cnt) + led_pwm_on(); + else + led_pwm_off(); + + if (pwm_cnt==(uint8_t)(PWM_STEPS-1)) + pwm_cnt=0; + else + pwm_cnt++; + if (!pwm_overflow++){ + pwm_setting = pwm_sine_table[pwm_idx++]; + if (PWM_SINE_MAX == pwm_idx) + pwm_idx = 0; + } +} + +void pwm_set(uint8_t val) { + pwm_setting = val; +} + +void pwm_stop(void) { + while(pwm_setting!=0xfd); + //cli(); + TIMSK2 = 0; + //sei(); +} + +void pwm_init(void) { + pwm_setting = 0x7f; + pwm_overflow = 0; + //cli(); + TCCR2B = 1; + TIMSK2 |= (1<__|_ \/_______ /\___ >\_/ |___|\_____ / + * \__> \/ \/ \/ \/ \/ + * + * www.optixx.org + * + * + * Version: 1.0 + * Created: 07/21/2009 03:32:16 PM + * Author: david@optixx.org + * + * ===================================================================================== + */ + +#ifndef __PWM_H__ +#define __PWM_H__ + +#define F_PWM 100 // PWM-Frequenz in Hz +#define PWM_STEPS 256 // PWM-Schritte pro Zyklus(1..256) + +#define T_PWM (F_CPU/(F_PWM*PWM_STEPS)) // Systemtakte pro PWM-Takt + +#if (T_PWM<(93+5)) + #error T_PWM zu klein, F_CPU muss vergrösst werden oder F_PWM oder PWM_STEPS verkleinert werden +#endif + + void pwm_init(void); +void pwm_stop(void); + + +#endif diff --git a/avr/usbload/sram.h b/avr/usbload/sram.h index 623d01c..02f3d57 100644 --- a/avr/usbload/sram.h +++ b/avr/usbload/sram.h @@ -137,6 +137,8 @@ #define led_off() ((LED_PORT &=~ (1 << LED_PIN)),\ (LED_DIR |= (1 << LED_PIN))) +#define led_pwm_on() (LED_DIR &=~ (1 << LED_PIN)) +#define led_pwm_off() (LED_DIR |= (1 << LED_PIN)) /* ---------------------------- PORT D ---------------------------- */