18F46J50 - USB HID LIBRARY

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
mdb80
Registered User
Registered User
Posts: 9
Joined: Tue Dec 21, 2010 11:40 am
Location: Milan

18F46J50 - USB HID LIBRARY

Post by mdb80 » Mon Jan 17, 2011 3:00 pm

I apologize for my cheap English :) .


This is the Device code of PIC18F46J50: i've used SystemConvert software, you can find here

Code: Select all

{
****************************************************************
*  Name    : 18F46J50                                          *
*  Author  : David John Barker                                 *
*  Notice  : Copyright (c) 2011 Mecanique                      *
*          : All Rights Reserved                               *
*  Date    : 10/01/2011                                        *
****************************************************************
}

module SystemTypes

// system header...
#const _core = $0012                   // processor core
#const _ram_banks = $0F                // 15 RAM bank(s) used
#variable _maxaccess = $60             // access ram is 96 bytes
#variable _maxram = $0E80              // 3712 bytes of user RAM
#variable _maxrom = $00FFF8            // 63 KB of program ROM
#const _eeprom = $0000                 // 0 bytes of EEPROM
#const _eeprom_start = $F00000         // EEPROM start address
#const _ports = $05                    // 5 available ports
#const _ccp = $00                      // CCP module NOT supported
#const _eccp = $02                     // 2 ECCP module(s) available
#const _mssp = $02                     // 2 MSSP module(s) available
#const _usart = $02                    // 2 USART(s) available
#const _adc = $0D                      // 13 ADC channels available
#const _adres = $0A                    // 10 bit ADC resolution
#const _comparator = $00               // comparator module NOT supported
#const _psp = $00                      // Parallel Slave Port (PSP) is NOT supported
#const _can = $00                      // onboard CAN is NOT supported
#const _usb = $01                      // onboard USB available
#const _ethernet = $00                 // onboard Ethernet is NOT supported
#const _flash_write = $01              // FLASH has write capability

