PDA

View Full Version : USB CDC with DT_INTS



MikeWinston
- 24th March 2011, 02:07
Hello All,
First of all let me say thanks to the lot of you for all the great info here in this forum. I've
recently used a few ideas from here and I didn't want to just take the code and run. Also
for DT_Ints. Code with this much functionality deserves a thanks. I'm sure it will probably
save my dignity some day.

I've been using the PIC for about 6 years now for work and home projects and I've managed
to learn on my own but I think I'm getting into deeper waters now. I need to ask for help.

I came across this post but I am having trouble making it work.
http://www.picbasic.co.uk/forum/show...4292#post94292

The PC does not recognize the device at all. But before you say, uh oh ... not another
clown who can't figure out USB :(, I have had success using other techniques.

I currently have a good connection using a timer interrupt to run USBSERVICE. The timer is set for 2.7ms. Anything slower and I have an intermittent connection. (or not at all)
I've used this at home with great success. But recently I've tried this on a computer at work
and it drops the connection after about 10 minutes. Tomorrow I am going to increase the
timer interrupt to see if that fixes it.

But I saw the post about CDC and USB interrupts and said that's a hell of lot better than a timer.

Here's what I'm using:
WinXP
PBPro v2.6b
MPLab v8.33
PIC18F2458
Vusb 0.33uF
PicKit2
20MHz Ext. Clock

Any suggestions is much appreciated
thanks -
Mike

mackrackit
- 24th March 2011, 05:12
The link you gave is bad and I could not quite figure out the thread you were looking at. And we do not have your code and configs....
But maybe this will help
http://www.picbasic.co.uk/forum/content.php?r=272-USB-SD-LOGGING

MikeWinston
- 24th March 2011, 12:49
Hi mackrackit,
Thanks for the reply. Sorry about the link. Let me try it again.
http://www.picbasic.co.uk/forum/showthread.php?t=13356&p=94292#post94292


Well I see in your code that it is different than Darrel's original post. It has more
USBINIT and USBSERVICE statements.

My Code: (copied from the bad link)
-----------
PAUSE 100
USBINIT
USBSERVICE
UIE = $7F
UEIE = $9F
@ INT_ENABLE USB_INT


Your Code:
-------------
PAUSE 100
USBINIT
USBSERVICE
UIE = $7F
UEIE = $9F
PAUSE 500
USBINIT
USBSERVICE
@ INT_ENABLE USB_INT
USBSERVICE

That would be great if that's all it is. I will try it tonight and let you know.
If it doesn't work, I will post my code and configs.

MikeWinston
- 24th March 2011, 15:53
Well I tried it at work with a 18F4458 and still couldn't get it to connect. I verified my hardware setup was good by loading the code with the timer interrupt and that works fine.

Here is my complete program:

'************************************************* **************************************
' Title: USB CDC and DT_INTS Testing
' CPU: 18F4458
' Clock: 20 MHz External Clock
'
'************************************************* ***************************************

ASM
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_ECPLLIO_EC_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _CCP2MX_ON_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_OFF_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
ENDASM


DEFINE OSC 48
DEFINE NO_CLRWDT 1
DEFINE USB_BUS_SENSE PORTD,3

INCLUDE "cdc_desc.bas"
INCLUDE "DT_INTS-18.bas"

'---[Interrupts List]---
ASM
INT_LIST macro ;Source, Label, Type, ResetFlag?
INT_Handler USB_INT, USBServiceISR, ASM, yes
endm
INT_CREATE
ENDASM


goto InitChip


'----[Interrupt handler -- Service USB]--------------------------------------------------
ASM
USBServiceISR:
USBSERVICE
INT_RETURN
ENDASM



'---[Initialize Chip]--------------------------------------------------------------------
InitChip:
PAUSE 100
USBINIT
USBSERVICE
UIE = $7F
UEIE = $9F
PAUSE 500
USBINIT
USBSERVICE
@ INT_ENABLE USB_INT
USBSERVICE

PORTA = 0
PORTB = 0
PORTC = 0
PORTD = 0
PORTE = 0

'setup port i/o
TRISA = %00000000
TRISB = %11111111
TRISC = %00110000
TRISD = %00001000
TRISE = %00000000

'all dig
ADCON1 = %00001111

'enable port b pullups
INTCON2.7 = 0



Main:
goto Main


end

Darrel Taylor
- 24th March 2011, 18:29
Tachyon,

USBSERVICE is a PBP statement, not an ASM statement. So it can't be in an ASM/ENDASM block.
It must be like this ...
'----[Interrupt handler -- Service USB]--------------------------------------------------
USBServiceISR:
USBSERVICE
@ INT_RETURN

