/* appMain is the application.  it is called every loop through main, as does the USB handler code.
 * do not block if you want USB to work.                                                           */
void appMainLoop(void)
{
    __xdata u8 processbuffer;
#ifdef TRANSMIT_TEST
    __xdata u8 testPacket[14];


    if (loopCnt++ == 90000)
    {
        /* Send a packet */
        testPacket[0] = 0x0D;
        testPacket[1] = xmitCnt++;
        testPacket[2] = 0x48;
        testPacket[3] = 0x41;
        testPacket[4] = 0x4C;
        testPacket[5] = 0x4C;
        testPacket[6] = 0x4F;
        //testPacket[6] = 0x43;
        //testPacket[7] = 0x43;
        testPacket[7] = lastCode[0];
        testPacket[8] = lastCode[1];
        testPacket[9] = 0x31;
        testPacket[10] = 0x31;
        testPacket[11] = 0x31;
        testPacket[12] = 0x31;

        transmit(testPacket, 13, 0, 0);
        //blink(400,400);
        REALLYFASTBLINK();
#ifndef VIRTUAL_COM
        debug("sent packet...");
#endif
        loopCnt = 0;
    }
#endif

    if (rfif)
    {
        lastCode[0] = 0xd;
        IEN2 &= ~IEN2_RFIE;

        if(rfif & RFIF_IRQ_DONE)
        {
            processbuffer = !rfRxCurrentBuffer;
            if(rfRxProcessed[processbuffer] == RX_UNPROCESSED)
            {
                if (PKTCTRL0&1)     // variable length packets have a leading "length" byte, let's skip it
                    txdata(APP_NIC, NIC_RECV, (u8)rfrxbuf[processbuffer][0], (u8*)&rfrxbuf[processbuffer][1]);
                else
                    txdata(APP_NIC, NIC_RECV, PKTLEN, (u8*)&rfrxbuf[processbuffer]);

                /* Set receive buffer to processed so it can be used again */
                rfRxProcessed[processbuffer] = RX_PROCESSED;
            }
        }

        rfif = 0;
        IEN2 |= IEN2_RFIE;
    }
}
/* appMain is the application.  it is called every loop through main, as does the USB handler code.
 * do not block if you want USB to work.                                                           */
void appMainLoop(void)
{
    xdata u8 processbuffer;
#ifdef TRANSMIT_TEST
    xdata u8 testPacket[14];


    if (loopCnt++ == 90000)
    {
        /* Send a packet */
        testPacket[0] = 0x0D;
        testPacket[1] = xmitCnt++;
        testPacket[2] = 0x48;
        testPacket[3] = 0x41;
        testPacket[4] = 0x4C;
        testPacket[5] = 0x4C;
        testPacket[6] = 0x4F;
        //testPacket[6] = 0x43;
        //testPacket[7] = 0x43;
        testPacket[7] = lastCode[0];
        testPacket[8] = lastCode[1];
        testPacket[9] = 0x31;
        testPacket[10] = 0x31;
        testPacket[11] = 0x31;
        testPacket[12] = 0x31;

        transmit(testPacket, 14);
        //blink(400,400);
        REALLYFASTBLINK();
        debug("sent packet...");
        loopCnt = 0;
    }
#endif

    if (rfif)
    {
        lastCode[0] = 0xd;
        IEN2 &= ~IEN2_RFIE;

        if(rfif & RFIF_IRQ_DONE)
        {
            processbuffer = !rfRxCurrentBuffer;
            if(rfRxProcessed[processbuffer] == RX_UNPROCESSED)
            {
                txdata(0xfe, 0xf0, (u8)rfrxbuf[processbuffer][0], (u8*)&rfrxbuf[processbuffer]);

                /* Set receive buffer to processed so it can be used again */
                rfRxProcessed[processbuffer] = RX_PROCESSED;
            }
        }

        rfif = 0;
        IEN2 |= IEN2_RFIE;
    }
}
/*************************************************************************************************
 * experimental!  don't know the full ramifications of using this function yet.  it could cause  *
 * the universe to explode!                                                                      *
 ************************************************************************************************/
void txdataold(u8 app, u8 cmd, u16 len, u8* dataptr)      // assumed EP5 for application use
    // gonna try this direct this time, and ignore all the "state tracking" for the endpoint.
    // wish me luck!  this could horribly crash and burn.
{
    u16 loop;
    u8 firsttime=1;
    USBINDEX=5;
    //EA=0; 
    while (len>0)
    {
        // if we do this in the loop, for some reason ep5iobuf.flags never clears between frames.  
        // don't know why since this bit is cleared in the USB ISR.
        loop = TXDATA_MAX_WAIT;
        while (ep5iobuf.flags & EP_INBUF_WRITTEN && loop>0)                   // has last msg been recvd?
        {
            //REALLYFASTBLINK();
            //REALLYFASTBLINK();
            REALLYFASTBLINK();
            //blink(100,50);
            lastCode[1] = 1;
            //loop--;
        }
        //blink(100,50);

        if (firsttime==1){                                             // first time through only please
            //blink(100,50);

            firsttime=0;
            USBF5 = 0x40;
            USBF5 = app;
            USBF5 = cmd;
            USBF5 = len & 0xff;
            USBF5 = len >> 8;
            if (len>EP5IN_MAX_PACKET_SIZE-5)
                loop=EP5IN_MAX_PACKET_SIZE-5;
            else
                loop=len;

        } else {
            if (len>EP5IN_MAX_PACKET_SIZE)