1,驱动
#include <windows.h>
#include <types.h>
#include <excpt.h>
#include <tchar.h>
#include <cardserv.h>
#include <cardapi.h>
#include <tuple.h>
#include <devload.h>
#include <diskio.h>
#include <nkintr.h>
#include <windev.h>
#include "BSP.h"
#ifdef DEBUG
#define ZONE_GPIOCONTROL DEBUGZONE(0)
#define ZONE_FUNCTION DEBUGZONE(1)
#define ZONE_ERROR DEBUGZONE(15)
#else
#define ZONE_GPIOCONTROL 1
#define ZONE_FUNCTION 1
#define ZONE_ERROR 1
#endif
#if 0
#define DEBUG_GPIO 1
#else
#define DEBUG_GPIO 0
#endif
volatile static S3C2416_IOPORT_REG *v_pGPIORegs=NULL;
volatile S3C2416_PWM_REG * v_pPWMregs;
volatile S3C2416_INTR_REG * v_pINTRregs;
#define BKL_TCNTB_VALUE 100
#define BKL_TCMPB_MAX 48
#define BKL_TCMPB_MIN 8
#define BKL_TCNTB (v_pPWMregs->TCNTB0)
#define BKL_TCMPB (v_pPWMregs->TCMPB0)
#define BKL_TCON (v_pPWMregs->TCON)
#define BKL_TCON_OFFSETBIT (0)
#define BKL_TCON_Start (0x1 << BKL_TCON_OFFSETBIT)
#define BKL_TCON_Update (0x2 << BKL_TCON_OFFSETBIT)
#define BKL_TCON_AutoReload (0x8 << BKL_TCON_OFFSETBIT)
BOOL mInitialized;
BOOL InitializeAddresses(VOID); // Virtual allocation
BOOL InitializeAddresses(VOID)
{
BOOL RetValue = TRUE;
PHYSICAL_ADDRESS ioPhysicalBase = {0,0};
ioPhysicalBase.LowPart = S3C2416_BASE_REG_PA_IOPORT;
v_pGPIORegs = (volatile S3C2416_IOPORT_REG *)MmMapIoSpace(ioPhysicalBase, sizeof(S3C2416_IOPORT_REG), FALSE);
if (v_pGPIORegs == NULL)
{
RETAILMSG(ZONE_ERROR, (TEXT("[GIO] v_pGPIORegs MmMapIoSpace() Failed \r\n")));
RetValue = FALSE;
}
v_pPWMregs = (volatile S3C2416_PWM_REG *)VirtualAlloc(0, sizeof(S3C2416_PWM_REG), MEM_RESERVE, PAGE_NOACCESS);
if (v_pPWMregs == NULL)
{
ERRORMSG(1,(TEXT("For PWMregs : VirtualAlloc failed!\r\n")));
RetValue = FALSE;
}
else
{
if (!VirtualCopy((PVOID)v_pPWMregs, (PVOID)(S3C2416_BASE_REG_PA_PWM >> 8), sizeof(S3C2416_PWM_REG), PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE))
{
ERRORMSG(1,(TEXT("For PWMregs: VirtualCopy failed!\r\n")));
RetValue = FALSE;
}
}
if (!RetValue)
{
RETAILMSG (1, (TEXT("::: PWM_InitializeAddresses - Fail!!\r\n") ));
if (v_pPWMregs)
VirtualFree((PVOID) v_pPWMregs, 0, MEM_RELEASE);
v_pPWMregs = NULL;
}
v_pPWMregs->TCFG1 |= (0x0F << 0);
v_pPWMregs->TCFG1 |= (0 << 0);
return(RetValue);
}
BOOL WINAPI
DllEntry(HANDLE hinstDLL, DWORD dwReason, LPVOID /* lpvReserved */)
{
switch(dwReason)
{
case DLL_PROCESS_ATTACH:
DEBUGREGISTER((HINSTANCE)hinstDLL);
return TRUE;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
#ifdef UNDER_CE
case DLL_PROCESS_EXITING:
break;
case DLL_SYSTEM_STARTED:
break;
#endif
}
return TRUE;
}
BOOL BLK_Deinit(DWORD hDeviceContext)
{
BOOL bRet = TRUE;
if(v_pGPIORegs != NULL)
{
MmUnmapIoSpace((PVOID)v_pGPIORegs, sizeof(S3C2416_IOPORT_REG));
v_pGPIORegs = NULL;
}
RETAILMSG(DEBUG_GPIO,(TEXT("GPIO_Control: GIO_Deinit\r\n")));
return TRUE;
}
DWORD BLK_Init(DWORD dwContext)
{
HANDLE KeyThread;
DWORD threadID;
RETAILMSG(DEBUG_GPIO, (TEXT("BLK Initialize ...")));
if (!InitializeAddresses())
return (FALSE);
RETAILMSG(DEBUG_GPIO, (TEXT("OK !!!\r\n")));
return TRUE;
}
BOOL BLK_IOControl(DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut)
{
if(dwCode<2000)
{
v_pGPIORegs->GPBCON &= ~(0x03 << 0);
v_pGPIORegs->GPBCON |= (0x01 << 1);// rGPBCON[3:2] = 10,设置GPB0 为输入TOUT
BKL_TCNTB = 2000;
BKL_TCMPB = dwCode;
v_pPWMregs->TCFG0 &= ~(0xF << 0);
v_pPWMregs->TCFG1 &= ~(0xF << 0);
v_pPWMregs->TCFG1 |= (0 << 0);
BKL_TCON |= BKL_TCON_Update;
BKL_TCON &= ~BKL_TCON_Update;
BKL_TCON |=BKL_TCON_Start| BKL_TCON_AutoReload;//| (1<<2);;//
}
else
{
v_pGPIORegs->GPBCON &= ~(0x03 << 0);
v_pGPIORegs->GPBCON |= (0x01 << 0);// rGPBCON[1:0] = 00,设置GPB0 为输入OUT
v_pGPIORegs->GPBDAT = v_pGPIORegs->GPBDAT | 0x1;
}
RETAILMSG(1, (TEXT("GPIO_Control:Ioctl code = 0x%x\r\n"), dwCode));
return TRUE;
}
DWORD BLK_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)
{
RETAILMSG(DEBUG_GPIO, (TEXT("GPIO_Control: GPIO_Open\r\n")));
return TRUE;
}
BOOL BLK_Close(DWORD hOpenContext)
{
RETAILMSG(DEBUG_GPIO,(TEXT("GPIO_Control: GPIO_Close\r\n")));
return TRUE;
}
void BLK_PowerDown(DWORD hDeviceContext)
{
RETAILMSG(DEBUG_GPIO, (TEXT("GPIO_Control: GPIO_PowerDown\r\n")));
}
void BLK_PowerUp(DWORD hDeviceContext)
{
RETAILMSG(DEBUG_GPIO, (TEXT("GPIO_Control: GPIO_PowerUp\r\n")));
}
DWORD BLK_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
{
RETAILMSG(DEBUG_GPIO, (TEXT("GPIO_Control: GPIO_Read\r\n")));
return TRUE;
}
DWORD BLK_Seek(DWORD hOpenContext, long Amount, DWORD Type)
{
RETAILMSG(DEBUG_GPIO, (TEXT("GPIO_Control: GPIO_Seek\r\n")));
return 0;
}
DWORD BLK_Write(DWORD hOpenContext, LPCVOID pSourceBytes, DWORD NumberOfBytes)
{
RETAILMSG(DEBUG_GPIO, (TEXT("GPIO_Control: GPIO_Write\r\n")));
return 0;
}
2,应用程序
// blkDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "blk.h"
#include "blkDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CblkDlg 对话框
CblkDlg::CblkDlg(CWnd* pParent /*=NULL*/)
: CDialog(CblkDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CblkDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SLIDER3, m_ctrlSlider);
}
BEGIN_MESSAGE_MAP(CblkDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CblkDlg::OnBnChangeBlk)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER3, &CblkDlg::OnNMCustomdrawSlider3)
ON_BN_CLICKED(IDC_BUTTON2, &CblkDlg::OnBnClickedButton2)
END_MESSAGE_MAP()
// CblkDlg 消息处理程序
BOOL CblkDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
m_ctrlSlider.SetRange(1,100); //设置滑块宽度
// TODO: 在此添加额外的初始化代码
m_ctrlSlider.SetPos(100);//设置最亮
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CblkDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
if (AfxIsDRAEnabled())
{
DRA::RelayoutDialog(
AfxGetResourceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_BLK_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_BLK_DIALOG));
}
}
#endif
void CblkDlg::OnNMCustomdrawSlider3(NMHDR *pNMHDR, LRESULT *pResult)
{
CString cs;
int nPos = m_ctrlSlider.GetPos();
blk=CreateFile(_T("BLK1:"),GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
DeviceIoControl(blk,(nPos)*20,NULL,0,NULL,0,NULL,NULL);
}