Then add an underscore in the INT_Handler statement.
'---[Interrupts List]---
ASM
INT_LIST macro ;Source, Label, Type, ResetFlag?
INT_Handler USB_INT, _USBServiceISR, ASM, yes
endm
INT_CREATE
ENDASM


You can only have 1 USBINIT in your program.
And there should never be a USBSERVICE after the interrupt has been enabled.
Interrupting a USBSERVICE to do a USBSERVICE can cause bad things to happen.

'---[Initialize Chip]--------------------------------------------------------------------
InitChip:
PAUSE 100
USBINIT
USBSERVICE
UIE = $7F
UEIE = $9F
; PAUSE 500
; USBINIT
; USBSERVICE
@ INT_ENABLE USB_INT
; USBSERVICE

The following program works perfectly here on a LAB-XUSB with an 18F4550.
'************************************************* **************************************
' Title: USB CDC and DT_INTS Testing
' CPU: 18F4458
' Clock: 20 MHz External Clock
'
'************************************************* ***************************************

ASM
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
; __CONFIG _CONFIG1H, _FOSC_ECPLLIO_EC_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _CCP2MX_ON_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_OFF_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
ENDASM


DEFINE OSC 48
DEFINE NO_CLRWDT 1
DEFINE USB_BUS_SENSE PORTD,3

INCLUDE "cdc_desc.bas"
INCLUDE "DT_INTS-18.bas"

'---[Interrupts List]---
ASM
INT_LIST macro ;Source, Label, Type, ResetFlag?
INT_Handler USB_INT, _USBServiceISR, ASM, yes
endm
INT_CREATE
ENDASM


goto InitChip


'----[Interrupt handler -- Service USB]--------------------------------------------------
USBServiceISR:
USBSERVICE
@ INT_RETURN



'---[Initialize Chip]--------------------------------------------------------------------
InitChip:
PAUSE 100
USBINIT
USBSERVICE
UIE = $7F
UEIE = $9F
@ INT_ENABLE USB_INT

PORTA = 0
PORTB = 0
PORTC = 0
PORTD = 0
PORTE = 0

'setup port i/o
TRISA = %00000000
TRISB = %11111111
TRISC = %00110000
TRISD = %00001000
TRISE = %00000000

'all dig
ADCON1 = %00001111

'enable port b pullups
INTCON2.7 = 0



Main:
goto Main


endMake sure you have 0.1uF bypass capacitors from VDD to VSS.
And some people say that you also need 0.1uF from the USB +5V to VSS, but I've never had to do that.

MikeWinston
- 24th March 2011, 19:56
Hello Darrel,
Sorry but that code is not connecting either. I copied everything over. And it compiles OK.
What is also concerning me is the version I have with the timer interrupt is also dropping the connection after about a half hour. This is after I shortened the interrupt time to 1.3ms. Should I shorten the time even more? Is it because its a USB 1.1 computer? I haven't seen
it drop the connection on a USB 2.0 computer yet.

I've tried all I could think of on the USB Interrupt version. I might have to give up on that one
for now and just focus on the timer interrupt version.

So I'll increase the size of cap on Vusb pin and see if that helps.

Do you think shortening the interrupt time will help?

Thank you for all your help.
Regards,
Mike

MikeWinston
- 25th March 2011, 17:45
Just an update if anyone is interested. The PIC on the USB 2.0 computer is over 6 hours and
has not dropped its connection. The same PIC on the USB 1.1 computer could not hold its
connection for longer than 20 minutes or so.
I am using a timer interrupt (0.6ms) to execute the USBSERVICE routine. If I ever find an
answer why than I will post it back here.

As far as the USB interrupt using DT-INTS-18 v34 for CDC, I haven't a clue why it won't
connect for me. I know the interrupt is not happening at all. And I know my hardware
works with the timer interrupt. If I find what I'm missing than I'll post that also.

Mike

MikeWinston
- 26th March 2011, 02:40
Eureka! Yes I'm that happy. I found the reason why the USB interrupt was not working.
It was the line I uncommented in the cdc_desc.bas file (#define USE_USB_BUS_SENSE_IO).

I read somewhere, don't remember where, when using self powered circuit one should
define a Vbus sense I/0 and uncomment the line above in the cdc_desc file. This is supposed to make the design more USB compliant. If the user unplugs the USB cable, the 3V reg. is turned off. I verified this with a meter and peeked how it is done in the assembly USBSERVICE routine.

I don't mind if the 3V reg. stays on in my design. I am using a power supply. But I guess
it might be a concern in a battery powered design.

I should have mentioned the uncommenting bit in my post. I ASSUMED....

Anyway, thanks Darrel for giving me my timer back. :)
This is fantastic -
Mike