PDA

View Full Version : Help needed on using USB - CDC Routines Please



Dick Barton
8th January 2013, 20:58
Hello All,
I'm trying to get to grips with using the uSB in CDC mode but I am failing miserably. I understand that you need to keep polling the uSB bus either by using USBPoll, USBin or USBout or using an interrupt routine which is what I am attemting to master.


The above program is from an example found except I have added the Interrupt routine to give an interrupt of approx 2.5mS to keep the USB open. The interrupt routine also toggles a led and when scoped it does toggle at 2.7mS on, 2.7mS off.

If I compile the program without commenting out the While / Wend commands my PC sees it as Proton CDC Virtual Com Port(10) and works OK. When pressing any key, hyperterminal displays the messages and the ADC value.

If I comment out the While / Wend command and loop the ADC routine and compile, hyperterminal says cannot open port although the port is still seen by my PC as Proton Virtual Com Port(10).

The Hyperterminal error message displayed is "BadID, probably Port does not Exist, Specified com port doesn't exist."
Once again any help, advice or assistance would be gratefully received.
Regards
Dick


'************************************************* **************
'* Name : USBADC.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2006 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 05/01/2013 *
'* Version : 1.0 *
'* Notes : 18F2455 PROTON PLUS Compiler 3.5.5.4 EasyPic V7 *
'* : HSPLL Enable, J10, J23 Top LNK, VCC/RA5 N/C *
'************************************************* ***************
' USB demo program for CDC virtual serial emulation
' Wait for a byte from USB and transmit several characters
'
' Select CPU And clock speed
Declare Reminders = Off
;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 18F2455

Declare Reminders Off

Xtal = 48 ' Using 8Mhz Ext Xtal and PLL/2 [96/2]Mhz for internal clock
Declare Optimiser_Level = 1
Declare Dead_Code_Remove = On
Include "CDC_Desc.inc"
'
'
'
'ADC Setup
TRISA = 000001 ' All Outputs except AN0
CMCON = 7 ' Comparators OFF
ADCON0 = 000001 ' Select ADC AN0
ADCON1 = 001110 ' AN0 Analog - remainder digital
ADCON2 = 110110 '
'
' GP Registers
Dim PP0 As Byte System ' USBPOLL status return
Dim Byteout As Byte = $41
Dim Wordout As Word = $4142
Dim DWordout As Dword = $41424344
Dim RxBuffer[16] As Byte
Dim TxBuffer As String * 16 = "Hello World\n\r"
Dim CodeText As Code = "Hello World\n\r"
Dim Char As Byte '
Dim ADVAL As Word ' ADC value
Dim Timer0 As TMR0L.Word ' Combine TMR0L and TMR0H into unsigned Word
Dim TMR0IE As INTCON.5 ' Timer0 Interrupt Enable Flag
Dim TMR0IF As INTCON.2 ' Timer0 Interrupt Flag
Dim GIE As INTCON.7 ' Global Interrupt
Dim LED As PORTC.7 ' Led Port
Dim TRNIF As UIR.3 ' USB Int Status Reg, Transaction complete Interrupt'
Symbol TimerPreload = $FF00 ' approx 2.7ms
'
' Use an interrupt to keep USB connection alive...

On_Hardware_Interrupt GoTo USBServiceInterrupt
'
'
'
GoTo ProgramStart

' *******************************************
' * the USB interrupt service routine maintains the
' * connection to the bus - without this routine, a
' * call to USBPoll, USBIn or USBOut must be made
' * every couple of milliseconds or so
' *******************************************
USBServiceInterrupt:
Context Save ' Save any variables or SFRs before the interrupt starts
If TMR0IF = 1 Then ' Is it TMR0 that caused the interrupt?
'
If TRNIF = 1 Then ' Low If USB Busy
Repeat
USBPoll ' Wait for USB to become attached
Until USB_tConnected = 1 Or USB_tConfigured = 1
EndIf
'
Toggle LED ' Toggle LED
Timer0 = TimerPreload ' Reload Timer0
TMR0IF = 0 ' Clear the TMR0 overflow flag
EndIf
Context Restore ' Restore any variables or SFRs and exit the interrupt

'
'
'
' Main program loop
' ***************
ProgramStart:
Repeat
USBPoll ' Wait for USB to become attached
Until USB_tConnected = 1 Or USB_tConfigured = 1
'
' Initiate the interrupt
'
Timer0 = TimerPreload '
T0CON = 000110 ' Enable Timer0, config as 16 bits, Int Clk, Prescaler 1:128
TMR0IF = 0 ' Clear TMR0 initially
TMR0IE = 1 ' Enable a Timer0 overflow interrupt
GIE = 1 ' Enable global interrupts
'
While 1 = 1
Repeat
USBIn 3, RxBuffer, 16
Until STATUS.0 = 0 ' Wait for USB input
' Transmit to a serial terminal


