firmware/main.c

Go to the documentation of this file.
00001 
00010 #include <avr/io.h>
00011 #include <avr/interrupt.h>
00012 #include <avr/pgmspace.h>
00013 #include <util/delay.h>
00014 
00015 #include "usbdrv.h"
00016 #include "oddebug.h"
00017 #include "usbservo.h"
00018 
00020 static uint8_t angle;
00021 
00026 static void timerInterrupt(void) {
00027     int i;
00028     PORTC = 0xff;
00029     // wait 7200us (value by trial & error)
00030     for (i = 0; i < 72; i++) {
00031         _delay_us(100);
00032     }
00033     // still have to wait up to 17850us (value by trial & error)
00034     for (i = 0; i < angle; i++) {
00035         _delay_us(70);          // 17850 / 255 = 70
00036     }
00037     PORTC = 0x00;
00038 }
00039 
00046 uchar usbFunctionSetup(uchar data[8]) {
00047     static uchar replyBuffer[8];
00048     uchar replyLength;
00049 
00050     replyBuffer[0] = msgOK;
00051     switch (data[1]) {
00052     case CMD_ECHO:             /* echo */
00053         replyBuffer[0] = data[2];
00054         replyBuffer[1] = data[3];
00055         replyLength = 2;
00056         break;
00057     case CMD_GET:              /* read status */
00058         replyBuffer[0] = angle;
00059         replyLength = 1;
00060         break;
00061     case CMD_SET:              /* set status */
00062         angle = data[2];
00063         replyLength = 0;
00064         break;
00065     default:                   /* WTF? */
00066         replyBuffer[0] = msgErr;
00067         replyLength = 1;
00068         break;
00069     }
00070     usbMsgPtr = replyBuffer;
00071     return replyLength;
00072 }
00073 
00079 int main(void) {
00080     uchar i, j;
00081     odDebugInit();
00082     DDRD = ~0;                  /* output SE0 for USB reset */
00083     PORTD = 0x00;               /* no pullups on USB pins */
00084     DDRC = 0xff;                /* all outputs */
00085     PORTC = 0x00;
00086 
00087     j = 0;
00088     while (--j) {               /* USB Reset by device only required on
00089                                    Watchdog Reset */
00090         i = 0;
00091         while (--i);            /* delay >10ms for USB reset */
00092     }
00093     DDRD = ~USBMASK;            /* all outputs except USB data */
00094     TCCR0 = 5;                  /* set prescaler to 1/1024 */
00095     usbInit();
00096     sei();
00097 
00098     for (i = 0; i < 10; i++) {  /* wait one second to prevent strange
00099                                    effects when the USB-bus isn't
00100                                    initialized (e. g. when the host system
00101                                    is on standby. */
00102         _delay_ms(100);
00103     }
00104 
00105     angle = 0;
00106 
00107     while (1) {                 /* main event loop */
00108         usbPoll();
00109         if (TIFR & (1 << TOV0)) {
00110             TIFR |= 1 << TOV0;  /* clear pending flag */
00111             timerInterrupt();
00112         }
00113     }
00114     return 0;
00115 }

Generated on Sat Oct 28 14:48:23 2006 for USB-Servo by  doxygen 1.4.7