00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "stdafx.h"
00015 #include "ControlPanel.h"
00016
00017 #include "ControlPanelDoc.h"
00018 #include "MainFrm.h"
00019 #include "ControlPanelView.h"
00020 #include "../Common/u2c_common_func.h"
00021 #include ".\controlpaneldoc.h"
00022 #include "SpiConfigDlg.h"
00023
00024
00025 #ifdef _DEBUG
00026 #define new DEBUG_NEW
00027 #undef THIS_FILE
00028 static char THIS_FILE[] = __FILE__;
00029 #endif
00030
00031
00032
00033 IMPLEMENT_DYNCREATE(CControlPanelDoc, CDocument)
00034
00035 CControlPanelDoc::CControlPanelDoc()
00036 : m_hDevice(INVALID_HANDLE_VALUE), m_SSPin(0), m_UseSS(FALSE), m_ActiveHigh(FALSE)
00037 {
00038 }
00039
00040 BOOL CControlPanelDoc::OnNewDocument()
00041 {
00042 if (!CDocument::OnNewDocument())
00043 return FALSE;
00044 return TRUE;
00045 }
00046
00047 CControlPanelDoc::~CControlPanelDoc()
00048 {
00049 }
00050
00051
00052 BEGIN_MESSAGE_MAP(CControlPanelDoc, CDocument)
00053
00054
00055
00056 ON_COMMAND(IDC_I2C_READ, OnI2cRead)
00057 ON_COMMAND(IDC_I2C_WRITE, OnI2cWrite)
00058
00059 ON_COMMAND(IDC_I2C_START, OnI2cStart)
00060
00061
00062 ON_COMMAND(IDC_I2C_REPEATED_START, OnI2cRepeatedStart)
00063 ON_COMMAND(IDC_I2C_STOP, OnI2cStop)
00064 ON_COMMAND(IDC_I2C_WRITE_BYTE, OnI2cWriteByte)
00065 ON_COMMAND(IDC_READ_BYTE, OnI2cReadByte)
00066 ON_COMMAND(IDC_I2C_GET_ACK, OnI2cGetAck)
00067 ON_COMMAND(IDC_I2C_PUT_ACK, OnI2cPutAck)
00068
00069 ON_COMMAND(IDC_I2C_RELEASE_SCL, OnI2cReleaseScl)
00070 ON_COMMAND(IDC_I2C_DROP_SCL, OnI2cDropScl)
00071 ON_COMMAND(IDC_I2C_READ_SCL, OnI2cReadScl)
00072 ON_COMMAND(IDC_I2C_RELEASE_SDA, OnI2cReleaseSda)
00073 ON_COMMAND(IDC_I2C_DROP_SDA, OnI2cDropSda)
00074 ON_COMMAND(IDC_I2C_READ_SDA, OnI2cReadSda)
00075
00076 ON_COMMAND(IDC_SPI_READ_WRITE, OnSpiReadWrite)
00077 ON_COMMAND(IDC_SPI_WRITE, OnSpiWrite)
00078 ON_COMMAND(IDC_SPI_READ, OnSpiRead)
00079
00080 ON_COMMAND(IDC_OPEN_DEVICE, OnOpenDevice)
00081 ON_COMMAND(IDC_GET_SN, OnGetSN)
00082 ON_COMMAND(IDC_GET_VERSION, OnGetVersion)
00083 ON_COMMAND(IDC_SCAN_I2C_SLAVE, OnScanI2cSlave)
00084
00085 ON_COMMAND(ID_OPTIONS_I2C_FREQUENCY, OnOptionsI2cFrequency)
00086 ON_COMMAND(ID_OPTIONS_SPICONFIGURATION, OnOptionsSpiConfiguration)
00087 END_MESSAGE_MAP()
00088
00089 const CString CControlPanelDoc::m_line_states[] =
00090 {
00091 "LS_RELEASED",
00092 "LS_DROPPED_BY_I2C_BRIDGE",
00093 "LS_DROPPED_BY_SLAVE",
00094 "LS_RAISED_BY_I2C_BRIDGE"
00095 };
00096
00097
00098
00100
00101
00102 #ifdef _DEBUG
00103 void CControlPanelDoc::AssertValid() const
00104 {
00105 CDocument::AssertValid();
00106 }
00107
00108 void CControlPanelDoc::Dump(CDumpContext& dc) const
00109 {
00110 CDocument::Dump(dc);
00111 }
00112 #endif //_DEBUG
00113
00115
00116
00117 void CControlPanelDoc::Serialize(CArchive& ar)
00118 {
00119 if (ar.IsStoring())
00120 {
00121
00122 }
00123 else
00124 {
00125
00126 }
00127 }
00128
00130
00131
00132
00133 CControlPanelView* CControlPanelDoc::GetView()
00134 {
00135 ASSERT_VALID(m_pView);
00136 return m_pView;
00137 }
00138
00139 CMainFrame* CControlPanelDoc::GetMainFrame()
00140 {
00141 CMainFrame* pFrame = GetView()->GetMainFrame();
00142 ASSERT_VALID(pFrame);
00143 return pFrame;
00144 }
00145
00146
00147 void CControlPanelDoc::OnSpiReadWrite()
00148 {
00149 CMainFrame* pFrame = GetMainFrame();
00150 unsigned short len;
00151 BYTE InBuff[256], OutBuff[256];
00152 if (!pFrame->m_SpiBar.GetLength(&len))
00153 return;
00154 if (!pFrame->m_SpiBar.GetOutData(OutBuff, len))
00155 return;
00156 if (m_UseSS)
00157 {
00158 U2C_RESULT res = U2C_SpiReadWriteSS(m_hDevice, OutBuff, InBuff, len, m_SSPin, m_ActiveHigh);
00159 if (U2C_SUCCESS != res)
00160 GetView()->PrintError("U2C_SpiReadWriteSS", res);
00161 else
00162 {
00163 GetView()->PrintLine("U2C_SpiReadWriteSS succeeded");
00164 GetView()->IncIndent();
00165
00166 GetView()->PrintLine("Output Data:");
00167 GetView()->IncIndent();
00168 GetView()->PrintHexBuffer(OutBuff, len);
00169 GetView()->DecIndent();
00170 GetView()->PrintLine("Input Data:");
00171 GetView()->IncIndent();
00172 GetView()->PrintHexBuffer(InBuff, len);
00173 GetView()->DecIndent();
00174
00175 GetView()->DecIndent();
00176 }
00177 }
00178 else
00179 {
00180 U2C_RESULT res = U2C_SpiReadWrite(m_hDevice, OutBuff, InBuff, len);
00181 if (U2C_SUCCESS != res)
00182 GetView()->PrintError("U2C_SpiReadWrite", res);
00183 else
00184 {
00185 GetView()->PrintLine("U2C_SpiReadWrite succeeded");
00186 GetView()->IncIndent();
00187
00188 GetView()->PrintLine("Output Data:");
00189 GetView()->IncIndent();
00190 GetView()->PrintHexBuffer(OutBuff, len);
00191 GetView()->DecIndent();
00192 GetView()->PrintLine("Input Data:");
00193 GetView()->IncIndent();
00194 GetView()->PrintHexBuffer(InBuff, len);
00195 GetView()->DecIndent();
00196
00197 GetView()->DecIndent();
00198 }
00199 }
00200 GetView()->PrintLine("");
00201 }
00202
00203
00204 void CControlPanelDoc::OnSpiRead()
00205 {
00206 CMainFrame* pFrame = GetMainFrame();
00207 unsigned short len;
00208 BYTE InBuff[256];
00209 if (!pFrame->m_SpiBar.GetLength(&len))
00210 return;
00211
00212 if (m_UseSS)
00213 {
00214 U2C_RESULT res = U2C_SpiReadSS(m_hDevice, InBuff, len, m_SSPin, m_ActiveHigh);
00215 if (U2C_SUCCESS != res)
00216 GetView()->PrintError("U2C_SpiReadSS", res);
00217 else
00218 {
00219 GetView()->PrintLine("U2C_SpiReadSS succeeded");
00220 GetView()->IncIndent();
00221
00222 GetView()->PrintLine("Data:");
00223 GetView()->IncIndent();
00224 GetView()->PrintHexBuffer(InBuff, len);
00225 GetView()->DecIndent();
00226
00227 GetView()->DecIndent();
00228 }
00229 }
00230 {
00231 U2C_RESULT res = U2C_SpiRead(m_hDevice, InBuff, len);
00232 if (U2C_SUCCESS != res)
00233 GetView()->PrintError("U2C_SpiRead", res);
00234 else
00235 {
00236 GetView()->PrintLine("U2C_SpiRead succeeded");
00237 GetView()->IncIndent();
00238
00239 GetView()->PrintLine("Data:");
00240 GetView()->IncIndent();
00241 GetView()->PrintHexBuffer(InBuff, len);
00242 GetView()->DecIndent();
00243
00244 GetView()->DecIndent();
00245 }
00246 }
00247 GetView()->PrintLine("");
00248 }
00249
00250
00251 void CControlPanelDoc::OnSpiWrite()
00252 {
00253 CMainFrame* pFrame = GetMainFrame();
00254 unsigned short len;
00255 BYTE OutBuff[256];
00256 if (!pFrame->m_SpiBar.GetLength(&len))
00257 return;
00258 if (!pFrame->m_SpiBar.GetOutData(OutBuff, len))
00259 return;
00260 if (m_UseSS)
00261 {
00262 U2C_RESULT res = U2C_SpiWriteSS(m_hDevice, OutBuff, len, m_SSPin, m_ActiveHigh);
00263 if (U2C_SUCCESS != res)
00264 GetView()->PrintError("U2C_SpiWriteSS", res);
00265 else
00266 {
00267 GetView()->PrintLine("U2C_SpiWriteSS succeeded");
00268 GetView()->IncIndent();
00269
00270 GetView()->PrintLine("Data:");
00271 GetView()->IncIndent();
00272 GetView()->PrintHexBuffer(OutBuff, len);
00273 GetView()->DecIndent();
00274
00275 GetView()->DecIndent();
00276 }
00277 }
00278 {
00279 U2C_RESULT res = U2C_SpiWrite(m_hDevice, OutBuff, len);
00280 if (U2C_SUCCESS != res)
00281 GetView()->PrintError("U2C_SpiWrite", res);
00282 else
00283 {
00284 GetView()->PrintLine("U2C_SpiWrite succeeded");
00285 GetView()->IncIndent();
00286
00287 GetView()->PrintLine("Data:");
00288 GetView()->IncIndent();
00289 GetView()->PrintHexBuffer(OutBuff, len);
00290 GetView()->DecIndent();
00291
00292 GetView()->DecIndent();
00293 }
00294 }
00295 GetView()->PrintLine("");
00296 }
00297
00298
00299 void CControlPanelDoc::OnI2cRead()
00300 {
00301 U2C_TRANSACTION Transaction;
00302 CMainFrame* pFrame = GetMainFrame();
00303 CI2cReadBar *pBar = &pFrame->m_I2cReadBar;
00304 if(!pBar->InitTransaction(&Transaction))
00305 return;
00306
00307 U2C_RESULT res = U2C_Read(m_hDevice, &Transaction);
00308 if (U2C_SUCCESS != res)
00309 GetView()->PrintError("U2C_Read", res);
00310 else
00311 GetView()->PrintLine("U2C_Read succeeded");
00312
00313 GetView()->IncIndent();
00314 GetView()->PrintTransaction(Transaction, U2C_SUCCESS == res);
00315 GetView()->DecIndent();
00316 GetView()->PrintLine("");
00317 }
00318
00319 void CControlPanelDoc::OnI2cWrite()
00320 {
00321 U2C_TRANSACTION Transaction;
00322 CMainFrame* pFrame = GetMainFrame();
00323 CI2cWriteBar *pBar = &pFrame->m_I2cWriteBar;
00324 if (!pBar->InitTransaction(&Transaction))
00325 return;
00326
00327 U2C_RESULT res = U2C_Write(m_hDevice, &Transaction);
00328 if (U2C_SUCCESS != res)
00329 GetView()->PrintError("U2C_Write", res);
00330 else
00331 GetView()->PrintLine("U2C_Write succeeded");
00332 GetView()->IncIndent();
00333 GetView()->PrintTransaction(Transaction, true);
00334 GetView()->DecIndent();
00335 GetView()->PrintLine("");
00336 }
00337
00338 void CControlPanelDoc::OnI2cStart()
00339 {
00340 U2C_RESULT res = U2C_Start(m_hDevice);
00341 if (U2C_SUCCESS != res)
00342 GetView()->PrintError("U2C_Start", res);
00343 else
00344 GetView()->PrintLine("U2C_Start succeeded");
00345 GetView()->PrintLine("");
00346 }
00347
00348 void CControlPanelDoc::OnI2cRepeatedStart()
00349 {
00350 U2C_RESULT res = U2C_RepeatedStart(m_hDevice);
00351 if (U2C_SUCCESS != res)
00352 GetView()->PrintError("U2C_RepeatedStart", res);
00353 else
00354 GetView()->PrintLine("U2C_RepeatedStart succeeded");
00355 GetView()->PrintLine("");
00356 }
00357
00358 void CControlPanelDoc::OnI2cStop()
00359 {
00360 U2C_RESULT res = U2C_Stop(m_hDevice);
00361 if (U2C_SUCCESS != res)
00362 GetView()->PrintError("U2C_Stop", res);
00363 else
00364 GetView()->PrintLine("U2C_Stop succeeded");
00365 GetView()->PrintLine("");
00366 }
00367
00368 void CControlPanelDoc::OnI2cWriteByte()
00369 {
00370 CMainFrame* pFrame = GetMainFrame();
00371 BYTE Data;
00372 if (!pFrame->m_I2cLowLevelBar.GetWriteByteData(&Data))
00373 return;
00374
00375 CString strCommand;
00376 U2C_RESULT res;
00377 if (pFrame->m_I2cLowLevelBar.IsWriteByteAckNeeded())
00378 {
00379 strCommand = "U2C_PutByteWithAck";
00380 res = U2C_PutByteWithAck(m_hDevice, Data);
00381 }
00382 else
00383 {
00384 strCommand = "U2C_PutByte";
00385 res = U2C_PutByte(m_hDevice, Data);
00386 }
00387 if (U2C_SUCCESS != res)
00388 GetView()->PrintError(strCommand, res);
00389 else
00390 GetView()->PrintLine(strCommand + " succeeded");
00391 GetView()->PrintLine("");
00392 }
00393
00394
00395 void CControlPanelDoc::OnI2cReadByte()
00396 {
00397 CMainFrame* pFrame = GetMainFrame();
00398 BYTE Data;
00399 U2C_RESULT res;
00400 CString strCommand;
00401 BOOL bAckState;
00402 if (pFrame->m_I2cLowLevelBar.IsReadByteAckNeeded(&bAckState))
00403 {
00404 strCommand = "U2C_GetByteWithAck";
00405 res = U2C_GetByteWithAck(m_hDevice, &Data, bAckState);
00406 }
00407 else
00408 {
00409 strCommand = "U2C_GetByte";
00410 res = U2C_GetByte(m_hDevice, &Data);
00411 }
00412 if (U2C_SUCCESS != res)
00413 GetView()->PrintError(strCommand, res);
00414 else
00415 {
00416 GetView()->PrintLine(strCommand + " succeeded");
00417 GetView()->IncIndent();
00418 CString str;
00419 str.Format("Data: %02x", Data);
00420 GetView()->PrintLine(str);
00421 GetView()->DecIndent();
00422 }
00423 GetView()->PrintLine("");
00424 }
00425
00426
00427 void CControlPanelDoc::OnI2cGetAck()
00428 {
00429 U2C_RESULT res = U2C_GetAck(m_hDevice);
00430 if (U2C_SUCCESS != res)
00431 GetView()->PrintError("U2C_GetAck", res);
00432 else
00433 GetView()->PrintLine("U2C_GetAck succeeded");
00434 GetView()->PrintLine("");
00435 }
00436
00437 void CControlPanelDoc::OnI2cPutAck()
00438 {
00439 CMainFrame *pFrame = GetMainFrame();
00440 BOOL bAckState = pFrame->m_I2cLowLevelBar.GetPutAckState();
00441 U2C_RESULT res = U2C_PutAck(m_hDevice, bAckState);
00442 if (U2C_SUCCESS != res)
00443 GetView()->PrintError("U2C_PutAck", res);
00444 else
00445 GetView()->PrintLine("U2C_PutAck succeeded");
00446 GetView()->PrintLine("");
00447 }
00448
00449 void CControlPanelDoc::OnI2cReleaseScl()
00450 {
00451 U2C_RESULT res = U2C_ReleaseScl(m_hDevice);
00452 if (U2C_SUCCESS != res)
00453 GetView()->PrintError("U2C_ReleaseScl", res);
00454 else
00455 GetView()->PrintLine("U2C_ReleaseScl succeeded");
00456 GetView()->PrintLine("");
00457 }
00458
00459 void CControlPanelDoc::OnI2cDropScl()
00460 {
00461 U2C_RESULT res = U2C_DropScl(m_hDevice);
00462 if (U2C_SUCCESS != res)
00463 GetView()->PrintError("U2C_DropScl", res);
00464 else
00465 GetView()->PrintLine("U2C_DropScl succeeded");
00466 GetView()->PrintLine("");
00467 }
00468
00469 void CControlPanelDoc::OnI2cReadScl()
00470 {
00471 U2C_LINE_STATE State;
00472 U2C_RESULT res = U2C_ReadScl(m_hDevice, &State);
00473 if (U2C_SUCCESS != res)
00474 GetView()->PrintError("U2C_ReadScl", res);
00475 else
00476 {
00477 GetView()->PrintLine("U2C_ReadScl succeeded");
00478 GetView()->PrintLine("Current SCL state - " + m_line_states[State]);
00479 }
00480 GetView()->PrintLine("");
00481 }
00482
00483 void CControlPanelDoc::OnI2cReleaseSda()
00484 {
00485 U2C_RESULT res = U2C_ReleaseSda(m_hDevice);
00486 if (U2C_SUCCESS != res)
00487 GetView()->PrintError("U2C_ReleaseSda", res);
00488 else
00489 GetView()->PrintLine("U2C_ReleaseSda succeeded");
00490 GetView()->PrintLine("");
00491 }
00492
00493 void CControlPanelDoc::OnI2cDropSda()
00494 {
00495 U2C_RESULT res = U2C_DropSda(m_hDevice);
00496 if (U2C_SUCCESS != res)
00497 GetView()->PrintError("U2C_DropSda", res);
00498 else
00499 GetView()->PrintLine("U2C_DropSda succeeded");
00500 GetView()->PrintLine("");
00501 }
00502
00503 void CControlPanelDoc::OnI2cReadSda()
00504 {
00505 U2C_LINE_STATE State;
00506 U2C_RESULT res = U2C_ReadSda(m_hDevice, &State);
00507 if (U2C_SUCCESS != res)
00508 GetView()->PrintError("U2C_ReadSda", res);
00509 else
00510 {
00511 GetView()->PrintLine("U2C_ReadSda succeeded");
00512 GetView()->PrintLine("Current SDA state - " + m_line_states[State]);
00513 }
00514 GetView()->PrintLine("");
00515 }
00516
00517
00518 void CControlPanelDoc::OnGetSN()
00519 {
00520 long nSerial;
00521 U2C_RESULT res = U2C_GetSerialNum(m_hDevice, &nSerial);
00522 if (U2C_SUCCESS != res)
00523 GetView()->PrintError("U2C_GetSerialNum", res);
00524 else
00525 {
00526 GetView()->PrintLine("U2C_GetSerialNum succeeded");
00527 CString serial;
00528 serial.Format("Serial number - %d", nSerial);
00529 GetView()->PrintLine(serial);
00530 }
00531 GetView()->PrintLine("");
00532 }
00533
00534 void CControlPanelDoc::OnGetVersion()
00535 {
00536 U2C_VERSION_INFO VerInfo;
00537 VerInfo = U2C_GetDllVersion();
00538 CString str;
00539 str.Format("Dll version - (%d, %d)", VerInfo.MajorVersion, VerInfo.MinorVersion);
00540 GetView()->PrintLine(str);
00541 U2C_RESULT res;
00542 res = U2C_GetDriverVersion(m_hDevice, &VerInfo);
00543 if (U2C_SUCCESS != res)
00544 GetView()->PrintError("U2C_GetDriverVersion", res);
00545 else
00546 {
00547 str.Format("Driver version - (%d, %d)", VerInfo.MajorVersion, VerInfo.MinorVersion);
00548 GetView()->PrintLine(str);
00549 }
00550 res = U2C_GetFirmwareVersion(m_hDevice, &VerInfo);
00551 if (U2C_SUCCESS != res)
00552 GetView()->PrintError("U2C_GetFirmwareVersion", res);
00553 else
00554 {
00555 str.Format("Firmware version - (%d, %d)", VerInfo.MajorVersion, VerInfo.MinorVersion);
00556 GetView()->PrintLine(str);
00557 }
00558 GetView()->PrintLine("");
00559 }
00560
00561
00562 void CControlPanelDoc::OpenDevice()
00563 {
00564 if (m_hDevice != INVALID_HANDLE_VALUE)
00565 {
00566 U2C_CloseDevice(m_hDevice);
00567 m_hDevice = INVALID_HANDLE_VALUE;
00568 }
00569
00570 m_hDevice = OpenU2C();
00571
00572 if (m_hDevice != INVALID_HANDLE_VALUE)
00573 GetView()->EnableControls(true);
00574 else
00575 GetView()->EnableControls(false);
00576 }
00577
00578 void CControlPanelDoc::OnOpenDevice()
00579 {
00580 OpenDevice();
00581 }
00582
00583 void CControlPanelDoc::OnScanI2cSlave()
00584 {
00585 CMainFrame* pFrame = GetMainFrame();
00586 U2C_SLAVE_ADDR_LIST SlaveList;
00587 U2C_RESULT res = U2C_ScanDevices(m_hDevice, &SlaveList);
00588 if (U2C_SUCCESS != res)
00589 GetView()->PrintError("U2C_ScanDevices", res);
00590 else
00591 {
00592 GetView()->PrintLine("U2C_ScanDevices succeeded");
00593 GetView()->IncIndent();
00594 if (SlaveList.nDeviceNumber == 0)
00595 GetView()->PrintLine("No I2C slave devices connected");
00596 else
00597 {
00598 GetView()->PrintLine("Addresses of the I2C slave devices:");
00599 GetView()->IncIndent();
00600 GetView()->PrintHexBuffer(SlaveList.List, SlaveList.nDeviceNumber);
00601 GetView()->DecIndent();
00602
00603 pFrame->m_I2cReadBar.FillSlaveList(&SlaveList);
00604 pFrame->m_I2cWriteBar.FillSlaveList(&SlaveList);
00605 }
00606 GetView()->DecIndent();
00607 }
00608 GetView()->PrintLine("");
00609 }
00610
00611
00612 void CControlPanelDoc::OnOptionsI2cFrequency()
00613 {
00614 ConfigureI2cSpeed(m_hDevice);
00615 }
00616
00617 void CControlPanelDoc::OnOptionsSpiConfiguration()
00618 {
00619 ConfigureSpi(m_hDevice, &m_UseSS, &m_SSPin, &m_ActiveHigh);
00620 }