No announcement yet.

C++ to Powerbasic - USB2 programming with cypress CYUSB driver

  • Filter
  • Time
  • Show
Clear All
new posts

  • C++ to Powerbasic - USB2 programming with cypress CYUSB driver

    Hi all,

    I need a little assistance, I would like to convert the code below
    Powerbasic. I have already converted it but I am not getting the
    results I expect, in that passing the initialise type below to
    DeviceIoControl does not yield the correct response. The area I am
    concerned with is the replacing of the c++ memory allocation.
    I have tried 2 approaches, first using malloc routines present on
    the forum, reserving a block of ram, and the pointing a pointer to
    structure/type/record Single_Transfer, and initialising the structure
    and passing that to DeviceIoControl function. Basically what the
    code is doing is creating a block of ram, where the first part of
    ram holds the structure/record which immediately followed by data.
    Calling the DeviceIoControl realise write, involves placing data
    in the buffer after the structure/record, whilst reading on return
    from calling DeviceIoControl data is in the buffer.

    My second approached involved defining a the structure/record a
    global, but adding a new last field which consisted of an array of
    bytes - essentially the buffer area. Since an offset is require to
    compute the buffer, and is held in on of the fields of the structure/
    record, I work this out using the original structure/record (without
    the buffer in the last field) - using the sizeof command. Now all
    do is initialise the amended structure/record, and use varptr to
    pass it to DeviceIoControl. Still no joy, in the the sense it runs
    but does not do what I expect, in comparrison to my C++ program.
    No memory allocation routines require.

    Is there better way to generate blocks of memory for use like this, or
    pass parameters to to DeviceIoControl.
    I have been successful upto this point with DeviceIoControl, when
    did'nt have to pass the structure/record.
    I have already checked the ram block after calling DeviceIoControl,
    and it is as it should be. I check my Type definition against he c++
    structure. I take a step by step approach, checking my results with
    c++ program which uses an API. The sizeof returns a different value
    for structure/record compared to that under c++, I put that down
    to internal padding.
    Basically I am trying to program a
    USB2 device, it would nice to do it under Powerbasic.
    Any pointers (no pun intended) most welcome

    Thanks in advance

    (forgive the typos and the rest)

    *Setupacket is just a structure/record, as is pTransfer

    int iXmitBufSize = sizeof(SINGLE_TRANSFER) + bufLen; // The size of the two-part structure

    ** UCHAR *pXmitBuf = new UCHAR[iXmitBufSize]; // Allocate the memory

    ZeroMemory(pXmitBuf, iXmitBufSize);

    * PSINGLE_TRANSFER pTransfer = (PSINGLE_TRANSFER)pXmitBuf; // The SINGLE_TRANSFER comes first
    * pTransfer->SetupPacket.bmRequest = bmReq;
    * pTransfer->SetupPacket.bRequest = ReqCode;
    * pTransfer->SetupPacket.wValue = Value;
    * pTransfer->SetupPacket.wIndex = Index;
    * pTransfer->SetupPacket.wLength = bufLen;
    * pTransfer->SetupPacket.ulTimeOut = TimeOut / 1000;
    * pTransfer->WaitForever = false;
    * pTransfer->ucEndpointAddress = 0x00; // Control pipe
    * pTransfer->IsoPacketLength = 0;
    * pTransfer->BufferOffset = sizeof (SINGLE_TRANSFER);
    * pTransfer->BufferLength = bufLen;
    * DWORD dwReturnBytes;

    * DeviceIoControl (hDevice, IOCTL_ADAPT_SEND_EP0_CONTROL_TRANSFER,
    * pXmitBuf, iXmitBufSize,
    * pXmitBuf, iXmitBufSize,
    * &dwReturnBytes, NULL);

    powerbasic version approach 1:
    'pTransfer is pointer to structure/record initialise to free
    ' block of ram
    Sub ResetFX2
    Local count As Dword
    Local txsize As Byte
    Local ret As Long
    @pTransfer.SetupPacket.bmReqType = &h40
    @pTransfer.SetupPacket.bRequest = &hA0
    @pTransfer.SetupPacket.wValue = &hE600
    @pTransfer.SetupPacket.wIndex = 0
    @pTransfer.SetupPacket.wLength = 1
    @pTransfer.WaitForever =0
    @pTransfer.NtStatus = 0
    @pTransfer.UsbdStatus = 0
    @pTransfer.IsoPacketOffset = 0
    @pTransfer.SetupPacket.ulTimeOut = 1
    @pTransfer.IsoPacketLength = 0
    @pTransfer.BufferOffset = SizeOf(SINGLE_TRANSFER)

    txsize = SizeOf(SINGLE_TRANSFER) + 1 ' Actual amount of data being set
    USBbuffer = pTransfer + SizeOf(SINGLE_TRANSFER)
    @USBbuffer = 1
    ret = DeviceIoControl(ByVal hUSBDevice, %IOCTL_ADAPT_SEND_EP0_CONTROL_TRANSFER,_
    ByVal pTransfer,ByVal SizeOf(SINGLE_TRANSFERz)+1,_
    ByVal pTransfer,ByVal SizeOf(SINGLE_TRANSFERz)+1,_
    ByVal VarPtr(count), ByVal %Null)

    End Sub

    ' 2nd approach
    Sub ResetFX2a
    Local count As Dword
    Local count1 As Long
    Local txsize As Long
    Local ret As Long

    pTransfer1.SetupPacket.bmReqType = &h40
    pTransfer1.SetupPacket.bRequest = &hA0
    pTransfer1.SetupPacket.wValue = &hE600
    pTransfer1.SetupPacket.wIndex = 0
    pTransfer1.SetupPacket.wLength = 1
    pTransfer1.WaitForever =0
    pTransfer1.NtStatus = 1
    pTransfer1.UsbdStatus = 1
    pTransfer1.SetupPacket.ulTimeOut = 1
    pTransfer1.IsoPacketLength = 0
    pTransfer1.IsoPacketOffset = 0
    pTransfer1.BufferOffset = SizeOf(SINGLE_TRANSFERz)
    pTransfer1.BufferLength = 1
    pTransfer1.ucEndpointAddress = 0

    txsize = SizeOf(SINGLE_TRANSFER) + 1 ' Actual amount of data being set

    count = 0 'data returned count
    ret = DeviceIoControl(ByVal hUSBDevice, %IOCTL_ADAPT_SEND_EP0_CONTROL_TRANSFER,_
    ByVal VarPtr(pTransfer1), txsize,_
    ByVal VarPtr(pTransfer1), txsize,_
    ByVal VarPtr(count), ByVal %Null)


  • #2
    >txsize = SizeOf(SINGLE_TRANSFER) + 1 ' Actual amount of data being set

    No "+1" required. SIZEOF is the amount of data. You are sending an extra (null, maybe) byte of data on each call.

    You also never set pTransfer to VARPTR(referenced UDT) when used as a pointer.

    Also please use Code Tags

    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]


    • #3
      Sorted it out myself, nothing like memory dump to relieve things.


      Herbert Larbie



      • #4
        Thanks Michael, in the second approach pTransfer1 is an UDT
        not a pointer.




        • #5
          Hi Michael,

          txsize = sizeof(SINGLE_TRANSFER) + 1, is size of UDT + 1 data item
          which hence the total amount of data being sent.