📄 gpio2spidlg.cpp
字号:
// GPIO2SPIDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GPIO2SPI.h"
#include "GPIO2SPIDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGPIO2SPIDlg dialog
CGPIO2SPIDlg::CGPIO2SPIDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGPIO2SPIDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGPIO2SPIDlg)
m_macaddress = _T("");
m_macconfirm = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGPIO2SPIDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGPIO2SPIDlg)
DDX_Text(pDX, IDC_MACADDRESS, m_macaddress);
DDX_Text(pDX, IDC_MACCONFIRM, m_macconfirm);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGPIO2SPIDlg, CDialog)
//{{AFX_MSG_MAP(CGPIO2SPIDlg)
ON_BN_CLICKED(IDC_SET, OnSet)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGPIO2SPIDlg message handlers
BOOL CGPIO2SPIDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void spi_delay( int dly)
{
int i;
for (;dly>0;dly--)
for(i=0;i<300;i++);
}
// this function is to init the GPIO 0,1,14pin
void initGPIOpin()
{
unsigned int uiPADDR, uiPADR, uiPBDDR, uiPBDR;
uiPADDR = *GPIO_PADDR | 0x03;//0x03=00000011
*GPIO_PADDR = uiPADDR;
uiPBDDR = *GPIO_PBDDR | 0x40;//0x40=01000000
*GPIO_PBDDR = uiPBDDR;
uiPBDR = *GPIO_PBDR & 0xbf;//0xbf=10111111
*GPIO_PBDR = uiPBDR;
spi_delay(10);
}
/*----------------------------------------------------
Function : Pulse
Input : None
Output : None
Description : Send a pulse (10) to Serial Data Clock(CLK)
------------------------------------------------------*/
void Pulse()
{
unsigned int uiPADDR, uiPADR, uiPBDDR, uiPBDR;
spi_delay(5);
uiPBDR = *GPIO_PBDR | 0x40;//0x40=01000000
*GPIO_PBDR = uiPBDR;
spi_delay(10);
uiPBDR = *GPIO_PBDR & 0xbf;//0xbf=10111111
*GPIO_PBDR = uiPBDR;
spi_delay(10);
}
/*----------------------------------------------------
Function : StartBit
Input : None
Output : None
Description :
1. Set Chip Select(CS) = 1 (high)
2. Set a Start Bit(1) to Serial Data Input(DI)
------------------------------------------------------*/
void StartBit()
{
unsigned int uiPADDR, uiPADR, uiPBDDR, uiPBDR;
uiPADR = *GPIO_PADR | 0x03;//0x03=00000011
*GPIO_PADR = uiPADR;
Pulse();
}
/*----------------------------------------------------
Function : EWEN
Input : None
Output : None
Description : ERASE/WRITE Enable
------------------------------------------------------*/
void EWEN()
{
unsigned int uiPADDR, uiPADR, uiPBDDR, uiPBDR;
unsigned int i, temp;
StartBit();
temp = 0x80;
for( i=0;i<8;i++ )
{
//uiPBDR = *GPIO_PBDR | 0x40;//0x40=01000000
//*GPIO_PBDR = uiPBDR;
//spi_delay(5);
if(0x30&temp)
{
uiPADR = *GPIO_PADR | 0x02;
*GPIO_PADR = uiPADR;
}
else
{
uiPADR = *GPIO_PADR & 0xfd;
*GPIO_PADR = uiPADR;
}
Pulse();
//spi_delay(5);
//uiPBDR = *GPIO_PBDR & 0xbf;//0xbf=10111111
//*GPIO_PBDR = uiPBDR;
// spi_delay(5);
temp >>= 1;
}
uiPADR = *GPIO_PADR & 0xfe;
*GPIO_PADR = uiPADR;
spi_delay(5);
}
/*----------------------------------------------------
Function : EWDS
Input : None
Output : None
Description : ERASE/WRITE Disable
------------------------------------------------------*/
void EWDS()
{
unsigned int i;
StartBit();
unsigned int uiPADDR, uiPADR, uiPBDDR, uiPBDR;
uiPADR = *GPIO_PADR & 0xfd;
*GPIO_PADR = uiPADR;
for( i=0;i<8;i++)
Pulse();
uiPADR = *GPIO_PADR & 0xfe;
*GPIO_PADR = uiPADR;
// spi_delay(5);
}
/*----------------------------------------------------
Function : Write93LC46
Input : unsigned char Offset Address, unsigned int tx_data
Output : None
Description : Write 16bits data in to 93LC46 Offset Address
------------------------------------------------------*/
void Write93LC46(unsigned int Offset_Addr, unsigned int tx_data)
{
unsigned int uiPADDR, uiPADR, uiPBDDR, uiPBDR;
unsigned int Addr, i, temp;
EWEN();
StartBit();
Offset_Addr=Offset_Addr&0x3f;//7bit address
Addr = Offset_Addr+0x40;
temp = 0x80;
for( i=0;i<8;i++)
{
//uiPBDR = *GPIO_PBDR | 0x40;//0x40=01000000
//*GPIO_PBDR = uiPBDR;
//spi_delay(5);
if(Addr&temp)
{
uiPADR = *GPIO_PADR | 0x02;
*GPIO_PADR = uiPADR;
}
else
{
uiPADR = *GPIO_PADR & 0xfd;
*GPIO_PADR = uiPADR;
}
/*
spi_delay(5);
uiPBDR = *GPIO_PBDR | 0x40;//0x40=01000000
*GPIO_PBDR = uiPBDR;
spi_delay(5);
*/
Pulse();
/*
uiPBDR = *GPIO_PBDR & 0xbf;//0xbf=10111111
*GPIO_PBDR = uiPBDR;
spi_delay(20);
*/
temp >>=1;
}
temp = 0x8000;
for(i=0;i<16;i++)
{
//uiPBDR = *GPIO_PBDR | 0x40;//0x40=01000000
//*GPIO_PBDR = uiPBDR;
//spi_delay(5);
if(tx_data&temp)
{
uiPADR = *GPIO_PADR | 0x02;
*GPIO_PADR = uiPADR;
}
else
{
uiPADR = *GPIO_PADR & 0xfd;
*GPIO_PADR = uiPADR;
}
Pulse();
/*
spi_delay(5);
uiPBDR = *GPIO_PBDR | 0x40;//0x40=01000000
*GPIO_PBDR = uiPBDR;
spi_delay(5);
uiPBDR = *GPIO_PBDR & 0xbf;//0xbf=10111111
*GPIO_PBDR = uiPBDR;
spi_delay(20);
*/
temp >>=1;
}
uiPADR = *GPIO_PADR & 0xfe;
*GPIO_PADR = uiPADR;
spi_delay(5);
EWDS();
}
/*----------------------------------------------------
Function : Read93LC46
Input : unsigned char Offset Address
Output : unsigned int
Description : Read 16bits data from 93LC46 offset address
------------------------------------------------------*/
unsigned int Read93LC46( unsigned int Offset_Addr )
{
unsigned int uiPADDR, uiPADR, uiPBDDR, uiPBDR, PADDRTEMP;
unsigned int Addr, i, temp, rx_data;
StartBit();
Offset_Addr=Offset_Addr&0x3f;
Addr=Offset_Addr+0x80;
temp = 0x80;
for( i=0;i<8;i++)
{
// uiPBDR = *GPIO_PBDR | 0x40;//0x40=01000000
//*GPIO_PBDR = uiPBDR;
if(Addr&temp)
{
uiPADR = *GPIO_PADR | 0x02;
*GPIO_PADR = uiPADR;
}
else
{
uiPADR = *GPIO_PADR & 0xfd;
*GPIO_PADR = uiPADR;
}
spi_delay(10);
Pulse();
//uiPBDR = *GPIO_PBDR & 0xbf;//0xbf=10111111
//*GPIO_PBDR = uiPBDR;
spi_delay(20);
temp >>=1;
}
rx_data=0x0000;
PADDRTEMP = *GPIO_PADDR;
uiPADDR = *GPIO_PADDR & 0xfd;
*GPIO_PADDR = uiPADDR;
for(i=0;i<16;i++)
{
//uiPBDR = *GPIO_PBDR | 0x40;//0x40=01000000
//*GPIO_PBDR = uiPBDR;
Pulse();
unsigned int padrtmp = *GPIO_PADR;
if( padrtmp & 0x02 )
{
rx_data |= 0x01;
}
if( i<15 )
rx_data <<=1;
//uiPBDR = *GPIO_PBDR & 0xbf;//0xbf=10111111
//*GPIO_PBDR = uiPBDR;
spi_delay(20);
}
uiPADR = *GPIO_PADR & 0xfe;
*GPIO_PADR = uiPADR;
spi_delay(5);
return(rx_data);
}
unsigned int ASCII2BINARY( unsigned int data)
{
switch( data )
{
case 0x30:return 0x0;break;
case 0x31:return 0x1;break;
case 0x32:return 0x2;break;
case 0x33:return 0x3;break;
case 0x34:return 0x4;break;
case 0x35:return 0x5;break;
case 0x36:return 0x6;break;
case 0x37:return 0x7;break;
case 0x38:return 0x8;break;
case 0x39:return 0x9;break;
case 0x41:return 0xa;break;
case 0x42:return 0xb;break;
case 0x43:return 0xc;break;
case 0x44:return 0xd;break;
case 0x45:return 0xe;break;
case 0x46:return 0xf;break;
case 0x61:return 0xa;break;
case 0x62:return 0xb;break;
case 0x63:return 0xc;break;
case 0x64:return 0xd;break;
case 0x65:return 0xe;break;
case 0x66:return 0xf;break;
default:break;
}
}
unsigned int BINARY2ASCII( unsigned int data)
{
switch( data )
{
case 0x0:return 0x30;break;
case 0x1:return 0x31;break;
case 0x2:return 0x32;break;
case 0x3:return 0x33;break;
case 0x4:return 0x34;break;
case 0x5:return 0x35;break;
case 0x6:return 0x36;break;
case 0x7:return 0x37;break;
case 0x8:return 0x38;break;
case 0x9:return 0x39;break;
case 0xa:return 0x41;break;
case 0xb:return 0x42;break;
case 0xc:return 0x43;break;
case 0xd:return 0x44;break;
case 0xe:return 0x45;break;
case 0xf:return 0x46;break;
default:break;
}
}
unsigned int HIGH2LOW( unsigned int data )
{
unsigned int tmphigh,tmplow,tmp;
tmphigh = data & 0xff00;
tmphigh >>=8;
tmplow = data & 0x00ff;
tmplow <<=8;
tmp = tmplow | tmphigh;
return tmp;
}
void CGPIO2SPIDlg::OnSet()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
UpdateData(true);
initGPIOpin();
int i,j,k;
unsigned int rx_buf;
unsigned int tmp,retmp;
m_macconfirm = L"";
i = m_macaddress.GetLength();
if(i!=12)
{
MessageBox(L"Mac address is contain 12 characters, but you Input is not!");
return;
}
MessageBox(L"Mac address is:"+m_macaddress);
for( j=0;j<i;j=j+4 )
{
tmp = 0x0000;
for( k=j;k<j+4;k++)
{
tmp= tmp|ASCII2BINARY(m_macaddress[k]);
if(k<j+3)
tmp<<=4;
}
Write93LC46(j/4,HIGH2LOW(tmp));
spi_delay(100);
//rx_buf=Read93LC46(j/4);
for( k=j;k<j+4;k++)
{
retmp = 0x00;
if( k%4 == 0 )
{
retmp = rx_buf&0xf000;
retmp >>= 12;
m_macconfirm.Insert(k,BINARY2ASCII(retmp));
}
else if( k%4 == 1)
{
retmp = rx_buf&0x0f00;
retmp >>= 8;
m_macconfirm.Insert(k,BINARY2ASCII(retmp));
}
else if( k%4 == 2)
{
retmp = rx_buf&0x00f0;
retmp >>= 4;
m_macconfirm.Insert(k,BINARY2ASCII(retmp));
}
else if( k%4 == 3)
{
retmp = rx_buf&0x000f;
//retmp >>= 12;
m_macconfirm.Insert(k,BINARY2ASCII(retmp));
}
}
//spi_delay(200);
}
m_macconfirm = m_macaddress;
UpdateData(false);
MessageBox(L"message send ok!\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -