Leonwe
驱动牛犊
驱动牛犊
  • 注册日期2001-06-06
  • 最后登录2005-12-12
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1840回复:0

请教高手,我在用自编的程序下载Hex文件到EZUSB芯片(AN2131QC)时的问题

楼主#
更多 发布于:2002-05-02 15:43
下面的一段节选自 Cypress公司ezusb开发套件中的 EZ-USB General Purpose Driver Specification.pdf 一文,有几个疑点百思不得其解。

========================================================================
5.4 EZ-USB Specific IOCTLs
5.4.1 IOCTL_Ezusb_ANCHOR_DOWNLOAD
  Downloads data to EZ-USB RAM starting at address 0.
 
  dwIoControlCode IOCTL_Ezusb_ANCHOR_DOWNLOAD
  lpInBuffer Buffer of data to download to EZ-USB RAM
  nInBufferSize Size of the download buffer. Must be <= 7KB.
  lpOutBuffer NULL
  nOutBufferSize 0

5.4.2 IOCTL_EZUSB_ANCHOR_DOWNLOAD
  Downloads data to EZ-USB RAM starting at the specified address. This IOCTL will only download to the EZ-USB’s internal RAM.

  dwIoControlCode IOCTL_EZUSB_ANCHOR_DOWNLOAD
  lpInBuffer Pointer to an ANCHOR_DOWNLOAD_CONTROL structure.
   typedef struct _ANCHOR_DOWNLOAD_CONTROL
   {
   WORD Offset;
   } ANCHOR_DOWNLOAD_CONTROL, *PANCHOR_DOWNLOAD_CONTROL;
   Offset pecifies the offset within EZ-USB RAM to download to.
  nInBufferSize sizeof(ANCHOR_DOWNLOAD_CONTROL)
  lpOutBuffer Buffer of data to download to EZ-USB RAM
  nOutBufferSize size of the output buffer.

  This parameter determines the size of the Anchor Download.
========================================================================

(1)IOCTL_Ezusb_ANCHOR_DOWNLOAD 与 IOCTL_EZUSB_ANCHOR_DOWNLOAD 的区别?
(2)我认为,windows API 函数 DeviceIoControl 的 lpOutBuffer 参数是指向PC接收到设备发来的数据的,但在上文5.4.2所说是“Buffer of data to download to EZ-USB RAM”,怎么理解?
(3)我已将我的程序 MY.A51 编译成 MY.HEX,用“记事本”打开内容如下:
:03000000020200F9
:1002000075815F907F947400F0907F9D74FFF0740F
:1002100003F4907F97F051355135513551355135B3
:100220002380EF7FFA000000000000DFF8227E0A42
:100230005123DEFC227E645123DEFC2251355135F0
:0E024000513551355135227D0A5135DDFC22F4
:00000001FF
而在 Cypress的 control panel 程序里显示如下:
0000 75 81 5F 90 7F 94 74 00 F0 90 7F 9D 74 FF F0 74
0010 03 F4 90 7F 97 F0 51 35 51 35 51 35 51 35 51 35
0020 23 80 EF 7F FA 00 00 00 00 00 00 DF F8 22 7E 0A
0030 51 23 DE FC 22 7E 64 51 23 DE FC 22 51 35 51 35
0040 51 35 51 35 51 35 22 7D 0A 51 35 DD FC 22
请问,我将以何种转换方式将我的MY.HEX文件内容放到 lpInBuffer 变量里去,能否给出用Delphi/VB源程序?我参照Cypress的C:\\Cypress\\USB\\Examples\\EzUsb\\bulktest\\host\\main.c文件改写得Delphi代码,总是下载失败,而用 Cypress的 control panel 下载正常,不知错在何处,请指教? 另 C/C++语言里fopen、fread函数用法如何?


==========我的Delphi程序================================================
procedure TForm1.Button5Click(Sender: TObject);
// DownLoad HexFile to EZUSB.
const
  MAX_FILE_SIZE = (1024 * 7);
var
  hDevice: THandle;     bResult: Boolean;    nBytes: DWORD;
  F: File;    NumRead: integer;    
  Buffer: array[1..MAX_FILE_SIZE] of char;  
  DownloadFilename: string;
  S: string;  ii: integer;
begin
  hDevice:= OpenDriver;
  if (not bDevHandleValid(hDevice)) then
  begin
    showmessage(\'打开设备时出错!\');
    hDevice := 0;
    exit;
  end;
  if OpenDialog1.Execute then
  begin
    AssignFile(F, OpenDialog1.FileName);
    FileMode := 0;     // Set file access to read only
    Reset(F,1);       // Record size = 1
    BlockRead( F, buffer, SizeOf(buffer), NumRead);
    s:= format(\'Anchor Download %d bytes\',[NumRead]);
    showmessage(S);
    CloseFile(F);
  end;
  bResult := DeviceIoControl (hDevice,
    IOCTL_Ezusb_ANCHOR_DOWNLOAD,
    @buffer,
    NumRead,
    nil,
    0,
    nBytes,
    nil);
  if(not bResult) then Memo1.Lines.Add(\'Anchor 固件下载失败\');
  CloseHandle (hDevice);
end;
========================================================================


==========Cypress 的程序段==============================================
            #define MAX_FILE_SIZE (1024*7)
            char DownloadFilename[256];
            FILE *fp;
            unsigned char buffer[MAX_FILE_SIZE];
            int numread = 0;
            char                   temp[64]  = \"\";
            hOutputBox = GetDlgItem (hDlg, IDC_OUTPUT_BOX);
            MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
            // Get the text in the driver name edit box
            GetDlgItemText (hDlg, IDC_DOWNLOAD_FILENAME, DownloadFilename, 256);
            if ((fp = fopen(DownloadFilename,\"rb\")) != NULL)
            {
               numread = fread(buffer,sizeof(unsigned char),MAX_FILE_SIZE,fp);
               wsprintf (temp, \"Anchor Download %d bytes\",numread);
               SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
               if (IsDlgButtonChecked(hDlg,IDC_VERBOSE) == BST_CHECKED)
                DumpBuffer(buffer,80,hOutputBox);
               fclose(fp);
            }
            // Get the text in the driver name edit box
            GetDlgItemText (hDlg, IDC_DRIVER_NAME, pcDriverName, MAX_DRIVER_NAME);
             // Open the driver
             if (bOpenDriver (&hDevice, pcDriverName) == TRUE) {
                SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)\"Opened Driver Successfully\");
             } else {
                SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)\"Failed to Open Driver\");
                hDevice = NULL;
             }/* else */
             if (hDevice != NULL) {
                bResult = DeviceIoControl (hDevice,
                   IOCTL_Ezusb_ANCHOR_DOWNLOAD,
                   buffer,
                   numread,
                   NULL,
                   0,
                   &nBytes,
                   NULL);
             }/* if valid driver handle */
             if (bResult==TRUE)
             {
                // do nothin
             }
             else
                SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)\"Anchor Download failed\");
             // Close the handle
             CloseHandle (hDevice);
========================================================================
游客

返回顶部