GPIO驱动程序及测试程序(总8页)
--本页仅作为文档封面,使用时请直接删除即可-- --内页可以根据需求调整合适字体及大小--
GPIO驱动程序,主要是GIO_Control:
BOOL GIO_IOControl(DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut){switch(dwCode){case IO_CTL_GPIO_A_CON: v_pIOPregs->GPACON= *(DWORD*)pBufIn;//GPACON
SETbreak; case IO_CTL_GPIO_A_DAT_OUTPUT:v_pIOPregs->GPADAT= *(DWORD*)pBufIn; //GPADAT SET (OUTPUT) also for Functional Pinbreak; case
IO_CTL_GPIO_B_CON: v_pIOPregs->GPBCON= *(DWORD*)pBufIn;//GPBCON SETbreak;case IO_CTL_GPIO_B_DAT_INPUT: *(DWORD*)pBufOut=v_pIOPregs->GPBDAT; //GPBDAT SET (INPUT) also for Functional Pinbreak; case IO_CTL_GPIO_B_DAT_OUTPUT:v_pIOPregs->GPBDAT= *(DWORD*)pBufIn; //GPBDAT SET (OUTPUT) also for Functional Pinbreak;case
IO_CTL_GPIO_B_UP: v_pIOPregs->GPBUP= *(DWORD*)pBufIn;//GPBUP SETbreak; case IO_CTL_GPIO_C_CON: v_pIOPregs->GPCCON= *(DWORD*)pBufIn;//GPCCON SETbreak;case IO_CTL_GPIO_C_DAT_INPUT: *(DWORD*)pBufOut=v_pIOPregs->GPCDAT; //GPCDAT SET (INPUT) also for Functional Pinbreak; case IO_CTL_GPIO_C_DAT_OUTPUT:v_pIOPregs->GPCDAT= *(DWORD*)pBufIn; //GPCDAT SET (OUTPUT) also for Functional Pinbreak;case IO_CTL_GPIO_C_UP: v_pIOPregs->GPCUP= *(DWORD*)pBufIn;//GPCUP SETbreak; case
IO_CTL_GPIO_D_CON: v_pIOPregs->GPDCON= *(DWORD*)pBufIn;//GPDCON SETbreak;case IO_CTL_GPIO_D_DAT_INPUT: *(DWORD*)pBufOut=v_pIOPregs->GPDDAT; //GPDDAT SET (INPUT) also for Functional Pinbreak; case IO_CTL_GPIO_D_DAT_OUTPUT:v_pIOPregs->GPDDAT= *(DWORD*)pBufIn; //GPDDAT SET (OUTPUT) also for Functional Pinbreak;case IO_CTL_GPIO_D_UP: v_pIOPregs->GPDUP= *(DWORD*)pBufIn;//GPDUP SETbreak; case
IO_CTL_GPIO_E_CON: v_pIOPregs->GPECON= *(DWORD*)pBufIn;//GPECON SETbreak;case IO_CTL_GPIO_E_DAT_INPUT: *(DWORD*)pBufOut=v_pIOPregs->GPEDAT; //GPEDAT SET (INPUT) also for Functional Pinbreak; case IO_CTL_GPIO_E_DAT_OUTPUT:v_pIOPregs->GPEDAT= *(DWORD*)pBufIn; //GPEDAT SET (OUTPUT) also for Functional Pinbreak;case IO_CTL_GPIO_E_UP: v_pIOPregs->GPEUP= *(DWORD*)pBufIn;//GPEUP SETbreak;case
IO_CTL_GPIO_F_CON: v_pIOPregs->GPFCON= *(DWORD*)pBufIn;//GPFCON SETbreak;case IO_CTL_GPIO_F_DAT_INPUT: *(DWORD*)pBufOut=v_pIOPregs->GPFDAT; //GPFDAT SET (INPUT) also for Functional Pinbreak; case IO_CTL_GPIO_F_DAT_OUTPUT:v_pIOPregs->GPFDAT= *(DWORD*)pBufIn; //GPFDAT SET (OUTPUT) also for Functional Pinbreak;case IO_CTL_GPIO_F_UP: v_pIOPregs->GPFUP= *(DWORD*)pBufIn;//GPFUP SETbreak; case
IO_CTL_GPIO_G_CON: v_pIOPregs->GPGCON= *(DWORD*)pBufIn;//GPGCON SETbreak;case IO_CTL_GPIO_G_DAT_INPUT: *(DWORD*)pBufOut=v_pIOPregs->GPGDAT; //GPGDAT SET (INPUT) also for Functional Pinbreak; case IO_CTL_GPIO_G_DAT_OUTPUT:v_pIOPregs->GPGDAT= *(DWORD*)pBufIn; //GPGDAT SET (OUTPUT) also for Functional Pinbreak;case
IO_CTL_GPIO_G_UP: v_pIOPregs->GPGUP= *(DWORD*)pBufIn;//GPGUP SETbreak; case IO_CTL_GPIO_H_CON: v_pIOPregs->GPHCON= *(DWORD*)pBufIn;//GPHCON SETbreak;case IO_CTL_GPIO_H_DAT_INPUT: *(DWORD*)pBufOut=v_pIOPregs->GPHDAT; //GPHDAT SET (INPUT) also for Functional Pinbreak; case IO_CTL_GPIO_H_DAT_OUTPUT:v_pIOPregs->GPHDAT= *(DWORD*)pBufIn; //GPHDAT SET (OUTPUT) also for Functional Pinbreak;case IO_CTL_GPIO_H_UP: v_pIOPregs->GPHUP= *(DWORD*)pBufIn;//GPHUP SETbreak; case
IO_CTL_GPIO_J_CON: v_pIOPregs->GPJCON= *(DWORD*)pBufIn;//GPJCON SETbreak;case IO_CTL_GPIO_J_DAT_INPUT: *(DWORD*)pBufOut=v_pIOPregs->GPJDAT; //GPJDAT SET (INPUT) also for Functional Pinbreak; case IO_CTL_GPIO_J_DAT_OUTPUT:v_pIOPregs->GPJDAT=
2
*(DWORD*)pBufIn; //GPJDAT SET (OUTPUT) also for Functional Pinbreak;case IO_CTL_GPIO_J_UP: v_pIOPregs->GPJUP= *(DWORD*)pBufIn;//GPJUP
SETbreak; default:break;} RETAILMSG(0,(TEXT(\0x%x\\r\\n\
测试程序,封装了2个输入输出GPIO函数,调用十分方便:
#include \DEBUG_NEW#endif DWORD RData,BUFCON,BUFOUT,BUFIN; struct
GPIO { DWORD GPIOCON; DWORD GPIOIN;DWORD GPIOOUT; }; struct
GPIO GPA={gpacon,NULL,gpaout }, GPB={gpbcon,gpbin,gpbout }, GPC={gpccon,gpcin,gpcout }, GPD={gpdcon,gpdin,gpdout }, GPE={gpecon,gpein,gpeout }, GPF={gpfcon,gpfin,gpfout }, GPG={gpgcon,gpgin,gpgout }, GPH={gphcon,gphin,gphout }, GPJ={gpjcon,gpjin,gpjout }; //define and choose GPIO // CRelay2Dlg 对话框
CRelay2Dlg::CRelay2Dlg(CWnd* pParent /*=NULL*/): CDialog(CRelay2Dlg::IDD, pParent){m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CRelay2Dlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CRelay2Dlg, CDialog)#if defined(_DEVICE_RESOLUTION_AWARE)
&& !defined(WIN32_PLATFORM_WFSP)ON_WM_SIZE()#endif//}}AFX_MSG_MAPON_BN_CLICKED(IDC_BUTTON1, &CRelay2Dlg::OnBnClickedButton1)ON_BN_CLICKED(IDC_BUTTON2,
&CRelay2Dlg::OnBnClickedButton2)END_MESSAGE_MAP()// CRelay2Dlg 消息处理程序BOOL
CRelay2Dlg::OnInitDialog(){CDialog::OnInitDialog();// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动// 执行此操作SetIcon(m_hIcon, TRUE);// 设置大图标SetIcon(m_hIcon, FALSE);// 设置小图标// TODO: 在此添加额外的初始化代码
gpiodriver=CreateFile(L\
0,NULL,OPEN_EXISTING,0,NULL );//gpiodriver initif(!gpiodriver)MessageBox(L\打开GPIO设备失败!\// 除非将焦点设置到控件,否则返回 TRUE}#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)void CRelay2Dlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/){if
(AfxIsDRAEnabled()){DRA::RelayoutDialog(AfxGetResourceHandle(), this->m_hWnd, DRA::GetDisplayMode() !=
DRA::Portrait MAKEINTRESOURCE(IDD_RELAY2_DIALOG_WIDE) : MAKEINTRESOURCE(IDD_RELAY2_DIALOG));}}#endif DWORD CRelay2Dlg::GPIOINPUT(DWORD GPIOCON,DWORD GPIOIN,DWORD BUFCON,DWORD BUFOUT ) //the function is used for GPIO INPUT{ DWORD
RData; DeviceIoControl(gpiodriver,GPIOCON,&BUFCON,4,NULL,0,NULL,NULL); DeviceIoControl(gpiodriver,GPIOIN,NULL,0,&BUFOUT,4,NULL,NULL); memcpy(&RData,&BUFOUT,4);return RData;} DWORD CRelay2Dlg::GPIOINPUT1(struct GPIO GP,USHORT NUM ) //the function is used for GPIO INPUT BETTER { BUFCON
&=~(3<<(2*NUM)); DeviceIoControl(gpiodriver,,&BUFCON,4,NULL,0,NULL,NULL); DeviceIoControl(gpiodriver,,NULL,0,&BUFOUT,4,NULL,NULL); memcpy(&RData,&BUFOUT,4);return RData; }VOID CRelay2Dlg::GPIOOUTPUT(DWORD GPIOCON,DWORD GPIOOUT,DWORD BUFCON,DWORD BUFIN ) //the function is used for GPIO
OUTPUT{ DeviceIoControl(gpiodriver,GPIOCON,&BUFCON,4,NULL,0,NULL,NULL); DeviceIoControl(gpiodriver,GPIOOUT,&BUFIN,4,NULL,0,NULL,0); }VOID
3