How stable and usable is the USB module of Swordfish basic?

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

How stable and usable is the USB module of Swordfish basic?

Post by octal » Tue Jul 03, 2007 6:46 pm

Hi,
I would like to know how stable and usable is the USB module of Swordfish basic?
Have anyone used it in pro developments (at least in HID mode)?

Regards

Octal

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Tue Jul 03, 2007 7:16 pm

Hello,

I have not used USB in anger yet, but from testing, both CDC and HID work well. I know that Dave has gone to a lot of trouble to try and ensure that the USB library is robust and will enumerate reliably. In addition, it is very cleverly done to allow easy transmission of data using structures. I would not hesitate from trying to use it in a real app.

I know that this isn't quite what you were asking, but hope it helps.

Regards,

Steve

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Jul 03, 2007 7:23 pm

The USB libraries are Swordfish ports of the Microchip 'C' USB drivers. In that respect, the Swordfish modules are logically as good (or bad) as the Microchip version. Many (if not most) other BASIC compilers also base there USB support around the Microchip firmware. The main difference is that the Swordfish modules are written in the Swordfish language, rather than ASM.

There were a few minor problems with the initial library release (due to a coding error by myself) but I have found 1.1 of the HID module very robust. I have personally spent many hours testing HID on XP and Vista, with no problems encountered.

I would say that using USB on a PIC does require some thought with regard to design, as the firmware is required to maintain a connection with the PC very few milliseconds or so. Note that this figure can vary quite a bit, depending on the PC/OS/hub used.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Tue Jul 03, 2007 9:01 pm

Ok,
thank you Steven, thank you David.

I have some experience with Microchip firmware. I already used it for two products and it seems to be reliable.
I only need USB to communicate data from a data logger to a PC, and collected data do not exceed 512Ko (collected once per week), so HID is entirely sufficient and do not require a driver to be installed.

At this time, I have just done my purchase for a dual license at Mecanique web site, using standard shipment. I hope it will arrive soon to France (at Argenteuil, close to Paris).
By the way, I'll be a happy owner of a Swordfish license, because I'll be able to use the free license of the GLCD Font Creator plugin :lol: :lol: :lol:

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Sat Jul 07, 2007 1:40 pm

Hi,
I received today my dual license Swordfish Compiler :o :o :o wow ... taked only 3 days, bought last wednesday, and arrived by saturday morning at Argenteuil (close to Paris). Very quick, and very well packaged. Thanks to Mecanique (and David of course :wink: ).

Well I have done some tests using USB-HID of course under XP and all seems to work very fine. I have written a little program sending and receiving data to/from PC, and 100% of data was correct (pc software compares data) sent and received data was identical. I tested it for more than 10Mo of data ... works very nice. my Data Logger will be ready soon :)
I also changed some descriptors (the manufacturer name, product ID and name, .... and of course the size of their container arrays dimension) this worked fine from first shot.
I'll do more tests under Win98SE, W2K and Vista and will let you informed. If I have enought time next week I'll do the same tests using CDC.

Regards
Octal

garryp4
Posts: 126
Joined: Mon May 21, 2007 7:18 am
Location: Loveland, CO USA

Post by garryp4 » Mon Jul 09, 2007 1:06 pm

I'm having a bit of a problem getting the correct data through on W2K. As I'm just learning this the problem is most likely my code. I have used the code generated with EasyHid. Right now I just want to send from the PIC to VB so a lot is commented out. anyway...

Code: Select all

// device and clock...                                                                                                                                                                                                                                         
Device = 18F4550
Clock = 48
   
// 20Mhz crystal, 48Mhz internal (FS USB)
Config
   PLLDIV = 5,
   CPUDIV = OSC1_PLL2,
   USBDIV = 2,
   FOSC = HSPLL_HS,
   VREGEN = ON

// this is the programs descriptor, generated by EasyHID - you
// can find it in the same folder as this program...
#option USB_DESCRIPTOR = "USBProjectDesc.bas"

// import the HID module...
Include "usbhid.bas"

// an example TX report structure, which contains a message
// string and two word values - the variable TXReport will overlay
// the structure over the USB dual port RAM buffer...
Structure TTXReport
   Message As String
   ValueA As Word
   ValueB As Word
End Structure
Dim TXReport As TTXReport Absolute TXReportRAM  

// an example RX report structure, which contains some bit
// value and a configuration byte - the variable RXReport will overlay
// the structure over the USB dual port RAM buffer...
Structure TRXReport
   LED0 As Bit
   LED1 As Bit
   Configuration As Byte
End Structure    
Dim RXReport As TRXReport Absolute RXReportRAM

// rather than using the above structures, you can read and write data
// directly into a buffer array - choose which method you prefer - examples
// are given in the main program block...
Dim data(10) As Byte
Dim loop1    As Byte


// connect to USB...
Repeat
Until HID.Attached
   DelayMS(2000)
