/****************************************Copyright (c)**************************************************
** Guangzhou ZHIYUAN electronics Co.,LTD.
**
** http://www.zyinside.com
**
**--------------File Info-------------------------------------------------------------------------------
** File Name: Ds2401.c
** Last modified Date: 2006-08-11
** Last Version: V1.0
** Description: This driver uses EINT4 button(DS2401) On MagicARM2410
**
**------------------------------------------------------------------------------------------------------
** Created By: Ding Bao hua
** Created date: 2009-12-30
** Version: V1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Description:
**
********************************************************************************************************/
//EINT6 IS OUTPUT,EINT5 IS INPUT 。
#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 <s3c2440a.h>
#include "dskey.h"
#define PRIVATE static
#define PUBLIC
/* 驱动打开计数器 */
PRIVATE UINT32 gOpenCount = 0;
/* GPIO 寄存器对应的虚拟地址 */
PRIVATE volatile S3C2440A_IOPORT_REG * v_pIOPregs;
unsigned char GetOneCrc( unsigned char *data, int length)
{
unsigned char cFcs = 0;
unsigned short crc8 = 0x8c;
int i, j;
for(i = 0; i < length; i++)
{
cFcs ^= data[i];
for(j = 0; j < 8; j++)
{
if(cFcs & 0x01)
{
cFcs >>= 1;
cFcs ^= crc8;
}
else
cFcs >>= 1;
}
}
return cFcs;
}
void Delay_n_us(int n)
{
LARGE_INTEGER litmp;
LONGLONG QPart1,QPart2;
double dfMinus, dfFreq, dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq = (double)litmp.QuadPart;
QueryPerformanceCounter(&litmp);
QPart1 = litmp.QuadPart;
do
{
QueryPerformanceCounter(&litmp);
QPart2=litmp.QuadPart;
dfMinus=(double)(QPart2-QPart1);
dfTim=dfMinus/dfFreq;
}while(dfTim <0.000001*n);
}
/******************************************************************************
* 功能:
*****************************************************************************/
//unsigned char DS2401ReadByte( void )
//{
// RETAILMSG(1,(TEXT("Read: %X \r\n"), tmp));
// return( tmp );
//}
/******************************************************************************
* 功能:
*****************************************************************************/
void DS2401WriteByte( unsigned char Code )
{
unsigned char i;
//RETAILMSG(1,(TEXT("Code: %X \r\n"), Code));
while((v_pIOPregs-> GPFDAT&0x20)==0); //read EINT5 is 0,then wait
Delay_n_us(2);
for( i=0; i<8; i++ )
{
if((Code & 0x01)==0)
{
v_pIOPregs-> GPFDAT |=0X40; //输出高电平
//TX 发送低60US
Delay_n_us(100);
v_pIOPregs-> GPFDAT &=0XBF; //输出低电平
} else
{
v_pIOPregs-> GPFDAT |=0X40; //输出高电平
//TX 发送低10US
Delay_n_us(10);
v_pIOPregs-> GPFDAT &=0XBF; //输出低电平
Delay_n_us(50);
}
Code >>= 1;
while((v_pIOPregs-> GPFDAT&0x20)==0); //read EINT5 is 0,then wait
Delay_n_us(2);
}
}
/******************************************************************************
* 功能:
*****************************************************************************/
void DS2401ReadROM()
{
DS2401WriteByte( 0x0f );
}
void ResetDS2401()
{
unsigned char i;
v_pIOPregs-> GPFDAT &=0XBF; //输出TX变高,禁止TX发送低。
while((v_pIOPregs-> GPFDAT&0x20)==0); //read EINT5 is 0,then wait
Delay_n_us(2);
v_pIOPregs-> GPFDAT |=0X40; //输出高电平
//TX 发送低500US
Delay_n_us(500);
v_pIOPregs-> GPFDAT &=0XBF; //输出TX变高,禁止TX发送低。
for(i=0;i<100;i++)
{
Delay_n_us(1);
if((v_pIOPregs-> GPFDAT&0x20)==0) //read EINT5 is 0,THEN BREAK
{
//RETAILMSG (1, (TEXT("Ds2401 output low\r\n") ));
break;
}
}
}
/*******************************************************************************************
函数名称: EINT_InitializeAddresses
描 述: 取得相关寄存器的虚拟地址
输入参数: 无
输出参数: 无
返 回: > 0 分配得到的虚拟地址; FALSE: 分配失败
*******************************************************************************************/
PRIVATE BOOL EINT_InitializeAddresses(VOID)
{
BOOL RetValue = TRUE;
RETAILMSG(1, (TEXT(">>> EINT_initalization address..set..\r\n")));
//IO Register Allocation
v_pIOPregs = (volatile S3C2440A_IOPORT_REG *)VirtualAlloc(0, sizeof(S3C2440A_IOPORT_REG), MEM_RESERVE, PAGE_NOACCESS);
if (v_pIOPregs == NULL)
{
ERRORMSG(1,(TEXT("For IOPregs : VirtualAlloc failed!\r\n")));
RetValue = FALSE;
}
else
{
if (!VirtualCopy((PVOID)v_pIOPregs, (PVOID)(S3C2440A_BASE_REG_PA_IOPORT >> 8), sizeof(S3C2440A_IOPORT_REG), PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE))
{
ERRORMSG(1,(TEXT("For IOPregs: VirtualCopy failed!\r\n")));
RetValue = FALSE;
}
}
if (!RetValue)
{
RETAILMSG (1, (TEXT("::: EINT_InitializeAddresses - Fail!!\r\n") ));
if (v_pIOPregs)
{
VirtualFree((PVOID) v_pIOPregs, 0, MEM_RELEASE);
}
v_pIOPregs = NULL;
}
else
RETAILMSG (1, (TEXT("::: EINT_InitializeAddresses - Success\r\n") ));
return(RetValue);
}
/*******************************************************************************************
函数名称: DSY_Init
描 述: 驱动程序初始化函数
输入参数: DWORD dwContext: 设备管理器传递给本驱动的参数, 通常为流接口驱动在注册表内的位置
输出参数: 无
返 回: 驱动程序句柄
*******************************************************************************************/
PUBLIC DWORD DSY_Init(DWORD dwContext)
{
RETAILMSG(1, (TEXT("start DS2401\r\n")));
return (DWORD)1;
}
/*******************************************************************************************
函数名称: DllEntry
描 述: 驱动程序动态库入口
输入参数:
输出参数:
返 回:
*******************************************************************************************/
PUBLIC BOOL WINAPI
DllEntry(HANDLE hInstDll, DWORD dwReason, LPVOID lpvReserved)
{
switch ( dwReason )
{
case DLL_PROCESS_ATTACH:
RETAILMSG(1, (TEXT("DSK: DLL_PROCESS_ATTACH. \r\n")));
DisableThreadLibraryCalls((HMODULE) hInstDll);
break;
case DLL_PROCESS_DETACH:
RETAILMSG(1, (TEXT("DSK: DLL_PROCESS_DETACH. \r\n")));
break;
}
return (TRUE);
}
/*******************************************************************************************
函数名称: DSY_Close
描 述: 驱动程序关闭函数
输入参数: DWORD Handle:驱动程序句柄
输出参数: 无
返 回: FALSE: 失败 TRUE: 成功
*******************************************************************************************/
BOOL DSY_Close(DWORD Handle)
{
gOpenCount = 0;
RETAILMSG (1, (TEXT("ds2401_CLOSE - success!!\r\n") ));
return TRUE;
} // DSY_Close
/*******************************************************************************************
函数名称: DSY_Deinit
描 述: 驱动程序卸载函数
输入参数: DWORD dwContext: 驱动程序句柄
输出参数: 无
返 回: FALSE: 失败 TRUE: 成功
*******************************************************************************************/
BOOL DSY_Deinit(DWORD dwContext)
{
gOpenCount = 0;
RETAILMSG (1, (TE