Almost working.

This commit is contained in:
Adrian Woodley 2018-06-05 23:25:53 +08:00
parent b952af135b
commit c173d795c6

191
main.c
View File

@ -1,5 +1,6 @@
#include <avr/io.h>
#include <avr/interrupt.h> /* for sei() */
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <util/delay.h> /* for _delay_ms() */
@ -13,7 +14,36 @@
/* ----------------------------- USB interface ----------------------------- */
/* ------------------------------------------------------------------------- */
PROGMEM const char usbHidReportDescriptor[52] = { /* USB report descriptor, size must match usbconfig.h */
// PROGMEM const char usbHidReportDescriptor[52] = { /* USB report descriptor, size must match usbconfig.h */
// 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
// 0x09, 0x02, // USAGE (Mouse)
// 0xa1, 0x01, // COLLECTION (Application)
// 0x09, 0x01, // USAGE (Pointer)
// 0xA1, 0x00, // COLLECTION (Physical)
// 0x05, 0x09, // USAGE_PAGE (Button)
// 0x19, 0x01, // USAGE_MINIMUM
// 0x29, 0x03, // USAGE_MAXIMUM
// 0x15, 0x00, // LOGICAL_MINIMUM (0)
// 0x25, 0x01, // LOGICAL_MAXIMUM (1)
// 0x95, 0x03, // REPORT_COUNT (3)
// 0x75, 0x01, // REPORT_SIZE (1)
// 0x81, 0x02, // INPUT (Data,Var,Abs)
// 0x95, 0x01, // REPORT_COUNT (1)
// 0x75, 0x05, // REPORT_SIZE (5)
// 0x81, 0x03, // INPUT (Const,Var,Abs)
// 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
// 0x09, 0x30, // USAGE (X)
// 0x09, 0x31, // USAGE (Y)
// 0x09, 0x38, // USAGE (Wheel)
// 0x15, 0x81, // LOGICAL_MINIMUM (-127)
// 0x25, 0x7F, // LOGICAL_MAXIMUM (127)
// 0x75, 0x08, // REPORT_SIZE (8)
// 0x95, 0x03, // REPORT_COUNT (3)
// 0x81, 0x06, // INPUT (Data,Var,Rel)
// 0xC0, // END_COLLECTION
// 0xC0, // END COLLECTION
// };
PROGMEM const char usbHidReportDescriptor[] = { /* USB report descriptor, size must match usbconfig.h */
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x02, // USAGE (Mouse)
0xa1, 0x01, // COLLECTION (Application)
@ -33,12 +63,14 @@ PROGMEM const char usbHidReportDescriptor[52] = { /* USB report descriptor, size
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x09, 0x38, // USAGE (Wheel)
0x15, 0x81, // LOGICAL_MINIMUM (-127)
0x25, 0x7F, // LOGICAL_MAXIMUM (127)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x03, // REPORT_COUNT (3)
0x95, 0x03, // REPORT_COUNT (2)
0x81, 0x06, // INPUT (Data,Var,Rel)
0x09, 0x38, // USAGE (Wheel)
0x95, 0x01, // REPORT_COUNT (1)
0x81, 0x06, // INPUT (Data, Variable, Relative)
0xC0, // END_COLLECTION
0xC0, // END COLLECTION
};
@ -60,22 +92,6 @@ static report_t reportBuffer;
static uchar idleRate; /* repeat rate for keyboards, never used for mice */
/* The following function advances sin/cos by a fixed angle
* and stores the difference to the previous coordinates in the report
* descriptor.
* The algorithm is the simulation of a second order differential equation.
*/
// static void advanceCircleByFixedAngle(void)
// {
// char d;
// #define DIVIDE_BY_64(val) (val + (val > 0 ? 32 : -32)) >> 6 /* rounding divide */
// reportBuffer.dx = d = DIVIDE_BY_64(cosinus);
// sinus += d;
// reportBuffer.dy = d = DIVIDE_BY_64(sinus);
// cosinus -= d;
// }
static void read_Wheel(void)
{
reportBuffer.dx = 0;
@ -84,7 +100,9 @@ static void read_Wheel(void)
int Direction = (AnalogIn < 512) ? -1 : 1;
// PORTD = 0;
timing = get_timing(AnalogIn);
// PINB |= _BV(PB1);
if (AnalogIn < LowerDead[Debounce] || AnalogIn > UpperDead[Debounce])
{
@ -92,7 +110,9 @@ static void read_Wheel(void)
{
reportBuffer.dWheel = Direction;
Debounce = 0;
// ticktock = 0;
ticktock = 0;
PINB |= _BV(PB1);
// PINB &= ~(1<<PB1);
// LEDs_ToggleLEDs(LEDS_LED2);
}
}
@ -100,16 +120,23 @@ static void read_Wheel(void)
{
if (Debounce == 0)
{
// PORTD = (1<<PD1);
// PINB |= (1<<PB1);
Debounce = 1;
}
}
// *ReportSize = sizeof(USB_MouseReport_Data_t);
// return true;
// *ReportSize = sizeof(USB_MouseReport_Data_t);
// return true;
}
void clearReport()
{
reportBuffer.dx = 0;
reportBuffer.dy = 0;
reportBuffer.dWheel = 0;
reportBuffer.buttonMask = 0;
}
uint8_t get_timing(uint16_t value)
{
@ -126,27 +153,26 @@ uint8_t get_timing(uint16_t value)
ISR(ADC_vect)
{
AnalogIn = ADC;
timing = get_timing(ADC);
}
ISR(TIMER1_COMPA_vect)
ISR(TIM0_COMPA_vect)
{
// usbPoll();
// PINB |= _BV(PB1);
if (ticktock > 0)
if (ticktock > 10)
{
PINB |= _BV(PB1);
usbPoll();
// PINB |= _BV(PB1);
// usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
// PINB |= (1<<PB1);
// PINB |= _BV(PB1);
read_Wheel();
if(usbInterruptIsReady())
{
// PINB |= (1<<PB1);
/* called after every poll of the interrupt endpoint */
read_Wheel();
// DBG1(0x03, 0, 0); /* debug output: interrupt report prepared */
// PINB |= _BV(PB1);
usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
}
// HID_Device_USBTask(&Mouse_HID_Interface);
// USB_USBTask();
ticktock = 0;
}
else
@ -155,65 +181,78 @@ ISR(TIMER1_COMPA_vect)
ticktock++;
}
// ticktock++;
// LEDs_ToggleLEDs(LEDS_LED1);
wdt_reset();
// wdt_reset();
}
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware(void)
{
cli();
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR &= ~(1 << WDRF);
wdt_disable();
/* Hardware Initialization */
// LEDs_Init();
// USB_Init();
// clearReport();
usbInit();
/* enforce re-enumeration, do this while interrupts are disabled! */
usbDeviceDisconnect();
for (int i = 0; i<250; i++)
{ /* fake USB disconnect for > 250 ms */
// wdt_reset();
_delay_ms(4);
}
usbDeviceConnect();
/* ADC Settings */
// prescaler = 128
ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
// VCC Ref, ADC6
// VCC Ref, ADC1
ADMUX = (1<<MUX0);
ADCSRA |= (1<<ADEN)|(1<<ADIE)|(1<<ADSC)|(1<<ADATE);
ADCSRA |= (1<<ADIE)|(1<<ADSC)|(1<<ADATE);
ADCSRA |= (1<<ADEN);
// /* Timer1 settings */
// TCCR1A = 0;
// // Mode = CTC, Prescaler = 64
// TCCR1B |= (1 << WGM12)|(1 << CS11)|(1 << CS10);
TCCR1 |= (1<<CTC1)|(1<<CS13)|(1<<CS11)|(1<<CS10);
// initialize counter
TCNT1 = 0;
// TCCR1 |= (1<<CTC1)|(1<<CS13)|(1<<CS11)|(1<<CS10);
// // initialize counter
// TCNT1 = 0;
// initialize compare value
OCR1A = 241; // 15ms
OCR1C = 241; // 15ms
// // initialize compare value
// OCR1A = 241; // 15ms
// // OCR1C = 241; // 15ms
// enable compare interrupt
TIMSK = (1 << OCIE1A);
// // // enable compare interrupt
// TIMSK |= (1 << OCIE1A);
// LEDs_TurnOffLEDs(LEDS_ALL_LEDS);
TCCR0A = (1<<WGM01);
TCCR0B = (1<<CS02);
TCNT0 = 0;
OCR0A = 241;
TIMSK |= (1<<OCIE0A);
DDRB |= (1<<PB1);
usbInit();
usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
for (int i = 0; i<250; i++)
{ /* fake USB disconnect for > 250 ms */
wdt_reset();
_delay_ms(2);
}
usbDeviceConnect();
sei();
wdt_enable(WDTO_1S);
// wdt_enable(WDTO_1S);
}
usbMsgLen_t usbFunctionSetup(uchar data[8])
{
usbRequest_t *rq = (void *)data;
usbRequest_t *rq = (void *)data;
/* The following requests are never used. But since they are required by
* the specification, we implement them in this example.
@ -237,12 +276,14 @@ usbRequest_t *rq = (void *)data;
}
// Called by V-USB after device reset
void hadUsbReset() {
void hadUsbReset()
{
cli();
int frameLength, targetLength = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
int bestDeviation = 9999;
uchar trialCal, bestCal, step, region;
bestCal = 0;
bestCal = OSCCAL;
// do a binary search in regions 0-127 and 128-255 to get optimum OSCCAL
for(region = 0; region <= 1; region++) {
@ -266,6 +307,7 @@ void hadUsbReset() {
}
OSCCAL = bestCal;
sei();
}
/* ------------------------------------------------------------------------- */
@ -290,10 +332,27 @@ int main(void)
// _delay_ms(500);
// }
for(;;){ /* main event loop */
for(;;)
{ /* main event loop */
// if (USBIN&USBMASK)
// {
// sleep_cpu(); // sleep, except at SE0, until SOF
// }
// DBG1(0x02, 0, 0); /* debug output: main loop iterates */
// wdt_reset();
//wdt_reset();
usbPoll();
// usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
// PINB |= _BV(PB1);
// if(usbInterruptIsReady())
// {
// // PINB |= _BV(PB1);
// // /* called after every poll of the interrupt endpoint */
// // read_Wheel();
// // // DBG1(0x03, 0, 0); /* debug output: interrupt report prepared */
// usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
// clearReport();
// }
}
return 0;