My_DMATransfer.cs
上传用户:huajielb
上传日期:2022-07-29
资源大小:626k
文件大小:14k
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.IO;
- using Jungo.wdapi_dotnet;
- using Jungo.plx_lib;
- using wdc_err = Jungo.wdapi_dotnet.WD_ERROR_CODES;
- using DWORD = System.UInt32;
- using BYTE = System.Byte;
- using BOOL = System.Boolean;
- namespace Jungo.PLX_Sample
- {
- public partial class DMATransfer : System.Windows.Forms.Form
- {
- private DmaInfoList dmaList;
- private Exception m_excp;
- private PLX_MasterDevice m_masterDevice;
- private DmaBuffer m_dmaBuffer;
- private PLX_DMA_CHANNEL m_dmaChannel;
- private COMPLETION m_compMethod;
- private BOOL m_direction;
- private DWORD m_dwBytes=0;
- private IntPtr m_pData;
- private BYTE[] m_buff;
- private UInt32[] m_DataTransBuffer;
- private BOOL m_bAutoInc;
- private ALLOCATION_TYPE m_wAllocType;
- private uint m_u32LocalAddr;
- private System.Windows.Forms.ComboBox cmboCompMethod;
- public DMATransfer(PLX_MasterDevice dev, bool bIsOpen)
- {
- InitializeComponent();
- m_masterDevice = dev;
- //m_dmaBuffer = dmaBuffer;
- //m_pData = pUserBuffer;
- dmaList = new DmaInfoList();
- chkBoxInc.Checked = true;
- txtLocalAddr.Text = "00";
- m_direction = true;
- btTRANS.Text = m_direction ? "READ" : "WRITE";
- txtBuffSize.Enabled = true;
-
-
- //bool bIsOpen = dmaList.IsDMAOpen(m_masterDevice.Handle, PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0);
- btSubmit.Text = bIsOpen ? "Close" : "Open";
- btTRANS.Enabled = bIsOpen ? true: false;
- }
- private void PLX_DMAIntHandler(PLX_MasterDevice dev, IntPtr pWdDma)
- {
- MarshalWdDma m_wdDmaMarshaler = new MarshalWdDma();
- WD_DMA wdDma = (WD_DMA)m_wdDmaMarshaler.MarshalNativeToManaged(pWdDma);
- BOOL bIsRead = (wdDma.dwOptions & (DWORD)WD_DMA_OPTIONS.DMA_FROM_DEVICE) != 0;
- byte[] data = new byte[wdDma.dwBytes];
- Marshal.Copy(wdDma.pUserAddr, data, 0, (int)wdDma.dwBytes);
- Log.TraceLog("interrupt for device {" + dev.ToString(false) +
- "} received! " + Environment.NewLine +
- (bIsRead ? "read: " : "wrote: ") +
- diag_lib.DisplayHexBuffer(data, wdDma.dwBytes));
- }
- private void TranslateInput()
- {
-
- DWORD dwStatus = 0;
- BOOL bIsRead = m_direction;
- //m_dwBytes = m_dmaBuffer.BuffSize;
- if (COMPLETION.INTERRUPT == m_compMethod)
- {
- if (m_masterDevice.IsEnabledInt())
- m_masterDevice.DisableInterrupts();
- else
- m_masterDevice.EnableInterrupts(new
- USER_INTERRUPT_MASTER_CALLBACK(PLX_DMAIntHandler), m_dmaChannel);
- }
- DmaBuffer m_dmaBuffer = dmaList.GetItem(m_masterDevice.Handle, PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0).DmaBuffer;
- MarshalWdDma wdDmaMarshaler = new MarshalWdDma();
- IntPtr m_pData = ((WD_DMA)wdDmaMarshaler.MarshalNativeToManaged(m_dmaBuffer.pWdDma)).pUserAddr;
- m_buff = new byte[m_dwBytes];
- if (!bIsRead)
- {
- if (m_dwBytes == 0)
- {
- m_excp = new Exception("Please open the data file to " +
- "written");
- throw m_excp;
- }
- m_excp = new Exception("The data you've entered is invalid. " +
- "please try again (hex)");
- //string str = diag_lib.PadBuffer(textBox1.Text,
- // (DWORD)textBox1.Text.Length, (DWORD)2 * m_dwBytes);
- //for (int i = 0; i < m_dwBytes; ++i)
- // m_buff[i] = Convert.ToByte(str.Substring(2 * i, 2), 16);
-
- UInt16 i = 0;
- m_buff = new byte[m_dwBytes];
- foreach (int number in m_DataTransBuffer)
- {
- for (UInt16 j = 0; j < 4; j++)
- {
- m_buff[i] = (BYTE)(m_DataTransBuffer[(BYTE)(i / 4)] >> (i * 8));
- i++;
- }
- }
-
- Marshal.Copy (m_buff,0, m_pData, (int)m_dwBytes);
- }
- dwStatus = m_masterDevice.DMATransfer(PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0, m_dmaBuffer,
- (m_compMethod == COMPLETION.INTERRUPT));
- if (dwStatus == (DWORD)wdc_err.WD_STATUS_SUCCESS)
- {
- if (m_compMethod != COMPLETION.POLLING)
- {
- DialogResult = DialogResult.OK;
- return;
- }
- else
- {
- if (bIsRead)
- Marshal.Copy(m_pData, m_buff, 0, (int)m_dwBytes);
- bt_Read_Click();
- }
- }
- TraceLog(bIsRead, (wdc_err)dwStatus);
- }
- private void TraceLog(BOOL bIsRead, wdc_err status)
- {
- string sData = "";
- string sInfo = "";
- if (status == wdc_err.WD_STATUS_SUCCESS)
- {
- sData = (bIsRead ? "R: " : "W: ")
- + diag_lib.DisplayHexBuffer(m_buff, m_dwBytes);
- sInfo = " (" + m_masterDevice.ToString(false) + ")";
- Log.TraceLog("DMATransferForm: " + sData + sInfo);
- }
- else
- {
- sData = "failed to complete the DMA transfer. status 0x" +
- status.ToString("X") + ": " + utils.Stat2Str((DWORD)status);
- sInfo = "(" + m_masterDevice.ToString(false) + ")";
- Log.ErrLog("DMATransferForm: " + sData + sInfo);
- }
- textBox1.Text += sData + Environment.NewLine;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string fileFullName;
- string Textbuf = "";
-
- if (openFileDialog2.ShowDialog() == DialogResult.OK)
- {
- fileFullName = openFileDialog2.FileName;
- TxtBox_OpenFilePath.Text += openFileDialog2.FileName + Environment.NewLine;
- using (StreamReader sr = new StreamReader(fileFullName))
- {
- string[] subStr = sr.ReadLine().Split(new char[] { '=' }, 2);
- textBox1.Text += subStr[0] + " is ";
- m_dwBytes = Convert.ToUInt32(subStr[1]) * 4;
- textBox1.Text += subStr[1] + Environment.NewLine;
- m_DataTransBuffer = new UInt32[m_dwBytes / 4];
- for (UInt16 i = 0; i < m_dwBytes / 4; i++)
- {
- if (sr.Peek() >= 0)
- {
- string temp = "";
- temp = sr.ReadLine();
- m_DataTransBuffer[i] = Convert.ToUInt32(temp, 16);
- Textbuf += temp;// + " ";
- textBox1.Text += temp + Environment.NewLine;
- }
- }
- //txtLog.Text += Textbuf;
- }
- }
- }
- private void OpenDma_Input()
- {
- m_bAutoInc = chkBoxInc.Checked;
- m_wAllocType = (Scatter.Checked) ? ALLOCATION_TYPE.SG : ALLOCATION_TYPE.CONTIG;
- m_direction = (RW.READ == ((Read.Checked) ? RW.READ : RW.WRITE)) ? true : false;
- m_compMethod = (Interrupt.Checked) ? COMPLETION.INTERRUPT : COMPLETION.POLLING;
- m_excp = new Exception("Enter local DMA address. " + "Entered value should be a hex number");
- m_u32LocalAddr = (DWORD)Convert.ToInt32(txtLocalAddr.Text, 16);
- if (m_dwBytes == 0 && m_direction==false)
- {
- m_excp = new Exception("Please assign the data file.");
- throw m_excp;
- }
- if (m_direction == true)
- {
- m_dwBytes = (DWORD)Convert.ToInt32(txtBuffSize.Text, 16);
- }
- if (m_dwBytes == 0 && m_direction == true)
- {
- m_excp = new Exception("Please set the Read data buffer size .");
- throw m_excp;
- }
- }
- private void btSubmit_Click(object sender, EventArgs e)
- {
- IntPtr pBuffer = IntPtr.Zero;
- DmaBuffer dmaBuff = null;
- if (btSubmit.Text == "Open")
- {
- try
- {
- OpenDma_Input();
- if (m_wAllocType == ALLOCATION_TYPE.SG)
- {
- pBuffer = Marshal.AllocHGlobal((int)m_dwBytes);
- if (pBuffer == IntPtr.Zero)
- {
- Log.ErrLog("PLX_Sample: Failed to allocate buffer for " +
- "Scatter-Gather DMA");
- return;
- }
- dmaBuff = new DmaBufferSG(m_masterDevice);
- textBox1.Text += "Set allocation type to scatter " + Environment.NewLine;
- }
- else
- {
- dmaBuff = new DmaBufferContig(m_masterDevice);
- textBox1.Text += "Set allocation type to contiguous " + Environment.NewLine;
- }
- 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)
- return;
- m_excp = new Exception("What's wrong!?");
- dmaList.AddItem(dmaBuff, PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0, pBuffer);
- btSubmit.Text = "Close";
- Log.TraceLog("DMA buffer for {" +
- m_masterDevice.ToString(false) + "}, Channel 0" + "was opened successfully");
- textBox1.Text += "DMA buffer for {" + m_masterDevice.ToString(false) + "}, Channel 0" + "was opened successfully" + Environment.NewLine;
- //btWrite.Enabled = true;
- EnableInput();
- return;
- }
- catch
- {
- MessageBox.Show(m_excp.Message, "Input Entry Error",
- MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- //DialogResult = DialogResult.Retry;
- }
- }
- else
- {
- DmaInfo dmaInfo=null;
- dmaInfo = dmaList.GetItem(m_masterDevice.Handle, PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0);
- dmaInfo.Close();
- dmaList.Remove(dmaInfo);
- btSubmit.Text = "Open";
- Log.TraceLog("DMA Channel " +
- (((DWORD)PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0 == 0) ? "0 " : "1 ") + "was closed. {" +
- m_masterDevice.ToString(false) + "}");
- textBox1.Text = "DMA Channel " +
- (((DWORD)PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0 == 0) ? "0 " : "1 ") + "was closed. {" +
- m_masterDevice.ToString(false) + "}";
- //btWrite.Enabled = false;
- EnableInput();
- return;
- }
- }
- private void EnableInput()
- {
- bool bIsOpen = dmaList.IsDMAOpen(m_masterDevice.Handle,PLX_DMA_CHANNEL.PLX_DMA_CHANNEL_0);
- btTRANS.Enabled = bIsOpen ?true:false;
- Scatter.Enabled=bIsOpen ? false:true;
- Contig.Enabled=bIsOpen ? false:true;
- Read.Enabled=bIsOpen ? false:true;
- Writer.Enabled = bIsOpen ? false : true;
- }
- private void btWrite_Click(object sender, EventArgs e)
- {
- try
- {
- TranslateInput();
- }
- catch
- {
- MessageBox.Show(m_excp.Message, "Input Entry Error",
- MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- //DialogResult = DialogResult.Retry;
- }
- }
- private void bt_Read_Click()
- {
- string fileFullName = "";
- Int32 m_dwBytes = 0;
- m_dwBytes = m_buff.GetLength(0);
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- fileFullName = saveFileDialog1.FileName;
- //Stream fileFullName = saveFileDialog1.OpenFile();
- StreamWriter writer = new StreamWriter(fileFullName);
- writer.WriteLine("Total Data Length is " + m_dwBytes);
- for (UInt16 i = 0; i < m_dwBytes; )
- {
- writer.WriteLine(m_buff[i++].ToString("X") + " " + m_buff[i++].ToString("X")
- + " " + m_buff[i++].ToString("X") + " " + m_buff[i++].ToString("X"));
- }
- writer.Close();
- }
- }
- private void Writer_CheckedChanged(object sender, EventArgs e)
- {
-
- m_direction = (RW.READ == ((Read.Checked) ? RW.READ : RW.WRITE)) ? true : false;
- btTRANS.Text = m_direction ? "READ" : "WRITE";
- txtBuffSize.Enabled = m_direction ? true : false;
- }
-
-
- }
- }