// the main program loop shows two different ways in which you can read and
// write HID data. The ReadReport() and WriteReport() techniques are very useful
// when sending and receiving highly structured data (for example, string, bits,
// words etc). If you just want to send and receive blocks of bytes, then
// ReadArray() and WriteArray() are probably more useful - you can of course
// use both ways!

While true
   
   // this is one way you can read some HID data...
'   If DataAvailable Then
 '     ReadReport
      // the data is now in our RX structure - for example, PORTD.0 = RXReport.LED0 etc...
      // Alternatively, you can access the buffer directly as an array. For example,
      // HID.RXReport(0), HID.RXReport(1)...
'   EndIf

   // rather than using ReadReport(), as shown above, you can read data directly 
   // into a buffer array. For example,
'   If DataAvailable Then
'      ReadArray(Buffer,10)
'   EndIf

   // this is one way you can write some HID data...
'   TXReport.Message = "Hello World"
'   TXReport.ValueA = $1234
'   TXReport.ValueB = $0000
'   WriteReport
   
   // rather than using WriteReport(), as shown above, you can write data directly
   // into a buffer array. For example,
'   Buffer(0) = 0
'   Buffer(1) = 1

   For loop1 = 1 To 10
     data(loop1) = loop1
   Next
     
   WriteArray(data,10)
   DelayMS(2000)
Wend
The VB code...

Code: Select all

' vendor and product IDs
Private Const VendorID = 6017
Private Const ProductID = 2000

' read and write buffers
Private Const BufferInSize = 20
Private Const BufferOutSize = 20
Dim BufferIn(0 To BufferInSize) As Byte
Dim BufferOut(0 To BufferOutSize) As Byte

' ****************************************************************
' when the form loads, connect to the HID controller - pass
' the form window handle so that you can receive notification
' events...
'*****************************************************************
Private Sub Form_Load()
   lblConnected.Caption = "HID Device Not Connected"
   ' do not remove!
   ConnectToHID (Me.hwnd)
End Sub

'*****************************************************************
' disconnect from the HID controller...
'*****************************************************************
Private Sub Form_Unload(Cancel As Integer)
   DisconnectFromHID
End Sub

'*****************************************************************
' a HID device has been plugged in...
'*****************************************************************
Public Sub OnPlugged(ByVal pHandle As Long)
   If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
      ' ** YOUR CODE HERE **
      lblConnected.Caption = "HID Device Connected"
   End If
End Sub

'*****************************************************************
' a HID device has been unplugged...
'*****************************************************************
Public Sub OnUnplugged(ByVal pHandle As Long)
   If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
      ' ** YOUR CODE HERE **
      lblConnected.Caption = "HID Device Not Connected"
   End If
End Sub

'*****************************************************************
' controller changed notification - called
' after ALL HID devices are plugged or unplugged
'*****************************************************************
Public Sub OnChanged()
   Dim DeviceHandle As Long
   
   ' get the handle of the device we are interested in, then set
   ' its read notify flag to true - this ensures you get a read
   ' notification message when there is some data to read...
   DeviceHandle = hidGetHandle(VendorID, ProductID)
   hidSetReadNotify DeviceHandle, True
End Sub

'*****************************************************************
' on read event...
'*****************************************************************
Public Sub OnRead(ByVal pHandle As Long)

  Dim loop1 As Integer
  txtDataIn.Text = " "
  
   ' read the data (don't forget, pass the whole array)...
   If hidRead(pHandle, BufferIn(0)) Then
      ' ** YOUR CODE HERE **
      ' first byte is the report ID, e.g. BufferIn(0)
      ' the other bytes are the data from the microcontrolller...
        
     txtDataIn.Text = pHandle & " "
        
     For loop1 = 0 To 10
       txtDataIn.Text = txtDataIn.Text & BufferIn(loop1) & " "
     Next loop1
     txtDataIn.Text = txtDataIn.Text & vbCrLf
     
   End If
End Sub

'*****************************************************************
' this is how you write some data...
'*****************************************************************
Public Sub WriteSomeData()
   BufferOut(0) = 0   ' first by is always the report ID
   BufferOut(1) = 10  ' first data item, etc etc

   ' write the data (don't forget, pass the whole array)...
   hidWriteEx VendorID, ProductID, BufferOut(0)
End Sub
 
When I run the VB all I get in the textbox is 54861028 0 8 9 11 69 36 138 23 17 0 0. I was expecting to get 1 through 10. Any suggestions?

Thanks in advance.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Mon Jul 09, 2007 1:16 pm

Sorry I only tested USB using Delphi (in HID mode). I do not use the mecanique lib, I use HID Suite from Soft-Gems web site http://www.soft-gems.net/index.php?opti ... &Itemid=33
witch provide a very nice HID component (delphi vcl).
I use it mainly for my devs.
Regards
Octal

Post Reply