PDA

View Full Version : PDS USB stack



dmTulsa
11th October 2012, 16:42
As I understand it the new USB Stack is microchips stack converted to PDS is this correct? If it is then I assume I should be able to use the generic windows HID USB driver or the microchip driver for windows to interface with a PC program. Is this correct?

Since I see no samples using this I again all I need to do is include the hid_discriptor.inc and usb_hid.inc then use the pds usb commands in the code.

The next issue is how to interface to the window hid driver in VB6 or some other compiler. I'll be using power basic which has api wrappers to the driver. I assume the same will be needed for VB6.

Am I on the right track here or am I making it way to hard?

Thanks
Doug

dmTulsa
11th October 2012, 17:27
Ok, just tried a very simple test wich won't compile due to errors in HID_Descriptor.Inc,USB_System.Inc,USB_MemAlloc.inc

'includes
Include "c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\HID_Descriptor.inc"
Include "c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\USB_HID.inc"
Include "c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\USB_UserHID.inc"
Include "c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\USB_System.inc"
Include "c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\USB_Defs.inc"




Xtal = 48
'Stack_Size = 20
Config_Start
PLLDIV = 5 ;Divide by 5 (20 MHz oscillator input)
CPUDIV = OSC1_PLL2 ;[Primary Oscillator Src: /1][96 MHz PLL Src: /2]
USBDIV = 2 ;USB clock source comes from the 96 MHz PLL divided by 2
FOSC = HS ;HS oscillator (HS)
FCMEN = OFF ;Fail-Safe Clock Monitor disabled
IESO = OFF ;Oscillator Switchover mode disabled
PWRT = OFF ;PWRT disabled
BOR = On ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
BORV = 3 ;Minimum setting
VREGEN = On ;USB voltage regulator enabled
WDT = OFF ;WDT disabled (control is placed on the SWDTEN bit)
WDTPS = 32768 ;1:32768
CCP2MX = On ;CCP2 input/output is multiplexed with RC1
PBADEN = OFF ;PORTB<4:0> pins are configured as digital I/O on Reset
LPT1OSC = OFF ;Timer1 configured for higher power operation
MCLRE = On ;MCLR pin enabled; RE3 input pin disabled
STVREN = On ;Stack full/underflow will cause Reset
LVP = On ;Single-Supply ICSP enabled
ICPRT = OFF ;ICPORT disabled
XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
Debug = OFF ;Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
CP0 = OFF ;Block 0 (000800-001FFFh) is not code-protected
CP1 = OFF ;Block 1 (002000-003FFFh) is not code-protected
CP2 = OFF ;Block 2 (004000-005FFFh) is not code-protected
CP3 = OFF ;Block 3 (006000-007FFFh) is not code-protected
CPB = OFF ;Boot block (000000-0007FFh) is not code-protected
CPD = OFF ;Data EEPROM is not code-protected
WRT0 = OFF ;Block 0 (000800-001FFFh) is not write-protected
WRT1 = OFF ;Block 1 (002000-003FFFh) is not write-protected
WRT2 = OFF ;Block 2 (004000-005FFFh) is not write-protected
WRT3 = OFF ;Block 3 (006000-007FFFh) is not write-protected
WRTC = OFF ;Configuration registers (300000-3000FFh) are not write-protected
WRTB = OFF ;Boot block (000000-0007FFh) is not write-protected
WRTD = OFF ;Data EEPROM is not write-protected
EBTR0 = OFF ;Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks
EBTR1 = OFF ;Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks
EBTR2 = OFF ;Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks
EBTR3 = OFF ;Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks
EBTRB = OFF ;Boot block (000000-0007FFh) is not protected from table reads executed in other blocks
Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
'includes
Include "c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\HID_Descriptor.inc"
Include "c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\USB_HID.inc"
Include "c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\USB_UserHID.inc"
Include "c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\USB_System.inc"
Include "c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\USB_Defs.inc"



