Open source USB echo demo

From DP

Revision as of 12:53, 8 May 2012 by Arakis (Talk | contribs)
Jump to: navigation , search

Contents

Overview

This is short into/demo for using the open source USB stack with the PIC24F microcontroller found in the Bus Pirate v4. The code just configures the PIC, sets up the USB CDC class interface, and implements a simple echo function. Once connected to the terminal, the firmware will echo the character you type in increased by one. Code itself contains functions and extras needed for it to function with other PICs such as the PIC18F, but in this demo we'll be focusing only on the code related to the PIC24F

Basic hardware config

CONFIG words

As with each PIC there are CONFIG words that need to be setup to let the uC know in which stat it should operate. These words are located in the confingwords.h file which is included at the to of main.c via the #include "configwords.h" statement.

_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
_CONFIG2( IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRIPLL & PLLDIV_DIV3 & IOL1WAY_ON & PLL_96MHZ_ON & DISUVREG_OFF)

In code Hardware setup

While the CONFIG words are programed in, further hardware setup need to be done, form inside the main function. This is done by calling the SetupBoard() function. Bellow is the function as it relates to the Bus Pirate hardware.

    INTCON1bits.NSTDIS = 1;
    volatile unsigned long delay = 0xffff;
    CLKDIV = 0x0000;    // Set PLL prescaler (1:1)
    CORCONbits.PSV = 1; // PSV initialisation
    PSVPAG = 0;         //
    OSCCONbits.SOSCEN = 0;
    AD1PCFGL = 0x7FD8; //BUSPIRATEV4 has five analog pins b0, b1, b2, b5, b15
    AD1PCFGH = 0x2;
    TRISBbits.TRISB10 = 0; // Let there be LEDS
    TRISBbits.TRISB8 = 0;
    TRISBbits.TRISB9 = 0;
    while (delay--);

This function sets up some interrupt options, the processor clock, and the analog and digital pinouts. At the end it delays for approximately 1s.

prj_usb_config.h Hardware deffinitions

#elif defined (BUSPIRATEV4)
 
    #define CDC_BUFFER_SIZE 64u
 
    #define CLOCK_FREQ 32000000
    #define BAUDCLOCK_FREQ 16000000 //  required for baud rate calculations
    #define UART_BAUD_setup(x)  U1BRG = x
    #define CDC_FLUSH_MS 4 // how many ms timeout before cdc in to host is sent
 
    //RB10
    #define LED_LAT  LATB
    #define LED_TRIS TRISB
    #define LED_PIN  0b10000000000
 
    #define LedOn()  LED_TRIS &=(~LED_PIN)  //JTR TODO uncomment
    #define LedOff() LED_TRIS|=LED_PIN //JTR TODO uncomment
    #define LedToggle() LED_LAT ^=LED_PIN
 
    #define uLedOn() LATBbits.LATB10 = 0
    #define uLedOff() LATBbits.LATB10 = 1
    #define uLedToggle() LATBbits.LATB10 ^= LATBbits.LATB10
 
    #define mLedOn() LATBbits.LATB8 = 1
    #define mLedOff() LATBbits.LATB8 = 0
    #define mLedToggle() LATBbits.LATB8 ^= LATBbits.LATB8
 
    #define vLedOn() LATBbits.LATB9 = 1
    #define vLedOff() LATBbits.LATB9 = 0
    #define vLedToggle() LATBbits.LATB9 ^= LATBbits.LATB9
 
    #define USB_INTERRUPTS //use interrupts instead of polling
<source lang="C"> #define USB_INTERRUPTS //use interrupts instead of polling

Located in the prj_usb_config.h are renaming definitions which are used by various functions through out the code. This header file is included in the main.c through the usb_stack_globas.h, which itself is directly included in main.c. </source> By either commenting out or leaving the USB_INTERRUPTS statement above, you can chose weather you want to use USB by polling or through interrupts,

Basic USB config

USB setup

Polling

Interrupt

Bus Pirate v4

C30 Source Code

Resources