// special function registers...
public system
   RPOR0 as byte absolute $0EC6,
   RPOR1 as byte absolute $0EC7,
   RPOR2 as byte absolute $0EC8,
   RPOR3 as byte absolute $0EC9,
   RPOR4 as byte absolute $0ECA,
   RPOR5 as byte absolute $0ECB,
   RPOR6 as byte absolute $0ECC,
   RPOR7 as byte absolute $0ECD,
   RPOR8 as byte absolute $0ECE,
   RPOR9 as byte absolute $0ECF,
   RPOR10 as byte absolute $0ED0,
   RPOR11 as byte absolute $0ED1,
   RPOR12 as byte absolute $0ED2,
   RPOR13 as byte absolute $0ED3,
   RPOR17 as byte absolute $0ED7,
   RPOR18 as byte absolute $0ED8,
   RPOR19 as byte absolute $0ED9,
   RPOR20 as byte absolute $0EDA,
   RPOR21 as byte absolute $0EDB,
   RPOR22 as byte absolute $0EDC,
   RPOR23 as byte absolute $0EDD,
   RPOR24 as byte absolute $0EDE,
   RPINR1 as byte absolute $0EE7,
   RPINR2 as byte absolute $0EE8,
   RPINR3 as byte absolute $0EE9,
   RPINR4 as byte absolute $0EEA,
   RPINR6 as byte absolute $0EEC,
   RPINR7 as byte absolute $0EED,
   RPINR8 as byte absolute $0EEE,
   RPINR12 as byte absolute $0EF2,
   RPINR13 as byte absolute $0EF3,
   RPINR16 as byte absolute $0EF6,
   RPINR17 as byte absolute $0EF7,
   RPINR21 as byte absolute $0EFB,
   RPINR22 as byte absolute $0EFC,
   RPINR23 as byte absolute $0EFD,
   RPINR24 as byte absolute $0EFE,
   PPSCON as byte absolute $0EFF,
   UEP0 as byte absolute $0F26,
   UEP1 as byte absolute $0F27,
   UEP2 as byte absolute $0F28,
   UEP3 as byte absolute $0F29,
   UEP4 as byte absolute $0F2A,
   UEP5 as byte absolute $0F2B,
   UEP6 as byte absolute $0F2C,
   UEP7 as byte absolute $0F2D,
   UEP8 as byte absolute $0F2E,
   UEP9 as byte absolute $0F2F,
   UEP10 as byte absolute $0F30,
   UEP11 as byte absolute $0F31,
   UEP12 as byte absolute $0F32,
   UEP13 as byte absolute $0F33,
   UEP14 as byte absolute $0F34,
   UEP15 as byte absolute $0F35,
   UIE as byte absolute $0F36,
   UEIE as byte absolute $0F37,
   UADDR as byte absolute $0F38,
   UCFG as byte absolute $0F39,
   PADCFG1 as byte absolute $0F3C,
   REFOCON as byte absolute $0F3D,
   RTCCAL as byte absolute $0F3E,
   RTCCFG as byte absolute $0F3F,
   ODCON3 as byte absolute $0F40,
   ODCON2 as byte absolute $0F41,
   ODCON1 as byte absolute $0F42,
   ANCON0 as byte absolute $0F48,
   ANCON1 as byte absolute $0F49,
   DSWAKEL as byte absolute $0F4A,
   DSWAKEH as byte absolute $0F4B,
   DSCONL as byte absolute $0F4C,
   DSCONH as byte absolute $0F4D,
   DSGPR0 as byte absolute $0F4E,
   DSGPR1 as byte absolute $0F4F,
   TCLKCON as byte absolute $0F52,
   CVRCON as byte absolute $0F53,
   PMSTATL as byte absolute $0F54,
   PMSTATH as byte absolute $0F55,
   PMEL as byte absolute $0F56,
   PMEH as byte absolute $0F57,
   PMDIN2L as byte absolute $0F58,
   PMDIN2H as byte absolute $0F59,
   PMDOUT2L as byte absolute $0F5A,
   PMDOUT2H as byte absolute $0F5B,
   PMMODEL as byte absolute $0F5C,
   PMMODEH as byte absolute $0F5D,
   PMCONL as byte absolute $0F5E,
   PMCONH as byte absolute $0F5F,
   UFRM as byte absolute $0F60,
   UFRML as byte absolute $0F60,
   UFRMH as byte absolute $0F61,
   UIR as byte absolute $0F62,
   UEIR as byte absolute $0F63,
   USTAT as byte absolute $0F64,
   UCON as byte absolute $0F65,
   DMABCH as byte absolute $0F66,
   DMABCL as byte absolute $0F67,
   RXADDRH as byte absolute $0F68,
   RXADDRL as byte absolute $0F69,
   TXADDRH as byte absolute $0F6A,
   TXADDRL as byte absolute $0F6B,
   PMDIN1L as byte absolute $0F6C,
   PMDIN1H as byte absolute $0F6D,
   PMADDRL as byte absolute $0F6E,
   PMDOUT1L as byte absolute $0F6E,
   PMADDRH as byte absolute $0F6F,
   PMDOUT1H as byte absolute $0F6F,
   CMSTAT as byte absolute $0F70,
   CMSTATUS as byte absolute $0F70,
   SSP2CON2 as byte absolute $0F71,
   SSP2CON1 as byte absolute $0F72,
   SSP2STAT as byte absolute $0F73,
   SSP2ADD as byte absolute $0F74,
   SSP2BUF as byte absolute $0F75,
   T4CON as byte absolute $0F76,
   PR4 as byte absolute $0F77,
   TMR4 as byte absolute $0F78,
   T3CON as byte absolute $0F79,
   TMR3L as byte absolute $0F7A,
   TMR3H as byte absolute $0F7B,
   BAUDCON2 as byte absolute $0F7C,
   SPBRGH2 as byte absolute $0F7D,
   BAUDCON as byte absolute $0F7E,
   BAUDCON1 as byte absolute $0F7E,
   BAUDCTL as byte absolute $0F7E,
   SPBRGH as byte absolute $0F7F,
   SPBRGH1 as byte absolute $0F7F,
   HLVDCON as byte absolute $0F85,
   DMACON2 as byte absolute $0F86,
   DMACON1 as byte absolute $0F88,
   LATA as byte absolute $0F89,
   LATB as byte absolute $0F8A,
   LATC as byte absolute $0F8B,
   LATD as byte absolute $0F8C,
   LATE as byte absolute $0F8D,
   ALRMVALL as byte absolute $0F8E,
   ALRMVALH as byte absolute $0F8F,
   ALRMRPT as byte absolute $0F90,
   ALRMCFG as byte absolute $0F91,
   TRISA as byte absolute $0F92,
   TRISB as byte absolute $0F93,
   TRISC as byte absolute $0F94,
   TRISD as byte absolute $0F95,
   TRISE as byte absolute $0F96,
   T3GCON as byte absolute $0F97,
   RTCVALL as byte absolute $0F98,
   RTCVALH as byte absolute $0F99,
   T1GCON as byte absolute $0F9A,
   OSCTUNE as byte absolute $0F9B,
   RCSTA2 as byte absolute $0F9C,
   PIE1 as byte absolute $0F9D,
   PIR1 as byte absolute $0F9E,
   IPR1 as byte absolute $0F9F,
   PIE2 as byte absolute $0FA0,
   PIR2 as byte absolute $0FA1,
   IPR2 as byte absolute $0FA2,
   PIE3 as byte absolute $0FA3,
   PIR3 as byte absolute $0FA4,
   IPR3 as byte absolute $0FA5,
   EECON1 as byte absolute $0FA6,
   EECON2 as byte absolute $0FA7,
   TXSTA2 as byte absolute $0FA8,
   TXREG2 as byte absolute $0FA9,
   RCREG2 as byte absolute $0FAA,
   SPBRG2 as byte absolute $0FAB,
   RCSTA as byte absolute $0FAC,
   RCSTA1 as byte absolute $0FAC,
   TXSTA as byte absolute $0FAD,
   TXSTA1 as byte absolute $0FAD,
   TXREG as byte absolute $0FAE,
   TXREG1 as byte absolute $0FAE,
   RCREG as byte absolute $0FAF,
   RCREG1 as byte absolute $0FAF,
   SPBRG as byte absolute $0FB0,
   SPBRG1 as byte absolute $0FB0,
   CTMUICON as byte absolute $0FB1,
   CTMUCONL as byte absolute $0FB2,
   CTMUCONH as byte absolute $0FB3,
   CCP2CON as byte absolute $0FB4,
   ECCP2CON as byte absolute $0FB4,
   CCPR2 as byte absolute $0FB5,
   CCPR2L as byte absolute $0FB5,
   CCPR2H as byte absolute $0FB6,
   ECCP2DEL as byte absolute $0FB7,
   PWM2CON as byte absolute $0FB7,
   ECCP2AS as byte absolute $0FB8,
   PSTR2CON as byte absolute $0FB9,
   CCP1CON as byte absolute $0FBA,
   ECCP1CON as byte absolute $0FBA,
   CCPR1 as byte absolute $0FBB,
   CCPR1L as byte absolute $0FBB,
   CCPR1H as byte absolute $0FBC,
   ECCP1DEL as byte absolute $0FBD,
   PWM1CON as byte absolute $0FBD,
   ECCP1AS as byte absolute $0FBE,
   PSTR1CON as byte absolute $0FBF,
   WDTCON as byte absolute $0FC0,
   ADCON1 as byte absolute $0FC1,
   ADCON0 as byte absolute $0FC2,
   ADRES as byte absolute $0FC3,
   ADRESL as byte absolute $0FC3,
   ADRESH as byte absolute $0FC4,
   SSP1CON2 as byte absolute $0FC5,
   SSPCON2 as byte absolute $0FC5,
   SSP1CON1 as byte absolute $0FC6,
   SSPCON1 as byte absolute $0FC6,
   SSP1STAT as byte absolute $0FC7,
   SSPSTAT as byte absolute $0FC7,
   SSP1ADD as byte absolute $0FC8,
   SSPADD as byte absolute $0FC8,
   SSP1BUF as byte absolute $0FC9,
   SSPBUF as byte absolute $0FC9,
   T2CON as byte absolute $0FCA,
   PR2 as byte absolute $0FCB,
   TMR2 as byte absolute $0FCC,
   T1CON as byte absolute $0FCD,
   TMR1L as byte absolute $0FCE,
   TMR1H as byte absolute $0FCF,
   RCON as byte absolute $0FD0,
   CM2CON as byte absolute $0FD1,
   CM2CON1 as byte absolute $0FD1,
   CM1CON as byte absolute $0FD2,
   CM1CON1 as byte absolute $0FD2,
   OSCCON as byte absolute $0FD3,
   T0CON as byte absolute $0FD5,
   TMR0L as byte absolute $0FD6,
   TMR0H as byte absolute $0FD7,
   STATUS as byte absolute $0FD8,
   FSR2L as byte absolute $0FD9,
   FSR2H as byte absolute $0FDA,
   PLUSW2 as byte absolute $0FDB,
   PREINC2 as byte absolute $0FDC,
   POSTDEC2 as byte absolute $0FDD,
   POSTINC2 as byte absolute $0FDE,
   INDF2 as byte absolute $0FDF,
   BSR as byte absolute $0FE0,
   FSR1L as byte absolute $0FE1,
   FSR1H as byte absolute $0FE2,
   PLUSW1 as byte absolute $0FE3,
   PREINC1 as byte absolute $0FE4,
   POSTDEC1 as byte absolute $0FE5,
   POSTINC1 as byte absolute $0FE6,
   INDF1 as byte absolute $0FE7,
   WREG as byte absolute $0FE8,
   FSR0L as byte absolute $0FE9,
   FSR0H as byte absolute $0FEA,
   PLUSW0 as byte absolute $0FEB,
   PREINC0 as byte absolute $0FEC,
   POSTDEC0 as byte absolute $0FED,
   POSTINC0 as byte absolute $0FEE,
   INDF0 as byte absolute $0FEF,
   INTCON3 as byte absolute $0FF0,
   INTCON2 as byte absolute $0FF1,
   INTCON as byte absolute $0FF2,
   PROD as byte absolute $0FF3,
   PRODL as byte absolute $0FF3,
   PRODH as byte absolute $0FF4,
   TABLAT as byte absolute $0FF5,
   TBLPTR as byte absolute $0FF6,
   TBLPTRL as byte absolute $0FF6,
   TBLPTRH as byte absolute $0FF7,
   TBLPTRU as byte absolute $0FF8,
   PC as byte absolute $0FF9,
   PCL as byte absolute $0FF9,
   PCLATH as byte absolute $0FFA,
   PCLATU as byte absolute $0FFB,
   STKPTR as byte absolute $0FFC,
   TOS as byte absolute $0FFD,
   TOSL as byte absolute $0FFD,
   TOSH as byte absolute $0FFE,
   TOSU as byte absolute $0FFF

// system ports...
public system port
   PORTE as byte absolute $0F84,
   PORTD as byte absolute $0F83,
   PORTC as byte absolute $0F82,
   PORTB as byte absolute $0F81,
   PORTA as byte absolute $0F80

// alias...
public dim
   FSR0 as FSR0L.AsWord,
   FSR1 as FSR1L.AsWord,
   FSR2 as FSR2L.AsWord,
   TABLEPTR as TBLPTRL.AsWord

// configuration fuses...
public config
   WDTEN(WDTEN) = [OFF, ON],
   PLLDIV(PLLDIV) = [12, 10, 6, 5, 4, 3, 2, 1],
   STVREN(STVREN) = [OFF, ON],
   XINST(XINST) = [OFF, ON],
   CPUDIV(CPUDIV) = [OSC4_PLL6, OSC3_PLL3, OSC2_PLL2, OSC1],
   CP0(CP0) = [ON, OFF],
   OSC(OSC) = [INTOSC, INTOSCO, INTOSCPLL, INTOSCPLLO, HS, HSPLL, EC, ECPLL],
   T1DIG(T1DIG) = [OFF, ON],
   LPT1OSC(LPT1OSC) = [ON, OFF],
   FCMEN(FCMEN) = [OFF, ON],
   IESO(IESO) = [OFF, ON],
   WDTPS(WDTPS) = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768],
   DSWDTOSC(DSWDTOSC) = [T1OSCREF, INTOSCREF],
   RTCOSC(RTCOSC) = [INTOSCREF, T1OSCREF],
   DSBOREN(DSBOREN) = [OFF, ON],
   DSWDTEN(DSWDTEN) = [OFF, ON],
   DSWDTPS(DSWDTPS) = [2, 8, 32, 128, 512, 2048, 8192, K32, K131, K524, M2, M8, M33, M134, M536, G2],
   IOL1WAY(IOL1WAY) = [OFF, ON],
   MSSP7B_EN(MSSP7B_EN) = [MSK5, MSK7],
   WPFP(WPFP) = [PAGE_0, PAGE_1, PAGE_2, PAGE_3, PAGE_4, PAGE_5, PAGE_6, PAGE_7, PAGE_8, PAGE_9, PAGE_10, PAGE_11, PAGE_12, PAGE_13, PAGE_14, PAGE_15, PAGE_16, PAGE_17, PAGE_18, PAGE_19, PAGE_20, PAGE_21, PAGE_22, PAGE_23, PAGE_24, PAGE_25, PAGE_26, PAGE_27, PAGE_28, PAGE_29, PAGE_30, PAGE_31, PAGE_32, PAGE_33, PAGE_34, PAGE_35, PAGE_36, PAGE_37, PAGE_38, PAGE_39, PAGE_40, PAGE_41, PAGE_42, PAGE_43, PAGE_44, PAGE_45, PAGE_46, PAGE_47, PAGE_48, PAGE_49, PAGE_50, PAGE_51, PAGE_52, PAGE_53, PAGE_54, PAGE_55, PAGE_56, PAGE_57, PAGE_58, PAGE_59, PAGE_60, PAGE_61, PAGE_62, PAGE_63],
   WRITE(WRITE) = [0],
   WPEND(WPEND) = [PAGE_0, PAGE_WPFP],
   WPCFG(WPCFG) = [ON, OFF],
   WPDIS(WPDIS) = [ON, OFF]

// default fuses...
config
   WDTEN = OFF,
   STVREN = ON,
   XINST = OFF,
   OSC = HS,
   FCMEN = OFF,
   IESO = OFF,
   WDTPS = 128
MDB

mdb80
Registered User
Registered User
Posts: 9
Joined: Tue Dec 21, 2010 11:40 am
Location: Milan

18F46J50 - USB HID LIBRARY

Post by mdb80 » Mon Jan 17, 2011 3:02 pm

This is main code:

Code: Select all

{
*****************************************************************************
*  Name    : ELWA4.bas                                                      *
*  Author  : Massimo Dalla Bona                                             *
*  Notice  : Copyright (c) 2010 ELETTROTERM SRL                             *
*          : All Rights Reserved                                            *
*  Date    : 15/12/2010                                                     *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}


Program ELWA4

Device = 18F46J50
Clock = 48
Config OSC = HSPLL
Config PLLDIV = 3 'PLL division
 

#option USB_DESCRIPTOR = "HIDDescriptor.bas"     // name of descriptor file 

#option org_reset = $1000 'RESET VECTOR FOR MCP USB HIDBOOTLOADER

Include "leds.inc"
Include "usbhid.bas"
Include "string.bas"

// TX report...
Structure TTXReport
   Message As String
End Structure

// RX report...
Structure TRXReport
   Message As String
End Structure  

  
Dim TXReport As TTXReport Absolute TXReportRAM  
Dim RXReport As TRXReport Absolute RXReportRAM



Dim ING1     As  PORTB.1
Dim ING2     As  PORTB.4
Dim NTC1     As  PORTA.3
Dim NTC2     As  PORTB.1
Dim IN_BATT  As  PORTC.1
Dim USB_INT  As  PIR2.4 

