Advertisement
Guest User

Untitled

a guest
Aug 14th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1.  private async void Init_FTDI()
  2.         {
  3.             byte[] dataToWrite = new byte[1];
  4.             dataToWrite[0] = 0x00;
  5.             UInt32 numBytesWritten = 0;
  6.  
  7.             ftStatus = myFtdiDevice.OpenBySerialNumber("B"); //port B
  8.             ftStatus = myFtdiDevice.SetBitMode(0xff, 0x1); //set async bitbang, all outputs
  9.             ftStatus = myFtdiDevice.SetBaudRate(9600);
  10.             dataToWrite[0] = 0x01; //set D0 to H (MCU reset)
  11.             ftStatus = myFtdiDevice.Write(dataToWrite, 1, ref numBytesWritten); //set all pins on port B to 0
  12.  
  13.             myFtdiDevice.Close(); //Close port B
  14.  
  15.             ftStatus = myFtdiDevice.OpenBySerialNumber("D"); //port D
  16.             ftStatus = myFtdiDevice.SetBitMode(0x37, 0x1); //set async bitbang
  17.             ftStatus = myFtdiDevice.SetBaudRate(9600);
  18.             dataToWrite[0] = 0x15;
  19.             ftStatus = myFtdiDevice.Write(dataToWrite, 1, ref numBytesWritten); //set all pins on port D to 0
  20.  
  21.             myFtdiDevice.Close(); //close port D
  22.  
  23.             ftStatus = myFtdiDevice.OpenBySerialNumber("C"); //port C - actual UART
  24.             ftStatus = myFtdiDevice.SetBaudRate(115200);
  25.             ftStatus = myFtdiDevice.SetDataCharacteristics(
  26.                 FTDI.FT_DATA_BITS.FT_BITS_8,
  27.                 FTDI.FT_STOP_BITS.FT_STOP_BITS_1,
  28.                 FTDI.FT_PARITY.FT_PARITY_NONE);
  29.             ftStatus = myFtdiDevice.SetTimeouts(0, 0);
  30.             // Start the received data thread
  31.             if (!DataHandler.IsBusy)
  32.                 DataHandler.RunWorkerAsync();
  33.  
  34.             // Subscribe to FT_EVENT_RXCHAR events
  35.             ftStatus = myFtdiDevice.SetEventNotification(FTDI.FT_EVENTS.FT_EVENT_RXCHAR, DataReceived);
  36.             //ftStatus = myFtdiDevice.Write(dataToWrite, 1, ref numBytesWritten);
  37.  
  38.             if (ftStatus != FTDI.FT_STATUS.FT_OK)
  39.             {
  40.  
  41.                 label2.Text = "Initializing board Error!";
  42.                 label2.ForeColor = System.Drawing.Color.Red;
  43.             }
  44.             else
  45.             {
  46.                 label2.Text = "LakeWest Emulator Initialized";
  47.                 label2.ForeColor = System.Drawing.Color.Green;
  48.             }
  49.  
  50.             ftdi_inited = true;
  51.         }
  52.  
  53.         //separate backround timer thread
  54.         private void callback(object state)
  55.         {
  56.             //this.Invoke((Action)(() => { label1.Text = counter.ToString(); }));
  57.         }
  58.  
  59.         private async void Get_FTDI_connected() //ping FTDI driver
  60.         {
  61.             ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
  62.             await Task.Run(() =>
  63.             {
  64.                 ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
  65.             });
  66.  
  67.            
  68.  
  69.             if (ftStatus == FTDI.FT_STATUS.FT_OK && ftdiDeviceCount == 4)
  70.             {
  71.                 if(!ftdi_inited)
  72.                 {
  73.                     label2.Text = "Initializing...";
  74.                     label2.ForeColor = System.Drawing.Color.Green;
  75.                     Update();
  76.                     Thread.Sleep(2000);
  77.                     Init_FTDI();
  78.                 }          
  79.             }
  80.             else
  81.             {
  82.                 label2.Text = "No LakeWest board found!";
  83.                 label2.ForeColor = System.Drawing.Color.Red;
  84.                 ftdi_inited = false;
  85.             }
  86.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement