1.实现GPIO的流驱动,源代码如下
#include <windows.h> #include <types.h> #include <ceddk.h> #include <memory.h> #include <nkintr.h> #include <pegdpar.h> #include <ddkreg.h> #include <Winuser.h> #include "AT91SAM9263_oal_intr.h" #include "atmel_spi_ioctl.h" #include "AT91SAM9263EK_ioctl.h" #include "at91sam9263_gpio.h" #define GIO_WRITE0 CTL_CODE(FILE_DEVICE_SERIAL_PORT, 2059, METHOD_BUFFERED, FILE_ANY_ACCESS) #define GIO_WRITE1 CTL_CODE(FILE_DEVICE_SERIAL_PORT, 2060, METHOD_BUFFERED, FILE_ANY_ACCESS) #define GIO_READ CTL_CODE(FILE_DEVICE_SERIAL_PORT, 2061, METHOD_BUFFERED, FILE_ANY_ACCESS) // 0 const struct pio_desc hw_pic20out[] = { {"OUT", AT91C_PIN_PC(20), 0, PIO_PULLUP, PIO_OUTPUT}, }; const struct pio_desc hw_pic20in[] = { {"OUT", AT91C_PIN_PC(20), 0, PIO_PULLUP, PIO_INPUT}, }; // 1 const struct pio_desc hw_pic21out[] = { {"OUT", AT91C_PIN_PC(21), 0, PIO_PULLUP, PIO_OUTPUT}, }; const struct pio_desc hw_pic21in[] = { {"OUT", AT91C_PIN_PC(21), 0, PIO_PULLUP, PIO_INPUT}, }; // 2 const struct pio_desc hw_pic22out[] = { {"OUT", AT91C_PIN_PC(22), 0, PIO_PULLUP, PIO_OUTPUT}, }; const struct pio_desc hw_pic22in[] = { {"OUT", AT91C_PIN_PC(22), 0, PIO_PULLUP, PIO_INPUT}, }; // 3 const struct pio_desc hw_pic23out[] = { {"OUT", AT91C_PIN_PC(23), 0, PIO_PULLUP, PIO_OUTPUT}, }; const struct pio_desc hw_pic23in[] = { {"OUT", AT91C_PIN_PC(23), 0, PIO_PULLUP, PIO_INPUT}, }; // 4 const struct pio_desc hw_pic24out[] = { {"OUT", AT91C_PIN_PC(24), 0, PIO_PULLUP, PIO_OUTPUT}, }; const struct pio_desc hw_pic24in[] = { {"OUT", AT91C_PIN_PC(24), 0, PIO_PULLUP, PIO_INPUT}, }; // 5 const struct pio_desc hw_pie18out[] = { {"OUT", AT91C_PIN_PE(18), 0, PIO_PULLUP, PIO_OUTPUT}, }; const struct pio_desc hw_pie18in[] = { {"OUT", AT91C_PIN_PE(18), 0, PIO_PULLUP, PIO_INPUT}, }; BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: //RETAILMSG(0,(TEXT("GIO Process Attach/r/n"))); break; case DLL_THREAD_ATTACH: //RETAILMSG(0,(TEXT("GIO Thread Attach/r/n"))); break; case DLL_THREAD_DETACH: //RETAILMSG(0,(TEXT("GIO Thread Detach/r/n"))); break; case DLL_PROCESS_DETACH: //RETAILMSG(0,(TEXT("GIO Process Detach/r/n"))); break; } return TRUE; } DWORD GIO_Init(LPCTSTR pContext,LPCVOID lpBusContext) { RETAILMSG(1,(TEXT("+GIO_Init/r/n"))); RETAILMSG(1,(TEXT("-GIO_Init/r/n"))); return(TRUE); } BOOL GIO_Deinit(LPCTSTR hDeviceContext) { return TRUE; } DWORD GIO_Open(LPCTSTR hDeviceContext,DWORD AccessCode,DWORD ShareMode) { return (DWORD)hDeviceContext; } DWORD GIO_Close(LPCTSTR pOpenContext) { return TRUE; } DWORD GIO_Write(LPCTSTR pOpenContext,LPCVOID pBuffer,DWORD Count) { return -1; } DWORD GIO_Read(LPCTSTR pOpenContext,LPCVOID pBuffer,DWORD Count) { return -1; } BOOL GIO_IOControl(DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut) { BOOL bError = FALSE; RETAILMSG(1,(TEXT("+GIO_IOControl/r/n"))); switch(dwCode) { case GIO_READ: switch(*pBufIn) { case 0: pio_setup(hw_pic20in, sizeof(hw_pic20in)/sizeof(struct pio_desc)); *(DWORD *)pBufOut=(DWORD)(pio_get_value(AT91C_PIN_PC(20))); break; case 1: pio_setup(hw_pic21in, sizeof(hw_pic21in)/sizeof(struct pio_desc)); *(DWORD*)pBufOut=(DWORD)(pio_get_value(AT91C_PIN_PC(21))); break; case 2: pio_setup(hw_pic22in, sizeof(hw_pic22in)/sizeof(struct pio_desc)); *(DWORD *)pBufOut=(DWORD)(pio_get_value(AT91C_PIN_PC(22))); break; case 3: pio_setup(hw_pic23in, sizeof(hw_pic23in)/sizeof(struct pio_desc)); *(DWORD *)pBufOut=(DWORD)(pio_get_value(AT91C_PIN_PC(23))); break; case 4: pio_setup(hw_pic24in, sizeof(hw_pic24in)/sizeof(struct pio_desc)); *(DWORD *)pBufOut=(DWORD)(pio_get_value(AT91C_PIN_PC(24))); break; case 5: pio_setup(hw_pie18in, sizeof(hw_pie18in)/sizeof(struct pio_desc)); *(DWORD *)pBufOut=(DWORD)(pio_get_value(AT91C_PIN_PE(18))); break; default: break; } break; case GIO_WRITE0: switch(*pBufIn) { case 0: pio_setup(hw_pic20out, sizeof(hw_pic20out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PC(20), 0); break; case 1: pio_setup(hw_pic21out, sizeof(hw_pic21out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PC(21), 0); break; case 2: pio_setup(hw_pic22out, sizeof(hw_pic22out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PC(22), 0); break; case 3: pio_setup(hw_pic23out, sizeof(hw_pic23out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PC(23), 0); break; case 4: pio_setup(hw_pic24out, sizeof(hw_pic24out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PC(24), 0); break; case 5: pio_setup(hw_pie18out, sizeof(hw_pie18out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PE(18), 0); break; } break; case GIO_WRITE1: switch(*pBufIn) { case 0: pio_setup(hw_pic20out, sizeof(hw_pic20out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PC(20), 1); break; case 1: pio_setup(hw_pic21out, sizeof(hw_pic21out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PC(21), 1); break; case 2: pio_setup(hw_pic22out, sizeof(hw_pic22out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PC(22), 1); break; case 3: pio_setup(hw_pic23out, sizeof(hw_pic23out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PC(23), 1); break; case 4: pio_setup(hw_pic24out, sizeof(hw_pic24out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PC(24), 1); break; case 5: pio_setup(hw_pie18out, sizeof(hw_pie18out)/sizeof(struct pio_desc)); pio_set_value(AT91C_PIN_PE(18),1); break; } break; } RETAILMSG(1,(TEXT("-GIO_IOControl/r/n"))); return bError; } DWORD GIO_Seek(LPCTSTR pOpenContext,LPCVOID pBuffer,DWORD Count) { return -1; }
2.sources文件
!if 0 Copyright (c) 2005. Samsung Electronics, co. ltd. All rights reserved. !endif !if 0 Use of this source code is subject to the terms of the Microsoft end-user license agreement (EULA) under which you licensed this SOFTWARE PRODUCT. If you did not accept the terms of the EULA, you are not authorized to use this source code. For a copy of the EULA, please see the LICENSE.RTF on your install media. !endif RELEASETYPE=PLATFORM TARGETNAME=IO TARGETTYPE=DYNLINK DLLENTRY=DllMain SOURCES= / IO.cpp / TARGETLIBS= / $(_COMMONSDKROOT)/lib/$(_CPUINDPATH)/coredll.lib / SOURCELIBS= / $(_TARGETPLATROOT)/lib/$(_CPUDEPPATH)/gpio.lib / MSC_WARNING_LEVEL = $(MSC_WARNING_LEVEL) /W3 /WX INCLUDES= $(_TARGETPLATROOT)/SRC/inc;$(_COMMONOAKROOT)/inc;$(INCLUDES)
3.def文件
; ; Copyright (c) Microsoft Corporation. All rights reserved. ; ; ; Use of this source code is subject to the terms of the Microsoft end-user ; license agreement (EULA) under which you licensed this SOFTWARE PRODUCT. ; If you did not accept the terms of the EULA, you are not authorized to use ; this source code. For a copy of the EULA, please see the LICENSE.RTF on your ; install media. ; EXPORTS GIO_Init GIO_Deinit GIO_Open GIO_Close GIO_Read GIO_Write GIO_Seek GIO_IOControl
4.Reg文件
[HKEY_LOCAL_MACHINE/Drivers/BuiltIn/IO] "Prefix"="GIO" "Index"=dword:1 "Dll"="IO.dll" "Order"=dword:12
5.应用程序调用
// GPIODEMODlg.cpp : implementation file // #include "stdafx.h" #include "GPIODEMO.h" #include "GPIODEMODlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CGPIODEMODlg dialog /************************************************************************/ //我们在这里手动添加GPIO测试相关的代码 //定义相关IO control code,需要用到winioctl.h文件 #include <winioctl.h> #define GIO_WRITE0 CTL_CODE(FILE_DEVICE_SERIAL_PORT, 2059, METHOD_BUFFERED, FILE_ANY_ACCESS) #define GIO_WRITE1 CTL_CODE(FILE_DEVICE_SERIAL_PORT, 2060, METHOD_BUFFERED, FILE_ANY_ACCESS) #define GIO_READ CTL_CODE(FILE_DEVICE_SERIAL_PORT, 2061, METHOD_BUFFERED, FILE_ANY_ACCESS) //定义驱动句柄 HANDLE m_hFile; //定义IO序号 INT i=0; /************************************************************************/ CGPIODEMODlg::CGPIODEMODlg(CWnd* pParent /*=NULL*/) : CDialog(CGPIODEMODlg::IDD, pParent) { //{{AFX_DATA_INIT(CGPIODEMODlg) m_state = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CGPIODEMODlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CGPIODEMODlg) DDX_Control(pDX, IDC_IONUM, m_ComboIOnum); DDX_Text(pDX, IDC_EDIT1, m_state); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CGPIODEMODlg, CDialog) //{{AFX_MSG_MAP(CGPIODEMODlg) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) ON_BN_CLICKED(IDC_BUTTON3, OnButton3) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CGPIODEMODlg message handlers BOOL CGPIODEMODlg::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 /************************************************************************/ //我们在这里手动添加GPIO测试相关的代码 //在这里我们去打开GPIO的驱动 m_hFile = CreateFile(TEXT("GIO1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0); if (m_hFile == INVALID_HANDLE_VALUE) { MessageBox(_T("GPIOTEST"),_T("Can not open GPIO1!"),MB_OK); return(FALSE); } else MessageBox(_T("GPIOTEST"),_T("Open GPIO1 success!"),MB_OK); m_ComboIOnum.SetCurSel(0); /************************************************************************/ return TRUE; // return TRUE unless you set the focus to a control } /************************************************************************/ //我们在这里手动添加GPIO测试相关的代码 void CGPIODEMODlg::OnButton1() { // TODO: Add your control notification handler code here DWORD dwReturn; UpdateData(TRUE); i=m_ComboIOnum.GetCurSel(); DeviceIoControl(m_hFile,GIO_WRITE1,&i,1,NULL,0,&dwReturn,NULL); } void CGPIODEMODlg::OnButton2() { // TODO: Add your control notification handler code here DWORD dwReturn; UpdateData(TRUE); i=m_ComboIOnum.GetCurSel(); DeviceIoControl(m_hFile,GIO_WRITE0,&i,1,NULL,0,&dwReturn,NULL); } void CGPIODEMODlg::OnButton3() { // TODO: Add your control notification handler code here DWORD dwReturn; DWORD result; UpdateData(TRUE); i=m_ComboIOnum.GetCurSel(); //SBC9261 DeviceIoControl(m_hFile,GIO_READ,&i,1,&result,1,&dwReturn,NULL); if(result==1) m_state=_T("GPIO input high:1"); else m_state=_T("GPIO input low :0"); //说明 //在sbc6000x已经发布的光盘的IO0-IO5是由TSC2301音频芯片扩展的来的IO,在驱动中读到的result的bit[5-0]对应IO5-IO5,所以在这里我们对 //result右移相应的bit位使得result的bit0代表相应IO的状态。即最终的result的bit0=1则相应的IO输入是高电平,bit0=0则相应的IO输入是低电平。 /* DeviceIoControl(m_hFile,GIO_READ,&i,1,&result,1,&dwReturn,NULL); if(i<6) result=(result>>i); if(result==1) m_state=_T("GPIO input high:1"); else m_state=_T("GPIO input low :0"); */ UpdateData(FALSE); } /************************************************************************/
-------------------------------------------------------------------------------
QQ:229425962
Phone:15820435302
Email:[email protected]
-------------------------------------------------------------------------------