PLX_Sample.cs
上传用户:huajielb
上传日期:2022-07-29
资源大小:626k
文件大小:50k
源码类别:

驱动编程

开发平台:

Visual C++

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9. using System.IO;
  10. using Jungo.wdapi_dotnet;
  11. using Jungo.plx_lib;
  12. using wdc_err = Jungo.wdapi_dotnet.WD_ERROR_CODES;
  13. using DWORD = System.UInt32;
  14. using WORD = System.UInt16;
  15. using BYTE = System.Byte;
  16. using BOOL = System.Boolean;
  17. using UINT32 = System.UInt32;
  18. using UINT64 = System.UInt64;
  19. using WDC_DEVICE_HANDLE = System.IntPtr;
  20. using WDC_ADDR_SIZE = System.UInt32;
  21. using HANDLE = System.IntPtr;
  22. namespace Jungo.PLX_Sample
  23. {
  24.     public enum RW
  25.     {
  26.         READ = 0,
  27.         WRITE = 1,
  28.         READ_ALL = 2
  29.     }
  30.     public enum TRANSFER_TYPE
  31.     {
  32.         BLOCK = 0,
  33.         NONBLOCK = 1
  34.     }
  35.     public enum COMPLETION
  36.     {
  37.         INTERRUPT = 0,
  38.         POLLING = 1
  39.     }
  40.     public enum ALLOCATION_TYPE
  41.     {
  42.         SG = 0,
  43.         CONTIG = 1,
  44.     }
  45.     public enum ACTION_TYPE
  46.     {
  47.         DMA = 0,
  48.         CFG = 1,
  49.         RT = 2,
  50.         EEPROM = 3
  51.     }
  52.     public class PLX_Sample_FilePath : System.Windows.Forms.Form
  53.     {
  54.         private IContainer components;
  55.         private DWORD PLX_DEFAULT_VENDOR_ID = 0x10b5;
  56.         private DWORD PLX_DEFAULT_DEVICE_ID = 0x0;
  57.         private PLX_DeviceList plxDevList;
  58.         private DmaInfoList dmaList;
  59.         private Log log;
  60.         private System.Windows.Forms.TextBox txtLog;
  61.         private System.Windows.Forms.MainMenu mainMenu1;
  62.         private System.Windows.Forms.MenuItem menuAddrSpaces;
  63.         private System.Windows.Forms.MenuItem menuEnableInt;
  64.         private System.Windows.Forms.MenuItem menuDMA;
  65.         private System.Windows.Forms.MenuItem menuEvents;
  66.         private System.Windows.Forms.MenuItem menuEEPROM;
  67.         private System.Windows.Forms.MenuItem menuRegisterEvent;
  68.         private System.Windows.Forms.MenuItem menuChannel0;
  69.         private System.Windows.Forms.MenuItem menuChannel1;
  70.         private System.Windows.Forms.MenuItem menuOpenChn0;
  71.         private System.Windows.Forms.MenuItem menuTransferChn0;
  72.         private System.Windows.Forms.MenuItem menuOpenChn1;
  73.         private System.Windows.Forms.MenuItem menuCfgSpace;
  74.         private System.Windows.Forms.MenuItem menuRTRegs;
  75.         private System.Windows.Forms.Label lblLog;
  76.         private System.Windows.Forms.Label lblPLXDev;
  77.         private System.Windows.Forms.Label label1;
  78.         private System.Windows.Forms.Button btLog;
  79.         private System.Windows.Forms.Button btExit;
  80.         private System.Windows.Forms.ListBox lstBxDevices;
  81.         private System.Windows.Forms.MenuItem menuTransferChn1;
  82.         private System.Windows.Forms.MenuItem menuCfgOffset;
  83.         private System.Windows.Forms.MenuItem menuCfgReg;
  84.         private System.Windows.Forms.MenuItem menuAddrRW;
  85.         private System.Windows.Forms.MenuItem menuLocalRW;
  86.         private System.Windows.Forms.MenuItem menuRTRegsRW;
  87.         private System.Windows.Forms.MenuItem menuEEpromRW;
  88.         private System.Windows.Forms.MenuItem menuInt0;
  89.         private System.Windows.Forms.MenuItem menuMasterInterrupts;
  90.         private System.Windows.Forms.MenuItem menuTargetInterrupts;
  91.         private System.Windows.Forms.MenuItem menuGenerateInt;
  92.         private System.Windows.Forms.MenuItem menuTargetEnableInt;
  93.         private System.Windows.Forms.Button btDevice;
  94.         private Button button1;
  95.         private TextBox TxtBox_OpenFilePath;
  96.         private System.Windows.Forms.MenuItem menuInt1;
  97.         private OpenFileDialog openFileDialog1;
  98.         private SaveFileDialog saveFileDialog1;
  99.         private Button button_SaveData;
  100.         private Button MyDMA;
  101.         public UInt32[] m_DataTransBuffer;
  102.         public PLX_Sample_FilePath()
  103.         {
  104.             InitializeComponent();
  105.             dmaList = new DmaInfoList();
  106.             log = new Log(new Log.TRACE_LOG(TraceLog),
  107.                new Log.ERR_LOG(ErrLog));
  108.             plxDevList = PLX_DeviceList.TheDeviceList();
  109.         }
  110.         protected override void Dispose(bool disposing)
  111.         {
  112.             if (disposing)
  113.             {
  114.                 if (components != null)
  115.                 {
  116.                     components.Dispose();
  117.                 }
  118.             }
  119.             base.Dispose(disposing);
  120.         }
  121.         #region Windows Form Designer generated code
  122.         /// Required method for Designer support - do not modify
  123.         /// the contents of this method with the code editor.
  124.         private void InitializeComponent()
  125.         {
  126.             this.components = new System.ComponentModel.Container();
  127.             this.txtLog = new System.Windows.Forms.TextBox();
  128.             this.lstBxDevices = new System.Windows.Forms.ListBox();
  129.             this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
  130.             this.menuAddrSpaces = new System.Windows.Forms.MenuItem();
  131.             this.menuAddrRW = new System.Windows.Forms.MenuItem();
  132.             this.menuLocalRW = new System.Windows.Forms.MenuItem();
  133.             this.menuTargetInterrupts = new System.Windows.Forms.MenuItem();
  134.             this.menuTargetEnableInt = new System.Windows.Forms.MenuItem();
  135.             this.menuGenerateInt = new System.Windows.Forms.MenuItem();
  136.             this.menuMasterInterrupts = new System.Windows.Forms.MenuItem();
  137.             this.menuEnableInt = new System.Windows.Forms.MenuItem();
  138.             this.menuInt0 = new System.Windows.Forms.MenuItem();
  139.             this.menuInt1 = new System.Windows.Forms.MenuItem();
  140.             this.menuDMA = new System.Windows.Forms.MenuItem();
  141.             this.menuChannel0 = new System.Windows.Forms.MenuItem();
  142.             this.menuOpenChn0 = new System.Windows.Forms.MenuItem();
  143.             this.menuTransferChn0 = new System.Windows.Forms.MenuItem();
  144.             this.menuChannel1 = new System.Windows.Forms.MenuItem();
  145.             this.menuOpenChn1 = new System.Windows.Forms.MenuItem();
  146.             this.menuTransferChn1 = new System.Windows.Forms.MenuItem();
  147.             this.menuEvents = new System.Windows.Forms.MenuItem();
  148.             this.menuRegisterEvent = new System.Windows.Forms.MenuItem();
  149.             this.menuEEPROM = new System.Windows.Forms.MenuItem();
  150.             this.menuEEpromRW = new System.Windows.Forms.MenuItem();
  151.             this.menuCfgSpace = new System.Windows.Forms.MenuItem();
  152.             this.menuCfgOffset = new System.Windows.Forms.MenuItem();
  153.             this.menuCfgReg = new System.Windows.Forms.MenuItem();
  154.             this.menuRTRegs = new System.Windows.Forms.MenuItem();
  155.             this.menuRTRegsRW = new System.Windows.Forms.MenuItem();
  156.             this.lblLog = new System.Windows.Forms.Label();
  157.             this.lblPLXDev = new System.Windows.Forms.Label();
  158.             this.label1 = new System.Windows.Forms.Label();
  159.             this.btLog = new System.Windows.Forms.Button();
  160.             this.btExit = new System.Windows.Forms.Button();
  161.             this.btDevice = new System.Windows.Forms.Button();
  162.             this.button1 = new System.Windows.Forms.Button();
  163.             this.TxtBox_OpenFilePath = new System.Windows.Forms.TextBox();
  164.             this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  165.             this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
  166.             this.button_SaveData = new System.Windows.Forms.Button();
  167.             this.MyDMA = new System.Windows.Forms.Button();
  168.             this.SuspendLayout();
  169.             // 
  170.             // txtLog
  171.             // 
  172.             this.txtLog.Location = new System.Drawing.Point(29, 191);
  173.             this.txtLog.Multiline = true;
  174.             this.txtLog.Name = "txtLog";
  175.             this.txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
  176.             this.txtLog.Size = new System.Drawing.Size(499, 144);
  177.             this.txtLog.TabIndex = 24;
  178.             // 
  179.             // lstBxDevices
  180.             // 
  181.             this.lstBxDevices.ItemHeight = 12;
  182.             this.lstBxDevices.Location = new System.Drawing.Point(29, 86);
  183.             this.lstBxDevices.Name = "lstBxDevices";
  184.             this.lstBxDevices.Size = new System.Drawing.Size(499, 52);
  185.             this.lstBxDevices.TabIndex = 27;
  186.             this.lstBxDevices.SelectedIndexChanged += new System.EventHandler(this.lstBxDevices_SelectedIndexChanged);
  187.             this.lstBxDevices.DoubleClick += new System.EventHandler(this.lstBxDevices_DoubleClicked);
  188.             // 
  189.             // mainMenu1
  190.             // 
  191.             this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  192.             this.menuAddrSpaces,
  193.             this.menuTargetInterrupts,
  194.             this.menuMasterInterrupts,
  195.             this.menuDMA,
  196.             this.menuEvents,
  197.             this.menuEEPROM,
  198.             this.menuCfgSpace,
  199.             this.menuRTRegs});
  200.             // 
  201.             // menuAddrSpaces
  202.             // 
  203.             this.menuAddrSpaces.Index = 0;
  204.             this.menuAddrSpaces.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  205.             this.menuAddrRW,
  206.             this.menuLocalRW});
  207.             this.menuAddrSpaces.Text = "Address Spaces";
  208.             // 
  209.             // menuAddrRW
  210.             // 
  211.             this.menuAddrRW.Index = 0;
  212.             this.menuAddrRW.Text = "Read/Write Address Space";
  213.             this.menuAddrRW.Click += new System.EventHandler(this.menuAddrRW_Click);
  214.             // 
  215.             // menuLocalRW
  216.             // 
  217.             this.menuLocalRW.Index = 1;
  218.             this.menuLocalRW.Text = "Read/Write Local Bus";
  219.             this.menuLocalRW.Click += new System.EventHandler(this.menuLocalRW_Click);
  220.             // 
  221.             // menuTargetInterrupts
  222.             // 
  223.             this.menuTargetInterrupts.Index = 1;
  224.             this.menuTargetInterrupts.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  225.             this.menuTargetEnableInt,
  226.             this.menuGenerateInt});
  227.             this.menuTargetInterrupts.Text = "Interrupts";
  228.             this.menuTargetInterrupts.Select += new System.EventHandler(this.menuTargetInterrupts_Select);
  229.             // 
  230.             // menuTargetEnableInt
  231.             // 
  232.             this.menuTargetEnableInt.Index = 0;
  233.             this.menuTargetEnableInt.Text = "Enable Interrupts";
  234.             this.menuTargetEnableInt.Click += new System.EventHandler(this.menuTargetEnableInt_Click);
  235.             // 
  236.             // menuGenerateInt
  237.             // 
  238.             this.menuGenerateInt.Index = 1;
  239.             this.menuGenerateInt.Text = "Generate Interrupt (slave only)";
  240.             this.menuGenerateInt.Click += new System.EventHandler(this.menuGenerateInt_Click);
  241.             // 
  242.             // menuMasterInterrupts
  243.             // 
  244.             this.menuMasterInterrupts.Index = 2;
  245.             this.menuMasterInterrupts.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  246.             this.menuEnableInt});
  247.             this.menuMasterInterrupts.Text = "Interrupts";
  248.             this.menuMasterInterrupts.Select += new System.EventHandler(this.menuMasterInterrupts_Select);
  249.             // 
  250.             // menuEnableInt
  251.             // 
  252.             this.menuEnableInt.Index = 0;
  253.             this.menuEnableInt.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  254.             this.menuInt0,
  255.             this.menuInt1});
  256.             this.menuEnableInt.Text = "Enable Interrupts";
  257.             this.menuEnableInt.Select += new System.EventHandler(this.menuEnableInt_Select);
  258.             // 
  259.             // menuInt0
  260.             // 
  261.             this.menuInt0.Index = 0;
  262.             this.menuInt0.Text = "On DMA Channel 0";
  263.             this.menuInt0.Click += new System.EventHandler(this.menuInt0_Click);
  264.             // 
  265.             // menuInt1
  266.             // 
  267.             this.menuInt1.Index = 1;
  268.             this.menuInt1.Text = "On DMA Channel 1";
  269.             this.menuInt1.Click += new System.EventHandler(this.menuInt1_Click);
  270.             // 
  271.             // menuDMA
  272.             // 
  273.             this.menuDMA.Index = 3;
  274.             this.menuDMA.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  275.             this.menuChannel0,
  276.             this.menuChannel1});
  277.             this.menuDMA.Text = "DMA";
  278.             // 
  279.             // menuChannel0
  280.             // 
  281.             this.menuChannel0.Index = 0;
  282.             this.menuChannel0.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  283.             this.menuOpenChn0,
  284.             this.menuTransferChn0});
  285.             this.menuChannel0.Text = "Channel 0";
  286.             this.menuChannel0.Select += new System.EventHandler(this.menuChannel0_Select);
  287.             // 
  288.             // menuOpenChn0
  289.             // 
  290.             this.menuOpenChn0.Index = 0;
  291.             this.menuOpenChn0.Text = "Open";
  292.             this.menuOpenChn0.Click += new System.EventHandler(this.menuOpenChn0_Click);
  293.             // 
  294.             // menuTransferChn0
  295.             // 
  296.             this.menuTransferChn0.Index = 1;
  297.             this.menuTransferChn0.Text = "Read/Write";
  298.             this.menuTransferChn0.Click += new System.EventHandler(this.menuTransferChn0_Click);
  299.             // 
  300.             // menuChannel1
  301.             // 
  302.             this.menuChannel1.Index = 1;
  303.             this.menuChannel1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  304.             this.menuOpenChn1,
  305.             this.menuTransferChn1});
  306.             this.menuChannel1.Text = "Channel 1";
  307.             this.menuChannel1.Select += new System.EventHandler(this.menuChannel1_Select);
  308.             // 
  309.             // menuOpenChn1
  310.             // 
  311.             this.menuOpenChn1.Index = 0;
  312.             this.menuOpenChn1.Text = "Open";
  313.             this.menuOpenChn1.Click += new System.EventHandler(this.menuOpenChn1_Click);
  314.             // 
  315.             // menuTransferChn1
  316.             // 
  317.             this.menuTransferChn1.Index = 1;
  318.             this.menuTransferChn1.Text = "Read/Write";
  319.             this.menuTransferChn1.Click += new System.EventHandler(this.menuTransferChn1_Click);
  320.             // 
  321.             // menuEvents
  322.             // 
  323.             this.menuEvents.Index = 4;
  324.             this.menuEvents.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  325.             this.menuRegisterEvent});
  326.             this.menuEvents.Text = "Events";
  327.             this.menuEvents.Select += new System.EventHandler(this.menuEvents_Select);
  328.             // 
  329.             // menuRegisterEvent
  330.             // 
  331.             this.menuRegisterEvent.Index = 0;
  332.             this.menuRegisterEvent.Text = "Regsiter Events";
  333.             this.menuRegisterEvent.Click += new System.EventHandler(this.menuRegisterEvent_Click);
  334.             // 
  335.             // menuEEPROM
  336.             // 
  337.             this.menuEEPROM.Index = 5;
  338.             this.menuEEPROM.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  339.             this.menuEEpromRW});
  340.             this.menuEEPROM.Text = "EEPROM";
  341.             // 
  342.             // menuEEpromRW
  343.             // 
  344.             this.menuEEpromRW.Index = 0;
  345.             this.menuEEpromRW.Text = "Read/Write EEPROM";
  346.             this.menuEEpromRW.Click += new System.EventHandler(this.menuEEpromRW_Click);
  347.             // 
  348.             // menuCfgSpace
  349.             // 
  350.             this.menuCfgSpace.Index = 6;
  351.             this.menuCfgSpace.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  352.             this.menuCfgOffset,
  353.             this.menuCfgReg});
  354.             this.menuCfgSpace.Text = "Configuration Space";
  355.             // 
  356.             // menuCfgOffset
  357.             // 
  358.             this.menuCfgOffset.Index = 0;
  359.             this.menuCfgOffset.Text = "By Offset ";
  360.             this.menuCfgOffset.Click += new System.EventHandler(this.menuCfgOffset_Click);
  361.             // 
  362.             // menuCfgReg
  363.             // 
  364.             this.menuCfgReg.Index = 1;
  365.             this.menuCfgReg.Text = "By Register";
  366.             this.menuCfgReg.Click += new System.EventHandler(this.menuCfgReg_Click);
  367.             // 
  368.             // menuRTRegs
  369.             // 
  370.             this.menuRTRegs.Index = 7;
  371.             this.menuRTRegs.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  372.             this.menuRTRegsRW});
  373.             this.menuRTRegs.Text = "RunTime Registers";
  374.             // 
  375.             // menuRTRegsRW
  376.             // 
  377.             this.menuRTRegsRW.Index = 0;
  378.             this.menuRTRegsRW.Text = "Read/Write RT Registers";
  379.             this.menuRTRegsRW.Click += new System.EventHandler(this.menuRTRegsRW_Click);
  380.             // 
  381.             // lblLog
  382.             // 
  383.             this.lblLog.Location = new System.Drawing.Point(29, 164);
  384.             this.lblLog.Name = "lblLog";
  385.             this.lblLog.Size = new System.Drawing.Size(120, 24);
  386.             this.lblLog.TabIndex = 28;
  387.             this.lblLog.Text = "Log";
  388.             // 
  389.             // lblPLXDev
  390.             // 
  391.             this.lblPLXDev.Location = new System.Drawing.Point(29, 60);
  392.             this.lblPLXDev.Name = "lblPLXDev";
  393.             this.lblPLXDev.Size = new System.Drawing.Size(144, 25);
  394.             this.lblPLXDev.TabIndex = 29;
  395.             this.lblPLXDev.Text = "PLX Devices Found:";
  396.             // 
  397.             // label1
  398.             // 
  399.             this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  400.             this.label1.Location = new System.Drawing.Point(48, 17);
  401.             this.label1.Name = "label1";
  402.             this.label1.Size = new System.Drawing.Size(278, 25);
  403.             this.label1.TabIndex = 30;
  404.             this.label1.Text = "Select a device to activate its menu";
  405.             // 
  406.             // btLog
  407.             // 
  408.             this.btLog.Location = new System.Drawing.Point(544, 191);
  409.             this.btLog.Name = "btLog";
  410.             this.btLog.Size = new System.Drawing.Size(96, 43);
  411.             this.btLog.TabIndex = 31;
  412.             this.btLog.Text = "Clear Log";
  413.             this.btLog.Click += new System.EventHandler(this.btLog_Click);
  414.             // 
  415.             // btExit
  416.             // 
  417.             this.btExit.Location = new System.Drawing.Point(544, 240);
  418.             this.btExit.Name = "btExit";
  419.             this.btExit.Size = new System.Drawing.Size(96, 43);
  420.             this.btExit.TabIndex = 32;
  421.             this.btExit.Text = "Exit";
  422.             this.btExit.Click += new System.EventHandler(this.btExit_Click);
  423.             // 
  424.             // btDevice
  425.             // 
  426.             this.btDevice.Location = new System.Drawing.Point(394, 17);
  427.             this.btDevice.Name = "btDevice";
  428.             this.btDevice.Size = new System.Drawing.Size(134, 25);
  429.             this.btDevice.TabIndex = 33;
  430.             this.btDevice.Text = "Open Device";
  431.             this.btDevice.Click += new System.EventHandler(this.btDevice_Click);
  432.             // 
  433.             // button1
  434.             // 
  435.             this.button1.Location = new System.Drawing.Point(432, 354);
  436.             this.button1.Name = "button1";
  437.             this.button1.Size = new System.Drawing.Size(75, 23);
  438.             this.button1.TabIndex = 34;
  439.             this.button1.Text = "OpenFile";
  440.             this.button1.UseVisualStyleBackColor = true;
  441.             this.button1.Click += new System.EventHandler(this.Opentxt_Click);
  442.             // 
  443.             // TxtBox_OpenFilePath
  444.             // 
  445.             this.TxtBox_OpenFilePath.Location = new System.Drawing.Point(29, 354);
  446.             this.TxtBox_OpenFilePath.Name = "TxtBox_OpenFilePath";
  447.             this.TxtBox_OpenFilePath.Size = new System.Drawing.Size(397, 21);
  448.             this.TxtBox_OpenFilePath.TabIndex = 35;
  449.             // 
  450.             // openFileDialog1
  451.             // 
  452.             this.openFileDialog1.FileName = "*.*";
  453.             this.openFileDialog1.Filter = "Text files|*.txt|All files|*.*";
  454.             // 
  455.             // saveFileDialog1
  456.             // 
  457.             this.saveFileDialog1.Filter = "Text files|*.txt|All files|*.*";
  458.             this.saveFileDialog1.Title = "数据保存到文件";
  459.             // 
  460.             // button_SaveData
  461.             // 
  462.             this.button_SaveData.Location = new System.Drawing.Point(513, 354);
  463.             this.button_SaveData.Name = "button_SaveData";
  464.             this.button_SaveData.Size = new System.Drawing.Size(75, 25);
  465.             this.button_SaveData.TabIndex = 38;
  466.             this.button_SaveData.Text = "SaveData";
  467.             this.button_SaveData.UseVisualStyleBackColor = true;
  468.             this.button_SaveData.Click += new System.EventHandler(this.button_SaveData_Click);
  469.             // 
  470.             // MyDMA
  471.             // 
  472.             this.MyDMA.Location = new System.Drawing.Point(544, 19);
  473.             this.MyDMA.Name = "MyDMA";
  474.             this.MyDMA.Size = new System.Drawing.Size(75, 23);
  475.             this.MyDMA.TabIndex = 39;
  476.             this.MyDMA.Text = "MyDMA";
  477.             this.MyDMA.UseVisualStyleBackColor = true;
  478.             this.MyDMA.Click += new System.EventHandler(this.MyDMA_Click);
  479.             // 
  480.             // PLX_Sample_FilePath
  481.             // 
  482.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  483.             this.ClientSize = new System.Drawing.Size(927, 412);
  484.             this.Controls.Add(this.MyDMA);
  485.             this.Controls.Add(this.button_SaveData);
  486.             this.Controls.Add(this.TxtBox_OpenFilePath);
  487.             this.Controls.Add(this.button1);
  488.             this.Controls.Add(this.btDevice);
  489.             this.Controls.Add(this.btExit);
  490.             this.Controls.Add(this.btLog);
  491.             this.Controls.Add(this.label1);
  492.             this.Controls.Add(this.lblPLXDev);
  493.             this.Controls.Add(this.lblLog);
  494.             this.Controls.Add(this.lstBxDevices);
  495.             this.Controls.Add(this.txtLog);
  496.             this.Menu = this.mainMenu1;
  497.             this.Name = "PLX_Sample_FilePath";
  498.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  499.             this.Text = "PLX .NET Sample";
  500.             this.Load += new System.EventHandler(this.PLX_Sample_Load);
  501.             this.Closed += new System.EventHandler(this.PLX_Sample_Closing);
  502.             this.ResumeLayout(false);
  503.             this.PerformLayout();
  504.         }
  505.         #endregion
  506.         /// The main entry point for the application.
  507.         [STAThread]
  508.         static void Main()
  509.         {
  510.             Application.EnableVisualStyles();
  511.             Application.Run(new PLX_Sample_FilePath());
  512.         }
  513.         /* Open a handle to a device */
  514.         private bool DeviceOpen(int iSelectedIndex)
  515.         {
  516.             DWORD dwStatus;
  517.             PLX_Device device = plxDevList.Get(iSelectedIndex);
  518.             /* Open a handle to the device */
  519.             dwStatus = device.DeviceOpen();
  520.             if (dwStatus != (DWORD)wdc_err.WD_STATUS_SUCCESS)
  521.             {
  522.                 Log.ErrLog("PLX_Sample.DeviceOpen: Failed opening a " +
  523.                     "handle to the device (" + device.ToString(false) + ")");
  524.                 return false;
  525.             }
  526.             Log.TraceLog("PLX_Sample.DeviceOpen: The device was successfully open." +
  527.             "You can now activate the device through the enabled menu above");
  528.             return true;
  529.         }
  530.         /* Close handle to a PLX device */
  531.         private BOOL DeviceClose(int iSelectedIndex)
  532.         {
  533.             PLX_Device device = plxDevList.Get(iSelectedIndex);
  534.             BOOL bStatus = false;
  535.             DmaInfoList dmaDevList = dmaList.GetItems(device.Handle);
  536.             foreach (DmaInfo dmaInfo in dmaDevList)
  537.             {
  538.                 dmaList.Remove(dmaInfo);
  539.                 dmaInfo.Close();
  540.                 Log.TraceLog("DMA Channel " +
  541.                     (((DWORD)dmaInfo.DmaChannel == 0) ? "0 " : "1 ") + "was closed. {" +
  542.                     device.ToString(false) + "}");
  543.             }
  544.             if (device.Handle != IntPtr.Zero && !(bStatus = device.DeviceClose()))
  545.             {
  546.                 Log.ErrLog("PLX_Sample.DeviceClose: Failed closing PLX "
  547.                     + "device (" + device.ToString(false) + ")");
  548.             }
  549.             else
  550.                 device.Handle = IntPtr.Zero;
  551.             return bStatus;
  552.         }
  553.         private void PLX_Sample_Load(object sender, System.EventArgs e)
  554.         {
  555.             DWORD dwStatus = plxDevList.Init();
  556.             if (dwStatus != (DWORD)wdc_err.WD_STATUS_SUCCESS)
  557.                 goto Error;
  558.             int index = plxDevList.Populate(PLX_DEFAULT_VENDOR_ID,
  559.                 PLX_DEFAULT_DEVICE_ID);
  560.             if (index < 0)
  561.                 goto Error;
  562.             foreach (PLX_Device dev in plxDevList)
  563.                 lstBxDevices.Items.Add(dev.ToString(true));
  564.             lstBxDevices.SelectedIndex = 0;
  565.             return;
  566.         Error:
  567.             DisableMenu();
  568.             btDevice.Enabled = false;
  569.         }
  570.         private void PLX_IntHandler(PLX_TargetDevice dev)
  571.         {
  572.             Log.TraceLog("interrupt for device {" + dev.ToString(false) +
  573.                 "} received!");
  574.         }
  575.         private void PLX_DMAIntHandler(PLX_MasterDevice dev, IntPtr pWdDma)
  576.         {
  577.             MarshalWdDma m_wdDmaMarshaler = new MarshalWdDma();
  578.             WD_DMA wdDma = (WD_DMA)m_wdDmaMarshaler.MarshalNativeToManaged(pWdDma);
  579.             BOOL bIsRead = (wdDma.dwOptions & (DWORD)WD_DMA_OPTIONS.DMA_FROM_DEVICE) != 0;
  580.             byte[] data = new byte[wdDma.dwBytes];
  581.             Marshal.Copy(wdDma.pUserAddr, data, 0, (int)wdDma.dwBytes);
  582.             Log.TraceLog("interrupt for device {" + dev.ToString(false) +
  583.                 "} received! " + Environment.NewLine +
  584.                 (bIsRead ? "read: " : "wrote: ") +
  585.                 diag_lib.DisplayHexBuffer(data, wdDma.dwBytes));
  586.         }
  587.         private void PLX_EventHandler(ref WD_EVENT wdEvent, PLX_Device dev)
  588.         {
  589.             string sAction;
  590.             switch ((WD_EVENT_ACTION)wdEvent.dwAction)
  591.             {
  592.                 case WD_EVENT_ACTION.WD_INSERT:
  593.                     sAction = "WD_INSERT";
  594.                     break;
  595.                 case WD_EVENT_ACTION.WD_REMOVE:
  596.                     sAction = "WD_REMOVE";
  597.                     break;
  598.                 case WD_EVENT_ACTION.WD_POWER_CHANGED_D0:
  599.                     sAction = "WD_POWER_CHANGED_D0";
  600.                     break;
  601.                 case WD_EVENT_ACTION.WD_POWER_CHANGED_D1:
  602.                     sAction = "WD_POWER_CHANGED_D1";
  603.                     break;
  604.                 case WD_EVENT_ACTION.WD_POWER_CHANGED_D2:
  605.                     sAction = "WD_POWER_CHANGED_D2";
  606.                     break;
  607.                 case WD_EVENT_ACTION.WD_POWER_CHANGED_D3:
  608.                     sAction = "WD_POWER_CHANGED_D3";
  609.                     break;
  610.                 case WD_EVENT_ACTION.WD_POWER_SYSTEM_WORKING:
  611.                     sAction = "WD_POWER_SYSTEM_WORKING";
  612.                     break;
  613.                 case WD_EVENT_ACTION.WD_POWER_SYSTEM_SLEEPING1:
  614.                     sAction = "WD_POWER_SYSTEM_SLEEPING1";
  615.                     break;
  616.                 case WD_EVENT_ACTION.WD_POWER_SYSTEM_SLEEPING2:
  617.                     sAction = "WD_POWER_SYSTEM_SLEEPING2";
  618.                     break;
  619.                 case WD_EVENT_ACTION.WD_POWER_SYSTEM_SLEEPING3:
  620.                     sAction = "WD_POWER_SYSTEM_SLEEPING3";
  621.                     break;
  622.                 case WD_EVENT_ACTION.WD_POWER_SYSTEM_HIBERNATE:
  623.                     sAction = "WD_POWER_SYSTEM_HIBERNATE";
  624.                     break;
  625.                 case WD_EVENT_ACTION.WD_POWER_SYSTEM_SHUTDOWN:
  626.                     sAction = "WD_POWER_SYSTEM_SHUTDOWN";
  627.                     break;
  628.                 default:
  629.                     sAction = wdEvent.dwAction.ToString("X");
  630.                     break;
  631.             }
  632.             Log.TraceLog("Received event notification of type " + sAction +
  633.                 " on " + dev.ToString(false));
  634.         }
  635.         private void PLX_Sample_Closing(object sender, System.EventArgs e)
  636.         {
  637.             plxDevList.Dispose();
  638.             dmaList.Dispose();
  639.         }
  640.         /* list box lstBxDevices */
  641.         private void lstBxDevices_SelectedIndexChanged(object sender,
  642.             System.EventArgs e)
  643.         {
  644.             if (lstBxDevices.SelectedIndex < 0)
  645.             {
  646.                 DisableMenu();
  647.                 btDevice.Enabled = false;
  648.             }
  649.             else
  650.             {
  651.                 PLX_Device dev =
  652.                     plxDevList.Get(lstBxDevices.SelectedIndex);
  653.                 UpdateMenu(lstBxDevices.SelectedIndex);
  654.                 btDevice.Enabled = true;
  655.                 if (dev.Handle == IntPtr.Zero)
  656.                     btDevice.Text = "Open Device";
  657.                 else
  658.                     btDevice.Text = "Close Device";
  659.             }
  660.         }
  661.         private void lstBxDevices_DoubleClicked(object sender,
  662.             System.EventArgs e)
  663.         {
  664.             btDevice_Click(sender, e);
  665.         }
  666.         /* device button */
  667.         private void btDevice_Click(object sender, System.EventArgs e)
  668.         {
  669.             if (btDevice.Text == "Open Device")
  670.             {
  671.                 if (DeviceOpen(lstBxDevices.SelectedIndex) == true)
  672.                 {
  673.                     btDevice.Text = "Close Device";
  674.                     EnableMenu();
  675.                 }
  676.             }
  677.             else
  678.             {
  679.                 PLX_Device dev =
  680.                     plxDevList.Get(lstBxDevices.SelectedIndex);
  681.                 DeviceClose(lstBxDevices.SelectedIndex);
  682.                 btDevice.Text = "Open Device";
  683.                 DisableMenu();
  684.             }
  685.         }
  686.         /* Menu*/
  687.         private void UpdateMenu(int index)
  688.         {
  689.             PLX_Device dev =
  690.                 plxDevList.Get(lstBxDevices.SelectedIndex);
  691.             if (dev.Handle == IntPtr.Zero)
  692.                 DisableMenu();
  693.             else
  694.                 EnableMenu();
  695.             menuTargetInterrupts.Visible = (dev.IsMaster) ? false : true;
  696.             menuMasterInterrupts.Visible = (dev.IsMaster) ? true : false;
  697.             menuDMA.Visible = (dev.IsMaster) ? true : false;
  698.         }
  699.         private void EnableMenu()
  700.         {
  701.             ToggleMenu(true);
  702.         }
  703.         private void DisableMenu()
  704.         {
  705.             ToggleMenu(false);
  706.         }
  707.         private void ToggleMenu(bool flag)
  708.         {
  709.             for (int index = 0; index < mainMenu1.MenuItems.Count; ++index)
  710.                 mainMenu1.MenuItems[index].Enabled = flag;
  711.         }
  712.         /* Address Space Item */
  713.         private void menuAddrRW_Click(object sender, System.EventArgs e)
  714.         {
  715.             PLX_Device dev =
  716.                 plxDevList.Get(lstBxDevices.SelectedIndex);
  717.             string[] sBars = dev.AddrDescToString(false);
  718.             AddrSpaceTransferForm addrSpcFrm = new
  719.                 AddrSpaceTransferForm(dev, sBars, false);
  720.             addrSpcFrm.GetInput();
  721.         }
  722.         private void menuLocalRW_Click(object sender, System.EventArgs e)
  723.         {
  724.             PLX_Device dev =
  725.                 plxDevList.Get(lstBxDevices.SelectedIndex);
  726.             string[] sBars = dev.AddrDescToString(true);
  727.             AddrSpaceTransferForm addrSpcFrm = new AddrSpaceTransferForm(dev
  728.                 , sBars, true);
  729.             addrSpcFrm.GetInput();
  730.         }
  731.         /* Interrupts items*/
  732.         private void menuTargetInterrupts_Select(object sender,
  733.             System.EventArgs e)
  734.         {
  735.             if (menuTargetInterrupts.Enabled == false)
  736.                 return;
  737.             PLX_TargetDevice dev = (PLX_TargetDevice)plxDevList.Get(
  738.                 lstBxDevices.SelectedIndex);
  739.             bool bIsEnb = dev.IsEnabledInt();
  740.             menuTargetEnableInt.Text = bIsEnb ? "Disable Interrupts" :
  741.                 "Enable Interrupts";
  742.             menuGenerateInt.Enabled = bIsEnb ? true : false;
  743.         }
  744.         private void menuTargetEnableInt_Click(object sender,
  745.             System.EventArgs e)
  746.         {
  747.             PLX_TargetDevice dev = (PLX_TargetDevice)plxDevList.Get(
  748.                 lstBxDevices.SelectedIndex);
  749.             if (menuTargetEnableInt.Text == "Enable Interrupts")
  750.             {
  751.                 DWORD dwStatus = dev.EnableInterrupts(new
  752.                     USER_INTERRUPT_TARGET_CALLBACK(PLX_IntHandler));
  753.                 if (dwStatus == (DWORD)wdc_err.WD_STATUS_SUCCESS)
  754.                     menuTargetEnableInt.Text = "Disable Interrupts";
  755.             }
  756.             else
  757.             {
  758.                 DWORD dwStatus = dev.DisableInterrupts();
  759.                 if (dwStatus == (DWORD)wdc_err.WD_STATUS_SUCCESS)
  760.                     menuTargetEnableInt.Text = "Enable Interrupts";
  761.             }
  762.         }
  763.         private void menuGenerateInt_Click(object sender, System.EventArgs e)
  764.         {
  765.             ((PLX_TargetDevice)(plxDevList.Get(
  766.                 lstBxDevices.SelectedIndex))).GenerateTargetInterrupt(0);
  767.         }
  768.         private void menuMasterInterrupts_Select(object sender,
  769.             System.EventArgs e)
  770.         {
  771.             if (menuMasterInterrupts.Enabled == false)
  772.                 return;
  773.             PLX_MasterDevice dev = (PLX_MasterDevice)plxDevList.Get(
  774.                 lstBxDevices.SelectedIndex);
  775.             bool bIsEnb = dev.IsEnabledInt();
  776.             menuEnableInt.Text = bIsEnb ? "Disable Interrupts" :
  777.                 "Enable Interrupts";
  778.         }
  779.         public void menuEnableInt_Select(object sender, System.EventArgs e)
  780.         {
  781.             if (menuEnableInt.Enabled == false)
  782.                 return;
  783.             PLX_MasterDevice dev = (PLX_MasterDevice)plxDevList.Get(
  784.                 lstBxDevices.SelectedIndex);
  785.             if (menuEnableInt.Text == "Enable Interrupts")
  786.             {
  787.                 menuInt0.Visible = true;
  788.                 menuInt1.Visible = true;
  789.             }
  790.             else
  791.             {
  792.                 bool bIsChn0 =
  793.                     (dev.IsChannelIntEnabled(PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0));
  794.                 menuInt0.Visible = bIsChn0 ? true : false;
  795.                 menuInt1.Visible = bIsChn0 ? false : true;
  796.             }
  797.         }
  798.         private void menuInt0_Click(object sender, System.EventArgs e)
  799.         {
  800.             menuInt_Click(PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0);
  801.         }
  802.         private void menuInt1_Click(object sender, System.EventArgs e)
  803.         {
  804.             menuInt_Click(PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_1);
  805.         }
  806.         private void menuInt_Click(PLX_DMA_CHANNEL chn)
  807.         {
  808.             PLX_MasterDevice dev = (PLX_MasterDevice)plxDevList.Get(
  809.                 lstBxDevices.SelectedIndex);
  810.             if (dev.IsEnabledInt())
  811.                 dev.DisableInterrupts();
  812.             else
  813.                 dev.EnableInterrupts(new
  814.                     USER_INTERRUPT_MASTER_CALLBACK(PLX_DMAIntHandler), chn);
  815.         }
  816.         /* DMA items */
  817.         private void menuChannel0_Select(object sender, System.EventArgs e)
  818.         {
  819.             if (menuChannel0.Enabled == false)
  820.                 return;
  821.             PLX_MasterDevice dev = (PLX_MasterDevice)plxDevList.Get(
  822.                 lstBxDevices.SelectedIndex);
  823.             bool bIsOpen = dmaList.IsDMAOpen(dev.Handle,
  824.                 PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0);
  825.             menuOpenChn0.Text = bIsOpen ? "Close" : "Open";
  826.             menuTransferChn0.Enabled = bIsOpen ? true : false;
  827.         }
  828.         private void menuOpenChn_Click(PLX_DMA_CHANNEL plxChn,
  829.             System.Windows.Forms.MenuItem chnItem)
  830.         {
  831.             PLX_MasterDevice dev = (PLX_MasterDevice)plxDevList.Get(
  832.                 lstBxDevices.SelectedIndex);
  833.             if (chnItem.Text == "Open")
  834.             {
  835.                 DmaBuffer dmaBuff = null;
  836.                 UINT32 u32localAddr = 0;
  837.                 RW direction = 0;
  838.                 ALLOCATION_TYPE wAllocType = 0;
  839.                 DWORD dwBytes = 0;
  840.                 BOOL bAutoInc = false;
  841.                 IntPtr pBuffer = IntPtr.Zero;
  842.                 OpenDMAForm openDmaFrm = new OpenDMAForm(plxChn);
  843.                 if (openDmaFrm.GetInput(ref u32localAddr, ref direction,
  844.                     ref wAllocType, ref dwBytes, ref bAutoInc) == false)
  845.                     return;
  846.                 if (wAllocType == ALLOCATION_TYPE.SG)
  847.                 {
  848.                     pBuffer = Marshal.AllocHGlobal((int)dwBytes);
  849.                     if (pBuffer == IntPtr.Zero)
  850.                     {
  851.                         Log.ErrLog("PLX_Sample: Failed to allocate buffer for " +
  852.                             "Scatter-Gather DMA");
  853.                         return;
  854.                     }
  855.                     dmaBuff = new DmaBufferSG(dev);
  856.                 }
  857.                 else
  858.                     dmaBuff = new DmaBufferContig(dev);
  859.                 if (dmaBuff.Open(u32localAddr, (direction == RW.READ), dwBytes,
  860.                     ref pBuffer, bAutoInc, WDC_ADDR_MODE.WDC_MODE_32) !=
  861.                     (DWORD)wdc_err.WD_STATUS_SUCCESS)
  862.                     return;
  863.                 dmaList.AddItem(dmaBuff, plxChn, pBuffer);
  864.                 chnItem.Text = "Close";
  865.                 Log.TraceLog("DMA buffer for {" +
  866.                     dev.ToString(false) + "}, Channel " +
  867.                     (((DWORD)plxChn == 0) ? "0 " : "1 ") +
  868.                     "was opened successfully");
  869.                 return;
  870.             }
  871.             else
  872.             {
  873.                 DmaInfo dmaInfo = dmaList.GetItem(dev.Handle, plxChn);
  874.                 dmaInfo.Close();
  875.                 dmaList.Remove(dmaInfo);
  876.                 chnItem.Text = "Open";
  877.                 Log.TraceLog("DMA Channel " +
  878.                     (((DWORD)plxChn == 0) ? "0 " : "1 ") + "was closed. {" +
  879.                     dev.ToString(false) + "}");
  880.                 return;
  881.             }
  882.         }
  883.         private void menuOpenChn0_Click(object sender, System.EventArgs e)
  884.         {
  885.             menuOpenChn_Click(PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0, menuOpenChn0);
  886.         }
  887.         private void menuTransferChn0_Click(object sender, System.EventArgs e)
  888.         {
  889.             menuTransferChn_Click(PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0);
  890.         }
  891.         private void menuChannel1_Select(object sender, System.EventArgs e)
  892.         {
  893.             if (menuChannel1.Enabled == false)
  894.                 return;
  895.             PLX_MasterDevice dev = (PLX_MasterDevice)plxDevList.Get(
  896.                 lstBxDevices.SelectedIndex);
  897.             bool bIsOpen = dmaList.IsDMAOpen(dev.Handle,
  898.                 PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_1);
  899.             menuOpenChn1.Text = bIsOpen ? "Close" : "Open";
  900.             menuTransferChn1.Enabled = bIsOpen ? true : false;
  901.         }
  902.         private void menuOpenChn1_Click(object sender, System.EventArgs e)
  903.         {
  904.             menuOpenChn_Click(PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_1, menuOpenChn1);
  905.         }
  906.         private void menuTransferChn1_Click(object sender, System.EventArgs e)
  907.         {
  908.             menuTransferChn_Click(PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_1);
  909.         }
  910.         private void menuTransferChn_Click(PLX_DMA_CHANNEL chn)
  911.         {
  912.             PLX_MasterDevice dev = (PLX_MasterDevice)plxDevList.Get(
  913.                 lstBxDevices.SelectedIndex);
  914.             DmaBuffer dmaBuffer = dmaList.GetItem(dev.Handle, chn).DmaBuffer;
  915.             MarshalWdDma wdDmaMarshaler = new MarshalWdDma();
  916.             IntPtr pBuffer = ((WD_DMA)wdDmaMarshaler.MarshalNativeToManaged(dmaBuffer.pWdDma)).pUserAddr;
  917.             DMATransferForm transFrom = new DMATransferForm(dev, dmaBuffer, pBuffer,
  918.                 chn, dmaBuffer.IsRead);
  919.             m_PollingBuff = transFrom.GetInput();
  920.         }
  921.         byte[] m_PollingBuff;
  922.         /* Event Items*/
  923.         private void menuEvents_Select(object sender, System.EventArgs e)
  924.         {
  925.             if (menuEvents.Enabled == false)
  926.                 return;
  927.             PLX_Device dev = plxDevList.Get(lstBxDevices.SelectedIndex);
  928.             menuRegisterEvent.Text = dev.IsEventRegistered() ?
  929.                 "Unregister Events" : "Register Events";
  930.         }
  931.         private void menuRegisterEvent_Click(object sender, System.EventArgs e)
  932.         {
  933.             if (menuRegisterEvent.Text == "Register Events")
  934.             {
  935.                 plxDevList.Get(lstBxDevices.SelectedIndex).
  936.                     EventRegister(new USER_EVENT_CALLBACK(PLX_EventHandler));
  937.                 menuRegisterEvent.Text = "Unregister Events";
  938.             }
  939.             else
  940.             {
  941.                 plxDevList.Get(lstBxDevices.SelectedIndex).
  942.                     EventUnregister();
  943.                 menuRegisterEvent.Text = "Register Events";
  944.             }
  945.         }
  946.         /* EEPROM Items*/
  947.         private void menuEEpromRW_Click(object sender, System.EventArgs e)
  948.         {
  949.             PLX_Device dev =
  950.                 plxDevList.Get(lstBxDevices.SelectedIndex);
  951.             RegistersForm EForm = new RegistersForm(dev, ACTION_TYPE.EEPROM);
  952.             EForm.GetInput();
  953.         }
  954.         /* Configuration Space Items*/
  955.         private void menuCfgOffset_Click(object sender, System.EventArgs e)
  956.         {
  957.             PLX_Device dev =
  958.                 plxDevList.Get(lstBxDevices.SelectedIndex);
  959.             CfgTransfersForm cfgOffsetFrom = new CfgTransfersForm(dev);
  960.             cfgOffsetFrom.GetInput();
  961.         }
  962.         private void menuCfgReg_Click(object sender, System.EventArgs e)
  963.         {
  964.             PLX_Device dev =
  965.                 plxDevList.Get(lstBxDevices.SelectedIndex);
  966.             RegistersForm regForm = new RegistersForm(dev, ACTION_TYPE.CFG);
  967.             regForm.GetInput();
  968.         }
  969.         /*RunTime Registers Items*/
  970.         private void menuRTRegsRW_Click(object sender, System.EventArgs e)
  971.         {
  972.             PLX_Device dev =
  973.                 plxDevList.Get(lstBxDevices.SelectedIndex);
  974.             RegistersForm regForm = new RegistersForm(dev, ACTION_TYPE.RT);
  975.             regForm.GetInput();
  976.         }
  977.         private void btExit_Click(object sender, System.EventArgs e)
  978.         {
  979.             Close();
  980.             Dispose();
  981.         }
  982.         private void btLog_Click(object sender, System.EventArgs e)
  983.         {
  984.             txtLog.Clear();
  985.         }
  986.         public void LogFunc(string str)
  987.         {
  988.             if (txtLog != null)
  989.                 txtLog.Text += str + Environment.NewLine;
  990.         }
  991.         public void TraceLog(string str)
  992.         {
  993.             if (this.InvokeRequired)
  994.                 Invoke(new Log.TRACE_LOG(LogFunc), new object[] { str });
  995.             else
  996.                 LogFunc(str);
  997.         }
  998.         public void ErrLog(string str)
  999.         {
  1000.             if (this.InvokeRequired)
  1001.                 Invoke(new Log.ERR_LOG(LogFunc), new object[] { str });
  1002.             else
  1003.                 LogFunc(str);
  1004.         }
  1005.         private void Opentxt_Click(object sender, EventArgs e)
  1006.         {
  1007.             string fileFullName;
  1008.             string Textbuf = "";
  1009.             DWORD m_dwBytes;
  1010.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  1011.             {
  1012.                 fileFullName = openFileDialog1.FileName;
  1013.                 TxtBox_OpenFilePath.Text += openFileDialog1.FileName + Environment.NewLine;
  1014.                 using (StreamReader sr = new StreamReader(fileFullName))
  1015.                 {
  1016.                     string[] subStr = sr.ReadLine().Split(new char[] { '=' }, 2);
  1017.                     txtLog.Text += subStr[0] + " is ";
  1018.                     m_dwBytes = Convert.ToUInt32(subStr[1]) * 4;
  1019.                     txtLog.Text += subStr[1] + Environment.NewLine;
  1020.                     m_DataTransBuffer = new UInt32[m_dwBytes / 4];
  1021.                     for (UInt16 i = 0; i < m_dwBytes / 4; i++)
  1022.                     {
  1023.                         if (sr.Peek() >= 0)
  1024.                         {
  1025.                             string temp = "";
  1026.                             temp = sr.ReadLine();
  1027.                             m_DataTransBuffer[i] = Convert.ToUInt32(temp, 16);
  1028.                             Textbuf += temp;// + " ";
  1029.                             txtLog.Text += temp + Environment.NewLine;
  1030.                         }
  1031.                     }
  1032.                     //txtLog.Text += Textbuf;
  1033.                 }
  1034.             }
  1035.         }
  1036.         private void button_SaveData_Click(object sender, EventArgs e)
  1037.         {
  1038.             string fileFullName = "";
  1039.             Int32 m_dwBytes=0;
  1040.             m_dwBytes = m_PollingBuff.GetLength(0);
  1041.             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  1042.             {
  1043.                 fileFullName = saveFileDialog1.FileName;
  1044.                 //Stream fileFullName = saveFileDialog1.OpenFile();
  1045.                 StreamWriter writer = new StreamWriter(fileFullName);
  1046.                 writer.WriteLine("Total Data Length is  " + m_dwBytes);
  1047.                 for (UInt16 i = 0; i < m_dwBytes; ) 
  1048.                 {
  1049.                     writer.WriteLine(m_PollingBuff[i++].ToString("X")+" "+m_PollingBuff[i++].ToString("X")
  1050.                         + " " + m_PollingBuff[i++].ToString("X") +" "+ m_PollingBuff[i++].ToString("X"));
  1051.                 }
  1052.                     writer.Close();
  1053.             }
  1054.         }
  1055.         private void MyDMA_Click(object sender, EventArgs e)
  1056.        { 
  1057.            
  1058.             PLX_MasterDevice dev = (PLX_MasterDevice)plxDevList.Get(
  1059.     lstBxDevices.SelectedIndex);
  1060.             bool bIsOpen = dmaList.IsDMAOpen(dev.Handle,PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0);
  1061.             menuOpenChn0.Text = bIsOpen ? "Close" : "Open";
  1062.             menuTransferChn0.Enabled = bIsOpen ? true : false;
  1063.             DMATransfer start = new DMATransfer(dev, bIsOpen);
  1064.             start.ShowDialog();
  1065.         }
  1066.  
  1067.     }
  1068.     public class diag_lib
  1069.     {
  1070.         public static string PadBuffer(string str, uint fromIndex, uint toIndex)
  1071.         {
  1072.             for (uint i = fromIndex; i < toIndex; ++i)
  1073.                 str += "0";
  1074.             return str;
  1075.         }
  1076.         public static string DisplayHexBuffer(object[] obj, DWORD dwBuffSize,
  1077.              WDC_ADDR_MODE mode)
  1078.         {
  1079.             string display = "";
  1080.             switch (mode)
  1081.             {
  1082.                 case WDC_ADDR_MODE.WDC_MODE_8:
  1083.                     {
  1084.                         BYTE[] buff = (BYTE[])obj[0];
  1085.                         for (uint i = 0; i < dwBuffSize; ++i)
  1086.                             display = string.Concat(display,
  1087.                                 buff[i].ToString("X2"), " ");
  1088.                         break;
  1089.                     }
  1090.                 case WDC_ADDR_MODE.WDC_MODE_16:
  1091.                     {
  1092.                         WORD[] buff = (WORD[])obj[0];
  1093.                         for (int i = 0; i < dwBuffSize / 2; ++i)
  1094.                             display = string.Concat(display,
  1095.                                 buff[i].ToString("X4"), " ");
  1096.                         break;
  1097.                     }
  1098.                 case WDC_ADDR_MODE.WDC_MODE_32:
  1099.                     {
  1100.                         UINT32[] buff = (UINT32[])obj[0];
  1101.                         for (int i = 0; i < dwBuffSize / 4; ++i)
  1102.                             display = string.Concat(display,
  1103.                                 buff[i].ToString("X8"), " ");
  1104.                         break;
  1105.                     }
  1106.                 case WDC_ADDR_MODE.WDC_MODE_64:
  1107.                     {
  1108.                         UINT64[] buff = (UINT64[])obj[0];
  1109.                         for (int i = 0; i < dwBuffSize / 8; ++i)
  1110.                             display = string.Concat(display,
  1111.                                 buff[i].ToString("X16"), " ");
  1112.                         break;
  1113.                     }
  1114.             }
  1115.             return display;
  1116.         }
  1117.         public static string DisplayHexBuffer(byte[] buff, uint dwBuffSize)
  1118.         {
  1119.             return DisplayHexBuffer(new object[] { buff }, dwBuffSize,
  1120.                 WDC_ADDR_MODE.WDC_MODE_8);
  1121.         }
  1122.     };
  1123.     /*holds the information relevant for a dma channel transfer*/
  1124.     public class DmaInfo
  1125.     {
  1126.         private DmaBuffer m_dmaBuffer;
  1127.         private PLX_DMA_CHANNEL m_plxChannel;
  1128.         private IntPtr m_pBuffer;
  1129.         public DmaInfo(DmaBuffer dmaBuffer, PLX_DMA_CHANNEL plxChn, IntPtr pBuffer)
  1130.         {
  1131.             m_dmaBuffer = dmaBuffer;
  1132.             m_plxChannel = plxChn;
  1133.             m_pBuffer = pBuffer;
  1134.         }
  1135.         public DmaBuffer DmaBuffer
  1136.         {
  1137.             get
  1138.             {
  1139.                 return m_dmaBuffer;
  1140.             }
  1141.             set
  1142.             {
  1143.                 m_dmaBuffer = value;
  1144.             }
  1145.         }
  1146.         public WDC_DEVICE_HANDLE DeviceHandle
  1147.         {
  1148.             get
  1149.             {
  1150.                 return m_dmaBuffer.DeviceHandle;
  1151.             }
  1152.         }
  1153.         public PLX_DMA_CHANNEL DmaChannel
  1154.         {
  1155.             get
  1156.             {
  1157.                 return m_plxChannel;
  1158.             }
  1159.         }
  1160.         public IntPtr Buffer
  1161.         {
  1162.             get
  1163.             {
  1164.                 return m_pBuffer;
  1165.             }
  1166.         }
  1167.         public void Close()
  1168.         {
  1169.             if (m_dmaBuffer is DmaBufferSG && m_pBuffer != IntPtr.Zero)
  1170.                 Marshal.FreeHGlobal(m_pBuffer);
  1171.             m_dmaBuffer.Close();
  1172.         }
  1173.         public void Dispose()
  1174.         {
  1175.             Close();
  1176.         }
  1177.     }
  1178.     public class DmaInfoList : ArrayList
  1179.     {
  1180.         public void AddItem(DmaBuffer dmaBuffer, PLX_DMA_CHANNEL plxChn, IntPtr pBuffer)
  1181.         {
  1182.             DmaInfo dmaInfo = new DmaInfo(dmaBuffer, plxChn, pBuffer);
  1183.             this.Add(dmaInfo);
  1184.         }
  1185.         public DmaInfo GetItem(WDC_DEVICE_HANDLE hDev, PLX_DMA_CHANNEL plxChn)
  1186.         {
  1187.             Exception excp = new Exception();
  1188.             foreach (DmaInfo dmaInfo in this)
  1189.                 if (dmaInfo.DeviceHandle == hDev && dmaInfo.DmaChannel == plxChn)
  1190.                     return dmaInfo;
  1191.             throw excp;
  1192.         }
  1193.         public DmaInfoList GetItems(WDC_DEVICE_HANDLE hDev)
  1194.         {
  1195.             DmaInfoList dmaDevList = new DmaInfoList();
  1196.             foreach (DmaInfo dmaInfo in this)
  1197.                 if (dmaInfo.DeviceHandle == hDev)
  1198.                     dmaDevList.Add(dmaInfo);
  1199.             return dmaDevList;
  1200.         }
  1201.         public bool IsDMAOpen(WDC_DEVICE_HANDLE hDev, PLX_DMA_CHANNEL plxChn)
  1202.         {
  1203.             try
  1204.             {
  1205.                 return (GetItem(hDev, plxChn) != null);
  1206.             }
  1207.             catch
  1208.             {
  1209.                 return false;
  1210.             }
  1211.         }
  1212.         public void Dispose()
  1213.         {
  1214.             foreach (DmaInfo dmaInfo in this)
  1215.                 dmaInfo.Dispose();
  1216.             this.Clear();
  1217.         }
  1218.     }
  1219. }