/***************************************************************
*****IIC驱动程序,先在文件oalintr.h添加I2C的中断*****************
*****号的宏定义,然后在文件cfw.c的文件中添加I2C中断的初**********
*****始化,禁止和复位。然后编写流驱动程序***********************
****************************************************************/
#include <windows.h>
#include <types.h>
#include <ceddk.h>
#include <memory.h>
#include <hw16550.h>
#include <nkintr.h>
#include <devload.h>
#include <windev.h>
#include <notify.h>
#include "s2410.h"
#include "serhw.h"
#include <bootarg.h>
#include <oalintr.h>
#include <pc.h>
#include "MyDriver.h"
//#define ZONE_INIT (1)
#define DEBUGMODE 0
#define BAUD_TABLE_SIZE 23
#define PRIVATE static
#define RETAILMODE 0
PRIVATE volatile IICreg *v_pIICregs = (IICreg *)IIC_BASE;
PRIVATE volatile IOPreg *v_pIOPregs = (IOPreg *)IOP_BASE;
PRIVATE volatile INTreg *v_pINTregs = (INTreg *)INT_BASE;
VOID IIC_VirtualFree(VOID);
PRIVATE PVOID IOP_RegAlloc(PVOID addr, INT sz);
PRIVATE BOOL IIC_VirtualAlloc(VOID);
void IICEventHandler();
void Run_IicPoll(void);
void IICPoll(void);
void Delay(int time);
static U8 _iicData[IICBUFSIZE];
static volatile int _iicDataCount;
static volatile int _iicStatus;
static volatile int _iicMode;
static int _iicPt;
HANDLE DispatchThread;
HANDLE IIC_Event = NULL;
/*static Uchar iicData[IICBUFSIZE];
static volatile int iicDataCount;
static volatile int iicStatus;
static volatile int iicMode;
static int iicPt;*/
static DWORD IICDispatchThread(void);
//void WriteBlock(U32 slvAddr,U32 addr,Uchar * data);
void WriteBlock(U32 slvAddr,U32 addr,Uchar data);
void ReadBlock(Uint slvAddr,Uint addr,Uchar *data);
BOOL StartDispatchThread();
BOOL IIC_Deinit(PVOID pHead);
/**********************************************************************************
***********函数名:IIC_RegAlloc(PVOID addr, INT sz)
***********参数: PVOID addr(总线相对地址), INT sz(需要映射的字节数)
***********功能: 把芯片中针对I2C的物理地址和操作系统的虚存空间联系起来.即虚拟地址分配
***********返回值:基虚拟地址
***********************************************************************************/
PRIVATE PVOID IIC_RegAlloc(PVOID addr, INT sz)
{
PVOID reg;
reg = (PVOID)VirtualAlloc(0, sz, MEM_RESERVE, PAGE_NOACCESS);
if (reg)
{
if (!VirtualCopy(reg, addr, sz, PAGE_READWRITE | PAGE_NOCACHE ))
{
RETAILMSG( DEBUGMODE,( TEXT( "For s2410IIC: VirtualCopy failed \n\r" ) ) );
VirtualFree(reg, sz, MEM_RELEASE);
reg = NULL;
}
}
else
{
RETAILMSG(DEBUGMODE,(TEXT("For s2410IIC: VirtualAlloc failed!\r\n")));
}
return reg;
}
/**********************************************************************************
***********函数名:INT_RegAlloc(PVOID addr, INT sz)
***********参数: PVOID addr(总线相对地址), INT sz(需要映射的字节数)
***********功能: 把芯片中针对INT的物理地址和操作系统的虚存空间联系起来.即虚拟地址分配
***********返回值:基虚拟地址
***********************************************************************************/
PRIVATE PVOID INT_RegAlloc(PVOID addr, INT sz)
{
PVOID reg;
reg = (PVOID)VirtualAlloc(0, sz, MEM_RESERVE, PAGE_NOACCESS);
if (reg)
{
if (!VirtualCopy(reg, addr, sz, PAGE_READWRITE | PAGE_NOCACHE ))
{
RETAILMSG( DEBUGMODE,( TEXT( "For s2410INT: VirtualCopy failed \n\r" ) ) );
VirtualFree(reg, sz, MEM_RELEASE);
reg = NULL;
}
}
else
{
RETAILMSG(DEBUGMODE,(TEXT("For s2410INT: VirtualAlloc failed!\r\n")));
}
return reg;
}
/*===============================================================================*/
/*
@doc INTERNAL
@func BOOL | DllEntry | Process attach/detach api.
*
@rdesc The return is a BOOL, representing success (TRUE) or failure (FALSE).
*/
BOOL
DllEntry(
HINSTANCE hinstDll, /*@parm Instance pointer. */
DWORD dwReason, /*@parm Reason routine is called. */
LPVOID lpReserved /*@parm system parameter. */
)
{
if ( dwReason == DLL_PROCESS_ATTACH ) {
DEBUGREGISTER(hinstDll);
// DEBUGMSG (ZONE_INIT, (TEXT("IIC process attach\r\n")));
DisableThreadLibraryCalls((HMODULE) hinstDll);
}
if ( dwReason == DLL_PROCESS_DETACH ) {
// DEBUGMSG (ZONE_INIT, (TEXT("IIC process detach called\r\n")));
}
return(TRUE);
}
/**********************************************************************************
***********函数名:IOP_RegAlloc(PVOID addr, INT sz)
***********参数: PVOID addr(总线相对地址), INT sz(需要映射的字节数)
***********功能: 把芯片中针对IOP的物理地址和操作系统的虚存空间联系起来.
***********返回值:基虚拟地址
***********************************************************************************/
PRIVATE PVOID IOP_RegAlloc(PVOID addr, INT sz)
{
PVOID reg;
reg = (PVOID)VirtualAlloc(0, sz, MEM_RESERVE, PAGE_NOACCESS);
if (reg)
{
if (!VirtualCopy(reg, addr, sz, PAGE_READWRITE | PAGE_NOCACHE ))
{
RETAILMSG( DEBUGMODE,( TEXT( "For s2410IOP: VirtualCopy failed!\n\r" ) ) );
VirtualFree(reg, sz, MEM_RELEASE);
reg = NULL;
}
}
else
{
RETAILMSG(DEBUGMODE,(TEXT("For s2410IOP: VirtualAlloc failed!\r\n")));
}
return reg;
}
/**********************************************************************************************
**********函数名: IIC_VirtualAlloc
**********功能: 为IIC取得注册表信息并且用它来得到COM端口序列号IO端口基地址和中断号
**********参数说明:LPCTSTR regKeyPath是传送给IIC的注册路径
**********返回值: 如果错误返回 r
**********************************************************************************************/
PRIVATE BOOL IIC_VirtualAlloc(VOID)
{
BOOL r = FALSE;
RETAILMSG(RETAILMODE,(TEXT("::: IIC_VirtualAlloc()\r\n")));
do
{
v_pIOPregs = (volatile IOPreg *)IOP_RegAlloc((PVOID)IOP_BASE, sizeof(IOPreg));
if (v_pIOPregs == NULL)
{
RETAILMSG(RETAILMODE,(TEXT("For IOPreg: VirtualAlloc failed!\r\n")));
break;
}
v_pIICregs = (volatile IICreg *)IIC_RegAlloc((PVOID)IIC_BASE, sizeof(IICreg));
if (v_pIICregs == NULL)
{
RETAILMSG(RETAILMODE,(TEXT("For IICreg: VirtualAlloc failed!\r\n")));
break;
}
/*v_pINTregs = (volatile INTreg *)INT_RegAlloc((PVOID)INT_BASE, sizeof(INTreg));
if (v_pINTregs == NULL)
{
ERRORMSG(RETAILMODE,(TEXT("For INTreg: VirtualAlloc failed!\r\n")));
break;
}*/
r = TRUE;
} while (0);
if (!r)
{
IIC_VirtualFree();
RETAILMSG(RETAILMODE,(TEXT("::: IIC_VirtualAlloc() - Fail\r\n")));
}
else
{
RETAILMSG(RETAILMODE,(TEXT("::: IIC_VirtualAlloc() - Success\r\n")));
}
return r;
}
/**********************************************************************************************
**********函数名: IIC_VirtualFree
**********功能: 为线程取消虚拟地址空间
**********参数说明:LPCTSTR regKeyPath是传送给IIC的注册路径
**********返回值: 如果错误返回 r
**********************************************************************************************/
PRIVATE void IIC_VirtualFree(VOID)
{
RETAILMSG(0,(TEXT("::: IIC_VirtualFree()\r\n")));
if (v_pIOPregs)
{
VirtualFree((PVOID)v_pIOPregs, sizeof(IOPreg), MEM_RELEASE);
v_pIOPregs = NULL;
}
if (v_pIICregs)
{
VirtualFree((PVOID)v_pIICregs, sizeof(IICreg), MEM_RELEASE);
v_pIICregs = NULL;
}
}
/********************************************************************************************
***********函数名:IIC_Init
***********功能: 初始化IIC各参数
***********参数: Identifier(设备标实符),pMddHead,pHWObj
***********返回值:
*********************************************************************************************/
DWORD IIC_Init(ULONG Identifier)
{
RETAILMSG(RETAILMODE,(TEXT("Go into IIC_Init \n"