int main(void)
#endif
{
    BYTE RecvdByte;

    initCDC(); // setup the CDC state machine
    SetupBoard(); //setup the hardware, customize for your hardware
    usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); // initialize USB. TODO: Remove magic with macro
    usb_start(); //start the USB peripheral

// PIC18 INTERRUPTS
// It is the users resposibility to set up high, low or legacy mode
// interrupt operation. The following macros for high and low interrupt
// setup have been removed:

//#define EnableUsbHighPriInterrupt()             do { RCONbits.IPEN = 1; IPR2bits.USBIP = 1; INTCONbits.GIEH = 1;} while(0) // JTR new
//#define EnableUsbLowPriInterrupt()              do { RCONbits.IPEN = 1; IPR2bits.USBIP = 0; INTCONbits.GIEL = 1;} while(0)  // JTR new

// By default, the interrupt mode will be LEGACY (ISR Vector 0x08)
// (Same as high priority vector wise but the operation (latency) is
// not the same. Consult the data sheet for details.)

// If a priority mode is enabled then this affects ALL other interrupt
// sources therefore it does not belong to the usb stack to be
// doing this. It is a global, user application choice.

#if defined USB_INTERRUPTS // See the prj_usb_config.h file.
    EnableUsbPerifInterrupts(USB_TRN + USB_SOF + USB_UERR + USB_URST);
#if defined __18CXX //turn on interrupts for PIC18
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;
#endif
    EnableUsbGlobalInterrupt(); // Only enables global USB interrupt. Chip interrupts must be enabled by the user (PIC18)
#endif


// Wait for USB to connect
    do {
#ifndef USB_INTERRUPTS
        usb_handler();
#endif
    } while (usb_device_state < CONFIGURED_STATE);

    usb_register_sof_handler(CDCFlushOnTimeout); // Register our CDC timeout handler after device configured

// Main echo loop
    do {

// If USB_INTERRUPT is not defined each loop should have at least one additional call to the usb handler to allow for control transfers.
#ifndef USB_INTERRUPTS
        usb_handler();
#endif

// Receive and send method 1
// The CDC module will call usb_handler each time a BULK CDC packet is sent or received.
// If there is a byte ready will return with the number of bytes available and received byte in RecvdByte
        if (poll_getc_cdc(&RecvdByte)) 
            putc_cdc(RecvdByte+1); //

// Receive and send method 2
// Same as poll_getc_cdc except that byte is NOT removed from queue.
// This function will wait for a byte and return and remove it from the queue when it arrives.
        if (peek_getc_cdc(&RecvdByte)) { 
            RecvdByte = getc_cdc(); 
            putc_cdc(RecvdByte+1);
        }

// Receive and send method 3
// If there is a byte ready will return with the number of bytes available and received byte in RecvdByte
// use CDC_Flush_In_Now(); when it has to be sent immediately and not wait for a timeout condition.
        if (poll_getc_cdc(&RecvdByte)) { 
            putc_cdc(RecvdByte+1); //
            CDC_Flush_In_Now(); 
        }
    } while (1);

} //end main
Ejemplo n.º 2
0
void main(void) {
    static BYTE ledtrig;
    BYTE OutByte;

    SetupBoard(); //setup the hardware, USB
    SetupRC5();
    usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); // TODO: Remove magic with macro
    initCDC(); // JTR this function has been highly modified It no longer sets up CDC endpoints.
    usb_start();
    //usbbufflush(); //flush USB input buffer system

    ledtrig = 1; //only shut LED off once
    //	Never ending loop services each task in small increments
    while (1) {
        do {
           // if (!TestGlobalUsbInterruptEnabled()) //JTR3 added
                usb_handler(); ////service USB tasks Guaranteed one pass in polling mode even when usb_device_state == CONFIGURED_STATE
            if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){
                LedOff();
            } else if (usb_device_state < CONFIGURED_STATE) {
                LedOff();
            } else if ((ledtrig == 1) && (usb_device_state == CONFIGURED_STATE)) {
               // LedOn();
                ledtrig = 0;
            }
        } while(usb_device_state < CONFIGURED_STATE);

        //TRISB &= 0x7f;
        //TRISB |= 0x40;
        //LATB |= 0x40;

        //mode = IRWIDGET;
        //irWidgetservice();

#ifdef UARTONLY
        //mode = USB_UART;
        Usb2UartService(); // Never returns
#endif

        // Test for commands: 0, 1, 2 (Entry to SUMP MODE.)
        // Do not remove from input buffer, just take a PEEK.
        // SUMPLogicCommand will remove from input buffer
        // (Standardized coding)

        if (peek_getc_cdc(&OutByte)) {
            switch (OutByte) {
                case 0: //SUMP clear
                case 1: //SUMP run
                case 2: //SUMP ID
                    mode = IR_SUMP; //put IR Toy in IR_SUMP mode
                    irSUMPservice(); // Fully self contained, does not return until exited via 0x00 command.
                    cdc_In_len = 0;
                    mode = IR_DECODER;
                    SetupRC5();
                    break;

                case 'r': //IRMAN decoder mode
                case 'R':
                    OutByte = getc_cdc(); // now ok to remove byte from the USB buffer
                    SetupRC5();
                    mode = IR_DECODER;
                    putc_cdc('O');
                    putc_cdc('K');
                    CDC_Flush_In_Now();
                    break;
                case 'S': //IRs Sampling Mode
                case 's':
                    OutByte = getc_cdc(); // now ok to remove byte from the USB buffer
                    mode = IR_S;
                    irsService(); // Fully self contained, does not return until exited via 0x00 command.
                    cdc_In_len = 0;
                    mode = IR_DECODER;
                    SetupRC5();
                    break;
                case 'U':
                case 'u':
                    OutByte = getc_cdc(); // now ok to remove byte from the USB buffer
                    mode = USB_UART;
                    Usb2UartService();
                    break;
                case 'P':
                case 'p':// IR Widget mode
                    OutByte = getc_cdc(); // now ok to remove byte from the USB buffer
                    mode = IRWIDGET;
                    GetPulseFreq(); // Never returns
                    //GetPulseTime();
                    break;

                case 'T':
                case 't'://self test
                    OutByte = getc_cdc(); // now ok to remove byte from the USB buffer
                    SelfTest(); //run the self-test
                    break;
                case 'V':
                case 'v':// Acquire Version
                    OutByte = getc_cdc(); // now ok to remove byte from the USB buffer
                    GetUsbIrToyVersion();
                    break;
                case '$'://bootloader jump
                    OutByte = getc_cdc(); // now ok to remove byte from the USB buffer
                    BootloaderJump();
                    break;

                default:
                    OutByte = getc_cdc();

            }//switch(OutByte
        } // if peek OutByte == 1
        ProcessIR(); //increment IR decoder state machine
    }//end while(1)
}//end main