'TRIS I/O SETTINGS
TRISA= %11001111  
TRISB= %10010111
TRISC= %10110011    
TRISD= %10111011
TRISE= %00000000

'PERIPHERAL PIN SELECT
'PPSCON   = 0  ' IOLOCK DISABLE 'disabilito il blocco dei PIN RP
'RPINR1   = 4  ' INT1 SU RP4  = RB1 - INGRESSO1
'RPINR2   = 5  ' INT2 SU RP5  = RB2 - PULSANTE
'RPINR3   = 12 ' INT3 SU RP12 = RC1 - IN BATTERIA
'RPINR16  = 10 ' HSERIN2 SU RP10 = RB7 - 2° SERIALE
'RPOR9    = 5  ' HSEROUT2 SU RP9 = RB6 - 2° SERIALE
'PPSCON   = 1  ' IOLOCK ENABLE 'riabilito il blocco dei PIN RP

    
Repeat
    LEDS.Off()
    DelayMS(100)
    LEDS.AllRed
    DelayMS(100)
Until HID.Attached


// main program loop...
While true
    LEDS.Off()
    DelayMS(100)
    LEDS.AllGreen()
    
    If DataAvailable Then 
        ReadReport
        If RXReport.Message="reset" Then 
            TXReport.Message= "bye"
            WriteReport
            DelayMS(100)
            Asm 
                RESET
            End Asm
        Else
            TXReport.Message= "error"
            WriteReport 
        EndIf
    EndIf
Wend
 
End
MDB

mdb80
Registered User
Registered User
Posts: 9
Joined: Tue Dec 21, 2010 11:40 am
Location: Milan

18F46J50 - USB HID LIBRARY

Post by mdb80 » Mon Jan 17, 2011 3:05 pm

this is USB HID LIBRARY:

Code: Select all

{
*****************************************************************************
*  Name    : USBHID.bas                                                     *
*  Author  : David John Barker                                              *
*  Notice  : Copyright (c) 2007 Mecanique                                   *
*          : All Rights Reserved                                            *
*  Date    : 17/01/2010                                                     *
*          : 1.2  Added PIC18F46J50 (by Massimo Dalla Bona)                 *
*  Version : 1.1 ISR now uses USB interrupts                                *
*          : Renamed 'Connected' to 'Attached'                              *
*          : 1.1 Release                                                    *
*  Notes   : This HID module provides a simple way for you to transfer      *
*          : blocks of data to and from your PC using the USB HID class.    *
*****************************************************************************
}
Module HID

// import USB modules...
#option USB_USE_HID = true
#option USB_SERVICE = true
Include USB_DESCRIPTOR
Include "usbsfr.bas"
Include "usbdefs.bas"
Include "usbsystem.bas"
Include "system.bas"

// interrupt driven service fired every 1 millisecond...
Const 
   PriorityLevel = USB_SERVICE_PRIORITY // ISR priority

// Public buffers - these are just mapped onto USB RAM space, thus saving
// program RAM. You can use a large 256 byte buffer, which can be useful
// for trasnferring large blocks of data using ReadArray() and WriteArray().
// The RX and TX buffers match the HID report size, and are typically use with
// ReaReport() and WriteReport(). However, they can also be used with ReadArray()
// and WriteArray(). Note that these buffers are only available for USB devices 
// that have extended dual port USB RAM...
#if USB_EXTENDED_RAM
Public Const
   #if _device = 18F46J50 Then
        BufferRAM = $500,
   #else
        BufferRAM = $600,
   #endif
   TXReportRAM = BufferRAM,
   RXReportRAM = BufferRAM + HIDReportBytesIn
Public Dim   
   Buffer(256) As Byte Absolute BufferRAM,
   TXReport(HIDReportBytesIn) As Byte Absolute TXReportRAM,
   RXReport(HIDReportBytesOut) As Byte Absolute RXReportRAM
#endif

#if USB_EXTENDED_RAM_K50
Public Const
   BufferRAM = $280,
   TXReportRAM = BufferRAM,
   RXReportRAM = BufferRAM + HIDReportBytesIn
Public Dim   
   Buffer(128) As Byte Absolute BufferRAM,
   TXReport(HIDReportBytesIn) As Byte Absolute TXReportRAM,
   RXReport(HIDReportBytesOut) As Byte Absolute RXReportRAM
#endif

Public Dim Attached As USBSystem.Attached
{
****************************************************************************
* Name    : Service                                                        *
* Purpose : Service a USB connection. You need to call this routine        *
*         : periodically from your main program to maintain a connection   *
*         : (every 1 ms or so). You DO NOT need to call this routine if    *
*         : you have enabled the OnService() interrupt handler (it is on   *
*         : by default)                                                    *
****************************************************************************
}
Public Inline Sub Service()
   USBCheckBusStatus
   USBDriverService
   ClrWDT
End Sub
{
****************************************************************************
* Name    : NoISRService                                                   *
* Purpose : Private service helper routine for maintain USB connection     *
*         : for some blocking calls                                        *
****************************************************************************
}
Inline Sub NoISRService()
   #if Not USB_SERVICE
   Service
   #endif
End Sub
{
****************************************************************************
* Name    : OnService (PRIVATE)                                            *
* Purpose : USB service event handler                                      *
****************************************************************************
}
#if USB_SERVICE
Interrupt OnService(PriorityLevel)
   PIR2Bits.USBIF = 0
   Save(FSR0, FSR1, Service)
      Service
   Restore 
End Interrupt
#endif
{
****************************************************************************
* Name    : EnableISR                                                      *
* Purpose : Enable the ISR to service USB connection                       *
****************************************************************************
}
Public Inline Sub EnableISR()
   #if USB_SERVICE
   Enable(OnService)
   #endif
End Sub
{
****************************************************************************
* Name    : DisableISR                                                     *
* Purpose : Disable the ISR that services the USB connection               *
****************************************************************************
}
Public Inline Sub DisableISR()
   #if USB_SERVICE
   Disable(OnService)
   #endif
End Sub
{
****************************************************************************
* Name    : WaitForTX (PRIVATE)                                            *
* Purpose : Blocks processing until USB TX buffer is available             *
****************************************************************************
}
Sub WaitForTX()
   While mHIDTxIsBusy <> 0 And Attached
      Service
   Wend 
End Sub
{
****************************************************************************
* Name    : DataAvailable                                                  *
* Purpose : Returns true if RX data is available, false otherwise          *
****************************************************************************
}
Public Function DataAvailable() As Boolean
   DisableISR
   Result = (mHIDRxIsBusy = 0)
   EnableISR
End Function
{
****************************************************************************
* Name    : ReadReport                                                     *
* Purpose : Read a HID report into RXReport buffer                         *
****************************************************************************
}
#if USB_EXTENDED_RAM Or USB_EXTENDED_RAM_K50
Public Sub ReadReport()

   // djb 06-09-2010
   While mHIDRxIsBusy <> 0 And Attached
   Wend
   HIDRxReport(@RXReport, HIDReportBytesOut)

'   DisableISR
'   While mHIDRxIsBusy <> 0 And Attached
'      Service
'   Wend
'   HIDRxReport(@RXReport, HIDReportBytesOut)
'   NoISRService
'   EnableISR
End Sub
{
****************************************************************************
* Name    : WriteReport                                                    *
* Purpose : Write the TXReport buffer                                      *
****************************************************************************
}
Public Sub WriteReport()
   // djb 06-09-2010
   While mHIDTxIsBusy <> 0 And Attached
   Wend 
   HIDTxReport(@TXReport, HIDReportBytesIn) 
   
'   DisableISR
'   WaitForTX
'   HIDTxReport(@TXReport, HIDReportBytesIn) 
'   NoISRService
'   EnableISR
End Sub
#endif
{
****************************************************************************
* Name    : ReadArray                                                      *
* Purpose : Read an array of bytes - pCount is an optional number of bytes *
*         : to read. If pCount is not given, the size of the array is used *
****************************************************************************
}
Public Sub ReadArray(ByRef pBuffer() As Byte, pCount As Word = 0)
   Dim BytesRead As Byte
   Dim Buffer As Word
   
   Buffer = @pBuffer
   If pCount = 0 Then
      pCount = Bound(pBuffer) + 1
   EndIf  

   DisableISR
   While pCount > 0 And Attached
   
      // wait for data
      If mHIDRxIsBusy = 0 Then
         
         // pCount > max report size
         If pCount > HIDReportBytesOut Then
            BytesRead = HIDRxReport(Buffer, HIDReportBytesOut)
            Dec(pCount, BytesRead)
            Inc(Buffer, BytesRead)
         
         // pCount <= max report size   
         Else
            BytesRead = HIDRxReport(Buffer, pCount.Byte0)
            Dec(pCount.Byte0, BytesRead)
            Inc(Buffer, BytesRead)
         EndIf
      EndIf   
      Service
   Wend
   EnableISR