'USB---------------------------------
Dim USBmsgFlag As Byte
Dim USBBuffer[64] As Byte ' USB Output buffer
Dim USBBufferRX[64] As Byte ' USB Input buffer
'Symbol CARRY_FLAG = STATUS.0 ' High if microcontroller does not have control over the buffer
'Symbol TRNIF = UIR.3 ' Low if USB Busy
'Symbol ATTACHED_STATE = 6 ' is USB attached


GoSub AttachToUSB

Main:
GoSub DoUSBIn
USBPoll ' Wait for the USB interface to become attached
' toggle portd.1
' delayms 1
' Toggle PORTD.1
' DelayMS 1
GoTo Main
'************************************************* *********

'************* USB routines *******************************

' ************************************************** **********
' * receive data from the USB bus *
' ************************************************** **********
DoUSBIn:

USBIn 1, USBBufferRX, Auto
If STATUS.0 = 1 Then Return
USBmsgFlag = USBBufferRX[0]
Select Case USBmsgFlag
Case 1
' GoSub Set_Exp_Ports 'need to set up in FF
Case 2
' GoSub Read_MUX
'GoSub Set_Exp_Ports
End Select
Return

' ************************************************** **********
' * transmit data *
' ************************************************** **********
'DoUSBOut:

' Repeat
' USBOut 1, USBBuffer,64
' Until STATUS.0 = 0 ' Wait for control over the buffer RAM


'goto main


' ************************************************** **********
' * wait for USB interface to attach *
' ************************************************** **********
AttachToUSB:

Repeat
USBPoll
Until USB_tConnected = 1 Or USB_tConfigured = 1

Return

'*************** end USB routines *****************************





Does anyone know how to use these .inc's properly ?

Doug

ElektroArt
11th October 2012, 19:45
Copy c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\*.inc
to your project directory.

Copy one of *.bas located c:\Users\doug\PDS\Samples\New Samples\Proton_USB_HID_Stack\
to your project directory.

Don't use DoUSBIn, USBPoll etc.

use similar USB functions present at C:\Users\a\PDS\Samples\New Samples\Proton_USB_HID_Stack\*.bas

HID_WriteArray(Buffer,3) etc.
(polling automatic)

modify bas file which copied to your project directory. ( this .bas and .inc files works fine before editing)

dmTulsa
11th October 2012, 20:01
Ok but the .bas samples are only for keyboard and mouse . Is there a list of these commands anyplace like HID_Report_In, HID_TxReport, ect........It doesn't do any good without some documentation. I need more than keyboard or mouse control. I need to be able to read and write 64 bytes.

By the way I used to use easyhid's mnhid.dll in my pc programs to interface with usb device. How is it done now?

Doug

dmTulsa
11th October 2012, 20:50
ElectroArt

Thank you. I did as you suggested and it now compiles but windows will not recognize it as an HID device. I then changed the pid/vid to the one I got from Microchip in the usb_defs and hid_descriptor. I changed nothing else.

any thoughts anyone. I really need to get this going. I think I can find all the interface stuff from mocrochip but it would be nice to have a sample

sorry I'm kind of mad now. A simple compiler update has cost my a few days work so far. Hope you understand

Doug

dmTulsa
11th October 2012, 21:26
Ok I got it to connect after changing the 18f4550 config in the usb_defs.inc file

now I just need a few answers like :


Device = 18F4550
Xtal = 48
;-------------------------------------------------------------------------------
Declare Dead_Code_Remove = On
'includes
Include "HID_Descriptor.inc"
'in the usb_defs.inc there is the conf_begain / end so no need for it here.
'I think there is an error in the defs for 18f4550

'usb auto ataches


Main:
' Clear HID_Report_In

' If USB_tAttached = True Then ' how to send attached msg to pc app
' HID_Report_In[?] 'is this comeing from PC app
' HID_TxReport(x,y) 'I assume this is msg to pc app (msg,num bytes)
' 'what are all the rest of the HID_xxxxx messages / commands
' end if
GoTo Main

'?How to know when I get a usb message from PC since there is no Do_USBin to poll?



Thanks
Doug

ElektroArt
11th October 2012, 22:42
Code should as:



clear buffer
main:
mUSBService()
HID_RxReport(buffer,64)

if buffer[0] = "A" and buffer[1] = "B" then
buffer[0] = "O"
buffer[1] = "K"
HID_TxReport(buffer,64)
clear buffer
endif


goto main

"64" is important which defined as report count. buffer size must equal report count !

HID_RxReport etc defined at USB_UserHID.inc. Your program uses this inc.

Les
12th October 2012, 12:46
The USB HID stack is a demo program that I created, not an official part of the language. I created it in case any user wanted to delve into the deeper regions of USB and possibly add to it. It took many hours to create it and was given as a freeby. If there is a problem with it then fix it, but don't just bloody gripe about it. The code has comments, try reading them and understanding the code.

Do I really need to add a disclaimer to every demo or sample that I create? I hoped that this would not be the case, but it seems that it is. Or do I simply stop creating demos or samples!

dmTulsa
12th October 2012, 13:42
I'm sorry Les I didn't know that. I also said that this will be great as it is the Microchip Stack. MY main grip was the 'supported' USB doesn't work any more in this version as noted in other threads and in Anomaly thread. So I thought I try the USB stack.
Sorry again as it has been very frustrating when making a change unrelated to USB in a program and then USB doesn't work any more. I'll continue working with the USB stack as it will be the best way to go in the end. I'll just use Michrochip DOC's and won't comment here. Shall I report errors if I find them? I had posted one already. I'll stop that too if you'd like.

Yes a simple txt or pdf noting that these are unsupported and to use at your own risk and to see Michrochip for documentation wold have avoided this confusion.

Doug

dmTulsa
12th October 2012, 13:44
Thank you ElectroArt, This should get me going. Thanks again.

Doug

Les
12th October 2012, 14:02
I am certainly not infallible, and the sample HID stack program written in Proton BASIC is guaranteed to have its foibles. Therefore, as a community, we should start a thread about it and discuss any changes, corrections and documentation required. However, what I don't want to read is "This is broken in sample X you need to fix it" or a constant stream of "How do I do this Les". The thread should have user input and user code changes.

It is definitely not a beginners piece of code, but it is commented and follows the Microchip USB stack as closely as possible. Users familiar with Proton and USB should have little difficulty following it. However, I must state for the record that I'm no expert on USB, in fact I don't think anyone is because even the, so called, experts get it wrong sometimes. Even Microchip's stack changes at least once a year because they omitted something or didn't handle an event correctly.

dmTulsa
12th October 2012, 14:26
Ok, BIG thanks to ElectroArt I have a working example using the new unsupported USB-Stack that Les work so hard to prot over from Microchip's USB Stack (Thank you LES). I must say it is much easyier to use than the 'OLD' way it was done.

For now I'm still using the mchid.ll that came with the very old EasyHid in my PC app so I won't post any PC code. I plan on changing to the Mindows HID interface next so I will not have to use the mchid.dll anymore.




Device = 18F4550
Xtal = 48
;-------------------------------------------------------------------------------
Declare Dead_Code_Remove = On
'includes
Include "HID_Descriptor.inc"
'in the usb_defs.inc there is the conf_begain / end so no need for it here.
'I think there is an error in the defs for 18f4550 PLLDIV should = 5 and USBDIV = 2

'USB defines---------------------------------
Dim USBmsgFlag As Byte
Dim USBBufferTx[64] As Byte ' USB Output buffer
Dim USBBufferRX[64] As Byte ' USB Input buffer

' HID_Report_In[] when to use?
' Clear HID_Report_In when to use?

'--------------------------------------------------------------------------
'define vars -------------------------------------------------------------
Dim i As Byte
'usb auto ataches correctly no code needed

Clear USBBufferTx ' ensure buffers are empty
Clear USBBufferRX
Clear HID_Report_In

'usb auto ataches correctly no code needed

Main:

If USB_tAttached = True Then
GoSub ChkUSBMsg
End If
GoTo Main


ChkUSBMsg:
mUSBService() 'I assume this checks for a message
HID_RxReport(USBBufferRX,64) ' read message into buffer
USBmsgFlag = USBBufferRX[0] 'get byte 0, I send this from PC app
Select Case USBmsgFlag 'test byte 0, I send this from PC app
Case 1 'test for message type 1 from pc
For i = 1 To 21
USBBufferTx[i] = 0x80 'send some values
Next
HID_TxReport(USBBufferTx,64)
Clear USBBufferTx
Clear USBBufferRX
Clear HID_Report_In
Case 2 'test for message type 1 from pc
For i = 1 To 21
USBBufferTx[i] = 0xf0
Next
HID_TxReport(USBBufferTx,64) 'send some different values
Clear USBBufferTx
Clear USBBufferRX
End Select
'clear buffers
Clear USBBufferTx
Clear USBBufferRX
Clear HID_Report_In

Return



Doug

dmTulsa
13th October 2012, 23:11
I just want to thank Les for his hard work in this port of the Standard Michrochip USB STACK. It is MUCH better than the old way. 1000% better. I want to say thanks to ElectroArt for helping me. He dug through the ported PDS code and the C code. Lots of work I know. As always it's easy once you know how to use it. I hope something like what I've posted makes it to the samples. The keybaord & mouse samples not so useful as they are do not show how to actually send and receive bytes at the same time. Pretty simplistic and not useful unless you want keyboard and mouse samples. I'm not sure I did my sample it correctly but it works great. Les ? At least it shows how to transfer data in both directions.

I hope it (the USB Stack) makes it into the next update and is fully documented.

One question? do the usb .inc's use/set the USB ram? For example: Yes from a different compiler
dim readbuff as byte[64] absolute 0x500 ' Buffers should be in USB RAM, please consult datasheet
dim writebuff as byte[64] absolute 0x540
or am I being fooled? by sales
Is that in the .inc or do we have to define it?

One last thing. The vendor and manufacture do not show up in the usb properties in the device manager for the new descriptor ether. I don't know if its related to a different report made on the forum but this is a totally different descriptor. All it says is "USB Input Device" for the ID's PID/VID is correct. No big deal as this is for in house use. But I'd like customers to see it on connect if not for in house use like all other devices. BTW win 8 does not show any message unless there is an error.
Note :Got VID/PID from michrochip

Great progress on the usb front. Using The Standard Michrochip usb stack opens up all kinds of custom uses if used with there custom driver.

I urge all to switch to the new usb stack. Just ensure the config's are correct. The fuse config when using the usb_xxx.inc (def's.inc I think) is not in you program any more but in the .inc. So make sure its correct if you have a problem like I did.

So..........as I'm about to be banned from the forum for nothing more than a matter of personal pride, and a bad comment, I hope this helps someone as I have been helped by others and have striven to help others also.

This is really a much better way to handel use stuff. No doubt and I've been fighting USB inconstancy a long time. This should solve that problem.

Doug

Tim
15th October 2012, 01:31
I'm thinking of doing some coding with the Pic18F25k50

Any pointers or code to get Hid working? I want to port my bootloader over.

dmTulsa
3rd November 2012, 14:38
Tim, other than what I posted a couple posts ago that does bi-directional comms I can't help much. I'm still working on the PC side getting windows HID.dll working. I've been really sick the last few weeks so slow to reply

doug

John Drew
4th November 2012, 00:26
Doug,
When you're feeling better any chance of doing a Wiki article on your solution Wiki/Code examples/USB
It would be very valuable.
Cheers
John

dmTulsa
4th November 2012, 16:42
John,
I'll write something up when I get a chance. I kink of want to wait so I can also post the info on using the Windows HID.DLL to interface to HID also.

Doug

John Drew
4th November 2012, 21:10
Good idea Doug, that will make it a complete package.
John

johngb
29th January 2013, 15:59
I have attempted to do what dmTulsa has done using his code posted on 1th Oct 12.
What changes were made to the config in USBDefs as I cannot get windows to recognise the device.
Also were there any changes necessary to the HID_Descriptors file?