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

驱动编程

开发平台:

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.Runtime.InteropServices;
  7. using Jungo.wdapi_dotnet;
  8. using Jungo.plx_lib;
  9. using wdc_err = Jungo.wdapi_dotnet.WD_ERROR_CODES;
  10. using DWORD = System.UInt32;
  11. using BYTE = System.Byte;
  12. using BOOL = System.Boolean;
  13. namespace Jungo.PLX_Sample
  14. {
  15.     public class DMATransferForm : System.Windows.Forms.Form
  16.     {
  17.         private Exception m_excp;
  18.         private PLX_MasterDevice m_masterDevice;
  19.         private DmaBuffer m_dmaBuffer;
  20.         private PLX_DMA_CHANNEL m_dmaChannel;
  21.         private COMPLETION m_compMethod;
  22.         private BOOL m_direction;
  23.         private DWORD m_dwBytes;
  24.         private IntPtr m_pData;
  25.         private BYTE[] m_buff;
  26.         private System.Windows.Forms.Label lblComp;
  27.         private System.Windows.Forms.ComboBox cmboCompMethod;
  28.         private System.Windows.Forms.Button btExit;
  29.         private System.Windows.Forms.Button btReadWrite;
  30.         private System.Windows.Forms.Label lblData;
  31.         private System.Windows.Forms.TextBox txtData;
  32.         private System.Windows.Forms.Button btLog;
  33.         private System.ComponentModel.Container components = null;
  34.         private void PLX_DMAIntHandler(PLX_MasterDevice dev, IntPtr pWdDma)
  35.         {
  36.             MarshalWdDma m_wdDmaMarshaler = new MarshalWdDma();
  37.             WD_DMA wdDma = (WD_DMA)m_wdDmaMarshaler.MarshalNativeToManaged(pWdDma);
  38.             BOOL bIsRead = (wdDma.dwOptions & (DWORD)WD_DMA_OPTIONS.DMA_FROM_DEVICE) != 0;
  39.             byte[] data = new byte[wdDma.dwBytes];
  40.             Marshal.Copy(wdDma.pUserAddr, data, 0, (int)wdDma.dwBytes);
  41.             Log.TraceLog("interrupt for device {" + dev.ToString(false) +
  42.                 "} received! " + Environment.NewLine +
  43.                 (bIsRead ? "read: " : "wrote: ") +
  44.                 diag_lib.DisplayHexBuffer(data, wdDma.dwBytes));
  45.         }
  46.         public DMATransferForm(PLX_MasterDevice dev, DmaBuffer dmaBuffer, IntPtr pUserBuffer,
  47.             PLX_DMA_CHANNEL dmaChannel, BOOL direction)
  48.         {
  49.             InitializeComponent();
  50.             m_masterDevice = dev;
  51.             m_dmaBuffer = dmaBuffer;
  52.             m_pData = pUserBuffer;
  53.             m_dmaChannel = dmaChannel;
  54.             m_direction = direction; 
  55.             txtData.ReadOnly  = m_direction; 
  56.             cmboCompMethod.Items.AddRange(new object[]{"Interrupt", "Polling"});
  57.             cmboCompMethod.SelectedIndex = 1;
  58.             btReadWrite.Text = (m_direction)? "Read" : "Write"; 
  59.                        
  60.         }
  61.         protected override void Dispose( bool disposing )
  62.         {
  63.             if( disposing )
  64.             {
  65.                 if(components != null)
  66.                 {
  67.                     components.Dispose();
  68.                 }
  69.             }
  70.             base.Dispose( disposing );
  71.         }
  72. #region Windows Form Designer generated code
  73.         private void InitializeComponent()
  74.         {
  75.             this.lblComp = new System.Windows.Forms.Label();
  76.             this.cmboCompMethod = new System.Windows.Forms.ComboBox();
  77.             this.btExit = new System.Windows.Forms.Button();
  78.             this.btReadWrite = new System.Windows.Forms.Button();
  79.             this.lblData = new System.Windows.Forms.Label();
  80.             this.txtData = new System.Windows.Forms.TextBox();
  81.             this.btLog = new System.Windows.Forms.Button();
  82.             this.SuspendLayout();
  83.             // 
  84.             // lblComp
  85.             // 
  86.             this.lblComp.Location = new System.Drawing.Point(29, 9);
  87.             this.lblComp.Name = "lblComp";
  88.             this.lblComp.Size = new System.Drawing.Size(144, 24);
  89.             this.lblComp.TabIndex = 2;
  90.             this.lblComp.Text = "Completion Method";
  91.             // 
  92.             // cmboCompMethod
  93.             // 
  94.             this.cmboCompMethod.Location = new System.Drawing.Point(29, 34);
  95.             this.cmboCompMethod.Name = "cmboCompMethod";
  96.             this.cmboCompMethod.Size = new System.Drawing.Size(145, 20);
  97.             this.cmboCompMethod.TabIndex = 3;
  98.             this.cmboCompMethod.SelectedIndexChanged += new System.EventHandler(this.cmboCompMethod_SelectedIndexChanged);
  99.             // 
  100.             // btExit
  101.             // 
  102.             this.btExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  103.             this.btExit.Location = new System.Drawing.Point(278, 207);
  104.             this.btExit.Name = "btExit";
  105.             this.btExit.Size = new System.Drawing.Size(90, 25);
  106.             this.btExit.TabIndex = 10;
  107.             this.btExit.Text = "Exit";
  108.             // 
  109.             // btReadWrite
  110.             // 
  111.             this.btReadWrite.Location = new System.Drawing.Point(29, 207);
  112.             this.btReadWrite.Name = "btReadWrite";
  113.             this.btReadWrite.Size = new System.Drawing.Size(90, 25);
  114.             this.btReadWrite.TabIndex = 8;
  115.             this.btReadWrite.Text = "Read";
  116.             this.btReadWrite.Click += new System.EventHandler(this.btReadWrite_Click);
  117.             // 
  118.             // lblData
  119.             // 
  120.             this.lblData.Location = new System.Drawing.Point(29, 78);
  121.             this.lblData.Name = "lblData";
  122.             this.lblData.Size = new System.Drawing.Size(120, 24);
  123.             this.lblData.TabIndex = 7;
  124.             this.lblData.Text = "Data:";
  125.             // 
  126.             // txtData
  127.             // 
  128.             this.txtData.Location = new System.Drawing.Point(29, 103);
  129.             this.txtData.Multiline = true;
  130.             this.txtData.Name = "txtData";
  131.             this.txtData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
  132.             this.txtData.Size = new System.Drawing.Size(345, 87);
  133.             this.txtData.TabIndex = 6;
  134.             this.txtData.Click += new System.EventHandler(this.txtData_Clicked);
  135.             // 
  136.             // btLog
  137.             // 
  138.             this.btLog.Location = new System.Drawing.Point(284, 29);
  139.             this.btLog.Name = "btLog";
  140.             this.btLog.Size = new System.Drawing.Size(90, 25);
  141.             this.btLog.TabIndex = 9;
  142.             this.btLog.Text = "Clear Log";
  143.             this.btLog.Click += new System.EventHandler(this.btLog_Click);
  144.             // 
  145.             // DMATransferForm
  146.             // 
  147.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  148.             this.CancelButton = this.btExit;
  149.             this.ClientSize = new System.Drawing.Size(427, 258);
  150.             this.Controls.Add(this.btLog);
  151.             this.Controls.Add(this.btExit);
  152.             this.Controls.Add(this.btReadWrite);
  153.             this.Controls.Add(this.lblData);
  154.             this.Controls.Add(this.txtData);
  155.             this.Controls.Add(this.lblComp);
  156.             this.Controls.Add(this.cmboCompMethod);
  157.             this.Name = "DMATransferForm";
  158.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  159.             this.Text = "DMATransferForm";
  160.             this.ResumeLayout(false);
  161.             this.PerformLayout();
  162.         }
  163. #endregion
  164.         public byte[] GetInput()
  165.         {
  166.             DialogResult result = DialogResult.Retry;
  167.             while((result = ShowDialog()) == DialogResult.Retry);
  168.             return m_buff;
  169.         }
  170.         private void btReadWrite_Click(object sender, System.EventArgs e)
  171.         {
  172.             DialogResult = DialogResult.None;
  173.             try
  174.             {
  175.                 TranslateInput();
  176.             }
  177.             catch
  178.             {
  179.                 MessageBox.Show(m_excp.Message,"Input Entry Error",
  180.                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  181.                 DialogResult = DialogResult.Retry;
  182.             }
  183.         }
  184.         private void TranslateInput()
  185.         {
  186.             DWORD dwStatus = 0;
  187.             BOOL bIsRead = m_direction;
  188.             m_dwBytes = m_dmaBuffer.BuffSize;
  189.             m_excp = new Exception("Select the Completion Method");
  190.             if((uint)cmboCompMethod.SelectedIndex == 0xffffffff)
  191.                 throw m_excp;
  192.             m_compMethod = (cmboCompMethod.SelectedIndex == 0)?
  193.                 COMPLETION.INTERRUPT : COMPLETION.POLLING;
  194.             m_buff = new byte[m_dwBytes];
  195.             if(!bIsRead)
  196.             {
  197.                 if(txtData.Text == "")
  198.                 {
  199.                     m_excp = new Exception("Please enter the data to be " +
  200.                         "written");
  201.                     throw m_excp;
  202.                 }
  203.                 m_excp = new Exception("The data you've entered is invalid. " +
  204.                     "please try again (hex)");
  205.                 string str = diag_lib.PadBuffer(txtData.Text,
  206.                     (DWORD)txtData.Text.Length,(DWORD)2*m_dwBytes);
  207.                 for(int i=0; i<m_dwBytes; ++i)
  208.                     m_buff[i] = Convert.ToByte(str.Substring(2*i,2),16);
  209.                 Marshal.Copy(m_buff, 0 , m_pData, (int)m_dwBytes);
  210.             }
  211.             dwStatus = m_masterDevice.DMATransfer(m_dmaChannel, m_dmaBuffer,
  212.                 (m_compMethod == COMPLETION.INTERRUPT));
  213.             if(dwStatus == (DWORD)wdc_err.WD_STATUS_SUCCESS)
  214.             {
  215.                 if(m_compMethod != COMPLETION.POLLING)
  216.                 {
  217.                     DialogResult = DialogResult.OK;
  218.                     return;
  219.                 }
  220.                 else
  221.                 {
  222.                     if(bIsRead)
  223.                         Marshal.Copy(m_pData, m_buff, 0, (int)m_dwBytes);
  224.                 }                                                                                                
  225.             }
  226.             TraceLog(bIsRead, (wdc_err)dwStatus);
  227.         }
  228.         private void txtData_Clicked(object sender, System.EventArgs e)
  229.         {
  230.             if(txtData.ReadOnly == false)
  231.                 txtData.Clear();
  232.         }
  233.         private void btLog_Click(object sender, System.EventArgs e)
  234.         {
  235.             txtData.Clear();
  236.         }        
  237.         private void TraceLog(BOOL bIsRead, wdc_err status)
  238.         {
  239.             string sData = "";
  240.             string sInfo = "";
  241.             if(status == wdc_err.WD_STATUS_SUCCESS)
  242.             {
  243.                 sData = (bIsRead? "R: " : "W: ")
  244.                     + diag_lib.DisplayHexBuffer(m_buff, m_dwBytes);
  245.                 sInfo = " (" + m_masterDevice.ToString(false) + ")";
  246.                 Log.TraceLog("DMATransferForm: " + sData + sInfo);
  247.             }
  248.             else 
  249.             {
  250.                 sData = "failed to complete the DMA transfer. status 0x" +
  251.                     status.ToString("X") + ": " + utils.Stat2Str((DWORD)status);
  252.                                 sInfo = "(" + m_masterDevice.ToString(false) + ")";
  253.                 Log.ErrLog("DMATransferForm: " + sData + sInfo);
  254.             }
  255.             txtData.Text += sData + Environment.NewLine;
  256.         }
  257.        //为了让中断在设置的时候就起作用
  258.         
  259.         private void cmboCompMethod_SelectedIndexChanged(object sender, EventArgs e)
  260.         {
  261.             m_compMethod = (cmboCompMethod.SelectedIndex == 0) ?
  262.     COMPLETION.INTERRUPT : COMPLETION.POLLING;
  263.             if (COMPLETION.INTERRUPT == m_compMethod)
  264.             {
  265.                 if (m_masterDevice.IsEnabledInt())
  266.                     m_masterDevice.DisableInterrupts();
  267.                 else
  268.                     m_masterDevice.EnableInterrupts(new
  269.                         USER_INTERRUPT_MASTER_CALLBACK(PLX_DMAIntHandler), m_dmaChannel);
  270.             }
  271.         }
  272.         private void button_SaveData_Click(object sender, EventArgs e)
  273.         {
  274.         }
  275.     }
  276. }