End Sub
{
****************************************************************************
* Name    : WriteArray                                                     *
* Purpose : Write an array of bytes - pCount is an optional number of      *
*         : to write. If pCount is not given, the array size is used       *
*         : Bytes are transfered in HIDReportBytesIn sized packets - 8, 16 *
*         : 32 or 64 bytes. If you pass an array which is only dimensioned *
*         : 4 bytes (for example, DIM Array(4) AS BYTE  then a             *
*         : 'HIDReportBytesIn' sized data packet is still transferred.     *
*         : This isn't really a problem, as the host application should    *
*         : know what to do with the data anyway.                          *
****************************************************************************
}
Public Sub WriteArray(ByRef pBuffer() As Byte, pLength As Word = 0)
   Dim Buffer As Word

   Buffer = @pBuffer
   If pLength = 0 Then
      pLength = Bound(pBuffer) + 1
   EndIf   
   
   DisableISR
   While (pLength > HIDReportBytesIn) And Attached
      WaitForTX
      HIDTxReport(Buffer, HIDReportBytesIn) 
      Dec(pLength, HIDReportBytesIn)
      Inc(Buffer, HIDReportBytesIn)
      Service
   Wend
   If pLength.Byte0 > 0 Then
      WaitForTX
      HIDTxReport(Buffer, HIDReportBytesIn)
      Service
   EndIf
   EnableISR 
End Sub
{
****************************************************************************
* Name    : Initialize                                                     *
* Purpose : Initialize the USB module                                      *
****************************************************************************
}
Sub Initialize()

	#if _device = 18F46J50
    	//On the PIC18F46J50 Family of USB microcontrollers, the PLL will not power up and be enabled
		//by default, even if a PLL enabled oscillator configuration is selected (such as HS+PLL).
		//This allows the device to power up at a lower initial operating frequency, which can be
		//advantageous when powered from a source which is not gauranteed to be adequate for 48MHz
		//operation.  On these devices, user firmware needs to manually set the OSCTUNE<PLLEN> bit to
		//power up the PLL.  
		//Enable the PLL and wait 2+ms until the PLL locks before enabling USB module
		OSCTUNE.6 = 1
		DelayMS(2)
  	#endif

   mInitializeUSBDriver()
   #if USB_SERVICE
   #if USB_SERVICE_PRIORITY = ipLow
   IPR2.4 = 0 'usb interrupt priority
   #endif        
   PIR2Bits.USBIF = 0 // clear USBIF
   PIE2Bits.USBIE = 1 // enable USB Int
  
   Service
   UIE = $7F   // Enable all interrupts
   UEIE = $9F  // Unmask all USB error interrupts  
   
   INTCON.6=1  // Enable GIE
   INTCON.7=1  // Enable PEIE
   
   Enable(OnService)     
   #endif
End Sub

// initialize the module...
Initialize

Last edited by mdb80 on Mon Jan 17, 2011 4:19 pm, edited 1 time in total.
MDB

mdb80
Registered User
Registered User
Posts: 9
Joined: Tue Dec 21, 2010 11:40 am
Location: Milan

Post by mdb80 » Mon Jan 17, 2011 3:15 pm

This is USBSFR:

Code: Select all

{
*****************************************************************************
*  Name    : USBSFR.bas                                                     *
*  Author  : David John Barker - rev.                                       *
*  Notice  : Copyright (c) 2007 Mecanique                                   *
*          : All Rights Reserved                                            *
*  Date    : 06/01/2007                                                     *
*  Version : 1.1                                                            *
*                                                                           *
*  History : 1.0 Release 06/01/2007                                         *
*            1.1 Add 18F46J50 by Massimo Dalla Bona                         *
*                                                                           *
*  Notes   : This module implements SFRs as structures, which allows you to *
*          : refer to bits using their respective datasheet names. For      *
*          : example, you can use something like UIRBits.STALLIF = 0. Note  *
*          : that if you want to access as a complete byte, you should use  *
*          : UIRBits._byte = $00 - this is because the compiler sees the    *
*          : variables as structures and not bytes                          *
*****************************************************************************
}
Module USBSFR

// UFRML
Structure TUFRML
  _byte As Byte
  FRM0 As _byte.0
  FRM1 As _byte.1
  FRM2 As _byte.2
  FRM3 As _byte.3
  FRM4 As _byte.4
  FRM5 As _byte.5
  FRM6 As _byte.6
  FRM7 As _byte.7
End Structure

// UFRMH
Structure TUFRMH
  _byte As Byte
  FRM8 As _byte.0
  FRM9 As _byte.1
  FRM10 As _byte.2
End Structure

// UIR
Structure TUIR
  _byte As Byte
  URSTIF As _byte.0
  UERRIF As _byte.1
  ACTVIF As _byte.2
  TRNIF As _byte.3
  IDLEIF As _byte.4
  STALLIF As _byte.5
  SOFIF As _byte.6
End Structure

// UIE
Structure TUIE
  _byte As Byte
  URSTIE As _byte.0
  UERRIE As _byte.1
  ACTVIE As _byte.2
  TRNIE As _byte.3
  IDLEIE As _byte.4
  STALLIE As _byte.5
  SOFIE As _byte.6
End Structure

// UEIR
Structure TUEIR
  _byte As Byte
  PIDEF As _byte.0
  CRC5EF As _byte.1
  CRC16EF As _byte.2
  DFN8EF As _byte.3
  BTOEF As _byte.4
  BTSEF As _byte.7
End Structure

// UEIE
Structure TUEIE
  _byte As Byte
  PIDEE As _byte.0
  CRC5EE As _byte.1
  CRC16EE As _byte.2
  DFN8EE As _byte.3
  BTOEE As _byte.4
  BTSEE As _byte.7
End Structure

// USTAT
Structure TUSTAT
  _byte As Byte
  PPBI As _byte.1
  DIR As _byte.2
  ENDP0 As _byte.3
  ENDP1 As _byte.4
  ENDP2 As _byte.5
  ENDP3 As _byte.6
End Structure

// UCON
Structure TUCON
  _byte As Byte
  SUSPND As _byte.1
  RESUME As _byte.2
  USBEN As _byte.3
  PKTDIS As _byte.4
  SE0 As _byte.5
  PPBRST As _byte.6
End Structure

// UADDR
Structure TUADDR
  _byte As Byte
  ADDR0 As _byte.0
  ADDR1 As _byte.1
  ADDR2 As _byte.2
  ADDR3 As _byte.3
  ADDR4 As _byte.4
  ADDR5 As _byte.5
  ADDR6 As _byte.6
End Structure

// UCFG
Structure TUCFG
  _byte As Byte
  PPB0 As _byte.0
  PPB1 As _byte.1
  FSEN As _byte.2
  UTRDIS As _byte.3
  UPUEN As _byte.4
  UOEMON As _byte.6
  UTEYE As _byte.7
End Structure

// UEP
Structure TUEP
  _byte As Byte
  EPSTALL As _byte.0
  EPINEN As _byte.1
  EPOUTEN As _byte.2
  EPCONDIS As _byte.3
  EPHSHK As _byte.4
End Structure

#if _device in (18F46J50) then
// PIR2
Structure TPIR2
  _byte As Byte
  OSCFIF As _byte.7
  CM2IF  As _byte.6
  CM1IF  As _byte.5
  USBIF  As _byte.4
  BCLIF  As _byte.3
  HLVDIF As _byte.2
  TMR3IF As _byte.1
  CCP2IF As _byte.0
End Structure

// INTCON
Structure TINTCON
  _byte As Byte
  RBIF   As _byte.0
  INT0IF As _byte.1
  TMR0IF As _byte.2
  RBIE   As _byte.3
  INT0IE As _byte.4
  TMR0IE As _byte.5
  PEIE   As _byte.6
  GIE    As _byte.7
  
  GIEL   As _byte.6
  GIEH   As _byte.7
End Structure

// PIE2
Structure TPIE2
  _byte As Byte
  CCP2IE  As _byte.0
  TMR3IE  As _byte.1
  HLVDIE  As _byte.2
  BCL1IE  As _byte.3
  USBIE   As _byte.4  
  CM1IE   As _byte.5
  CM2IE   As _byte.6
  OSCFIE  As _byte.7
End Structure

#else
// PIR2
Structure TPIR2
  _byte As Byte
  CCP2IF As _byte.0
  TMR3IF As _byte.1
  LVDIF As _byte.2
  BCLIF As _byte.3
  EEIF As _byte.4
  USBIF As _byte.5
  CMIF As _byte.6
  OSCFIF As _byte.7
  HLVDIF As _byte.2
End Structure

// INTCON
Structure TINTCON
  _byte As Byte
  RBIF As _byte.0
  INT0IF As _byte.1
  TMR0IF As _byte.2
  RBIE As _byte.3
  INT0IE As _byte.4
  TMR0IE As _byte.5
  PEIE As _byte.6
  GIE As _byte.7
  
  INT0F As _byte.1
  T0IF As _byte.2
  INT0E As _byte.4
  T0IE As _byte.5
  GIEL As _byte.6
  GIEH As _byte.7
End Structure

// PIE2
Structure TPIE2
  _byte As Byte
  CCP2IE As _byte.0
  TMR3IE As _byte.1
  LVDIE As _byte.2
  BCLIE As _byte.3
  EEIE As _byte.4
  USBIE As _byte.5
  CMIE As _byte.6
  OSCFIE As _byte.7
  HLVDIE As _byte.2
End Structure
#endif


// public aliases...                    
#if _device in (18F13K50, 18F14K50)
  Public Dim
   UEP0Bits As TUEP Absolute $0F53,
   UEP1Bits As TUEP Absolute $0F54,
   UEP2Bits As TUEP Absolute $0F55,
   UEP3Bits As TUEP Absolute $0F56,
   UEP4Bits As TUEP Absolute $0F57,
   UEP5Bits As TUEP Absolute $0F58,
   UEP6Bits As TUEP Absolute $0F59,
   UEP7Bits As TUEP Absolute $0F5A,
   UEIEBits As TUEIE Absolute $0F5B,
   UADDRBits As TUADDR Absolute $0F5C,
   UFRMLBits As TUFRML Absolute $0F5D,
   UFRMHBits As TUFRMH Absolute $0F5E,
   UEIRBits As TUEIR Absolute $0F5F,
   UIEBits As TUIE Absolute $0F60,
   UCFGBits As TUCFG Absolute $0F61,
   UIRBits As TUIR Absolute $0F62,
   USTATBits As TUSTAT Absolute $0F63,
   UCONBits As TUCON Absolute $0F64,
   PIE2Bits As TPIE2 Absolute $0FA0,
   PIR2Bits As TPIR2 Absolute $0FA1,
   INTCONBits As TINTCON Absolute $0FF2
   
#elseif _device in (18F46J50)
Public Dim
   //ACCESS BANK SPECIAL FUNCTION REGISTER MAP
   UFRMLBits As TUFRML Absolute $0F60,
   UFRMHBits As TUFRMH Absolute $0F61,
   UIRBits As TUIR Absolute $0F62,
   UEIRBits As TUEIR Absolute $0F63,
   USTATBits As TUSTAT Absolute $0F64,
   UCONBits As TUCON Absolute $0F65,
   //ACCESS BANK SPECIAL FUNCTION REGISTER MAP
   PIE2Bits As TPIE2 Absolute $0FA0,
   PIR2Bits As TPIR2 Absolute $0FA1,
   INTCONBits As TINTCON Absolute $0FF2, 
   //NON-ACCESS BANK SPECIAL FUNCTION REGISTER MAP
   UEP0Bits As TUEP Absolute $0F26,
   UEP1Bits As TUEP Absolute $0F27,
   UEP2Bits As TUEP Absolute $0F28,
   UEP3Bits As TUEP Absolute $0F29,
   UEP4Bits As TUEP Absolute $0F2A,
   UEP5Bits As TUEP Absolute $0F2B,
   UEP6Bits As TUEP Absolute $0F2C,
   UEP7Bits As TUEP Absolute $0F2D,
   UEP8Bits As TUEP Absolute $0F2E,
   UEP9Bits As TUEP Absolute $0F2F,
   UEP10Bits As TUEP Absolute $0F30,
   UEP11Bits As TUEP Absolute $0F31,
   UEP12Bits As TUEP Absolute $0F32,
   UEP13Bits As TUEP Absolute $0F33,
   UEP14Bits As TUEP Absolute $0F34,
   UEP15Bits As TUEP Absolute $0F35,    
   UIEBits As TUIE Absolute $0F36,
   UEIEBits As TUEIE Absolute $0F37,
   UADDRBits As TUADDR Absolute $0F38,
   UCFGBits As TUCFG Absolute $0F39
#else
Public Dim
   UFRMLBits As TUFRML Absolute $0F66,
   UFRMHBits As TUFRMH Absolute $0F67,
   UIRBits As TUIR Absolute $0F68,
   UIEBits As TUIE Absolute $0F69,
   UEIRBits As TUEIR Absolute $0F6A,
   UEIEBits As TUEIE Absolute $0F6B,
   USTATBits As TUSTAT Absolute $0F6C,
   UCONBits As TUCON Absolute $0F6D,
   UADDRBits As TUADDR Absolute $0F6E,               
   UCFGBits As TUCFG Absolute $0F6F,
   UEP0Bits As TUEP Absolute $0F70,
   UEP1Bits As TUEP Absolute $0F71,
   UEP2Bits As TUEP Absolute $0F72,
   UEP3Bits As TUEP Absolute $0F73,
   UEP4Bits As TUEP Absolute $0F74,
   UEP5Bits As TUEP Absolute $0F75,
   UEP6Bits As TUEP Absolute $0F76,
   UEP7Bits As TUEP Absolute $0F77,
   UEP8Bits As TUEP Absolute $0F78,
   UEP9Bits As TUEP Absolute $0F79,
   UEP10Bits As TUEP Absolute $0F7A,
   UEP11Bits As TUEP Absolute $0F7B,
   UEP12Bits As TUEP Absolute $0F7C,
   UEP13Bits As TUEP Absolute $0F7D,
   UEP14Bits As TUEP Absolute $0F7E,
   UEP15Bits As TUEP Absolute $0F7F,
   PIE2Bits As TPIE2 Absolute $0FA0,
   PIR2Bits As TPIR2 Absolute $0FA1,
   INTCONBits As TINTCON Absolute $0FF2
#endif             
MDB

mdb80
Registered User
Registered User
Posts: 9
Joined: Tue Dec 21, 2010 11:40 am
Location: Milan

Post by mdb80 » Mon Jan 17, 2011 3:30 pm

USBDefs.bas, USBSystem and System remain the same.


This is USBConfig Library:


Code: Select all

{
*****************************************************************************
*  Name    : USBConfig.bas                                                  *
*  Author  : David John Barker                                              *
*  Notice  : Copyright (c) 2007 Mecanique                                   *
*          : All Rights Reserved                                            *
*  Date    : 12/01/2007                                                     *
*          : 1.2 Added support for 18F46J50 (by Massimo Dalla Bona)         *
*  Version : 1.1 Removed USB_SERVICE_US as ISR now uses USB interrupts      *
*          : 1.0 Release                                                    *
*  Notes   : USB configuration options                                      *
*          : ==================== DO NOT EDIT THIS FILE =================== * 
*          : Change the options FROM YOU MAIN PROGRAM. For example,         *
*          : #option USB_VID = $1234                                        *
*          : This will AUTOMATICALLY override these default values. DONT    *
*          : FORGET to add options BEFORE any include files!!               *
*****************************************************************************
}
Module USBConfig

// ***************************************************************************
// device version (device_dsc)
// ***************************************************************************
#option USB_DEVICE_VERSION = $0110 // (BCD)
#if Not (USB_DEVICE_VERSION in (0 to 65535))
   #error USB_DEVICE_VERSION, "Invalid option. USB_DEVICE_VERSION must be between 0 and 65535."
#endif

// ***************************************************************************
// descriptor string index (device_desc)
// ***************************************************************************
#define USB_MANUFACTURER_INDEX = 1
#define USB_PRODUCT_INDEX      = 2
#variable USB_SERIAL_INDEX     = 0

#option USB_HAVE_SERIAL_STRING = false
#if Not (USB_HAVE_SERIAL_STRING in (true, false))
   #error USB_HAVE_SERIAL_STRING, "Invalid option. USB_HAVE_SERIAL_STRING must be TRUE or FALSE."
#endif
#if USB_HAVE_SERIAL_STRING
   #variable USB_SERIAL_INDEX = 3
#endif

// ***************************************************************************
// bus power (cfg01)
// ***************************************************************************
#option USB_BUS_POWER = 50 // x2                 
#if Not (USB_BUS_POWER in (50 to 250))
   #error USB_BUS_POWER, "Invalid option. USB_BUS_POWER must be between 50 and 250."
#endif

// ***************************************************************************
// link in HID code option...
// ***************************************************************************
#option USB_USE_HID = false                                                     
#if Not (USB_USE_HID in (true, false))
   #error USB_USE_HID, "Invalid option. USB_USE_HID must be TRUE or FALSE."
#endif

// ***************************************************************************
// link in CDC code option...
// ***************************************************************************
#option USB_USE_CDC = false
#if Not (USB_USE_CDC in (true, false))
   #error USB_USE_CDC, "Invalid option. USB_USE_CDC must be TRUE or FALSE."
#endif

// ***************************************************************************
// default option for CDC...
// ***************************************************************************
#if USB_USE_CDC
   #option USB_DESCRIPTOR = "CDCDescriptor.bas"     // name of descriptor file
   #option USB_VID = $1781                          // VID - 6017 is Mecanique
   #option USB_PID = $07D1                          // PID - 2001
   #define MAX_EP_NUMBER = 3           // UEP1 minimum, up to 16                   'usbcfg.h

// ***************************************************************************
// default options for HID...
// ***************************************************************************
#else
   #option USB_DESCRIPTOR = "HIDDescriptor.bas"     // name of descriptor file
   #option USB_VID = $1781                          // VID - 6017 is Mecanique
   #option USB_PID = $07D0                          // PID - 2000
   #define MAX_EP_NUMBER = 1           // UEP1 minimum, up to 16                   'usbcfg.h
#endif

// ***************************************************************************
// verify VID...
// ***************************************************************************
#if Not (USB_VID in (0 to 65535))
   #error USB_VID, "Invalid option. USB_VID must be between 0 and 65535."
#endif

// ***************************************************************************
// verify PID...
// ***************************************************************************
#if Not (USB_PID in (0 to 65535))
   #error USB_PID, "Invalid option. USB_PID must be between 0 and 65535."
#endif

// ***************************************************************************
// CDC options
// ***************************************************************************
#option CDC_RX_SIZE = 64    // CDCDataRX
#if Not (CDC_RX_SIZE in (8, 16, 32, 64))
   #error CDC_RX_SIZE, "Invalid option. CDC_RX_SIZE must be 8, 16, 32 or 64."
#endif

#option CDC_TX_SIZE = 64    // CDCDataTX
#if Not (CDC_TX_SIZE in (8, 16, 32, 64))
   #error CDC_TX_SIZE, "Invalid option. CDC_TX_SIZE must be 8, 16, 32 or 64."
#endif

// ***************************************************************************
// HID buffer size
// ***************************************************************************
#option HID_BUFFER_SIZE = 64                    // HID EP1 can be 8, 16, 32 or 64
#if Not (HID_BUFFER_SIZE in (8, 16, 32, 64))
   #error HID_BUFFER_SIZE, "Invalid option. HID_BUFFER_SIZE must be 8, 16, 32 or 64."
#endif

// will equal HID_BUFFER_SIZE unless overridden in main program...
#option HID_REPORT_BYTES_IN = HID_BUFFER_SIZE  // HID Report bytes in
#if Not (HID_REPORT_BYTES_IN in (8, 16, 32, 64))
   #error HID_REPORT_BYTES_IN, "Invalid option. HID_REPORT_BYTES_IN must be 8, 16, 32 or 64."
#endif

// will equal HID_BUFFER_SIZE unless overridden in main program...
#option HID_REPORT_BYTES_OUT = HID_BUFFER_SIZE // HID_Report bytes out
#if Not (HID_REPORT_BYTES_OUT in (8, 16, 32, 64))
   #error HID_REPORT_BYTES_OUT, "Invalid option. HID_REPORT_BYTES_OUT must be 8, 16, 32 or 64."
#endif

// ***************************************************************************
// HID EP Polling
// ***************************************************************************
#option HID_EP_OUT_POLLING_MS = 1   // HID - EP01 - 1ms (EP01Out)
#if Not (HID_EP_OUT_POLLING_MS in (1 to 10))
   #error HID_EP_OUT_POLLING_MS, "Invalid option. HID_EP_OUT_POLLING_MS must be between 1 and 10."
#endif

#option HID_EP_IN_POLLING_MS = 1   // HID - EP01 - 1ms (EP01In)
#if Not (HID_EP_IN_POLLING_MS in (1 to 10))
   #error HID_EP_IN_POLLING_MS, "Invalid option. HID_EP_IN_POLLING_MS must be between 1 and 10."
#endif

// ***************************************************************************
// self power sensing...
// ***************************************************************************
#option USB_POWER_SENSE_IO = false                                              'usbcfg.h
#option USB_POWER_SENSE_PIN = PORTA.2
#if IsOption(USB_POWER_SENSE_PIN) And Not IsValidPortPin(USB_POWER_SENSE_PIN) 
   #error USB_POWER_SENSE_PIN, "Invalid option. USB_POWER_SENSE_PIN must be a valid port pin."
#endif

#if USB_POWER_SENSE_IO                                                          'io_cfg.h
Public Dim self_power As USB_POWER_SENSE_PIN.USB_POWER_SENSE_PIN@
#else
Public Const self_power = 1
#endif

// ***************************************************************************
// bus sense...
// ***************************************************************************
#option USB_BUS_SENSE_IO = false                                                'usbcfg.h
#option USB_BUS_SENSE_PIN = PORTA.1
#if IsOption(USB_BUS_SENSE_PIN) And Not IsValidPortPin(USB_BUS_SENSE_PIN) 
   #error USB_BUS_SENSE_PIN, "Invalid option. USB_BUS_SENSE_PIN must be a valid port pin."
#endif

#if USB_BUS_SENSE_IO                                                            'io_cfg.h
Public Dim usb_bus_sense As USB_BUS_SENSE_PIN.USB_BUS_SENSE_PIN@
#else
Public Const usb_bus_sense = 1
#endif

// ***************************************************************************
// Use interrupt polling...
// ***************************************************************************
#option USB_SERVICE = true
#if Not (USB_SERVICE in (true, false))
   #error USB_SERVICE, "Invalid option. USB_SERVICE must be TRUE or FALSE."
#endif

// set interrupt priority level...
#if IsOption(USB_SERVICE_PRIORITY) And Not (USB_SERVICE_PRIORITY in (ipLow, ipHigh))
   #error USB_SERVICE_PRIORITY, "Invalid option. Priority must be ipHigh or ipLow."
#endif
#option USB_SERVICE_PRIORITY = ipHigh     

// ***************************************************************************
// Timeout between CONFIGURED_STATE and setting 'USBSystem.Connected'
// ***************************************************************************
#option USB_CONNECT_MS = 1200
#if Not (USB_CONNECT_MS in (0 to 5000))
   #error USB_CONNECT_MS, "Invalid option. USB_CONNECT_MS must be between 0 and 5000 ms."
#endif

// ***************************************************************************
// does device support extended dual port RAM
// ***************************************************************************
#if _device in (18F2455, 18F2550, 18F4455, 18F4550,18F46J50)
   #define USB_EXTENDED_RAM = true       
#else 
   #define USB_EXTENDED_RAM = false
#endif

// ***************************************************************************
// 18F1xK50 family series support extended dual port RAM
// ***************************************************************************
#if _device in (18F13K50, 18F14K50)
   #define USB_EXTENDED_RAM_K50 = true
#else 
   #define USB_EXTENDED_RAM_K50 = false
#endif   


MDB

mdb80
Registered User
Registered User
Posts: 9
Joined: Tue Dec 21, 2010 11:40 am
Location: Milan

Post by mdb80 » Mon Jan 17, 2011 3:44 pm

The code work perfectly!

[img]
http://img138.imageshack.us/img138/8431/pic18f46j50.jpg


Thank you!
MDB

Post Reply