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

驱动编程

开发平台:

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 System.IO;
  8. using Jungo.wdapi_dotnet;
  9. using Jungo.plx_lib;
  10. using wdc_err = Jungo.wdapi_dotnet.WD_ERROR_CODES;
  11. using DWORD = System.UInt32;
  12. using BYTE = System.Byte;
  13. using BOOL = System.Boolean;
  14. namespace Jungo.PLX_Sample
  15. {
  16.     public partial class DMATransfer : System.Windows.Forms.Form
  17.     {
  18.         private DmaInfoList dmaList;
  19.         private Exception m_excp;
  20.         private PLX_MasterDevice m_masterDevice;
  21.         private DmaBuffer m_dmaBuffer;
  22.         private PLX_DMA_CHANNEL m_dmaChannel;
  23.         private COMPLETION m_compMethod;
  24.         private BOOL m_direction;
  25.         private DWORD m_dwBytes=0;
  26.         private IntPtr m_pData;
  27.         private BYTE[] m_buff;
  28.         private UInt32[] m_DataTransBuffer;
  29.         private BOOL m_bAutoInc;
  30.         private ALLOCATION_TYPE m_wAllocType;
  31.         private uint m_u32LocalAddr;
  32.         private System.Windows.Forms.ComboBox cmboCompMethod;
  33.         public DMATransfer(PLX_MasterDevice dev, bool bIsOpen)
  34.         {
  35.             InitializeComponent();
  36.             m_masterDevice = dev;
  37.             //m_dmaBuffer = dmaBuffer;
  38.             //m_pData = pUserBuffer;
  39.             dmaList = new DmaInfoList();
  40.             chkBoxInc.Checked = true;
  41.             txtLocalAddr.Text = "00";
  42.             m_direction = true;
  43.             btTRANS.Text = m_direction ? "READ" : "WRITE";
  44.             txtBuffSize.Enabled = true;
  45.             
  46.             
  47.             //bool bIsOpen = dmaList.IsDMAOpen(m_masterDevice.Handle, PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0);
  48.             btSubmit.Text = bIsOpen ? "Close" : "Open";
  49.             btTRANS.Enabled = bIsOpen ? true: false;
  50.         }
  51.         private void PLX_DMAIntHandler(PLX_MasterDevice dev, IntPtr pWdDma)
  52.         {
  53.             MarshalWdDma m_wdDmaMarshaler = new MarshalWdDma();
  54.             WD_DMA wdDma = (WD_DMA)m_wdDmaMarshaler.MarshalNativeToManaged(pWdDma);
  55.             BOOL bIsRead = (wdDma.dwOptions & (DWORD)WD_DMA_OPTIONS.DMA_FROM_DEVICE) != 0;
  56.             byte[] data = new byte[wdDma.dwBytes];
  57.             Marshal.Copy(wdDma.pUserAddr, data, 0, (int)wdDma.dwBytes);
  58.             Log.TraceLog("interrupt for device {" + dev.ToString(false) +
  59.                 "} received! " + Environment.NewLine +
  60.                 (bIsRead ? "read: " : "wrote: ") +
  61.                 diag_lib.DisplayHexBuffer(data, wdDma.dwBytes));
  62.         }
  63.         private void TranslateInput()
  64.         {
  65.             
  66.             DWORD dwStatus = 0;
  67.             BOOL bIsRead = m_direction;
  68.             //m_dwBytes = m_dmaBuffer.BuffSize;
  69.             if (COMPLETION.INTERRUPT == m_compMethod)
  70.             {
  71.                 if (m_masterDevice.IsEnabledInt())
  72.                     m_masterDevice.DisableInterrupts();
  73.                 else
  74.                     m_masterDevice.EnableInterrupts(new
  75.                         USER_INTERRUPT_MASTER_CALLBACK(PLX_DMAIntHandler), m_dmaChannel);
  76.             }
  77.             DmaBuffer m_dmaBuffer = dmaList.GetItem(m_masterDevice.Handle, PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0).DmaBuffer;
  78.             MarshalWdDma wdDmaMarshaler = new MarshalWdDma();
  79.             IntPtr m_pData = ((WD_DMA)wdDmaMarshaler.MarshalNativeToManaged(m_dmaBuffer.pWdDma)).pUserAddr;
  80.             m_buff = new byte[m_dwBytes];
  81.             if (!bIsRead)
  82.             {
  83.                 if (m_dwBytes == 0)
  84.                 {
  85.                     m_excp = new Exception("Please open the data file to " +
  86.                         "written");
  87.                     throw m_excp;
  88.                 }
  89.                 m_excp = new Exception("The data you've entered is invalid. " +
  90.                     "please try again (hex)");
  91.                 //string str = diag_lib.PadBuffer(textBox1.Text,
  92.                 //    (DWORD)textBox1.Text.Length, (DWORD)2 * m_dwBytes);
  93.                 //for (int i = 0; i < m_dwBytes; ++i)
  94.                 //    m_buff[i] = Convert.ToByte(str.Substring(2 * i, 2), 16);
  95.  
  96.                 UInt16 i = 0;
  97.                 m_buff = new byte[m_dwBytes];
  98.                 foreach (int number in m_DataTransBuffer)
  99.                 {
  100.                     for (UInt16 j = 0; j < 4; j++)
  101.                     {
  102.                         m_buff[i] = (BYTE)(m_DataTransBuffer[(BYTE)(i / 4)] >> (i * 8));
  103.                         i++;
  104.                     }
  105.                 }
  106.             
  107.                 Marshal.Copy (m_buff,0, m_pData, (int)m_dwBytes);
  108.                 }
  109.             dwStatus = m_masterDevice.DMATransfer(PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0, m_dmaBuffer,
  110.                 (m_compMethod == COMPLETION.INTERRUPT));
  111.             if (dwStatus == (DWORD)wdc_err.WD_STATUS_SUCCESS)
  112.             {
  113.                 if (m_compMethod != COMPLETION.POLLING)
  114.                 {
  115.                     DialogResult = DialogResult.OK;
  116.                     return;
  117.                 }
  118.                 else
  119.                 {
  120.                     if (bIsRead)
  121.                         Marshal.Copy(m_pData, m_buff, 0, (int)m_dwBytes);
  122.                     bt_Read_Click();
  123.                 }
  124.             }
  125.             TraceLog(bIsRead, (wdc_err)dwStatus);
  126.         }
  127.         private void TraceLog(BOOL bIsRead, wdc_err status)
  128.         {
  129.             string sData = "";
  130.             string sInfo = "";
  131.             if (status == wdc_err.WD_STATUS_SUCCESS)
  132.             {
  133.                 sData = (bIsRead ? "R: " : "W: ")
  134.                     + diag_lib.DisplayHexBuffer(m_buff, m_dwBytes);
  135.                 sInfo = " (" + m_masterDevice.ToString(false) + ")";
  136.                 Log.TraceLog("DMATransferForm: " + sData + sInfo);
  137.             }
  138.             else
  139.             {
  140.                 sData = "failed to complete the DMA transfer. status 0x" +
  141.                     status.ToString("X") + ": " + utils.Stat2Str((DWORD)status);
  142.                 sInfo = "(" + m_masterDevice.ToString(false) + ")";
  143.                 Log.ErrLog("DMATransferForm: " + sData + sInfo);
  144.             }
  145.             textBox1.Text += sData + Environment.NewLine;
  146.         }
  147.         private void button1_Click(object sender, EventArgs e)
  148.         {
  149.             string fileFullName;
  150.             string Textbuf = "";
  151.             
  152.             if (openFileDialog2.ShowDialog() == DialogResult.OK)
  153.             {
  154.                 fileFullName = openFileDialog2.FileName;
  155.                 TxtBox_OpenFilePath.Text += openFileDialog2.FileName + Environment.NewLine;
  156.                 using (StreamReader sr = new StreamReader(fileFullName))
  157.                 {
  158.                     string[] subStr = sr.ReadLine().Split(new char[] { '=' }, 2);
  159.                     textBox1.Text += subStr[0] + " is ";
  160.                     m_dwBytes = Convert.ToUInt32(subStr[1]) * 4;
  161.                     textBox1.Text += subStr[1] + Environment.NewLine;
  162.                     m_DataTransBuffer = new UInt32[m_dwBytes / 4];
  163.                     for (UInt16 i = 0; i < m_dwBytes / 4; i++)
  164.                     {
  165.                         if (sr.Peek() >= 0)
  166.                         {
  167.                             string temp = "";
  168.                             temp = sr.ReadLine();
  169.                             m_DataTransBuffer[i] = Convert.ToUInt32(temp, 16);
  170.                             Textbuf += temp;// + " ";
  171.                             textBox1.Text += temp + Environment.NewLine;
  172.                         }
  173.                     }
  174.                     //txtLog.Text += Textbuf;
  175.                 }
  176.             }
  177.         }
  178.         private void OpenDma_Input() 
  179.         {
  180.             m_bAutoInc = chkBoxInc.Checked;
  181.             m_wAllocType = (Scatter.Checked) ? ALLOCATION_TYPE.SG : ALLOCATION_TYPE.CONTIG;
  182.             m_direction = (RW.READ == ((Read.Checked) ? RW.READ : RW.WRITE)) ? true : false;
  183.             m_compMethod = (Interrupt.Checked) ? COMPLETION.INTERRUPT : COMPLETION.POLLING;
  184.             m_excp = new Exception("Enter local DMA address. " + "Entered value should be a hex number");
  185.             m_u32LocalAddr = (DWORD)Convert.ToInt32(txtLocalAddr.Text, 16);
  186.             if (m_dwBytes == 0 && m_direction==false)
  187.             {
  188.                 m_excp = new Exception("Please assign the data file.");
  189.                 throw m_excp;
  190.             }
  191.             if (m_direction == true)
  192.             {
  193.                 m_dwBytes = (DWORD)Convert.ToInt32(txtBuffSize.Text, 16); 
  194.             }
  195.             if (m_dwBytes == 0 && m_direction == true)
  196.             {
  197.                 m_excp = new Exception("Please set the Read data buffer size .");
  198.                 throw m_excp;
  199.             }
  200.         }
  201.         private void btSubmit_Click(object sender, EventArgs e)
  202.         {
  203.             IntPtr pBuffer = IntPtr.Zero;
  204.             DmaBuffer dmaBuff = null;
  205.             if (btSubmit.Text == "Open")
  206.             {
  207.                 try
  208.                 {
  209.                     OpenDma_Input();
  210.                     if (m_wAllocType == ALLOCATION_TYPE.SG)
  211.                     {
  212.                         pBuffer = Marshal.AllocHGlobal((int)m_dwBytes);
  213.                         if (pBuffer == IntPtr.Zero)
  214.                         {
  215.                             Log.ErrLog("PLX_Sample: Failed to allocate buffer for " +
  216.                                 "Scatter-Gather DMA");
  217.                             return;
  218.                         }
  219.                         dmaBuff = new DmaBufferSG(m_masterDevice);
  220.                         textBox1.Text += "Set allocation type to scatter " + Environment.NewLine;
  221.                     }
  222.                     else
  223.                     {
  224.                         dmaBuff = new DmaBufferContig(m_masterDevice);
  225.                         textBox1.Text += "Set allocation type to contiguous " + Environment.NewLine;
  226.                     }
  227.                     if (dmaBuff.Open(m_u32LocalAddr, m_direction, m_dwBytes, ref pBuffer, m_bAutoInc, WDC_ADDR_MODE.WDC_MODE_32) != (DWORD)wdc_err.WD_STATUS_SUCCESS)
  228.                         return;
  229.                     m_excp = new Exception("What's wrong!?");
  230.                     dmaList.AddItem(dmaBuff, PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0, pBuffer);
  231.                     btSubmit.Text = "Close";
  232.                     Log.TraceLog("DMA buffer for {" +
  233.                         m_masterDevice.ToString(false) + "}, Channel 0" + "was opened successfully");
  234.                     textBox1.Text += "DMA buffer for {" + m_masterDevice.ToString(false) + "}, Channel 0" + "was opened successfully" + Environment.NewLine;
  235.                     //btWrite.Enabled = true;
  236.                     EnableInput();
  237.                     return;
  238.                 }
  239.                 catch
  240.                 {
  241.                     MessageBox.Show(m_excp.Message, "Input Entry Error",
  242.                         MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  243.                     //DialogResult = DialogResult.Retry;
  244.                 }
  245.             }
  246.         else 
  247.             {
  248.                 DmaInfo dmaInfo=null; 
  249.                 dmaInfo = dmaList.GetItem(m_masterDevice.Handle, PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0);
  250.                 dmaInfo.Close();
  251.                 dmaList.Remove(dmaInfo);
  252.                 btSubmit.Text = "Open";
  253.                 Log.TraceLog("DMA Channel " +
  254.                     (((DWORD)PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0 == 0) ? "0 " : "1 ") + "was closed. {" +
  255.                     m_masterDevice.ToString(false) + "}");
  256.                 textBox1.Text = "DMA Channel " +
  257.                     (((DWORD)PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0 == 0) ? "0 " : "1 ") + "was closed. {" +
  258.                     m_masterDevice.ToString(false) + "}";
  259.                 //btWrite.Enabled = false;
  260.                 EnableInput();
  261.                 return; 
  262.             }
  263.         }
  264.         private void EnableInput()
  265.         {
  266.             bool bIsOpen = dmaList.IsDMAOpen(m_masterDevice.Handle,PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0);
  267.             btTRANS.Enabled = bIsOpen ?true:false;
  268.             Scatter.Enabled=bIsOpen ? false:true;
  269.             Contig.Enabled=bIsOpen ? false:true;
  270.             Read.Enabled=bIsOpen ? false:true;
  271.             Writer.Enabled = bIsOpen ? false : true;
  272.         }
  273.         private void btWrite_Click(object sender, EventArgs e)
  274.         {
  275.             try
  276.             {
  277.                 TranslateInput();
  278.             }
  279.             catch
  280.             {
  281.                 MessageBox.Show(m_excp.Message, "Input Entry Error",
  282.                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  283.                 //DialogResult = DialogResult.Retry;
  284.             }
  285.         }
  286.         private void bt_Read_Click()
  287.         {
  288.             string fileFullName = "";
  289.             Int32 m_dwBytes = 0;
  290.             m_dwBytes = m_buff.GetLength(0);
  291.             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  292.             {
  293.                 fileFullName = saveFileDialog1.FileName;
  294.                 //Stream fileFullName = saveFileDialog1.OpenFile();
  295.                 StreamWriter writer = new StreamWriter(fileFullName);
  296.                 writer.WriteLine("Total Data Length is  " + m_dwBytes);
  297.                 for (UInt16 i = 0; i < m_dwBytes; )
  298.                 {
  299.                     writer.WriteLine(m_buff[i++].ToString("X") + " " + m_buff[i++].ToString("X")
  300.                         + " " + m_buff[i++].ToString("X") + " " + m_buff[i++].ToString("X"));
  301.                 }
  302.                 writer.Close();
  303.             }
  304.         }
  305.         private void Writer_CheckedChanged(object sender, EventArgs e)
  306.         {
  307.             
  308.             m_direction = (RW.READ == ((Read.Checked) ? RW.READ : RW.WRITE)) ? true : false;
  309.             btTRANS.Text = m_direction ? "READ" : "WRITE";
  310.             txtBuffSize.Enabled = m_direction ? true : false;
  311.         }
  312.        
  313.         
  314.     }
  315. }