📄 readwritecmdtab.cpp
字号:
/*----------------------------------------------------------------------------
* Copyright (c) 2001 by National Semiconductor Corporation
* National Semiconductor Corporation
* 2900 Semiconductor Drive
* Santa Clara, California 95051
*
* All rights reserved
*
*<<<-------------------------------------------------------------------------
* File Contents:
* readWriteCmdTab.cpp - this class defines Diagnostic GUI tab. The diagnostic settings
* are received from the user and are transfered to the ControlFunc class or
* Uart class
*
* Project: USB Demo Application
* Author : Yan Nosovitsky
* Date : Dec 2001
*----------------------------------------------------------------------->>>*/
// readWriteCmdTab.cpp : implementation file
//
#include "stdafx.h"
#include "Demo.h"
#include "readWriteCmdTab.h"
#include "usbn9603reg.h"
#include "ControlFunc.h"
#include "command_api.h"
#include "USBDriver.h"
#include "uart\Uart.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// readWriteCmdTab dialog
readWriteCmdTab::readWriteCmdTab(CWnd* pParent /*=NULL*/)
: CDialog(readWriteCmdTab::IDD, pParent)
{
//{{AFX_DATA_INIT(readWriteCmdTab)
//}}AFX_DATA_INIT
m_bitArray[0] = &m_bit0Edit;
m_bitArray[1] = &m_bit1Edit;
m_bitArray[2] = &m_bit2Edit;
m_bitArray[3] = &m_bit3Edit;
m_bitArray[4] = &m_bit4Edit;
m_bitArray[5] = &m_bit5Edit;
m_bitArray[6] = &m_bit6Edit;
m_bitArray[7] = &m_bit7Edit;
COMNum = "Com1";
UARTChannel = TRUE;
regIndex = 0;
}
void readWriteCmdTab::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(readWriteCmdTab)
DDX_Control(pDX, IDC_BIT7, m_bit7Edit);
DDX_Control(pDX, IDC_BIT6, m_bit6Edit);
DDX_Control(pDX, IDC_BIT5, m_bit5Edit);
DDX_Control(pDX, IDC_BIT4, m_bit4Edit);
DDX_Control(pDX, IDC_BIT3, m_bit3Edit);
DDX_Control(pDX, IDC_BIT2, m_bit2Edit);
DDX_Control(pDX, IDC_BIT1, m_bit1Edit);
DDX_Control(pDX, IDC_BIT0, m_bit0Edit);
DDX_Control(pDX, IDC_SERIAL_RADIO, m_serialRadio);
DDX_Control(pDX, IDC_COM_COMBO, m_comList);
DDX_Control(pDX, IDC_ADDRESS_SPIN, m_addressSpin);
DDX_Control(pDX, IDC_REG_ADDRESS_EDIT, m_regAddress);
DDX_Control(pDX, IDC_REG_NAMES_COMBO, m_regName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(readWriteCmdTab, CDialog)
//{{AFX_MSG_MAP(readWriteCmdTab)
ON_CBN_SELENDOK(IDC_REG_NAMES_COMBO, OnSelendokRegNamesCombo)
ON_EN_CHANGE(IDC_REG_ADDRESS_EDIT, OnChangeRegAddressEdit)
ON_NOTIFY(UDN_DELTAPOS, IDC_ADDRESS_SPIN, OnDeltaposAddressSpin)
ON_CBN_SELENDOK(IDC_COM_COMBO, OnSelendokComCombo)
ON_BN_CLICKED(IDC_SERIAL_RADIO, OnSerialRadio)
ON_BN_CLICKED(IDC_USB_RADIO, OnUsbRadio)
ON_BN_CLICKED(IDC_READ_BUTTON, OnReadButton)
ON_BN_CLICKED(IDC_WRITE_BUTTON, OnWriteButton)
ON_COMMAND(IDCANCEL, OnCancel)
ON_COMMAND(IDOK, OnOK)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// readWriteCmdTab message handlers
void readWriteCmdTab::OnSelendokRegNamesCombo()
{
int index = m_regName.GetCurSel();
/* Find reg. address */
int i = index - 1;
if (index == 0)
i=0;
while (USBReg[i++].listIndex != index) {}
/* update address field */
regIndex = i - 1;
CString address;
if (USBReg[i-1].address < 0x10)
address.Format("0x000%x",USBReg[regIndex].address);
else
address.Format("0x00%x",USBReg[regIndex].address);
m_regAddress.SetWindowText(address);
m_addressSpin.SetPos(USBReg[regIndex].address);
ClearBits();
}
void readWriteCmdTab::OnChangeRegAddressEdit()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
void readWriteCmdTab::BuildRegList()
{
int i = 0;
int index = 1;
m_regName.AddString("Reserved");
for(; i<= LAST_REG; i++)
{
if (!(USBReg[i].type&NO_USE))
{/* add to the list */
m_regName.AddString(USBReg[i].name);
USBReg[i].listIndex = index++;;
}
else
USBReg[i].listIndex = 0;
}
m_regName.SetCurSel(1);
}
void readWriteCmdTab::OnDeltaposAddressSpin(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
int index = pNMUpDown->iPos + pNMUpDown->iDelta;
*pResult = 0;
if ((index >= 0)&&(index <= LAST_REG))
regIndex = index;
else
{
CString address;
if (index == -1)
{
regIndex = LAST_REG;
// address = "0x0000";
// m_addressSpin.SetPos(0);
}
else
{
regIndex = LAST_REG;
// address = "0x003F";
// m_addressSpin.SetPos(LAST_REG);
}
// m_regAddress.SetWindowText(address);
}
m_regName.SetCurSel(USBReg[regIndex].listIndex);
ClearBits();
}
void readWriteCmdTab::OnSelendokComCombo()
{
COMNum.Format(_T("Com%d"),(m_comList.GetCurSel() + 1));
}
void readWriteCmdTab::OnSerialRadio()
{
m_comList.EnableWindow(TRUE);
UARTChannel = TRUE;
}
void readWriteCmdTab::OnUsbRadio()
{
m_comList.EnableWindow(FALSE);
UARTChannel = FALSE;
}
void readWriteCmdTab::ClearBits()
{
int i = 0;
for (; i < BIT_NUM ; i++)
m_bitArray[i]->SetWindowText("");
}
void readWriteCmdTab::OnReadButton()
{
USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
if (curUSBDriver->IsUSBDriverHaveError())
{
MessageBox(curUSBDriver->GetErrorDescriptor(),"Error",MB_ICONERROR | MB_OK);
return;
}
if (curUSBDriver->IsBulkOutTestRun())
{
/* Bulk OUT test run */
MessageBox("Cannot process current command.\nWait until Bulk OUT test is finished.","Error",MB_ICONERROR | MB_OK);
return;
}
/* check reg. type */
if (!(USBReg[regIndex].type & READ_ONLY) &&
!(USBReg[regIndex].type & CLEARED_READ) &&
!(USBReg[regIndex].type & NO_LIMIT) &&
!(USBReg[regIndex].type & NO_USE))
{
/* Not readable register */
MessageBox("The selected register is write only","Error",MB_ICONERROR | MB_OK);
return;
}
if (USBReg[regIndex].type & CLEARED_READ)
{
if (MessageBox("Some bits of the selected register will be cleared on read.\nReading this register may change the firmware behavior.\nDo you want to continue?\n",
"Warning",MB_ICONWARNING|MB_YESNO ) == IDNO)
return;
}
if (USBReg[regIndex].type & NO_USE)
{
if (MessageBox("The selected address is reserved.\nResult of reading this address is unpredictable.\nDo you want to continue?\n",
"Warning",MB_ICONWARNING|MB_YESNO ) == IDNO)
return;
}
if (UARTChannel)
{
/* Use UART */
Uart uart;
BYTE buffer[2];
if (!uart.OpenUART(COMNum))
{
MessageBox("Cannot open communication channel.","Error",MB_ICONERROR | MB_OK);
return;
}
buffer[0] = READ_USB_REG;
buffer[1] = USBReg[regIndex].address;
if (!uart.WriteData(buffer,2))
{
MessageBox("Cannot write to communication channel.","Error",MB_ICONERROR | MB_OK);
uart.CloseUART();
return;
}
if(uart.ReadData(®Value,1,100) != 1)
{
MessageBox("Cannot read register from USB device using " + COMNum,"Error",MB_ICONERROR | MB_OK);
uart.CloseUART();
return;
}
uart.CloseUART();
}
else
{
/* Use USB conrol */
if (!ControlFunc::SendSetupPacketIn(READ_USB_REG, USBReg[regIndex].address,0,1,®Value))
{
/* Can't send setup packet */
MessageBox("Cannot read register from USB device.","Error",MB_ICONERROR | MB_OK);
return;
}
}
/* Update bits field */
int i = 0;
CString bit;
for (; i < BIT_NUM ; i++)
{
bit.Format(_T("%d"),(regValue>>i)&0x1);
m_bitArray[i]->SetWindowText(bit);
}
}
void readWriteCmdTab::OnWriteButton()
{
int i;
CString bit;
CString error;
byte curBit;
USBDriver *curUSBDriver = USBDriver::GetUSBDriver();
if (curUSBDriver->IsUSBDriverHaveError())
{
MessageBox(curUSBDriver->GetErrorDescriptor(),"Error",MB_ICONERROR | MB_OK);
return ;
}
if (curUSBDriver->IsBulkOutTestRun())
{
/* Bulk OUT test run */
MessageBox("Cannot process current command.\nWait until Bulk OUT test is finished.","Error",MB_ICONERROR | MB_OK);
return;
}
regValue = 0;
/* read bits field */
for (i = 0; i< BIT_NUM; i++)
{
m_bitArray[i]->GetWindowText(bit);
if (bit.IsEmpty())
{
error.Format(_T("Bit %d value is not specified"),i);
MessageBox(error,"Error",MB_ICONERROR | MB_OK);
return;
}
sscanf(bit.GetBuffer(bit.GetLength()),"%d",&curBit);
if ((curBit!= 0)&&(curBit!=1))
{
error.Format(_T("Bit %d value should be binary (0 or 1)"),i);
MessageBox(error,"Error",MB_ICONERROR | MB_OK);
return;
}
regValue |= (curBit<<i);
}
/* check reg. type */
if (!(USBReg[regIndex].type & WRITE_ONLY) &&
!(USBReg[regIndex].type & CLEARED_WRITE) &&
!(USBReg[regIndex].type & NO_LIMIT) &&
!(USBReg[regIndex].type & NO_USE))
{
/* Not readable register */
MessageBox("The selected register is read only","Error",MB_ICONERROR | MB_OK);
return;
}
if (USBReg[regIndex].type & NO_USE)
{
if (MessageBox("The selected address is reserved.\nResult of writing to this address is unpredictable.\nDo you want to continue?\n",
"Warning",MB_ICONWARNING|MB_YESNO ) == IDNO)
return;
}
else
{
if (USBReg[regIndex].type & CLEARED_WRITE)
{
if (MessageBox("Some bits of the selected register will be cleared on write.\nWriting to this register may change the firmware behavior.\nDo you want to continue?\n",
"Warning",MB_ICONWARNING|MB_YESNO ) == IDNO)
return;
}
else
if ((USBReg[regIndex].type & READ_ONLY)||(USBReg[regIndex].type & CLEARED_READ))
{
if (MessageBox("Some bits of the selected register are read only.\nWriting to these bits has no effect.\nDo you want to continue?\n",
"Warning",MB_ICONWARNING|MB_YESNO ) == IDNO)
return;
}
}
if (UARTChannel)
{
/* Use UART */
Uart uart;
BYTE buffer[3];
if (!uart.OpenUART(COMNum))
{
MessageBox("Cannot open communication channel.","Error",MB_ICONERROR | MB_OK);
return;
}
buffer[0] = WRITE_USB_REG;
buffer[1] = USBReg[regIndex].address;
buffer[2] = regValue;
if (!uart.WriteData(buffer,3))
{
MessageBox("Cannot write to communication channel.","Error",MB_ICONERROR | MB_OK);
uart.CloseUART();
return;
}
uart.CloseUART();
}
else
{
/* Use USB conrol */
if (!ControlFunc::SendSetupPacketOut(WRITE_USB_REG, USBReg[regIndex].address,regValue,0))
{
/* Can't send setup packet */
MessageBox("Write register failed.","Error",MB_ICONERROR | MB_OK);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -