Ejemplo n.º 1
0
int main(void) {

    Initialize(); //setup bus pirate

    //wait for the USB connection to enumerate
#if defined (BUSPIRATEV4) && !defined (BPV4_DEBUG)
    BP_LEDUSB_DIR = 0;
    //BP_LEDUSB = 1;
    BP_USBLED_ON();
    //BP_VREGEN_DIR = 0;
   // BP_VREGEN = 1;
    //BP_LEDMODE_DIR = 0;
    //BP_LEDMODE = 1;
#ifdef USB_INTERRUPTS
    EnableUsbPerifInterrupts(USB_TRN + USB_SOF + USB_UERR + USB_URST);
    EnableUsbGlobalInterrupt();
#endif    

    do {
#ifndef USB_INTERRUPTS
//        if (!TestGlobalUsbInterruptEnable()) //JTR3 added
            usb_handler(); ////service USB tasks Guaranteed one pass in polling mode even when usb_device_state == CONFIGURED_STATE
#endif 
        //        if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){
        //        } else if (usb_device_state < CONFIGURED_STATE) {
        //        }
    } while (usb_device_state < CONFIGURED_STATE); // JTR addition. Do not proceed until device is configured.
    BP_USBLED_OFF();
    usb_register_sof_handler(CDCFlushOnTimeout); // For timeout value see: cdc_config.h -> BPv4 -> CDC_FLUSH_MS

#endif
    serviceuser();
    return 0;
}
Ejemplo n.º 2
0
int main(void) {
  firmware_signature = FIRMWARE_SIGNATURE;

  initialize_board();

#if defined(BUSPIRATEV4)
  BP_LEDUSB_DIR = OUTPUT;
  BP_USBLED_ON();

#ifdef USB_INTERRUPTS
  EnableUsbPerifInterrupts(USB_TRN | USB_SOF | USB_UERR | USB_URST);
  EnableUsbGlobalInterrupt();
#endif /* USB_INTERRUPTS */

  /* Wait until the USB interface is configured. */

  do {
#ifndef USB_INTERRUPTS
    usb_handler();
#endif /* !USB_INTERRUPTS */
  } while (usb_device_state < CONFIGURED_STATE);
  BP_USBLED_OFF();
  usb_register_sof_handler(CDCFlushOnTimeout);

#endif /* BUSPIRATEV4 */

  /* Starts processing user requests. */
  serviceuser();

  return 0;
}
Ejemplo n.º 3
0
void Usb2UartService(void) {
    BYTE tempout;

    Usb2UartSetup();

    do {
        usb_handler();
#ifdef USE_HNDSHK_POL_INV
        if (Hold_pol) {
            RTSpin = cls.RTS;
            DTRpin = cls.DTR;
        } else {
            RTSpin = ~cls.RTS;
            DTRpin = ~cls.DTR;
        }
#endif
        if (TxIf) {
            if (poll_getc_cdc(&tempout)) {
                TXREG = tempout;
            }
        }
        if (RCSTA & 0x06) // error handling
        {
            RCSTAbits.CREN = 0;
            RCSTAbits.CREN = 1;
        }
    } while (1);
}
void __attribute__((interrupt, auto_psv)) _USB1Interrupt() {
    //USB interrupt
    //IRQ enable IEC5bits.USB1IE
    //IRQ flag	IFS5bits.USB1IF
    //IRQ priority IPC21<10:8>
    usb_handler();
    ClearGlobalUsbInterruptFlag();
}
Ejemplo n.º 5
0
void __attribute__((interrupt, no_auto_psv)) _USB1Interrupt() {

  /* Handle USB operation. */
  usb_handler();

  /* Clear the USB Interrupt flag. */
  IFS5bits.USB1IF = OFF;
}
Ejemplo n.º 6
0
void usbbufservice(void) {

    if (usbbuf.cnt == 0) {//if the buffer is empty, get more data
        usbbuf.cnt = getsUSBUSART(usbbuf.inBuf, CDC_BUFFER_SIZE); //JTR2
        usb_handler();
        usbbuf.rdptr = 0;
    }
}
Ejemplo n.º 7
0
void __attribute__((interrupt, no_auto_psv)) _USB1Interrupt() {

    //USB interrupt
    //IRQ enable IEC5bits.USB1IE
    //IRQ flag	IFS5bits.USB1IF
    //IRQ priority IPC21<10:8>
    //{
    usb_handler();
    IFS5bits.USB1IF = 0; //	PIR2bits.USBIF = 0;
    //}
}
Ejemplo n.º 8
0
/**
 * @brief Services timer and sio handler
 *
 * This function calls sio & timer handling functions.
 */
void pal_task(void)
{
#if (TOTAL_NUMBER_OF_TIMERS > 0)
    timer_service();
#endif

/*
 * If the serial communication is done using USB, only then the 'usb_handler'
 * needs to be called. For UART, the data handling is done in the UART ISR.
 */
#ifdef USB0
    usb_handler();
#endif /* USB0 */
}
BYTE putda_cdc(BYTE count) {

    //    CDCFunctionError = 0;
    //    WaitInReady();
    while ((CDC_Inbdp->BDSTAT & UOWN));
    if (IsInBufferA) {
        CDC_Inbdp->BDADDR = cdc_In_bufferA;
        InPtr = cdc_In_bufferB;
    } else {
        CDC_Inbdp->BDADDR = cdc_In_bufferB;
        InPtr = cdc_In_bufferA;
    }
    CDC_Inbdp->BDCNT = count;
    CDC_Inbdp->BDSTAT = ((CDC_Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN;
    IsInBufferA ^= 0xFF;
#ifndef USB_INTERRUPTS
    usb_handler();
#endif
    return 0; //CDCFunctionError;
}
Ejemplo n.º 10
0
BYTE getda_cdc(void) {

    CDCFunctionError = 0;

    WaitOutReady();

    if ((IsOutBufferA & 1)) {
        OutPtr = &cdc_Out_bufferA[0];
        CDC_Outbdp->BDADDR = &cdc_Out_bufferB[0];
    } else {
        OutPtr = &cdc_Out_bufferB[0];
        CDC_Outbdp->BDADDR = &cdc_Out_bufferA[0];
    }
    IsOutBufferA ^= 0xFF;
    cdc_Out_len = CDC_Outbdp->BDCNT;
    CDC_Outbdp->BDCNT = CDC_BUFFER_SIZE;
    CDC_Outbdp->BDSTAT = ((CDC_Outbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN;
#ifndef USB_INTERRUPTS
    usb_handler();
#endif
    return cdc_Out_len;
}//end getCDC_Out_ArmNext
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.º 12
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
Ejemplo n.º 13
0
uint8_t sio2host_rx(uint8_t *data, uint8_t max_length)
{
    usb_handler();
    uint8_t data_received = 0;

    if (usb_rx_count == 0)
    {
        /* USB receive buffer is empty. */
        return 0;
    }

    if (USB_RX_BUF_SIZE <= usb_rx_count)
    {
        /*
         * Bytes between head and tail are overwritten by new data.
         * The oldest data in buffer is the one to which the tail is
         * pointing. So reading operation should start from the tail.
         */
        usb_rx_buf_head = usb_rx_buf_tail;

        /*
         * This is a buffer overflow case. Byt still only bytes equivalent to
         * full buffer size are useful.
         */
        usb_rx_count = USB_RX_BUF_SIZE;

        /* Bytes received is more than or equal to buffer. */
        if (USB_RX_BUF_SIZE <= max_length)
        {
            /*
             * Requested receive length (max_length) is more than the
             * max size of receive buffer, but at max the full
             * buffer can be read.
             */
            max_length = USB_RX_BUF_SIZE;
        }
    }
    else
    {
        /* Bytes received is less than receive buffer maximum length. */
        if (max_length > usb_rx_count)
        {
            /*
             * Requested receive length (max_length) is more than the data
             * present in receive buffer. Hence only the number of bytes
             * present in receive buffer are read.
             */
            max_length = usb_rx_count;
        }
    }

    data_received = max_length;

    while (max_length > 0)
    {
        /* Start to copy from head. */
        *data = usb_rx_buf[usb_rx_buf_head];

        if (++usb_rx_buf_head == USB_RX_BUF_SIZE)
        {
            usb_rx_buf_head = 0;
        }

        usb_rx_count--;
        data++;
        max_length--;
    }
    return data_received;
}
Ejemplo n.º 14
0
uint8_t sio2host_tx(uint8_t *data, uint8_t length)
{
uint8_t bytes_to_be_written;
    uint8_t size;
    uint8_t back;

    /*
     * USB Tx has been marked as busy, e.g. maybe the terminal is not open.
     * In order to continue the execution of the application even if
     * USB Tx data cannot be delivered, we are going to return here
     * immediately.
     * Once USB Tx is not busy anymore, regular USB Tx procedures will
     * be resumed.
     */
    if (usb_tx_busy)
    {
        return (0);
    }

    /*
     * Calculate available buffer space
     */
    if (usb_tx_buf_tail >= usb_tx_buf_head)
    {
        size = (USB_TX_BUF_SIZE - 1) - (usb_tx_buf_tail - usb_tx_buf_head);
    }
    else
    {
        size = (usb_tx_buf_head - 1) - usb_tx_buf_tail;
    }

    if (size < length)
    {
        /* Not enough buffer space available. Use the remaining size */
        bytes_to_be_written = size;
    }
    else
    {
        bytes_to_be_written = length;
    }

    /* Remember the number of bytes transmitted. */
    back = bytes_to_be_written;
    usb_tx_count += bytes_to_be_written;

    /* The data is copied to the transmit buffer. */
    while (bytes_to_be_written > 0)
    {
        usb_tx_buf[usb_tx_buf_tail] = *data;

        if ((USB_TX_BUF_SIZE - 1) == usb_tx_buf_tail)
        {
            /* Reached the end of buffer, revert back to beginning of buffer. */
            usb_tx_buf_tail = 0;
        }
        else
        {
            /*
             * Increment the index to point the next character to be
             * inserted.
             */
            usb_tx_buf_tail++;
        }

        bytes_to_be_written--;
        data++;
    }

    /*
     * If a transmission needs to be started, pal_usb_handler() takes
     * care about that once it is run.
     */
    usb_handler();
    return back;
    
}