Repeat '
USBOut 3, CodeText, Auto ' Send out Char string
Until STATUS.0 = 0 ' USB Bus available

Repeat '
USBOut 3, TxBuffer, Auto ' Send out Char string
Until STATUS.0 = 0 '
'
Repeat '
USBOut 3, Byteout, 1 ' Send out single byte
Until STATUS.0 = 0 '
'
Repeat '
USBOut 3, Wordout, 2 ' send out 2 bytes
Until STATUS.0 = 0 '
'
Repeat '
USBOut 3, DWordout, 4 ' Send out 4 bytes
Until STATUS.0 = 0 '
'
'
ReadADC:
ADVAL = ADIn 0 '
Swap ADVAL.HighByte, ADVAL.LowByte
Repeat
USBOut 3, ADVAL, 2
Until STATUS.0 = 0
DelayMS 5
' GoTo ReadADC
Wend
'
'
'
'-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
'
@ CONFIG_REQ = 0 ; Override Compiler's configuration settings
Asm-
Config PLLDIV = 2 ;Divide by 2 (8 MHz oscillator input)
Config CPUDIV = OSC1_PLL2 ;[Primary Oscillator Src: /1][96 MHz PLL Src: /2]
Config USBDIV = 2 ;USB clock source comes from the 96 MHz PLL divided by 2
Config FOSC = HSPLL_HS ;HS oscillator, PLL enabled (HSPLL)
Config FCMEN = OFF ;Fail-Safe Clock Monitor disabled
Config IESO = OFF ;Oscillator Switchover mode disabled
Config PWRT = OFF ;PWRT disabled
Config BOR = SOFT ;Brown-out Reset enabled and controlled by software (SBOREN is enabled)
Config BORV = 2 ;
Config VREGEN = OFF ;USB voltage regulator disabled
Config WDT = OFF ;WDT disabled (control is placed on the SWDTEN bit)
Config WDTPS = 32768 ;1:32768
Config CCP2MX = On ;CCP2 input/output is multiplexed with RC1
Config PBADEN = OFF ;PORTB<4:0> pins are configured as digital I/O on Reset
Config LPT1OSC = OFF ;Timer1 configured for higher power operation
Config MCLRE = On ;MCLR pin enabled; RE3 input pin disabled
Config STVREN = On ;Stack full/underflow will cause Reset
Config LVP = OFF ;Single-Supply ICSP disabled
Config XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled
Config Debug = OFF ;Background debugger disabled, RB6 and RB7 configured as GP I/O pins
Config CP0 = OFF ;Block 0 (000800-001FFFh) is not code-protected
Config CP1 = OFF ;Block 1 (002000-003FFFh) is not code-protected
Config CP2 = OFF ;Block 2 (004000-005FFFh) is not code-protected
Config CPB = OFF ;Boot block (000000-0007FFh) is not code-protected
Config CPD = OFF ;Data EEPROM is not code-protected
Config WRT0 = OFF ;Block 0 (000800-001FFFh) is not write-protected
Config WRT1 = OFF ;Block 1 (002000-003FFFh) is not write-protected
Config WRT2 = OFF ;Block 2 (004000-005FFFh) is not write-protected
Config WRTC = OFF ;Configuration registers (300000-3000FFh) are not write-protected
Config WRTB = OFF ;Boot block (000000-0007FFh) is not write-protected
Config WRTD = OFF ;Data EEPROM is not write-protected
Config EBTR0 = OFF ;Block 0 (000800-001FFFh)
Config EBTR1 = OFF ;Block 1 (002000-003FFFh)
Config EBTR2 = OFF ;Block 2 (004000-005FFFh
Config EBTRB = OFF ;Boot block (000000-0007FFh)
Endasm-
Declare Reminders On

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
'
End

Dick Barton
8th January 2013, 21:00
Can anyone tell me how I could have placed the code into a Window Text Box please since it looks a lot tidier and easier to read when looking at other posts.
Thanks
Dick

Dick Barton
18th January 2013, 14:53
Hello,
I've resolved my second question by using the Test Post section. However I could really do with some help on how to implement an interrupt driven CDC connection using ProtonPlus. Maybe someone has some code they would be willing to share.
Thanks
Dick

libra66
1st July 2013, 08:08
Yes I agree, I just finished reading it and posted a question relating to it.