private void btnReboot_Click(object sender, EventArgs e) { rbtCapture.Checked = false; rbtMonitor.Checked = false; rbtMaster.Checked = true; I2c.ModuleReboot(); }
private void ReadAndLog(byte bRxCnt) { bool Ack = I2c.ReadBytes(Convert.ToByte(tbxAddr.Text, 16), bRxCnt); string Log = ""; if (Ack == false) { uiNAKCnt = Convert.ToUInt16(tbxNAKCnt.Text); uiNAKCnt++; tbxNAKCnt.Text = Convert.ToString(uiNAKCnt); } else { FillRxBox(bRxCnt); if (I2c.LogOn == true) { I2c.fs = new FileStream("I2cDriver2.log", FileMode.Append); I2c.sw = new StreamWriter(I2c.fs); Log = "Rd: " + tbxI2cRx.Text + " "; if (Ack == true) { Log += "ACK"; } else { Log += "NACK"; } I2c.sw.WriteLine(Log); I2c.LogCounter++; I2c.sw.Close(); I2c.fs.Close(); } } }
// start cond. + slave addr (rd) + read bytes (Cnt), no stop cond. public static bool ReadBytes(byte Addr, byte Cnt) { int BytesRead = 0, i; if (I2c.StartCond((byte)(Addr + 1)) == true) { bWrBuffer[0] = (byte)(0x80 + Cnt - 1); ComPort._serialPort.DiscardInBuffer(); ComPort._serialPort.Write(bWrBuffer, 0, 1); for (i = 0; i < 10; i++) { BytesRead += ComPort._serialPort.Read(bRdBuffer, BytesRead, Cnt); if (BytesRead == Cnt) { break; } } if (i == 10) { return(false); } else { return(true); } } else { return(false); } }
private void rbtCapture_CheckedChanged(object sender, EventArgs e) { if (rbtCapture.Checked == true) { I2c.SetCaptureMode(); btnI2cTx.Enabled = false; btnI2cRx.Enabled = false; btnGetStatus.Enabled = false; btnI2cReset.Enabled = false; btnSetPullUpR.Enabled = false; btnSetSpeed.Enabled = false; I2c.LogOn = false; // stop Logfile btnStopLog.Enabled = false; btnStartLog.Enabled = false; /* Capfs = new FileStream("I2cDriver2Cap.bin", FileMode.Create); * Capbw = new BinaryWriter(Capfs); * Capbw.Close(); * Capfs.Close(); */ CaptureRunning = true; tmPolling.Interval = 3; tmPolling.Start(); } else { I2c.SetCaptureMode(); // also for capture off CaptureRunning = false; tmPolling.Stop(); // convert I2cDriver2Cap.bin to I2cDriver2Cap.csv CaptureConvert(); } }
public static bool Write() { bool Ack; byte CurrentDevAddr = DeviceAddr; // convert Addr to string "ll hh" byte AddrLow = (byte)Addr; byte AddrHigh = (byte)(Addr >> 8); string MemAddr = ""; if (TwoByteAddr == true) { MemAddr += Convert.ToString((AddrHigh & 0xf0) >> 4, 16); MemAddr += Convert.ToString((AddrHigh & 0x0f), 16) + " "; MemAddr += Convert.ToString((AddrLow & 0xf0) >> 4, 16); MemAddr += Convert.ToString(AddrLow & 0x0f, 16); } else { MemAddr += Convert.ToString((AddrLow & 0xf0) >> 4, 16); MemAddr += Convert.ToString(AddrLow & 0x0f, 16); CurrentDevAddr |= (byte)(AddrHigh << 1); } // Set address string TxBytes = MemAddr + " "; // Convert FileBuffer to string int i; for (i = FileBufferIndex; i < FileBufferIndex + PageSize - 1; i++) { TxBytes += Convert.ToString((FileBuffer[i] & 0xf0) >> 4, 16); TxBytes += Convert.ToString(FileBuffer[i] & 0x0f, 16) + " "; } TxBytes += Convert.ToString((FileBuffer[i] & 0xf0) >> 4, 16); TxBytes += Convert.ToString(FileBuffer[i] & 0x0f, 16); FileBufferIndex = i + 1; Ack = I2c.WriteBytes(CurrentDevAddr, TxBytes); I2c.StopCond(); if (Ack == false) { MemWrRunning = false; return(false); } else { Addr += PageSize; // check if finish if (Addr > EndAddr) { MemWrRunning = false; } return(true); } }
private void btnI2cReset_Click(object sender, EventArgs e) { I2c.I2cReset(); if (I2c.bRdBuffer[0] != 0) { I2c.GetStatus(); } StatusResult(); }
private void btnI2cRx_Click(object sender, EventArgs e) { if ((tbxRxCount.Text != "") || (tbxRxCount.Text != "0")) { // something to read ReadAndLog(Convert.ToByte(tbxRxCount.Text, 10)); I2c.StopCond(); } TxRepeatCounter = 0; tmPolling.Interval = Convert.ToUInt16(tbxRepeatDelay.Text); tmPolling.Start(); }
private void rbtMonitor_CheckedChanged(object sender, EventArgs e) { if (rbtMonitor.Checked == true) { I2c.SetMonitorMode(); } btnI2cTx.Enabled = false; btnI2cRx.Enabled = false; btnGetStatus.Enabled = false; btnI2cReset.Enabled = false; btnSetPullUpR.Enabled = false; btnSetSpeed.Enabled = false; CaptureRunning = false; }
private void btnSetSpeed_Click(object sender, EventArgs e) { if (cbxI2cSpeed.Text == "100") { I2c.I2cSetSpeed((byte)'1'); } else if (cbxI2cSpeed.Text == "400") { I2c.I2cSetSpeed((byte)'4'); } else { I2c.bRdBuffer[0] = 0; } if (I2c.bRdBuffer[0] != 0) { I2c.GetStatus(); } StatusResult(); }
private void btnI2cTx_Click(object sender, EventArgs e) { bool Ack; Ack = I2c.WriteBytes(Convert.ToByte(tbxAddr.Text, 16), tbxI2cTx.Text); if (cbxRepeatedStart.Checked == false) { I2c.StopCond(); } if (Ack == false) { uiNAKCnt = Convert.ToUInt16(tbxNAKCnt.Text); uiNAKCnt++; tbxNAKCnt.Text = Convert.ToString(uiNAKCnt); } TxRepeatCounter = 0; tmPolling.Interval = Convert.ToUInt16(tbxRepeatDelay.Text); tmPolling.Start(); }
private void btnSetPullUpR_Click(object sender, EventArgs e) { if (cbxPullUpR.Text == "4.7") { I2c.I2cSetPullupR(0x24); } else if (cbxPullUpR.Text == "4.3") { I2c.I2cSetPullupR(0x12); } else if (cbxPullUpR.Text == "2.2") { I2c.I2cSetPullupR(0x09); } else if (cbxPullUpR.Text == "1.5") { I2c.I2cSetPullupR(0x1b); } else if (cbxPullUpR.Text == "1.1") { I2c.I2cSetPullupR(0x3f); } else if (cbxPullUpR.Text == "disabled") { I2c.I2cSetPullupR(0x00); } else { I2c.bRdBuffer[0] = 0; } if (I2c.bRdBuffer[0] != 0) { I2c.GetStatus(); } StatusResult(); }
private void tmPolling_Tick(object sender, EventArgs e) { if (InvokeRequired) { BeginInvoke(new tmPollingTickDelegate(tmPolling_Tick), new object[] { sender, e }); } else { if (cbxRepeat.Checked == true) // Terminal Tx repeat { bool Ack = true; if (tbxI2cTx.Text != "") // if "", then supress I2cWr { Ack = I2c.WriteBytes(Convert.ToByte(tbxAddr.Text, 16), tbxI2cTx.Text); if (cbxRepeatedStart.Checked == false) { I2c.StopCond(); } } if (Ack == false) { uiNAKCnt++; tbxNAKCnt.Text = Convert.ToString(uiNAKCnt); } else { if ((tbxRxCount.Text != "") && (tbxRxCount.Text != "0")) { // something to read ReadAndLog(Convert.ToByte(tbxRxCount.Text, 10)); I2c.StopCond(); } // end of something to read } TxRepeatCounter++; tbxRepeatCounter.Text = Convert.ToString(TxRepeatCounter); if ((StopOnError == true) && (Ack == false)) { cbxRepeat.Checked = false; } } else if (Eeprom.MemRdRunning == true) { if (Eeprom.Read() == false) { uiNAKCnt = Convert.ToUInt16(tbxNAKCnt.Text); uiNAKCnt++; tbxNAKCnt.Text = Convert.ToString(uiNAKCnt); Eeprom.MemRdRunning = false; tbxCurrentAddr.Text = "failed"; } else { tbxCurrentAddr.Text = Convert.ToString(Eeprom.Addr); } } else if (Eeprom.MemWrRunning == true) { if (Eeprom.Write() == false) { uiNAKCnt = Convert.ToUInt16(tbxNAKCnt.Text); uiNAKCnt++; tbxNAKCnt.Text = Convert.ToString(uiNAKCnt); Eeprom.MemWrRunning = false; tbxCurrentAddr.Text = "failed"; } else { Thread.Sleep(10); // 10ms Eeprom Wr tbxCurrentAddr.Text = Convert.ToString(Eeprom.Addr); } } else if (I2c.LogOn == true) { tbxLogCounter.Text = Convert.ToString(I2c.LogCounter); } else if (CaptureRunning == true) { CaptureI2c(); } else { tmPolling.Stop(); } } }
private void btnGetStatus_Click(object sender, EventArgs e) { I2c.GetStatus(); StatusResult(); }
private void btnCommOpen_Click(object sender, EventArgs e) { if (btnCommOpen.Text == "Close") { ComPort.ComPortClose(); btnGetStatus.Enabled = false; btnI2cReset.Enabled = false; btnSetPullUpR.Enabled = false; btnSetSpeed.Enabled = false; btnI2cTx.Enabled = false; tbxCommParam.Text = ""; tbxMstVoltage.Text = ""; tbxSerialNmb.Text = ""; tbxSlvCurrent.Text = ""; tbxMstTemp.Text = ""; cbxI2cSpeed.Text = ""; tbxSDALevel.Text = ""; tbxSCLLevel.Text = ""; cbxPullUpR.Text = ""; btnCommOpen.Text = "Open"; } else { if (cbxCommPort.Text == "") { PortNames(); return; } ComPort.ComPortOpen(cbxCommPort.Text); I2c.GetStatus(); if (I2c.bRdBuffer[0] == 0) { ComPort.ComPortClose(); } else { btnGetStatus.Enabled = true; btnI2cReset.Enabled = true; btnSetPullUpR.Enabled = true; btnSetSpeed.Enabled = true; btnI2cTx.Enabled = true; btnI2cRx.Enabled = true; StatusResult(); tbxCommParam.Text = "Baudrate = " + Convert.ToString(I2cDriverDef.BaudRate) + " "; tbxCommParam.Text += "Databits = " + Convert.ToString(I2cDriverDef.DataBits) + " "; tbxCommParam.Text += "Stopbits = " + Convert.ToString(I2cDriverDef.StopBits) + " "; tbxCommParam.Text += "Parity = " + Convert.ToString(I2cDriverDef.Parity) + " "; tbxCommParam.Text += "Handshake = " + Convert.ToString(I2cDriverDef.Handshake) + " "; tbxCommParam.Text += "Read Timeout = " + Convert.ToString(I2cDriverDef.ReadTimeOut) + " "; tbxCommParam.Text += "Write Timeout = " + Convert.ToString(I2cDriverDef.WriteTimeOut); } btnCommOpen.Text = "Close"; } }
// start cond. + slave addr (wr) + write bytes (bTxCnt), no stop cond. public static bool WriteBytes(byte Addr, String TxbTxData) { byte[] Ack = new byte[1]; byte I, K; Byte bTxCnt; Byte bTxCnt1; bool ack; string Log = ""; if (TxbTxData == "") { return(true); } Ack[0] = 0xff; ComPort._serialPort.DiscardInBuffer(); ack = I2c.StartCond(Addr); if (ack == true) { bTxCnt1 = (Byte)TxbTxData.Length; bTxCnt = 0; K = 1; for (I = 0; I < bTxCnt1; I++) { if (TxbTxData[I] != ' ') { bTxCnt++; if ((Byte)TxbTxData[I] < 0x3a) { I2c.bWrBuffer[K] = (Byte)((TxbTxData[I++] - 0x30) * 16); if ((Byte)TxbTxData[I] < 0x3a) { I2c.bWrBuffer[K++] += (Byte)(TxbTxData[I] - 0x30); } else { I2c.bWrBuffer[K++] += (Byte)(TxbTxData[I] - 0x57); } } else { I2c.bWrBuffer[K] = (Byte)((TxbTxData[I++] - 0x57) * 16); if ((Byte)TxbTxData[I] < 0x3a) { I2c.bWrBuffer[K++] += (Byte)(TxbTxData[I] - 0x30); } else { I2c.bWrBuffer[K++] += (Byte)(TxbTxData[I] - 0x57); } } } } bWrBuffer[0] = (byte)(0xc0 + bTxCnt - 1); ComPort._serialPort.Write(bWrBuffer, 0, bTxCnt + 1); ComPort._serialPort.Read(Ack, 0, 1); if (Ack[0] == 0x31) // "1" bei Ack, "0" bei Nack { ack = true; } else { ack = false; } } if (LogOn == true) { fs = new FileStream("I2cDriver2.log", FileMode.Append); sw = new StreamWriter(fs); Log = "Wr: " + TxbTxData + " "; if (ack == true) { Log += "ACK"; } else { Log += "NACK"; } sw.WriteLine(Log); LogCounter++; sw.Close(); fs.Close(); } return(ack); }
//--------------Eeprom Read --------------------------------------------------- public static bool Read() { bool Ack; byte CurrentDevAddr = DeviceAddr; // convert Addr to string "ll hh" byte AddrLow = (byte)Addr; byte AddrHigh = (byte)(Addr >> 8); string MemAddr = ""; string Log = ""; if (TwoByteAddr == true) { MemAddr += " " + Convert.ToString((AddrHigh & 0xf0) >> 4, 16); MemAddr += Convert.ToString((AddrHigh & 0x0f), 16) + " "; MemAddr += Convert.ToString((AddrLow & 0xf0) >> 4, 16); MemAddr += Convert.ToString(AddrLow & 0x0f, 16); } else { MemAddr += Convert.ToString((AddrLow & 0xf0) >> 4, 16); MemAddr += Convert.ToString(AddrLow & 0x0f, 16); CurrentDevAddr |= (byte)(AddrHigh << 1); } // Set address Ack = I2c.WriteBytes(CurrentDevAddr, MemAddr); if (Ack == false) { MemRdRunning = false; I2c.StopCond(); return(false); } else { // Read page, no I2c.StopCond() -> repeated Start Ack = I2c.ReadBytes(CurrentDevAddr, PageSize); if (Ack == false) { MemRdRunning = false; } else { if (I2c.LogOn == true) { I2c.fs = new FileStream("I2cDriver2.log", FileMode.Append); I2c.sw = new StreamWriter(I2c.fs); Log = "Rd: " + Convert.ToString(PageSize) + " bytes "; if (Ack == true) { Log += "ACK"; } else { Log += "NACK"; } I2c.sw.WriteLine(Log); I2c.LogCounter++; I2c.sw.Close(); I2c.fs.Close(); } } I2c.StopCond(); } Addr += PageSize; // fill file buffer int i; for (i = 0; i < PageSize; i++) { FileBuffer[FileBufferIndex] = I2c.bRdBuffer[i]; FileBufferIndex++; } // check if finish if (Addr > EndAddr) { MemRdRunning = false; FileStream fs = new FileStream(FileName, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); for (i = 0; i < Length; i++) { bw.Write(FileBuffer[i]); } bw.Close(); fs.Close(); } return(Ack); }