Chinaunix首页 | 论坛 | 博客
  • 博客访问: 485598
  • 博文数量: 164
  • 博客积分: 4024
  • 博客等级: 上校
  • 技术积分: 1580
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-10 16:27
文章分类

全部博文(164)

文章存档

2011年(1)

2010年(108)

2009年(55)

我的朋友

分类: 嵌入式

2010-05-05 09:20:15

还是把这个最初版本保留下来吧 ,虽然程序结构安排的比较弱,也没进行整理,不过感觉这个看起来原理更清晰 呵呵 一些结构参照的周立功的i2c驱动程序 
#include
#include
#include
#include "s3c2440a_dmatransfer.h"
#include "s3c2440a_iicbus.h"
#include "s3c2440a_ioport.h"
#include
#include
#include
#include "Dbgapi.h"
#include "basetsd.h"
#include "BSP.h"
#include "pmplatform.h"
#include "Pkfuncs.h"
volatile S3C2440A_IOPORT_REG* v_pIOPregs;
volatile S3C2440A_IICBUS_REG* v_pIicbus;
volatile S3C2440A_INTR_REG* v_pINTRregs;
BYTE volatile IICSla=0xb0;
BYTE volatile I2cStatus;
void InitIOPort(void);
int Virtualaddress();
void SendInit();
UINT32 g_IICSysIntr = -1;
HANDLE gIICIntrEvent;
HANDLE gIICEvent;
#define IO_reg_ADDR 0x56000000
#define IIC_REG_ADDR 0x54000000
#define INT_reg_addr 0x4a000008 //Interrupt mask control
/* 定义 I2C 总线的各种状态, 中断服务线程根据该状态运行 */
#define IIC_STATUS_SETADDR 0x01    /* 设置地址状态 */
#define IIC_STATUS_RECEIVE 0x02
#define IIC_STATUS_SENDLEN  0x03
#define IIC_STATUS_SENDCMD  0x04
#define IIC_STATUS_SENDDAT  0x05
#define IIC_STATUS_SENDNOR  0x06
#define IICCON_DACK(IICCON)  IICCON = IICCON & (~(1 << 4)) | (1 << 7);   /* 使能 ACK 位 */
#define IICCON_DNACK(IICCON) IICCON &= ~((1 << 4) | (1 << 7));   /* 禁止 ACK 位 */
#define I2C_ERROR_NO_ERR 0x0  
#define I2C_ERROR_SETADDR   0x1
#define I2C_ERROR_RECEIVE   0x2
#define I2C_ERROR_SEND      0x3
#define I2C_RW_TIMEOUT  2000
/* 是否退出中断服务线程 */
UINT32 g_bKillIST = FALSE;
//volatile IICreg* v_pIICPregs;
BYTE IICError = 0;
void  InitIOPort(void)
{
 v_pIOPregs->GPECON&=0x0fffffff;  //gpe14 gpe15:1010
 v_pIOPregs->GPECON|=0xa0000000;
   
 v_pIicbus->IICCON=0xE0;     //中断有效
 v_pIicbus->IICSTAT=0xD0;       //主发送模式
}
void SendInit()
{
       v_pIicbus->IICCON = v_pIicbus->IICCON & (~(1 << 4)) | (1 << 7);   //使能 ACK 位 */ 
    v_pIicbus->IICSTAT = 0xD0;
    
    return;
}
/*******************************************************************************************
函数名称: IIC_RecByte
描    述: 从 IIC 总线上接收一个字节
输入参数: 无 
输出参数: 无
返    回: 无
********************************************************************************************/
BYTE IIC_RecByte(void)
{
 return (v_pIicbus->IICDS);
}
/*******************************************************************************************
函数名称: SendByteI2C
描    述: 发送一个字节的数据
输入参数: BYTE data: 要发送的字节
输出参数: 无
返    回: 无
********************************************************************************************/
void IIC_SendByte(BYTE data)
{
 // 将数据写入I2C数据寄存器
 v_pIicbus->IICDS = data; 
 
 // 清除中断标志,允许发送数据操作       
 IICCON_DACK(v_pIicbus->IICCON);  
 
}
void  StartI2C(BYTE sla)

 v_pIicbus->IICDS = sla;         // 设置从机地址    
 
 if(sla & 0x01)
 { 
     v_pIicbus->IICSTAT= (2 << 6) | (1 << 5) | (1 << 4); // 主接收模式,发送使能,启动总线
  IICCON_DACK(v_pIicbus->IICCON);      // 若是重启总线, 则需要有此操作
    }
    else
    { 
        v_pIicbus->IICSTAT = (3 << 6) | (1 << 5) | (1 << 4); // 主发送模式,发送使能,启动总线
    }
  

/*******************************************************************************************
函数名称: StopI2C
描    述: 结束总线
输入参数: BYTE send: I2C当前模模式.主发送模式时为1,否则为0(主接收模式)
输出参数: 无
返    回: 无
********************************************************************************************/
void  StopI2C(BYTE send)

  if(send)
 { 
     v_pIicbus->IICSTAT = (3 << 6) | (0 << 5) | (1 << 4);   // 发送结束信号
 }
 else
 { 
     v_pIicbus->IICSTAT = (2 << 6) | (0 << 5) | (1 << 4);   // 发送结束信号
 }
 IICCON_DACK(v_pIicbus->IICCON);
 Sleep(1);  // 等待结束信号产生完毕
}
int Virtualaddress()
{
 v_pIOPregs = (volatile S3C2440A_IOPORT_REG*)VirtualAlloc(0, sizeof(S3C2440A_IOPORT_REG), MEM_RESERVE, PAGE_NOACCESS);
 if (v_pIOPregs == NULL)
 {
  RETAILMSG(1, (TEXT("内存分配失败\r\n")));
    return 0;
  
 }
 if (!VirtualCopy((PVOID)v_pIOPregs, (PVOID)(IO_reg_ADDR>>8), sizeof(S3C2440A_IOPORT_REG), PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL))
 {
  RETAILMSG(1, (TEXT("地址映射失败\r\n")));
       return 0;
 }
 v_pIicbus = (volatile S3C2440A_IICBUS_REG*)VirtualAlloc(0, sizeof(S3C2440A_IICBUS_REG), MEM_RESERVE, PAGE_NOACCESS);
 if (v_pIicbus == NULL)
 {
  RETAILMSG(1, (TEXT("内存分配失败\r\n")));
       return 0;
 
 }
 if (!VirtualCopy((PVOID)v_pIicbus, (PVOID)(IIC_REG_ADDR>>8), sizeof(S3C2440A_IICBUS_REG), PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL))
 {
  RETAILMSG(1, (TEXT("地址映射失败\r\n")));
        return 0;
 }
 v_pINTRregs = (volatile S3C2440A_INTR_REG *)VirtualAlloc(0,
                sizeof(S3C2440A_INTR_REG), MEM_RESERVE,PAGE_NOACCESS);
  if (v_pINTRregs == NULL) //分配虚拟空间不成功
  {
     ERRORMSG(1,(TEXT("For INPregs : VirtualAlloc failed!\r\n")));
  return 0;    
  }
 
  /*把v_pINTRregs 虚拟空间映射到物理内存*/
    if(!VirtualCopy((PVOID)v_pINTRregs,(PVOID)(INT_reg_addr >>8) , sizeof(S3C2440A_INTR_REG),
       PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE))
 {
       ERRORMSG(1,(TEXT("For INPregs: VirtualCopy failed!\r\n")));
    return 0;     
 }
return TRUE;
}

DWORD RFDProcThread(PVOID pArg)
{
 RETAILMSG(1, (TEXT("RFDProcTread..\r\n")));
 DWORD ret;
 UINT32 IRQ;   //设置IIC中断
 // 创建I2C中断中断事件
 gIICIntrEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
 if(NULL==gIICIntrEvent)
 {
  RETAILMSG(1, (TEXT("ERROR: failed to Create event!\r\n")));
  return FALSE;
 }
 IRQ=IRQ_IIC;
 if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &IRQ, sizeof(UINT32),
  &g_IICSysIntr, sizeof(UINT32), NULL))
 {
  RETAILMSG(1,
   (TEXT("ERROR: RFID: Failed to request sysintr value.\r\n")));
  return FALSE;
 } 
 // 初始化 I2C 中断: 注册中断事件, 允许 I2C 中断
 if (!(InterruptInitialize(g_IICSysIntr, gIICIntrEvent, NULL, 0)))
 {
  CloseHandle(gIICIntrEvent);
  RETAILMSG(1, (TEXT("ERROR: I2C Bus: Interrupt initialize failed.\r\n")));
  return 0;
 }
 // IIC 中断线程开始运行
 while (1)
 {
  
  ret = WaitForSingleObject(gIICIntrEvent, INFINITE);
  if ((ret != WAIT_OBJECT_0) || (g_bKillIST == TRUE))     /* 驱动卸载或错误发生 */
  {  
   CloseHandle(gIICIntrEvent);
   RETAILMSG(1, (TEXT("INFO: I2C Bus Driver DeInit or Error Occur. \r\n")));
   return 0;              /* 线程退出 */
  }

  switch(I2cStatus)
  {
   
      // Start IIC Status
   case IIC_STATUS_SETADDR:    
    if((v_pIicbus->IICSTAT & 0x09) == 0)    // 发送地址成功
    {   
     IICError = I2C_ERROR_NO_ERR;    
     
    }
    else
    {
     if (v_pIicbus->IICDS & 0x01)
      v_pIicbus->IICSTAT = (2 << 6) | (0<<5) | (1<<4); // 读终止
     else  
      v_pIicbus->IICSTAT = (3 << 6) | (0<<5) | (1<<4); // 写终止   
      IICCON_DACK(v_pIicbus->IICCON); 
     Sleep(1); // 等待结束信号产生完毕  
   
     IICError = I2C_ERROR_SETADDR;
    }   
   
   break;
      // Send Bytes Status
      case IIC_STATUS_SENDLEN:
    if((v_pIicbus->IICSTAT & 0x09) == 0)      // 数据成功发送
       {  
        IICError = I2C_ERROR_NO_ERR;     
     
       }
       else
       {
     v_pIicbus->IICSTAT = (3 << 6) | (0 << 5) | (1 << 4);
     IICCON_DACK(v_pIicbus->IICCON); 
     Sleep(1);            // 等待结束信号产生完毕     
     IICError = I2C_ERROR_SEND;
         
    }
       break;
   
      // Receive Bytes
      case IIC_STATUS_SENDCMD:
       RETAILMSG(1, (TEXT("SENDCOM..\r\n")));
       if((v_pIicbus->IICSTAT & 0x09) == 0)      // 数据成功发送
       {  
        IICError = I2C_ERROR_NO_ERR;
     RETAILMSG(1, (TEXT("SENDCOM success..\r\n")));
     
    }
       else
       {
     v_pIicbus->IICSTAT = (3 << 6) | (0 << 5) | (1 << 4);  
     IICCON_DACK(v_pIicbus->IICCON); 
                    Sleep(1);// 等待结束信号产生完毕             
     IICError = I2C_ERROR_SEND;
          
    }
      break;
   case IIC_STATUS_SENDDAT:
       RETAILMSG(1, (TEXT("SENDDAT..\r\n")));
       if((v_pIicbus->IICSTAT & 0x09) == 0)      // 数据成功发送
       {  
        IICError = I2C_ERROR_NO_ERR;
     RETAILMSG(1, (TEXT("SENDDAT success..\r\n")));
            
       }
       else
       {
     v_pIicbus->IICSTAT = (3 << 6) | (0 << 5) | (1 << 4);
     IICCON_DACK(v_pIicbus->IICCON); 
                    Sleep(1);// 等待结束信号产生完毕             
     IICError = I2C_ERROR_SEND;
           
    }
      break;
   
   case IIC_STATUS_SENDNOR:
       RETAILMSG(1, (TEXT("SENDNOR..\r\n")));
       if((v_pIicbus->IICSTAT & 0x09) == 0)      // 数据成功发送
       {  
        IICError = I2C_ERROR_NO_ERR;
     RETAILMSG(1, (TEXT("SENDNOR success..\r\n")));
            
       }
       else
       {
     v_pIicbus->IICSTAT = (3 << 6) | (0 << 5) | (1 << 4);
     IICCON_DACK(v_pIicbus->IICCON); 
                    Sleep(1);// 等待结束信号产生完毕             
     IICError = I2C_ERROR_SEND;
          
    
    }
      break;
      // Receive Bytes
      case IIC_STATUS_RECEIVE:
       if((v_pIicbus->IICSTAT & 0x08) == 0)
       {    
     IICError = I2C_ERROR_NO_ERR;     
     
       }
       else
       {
        // 发送结束信号 
     v_pIicbus->IICSTAT = (2 << 6) | (0 << 5) | (1 << 4); 
     IICCON_DACK(v_pIicbus->IICCON);
     Sleep(1);           // 等待结束信号产生完毕
    
     IICError = I2C_ERROR_RECEIVE;
     
       }
      break;
   
   default:
     break; 
  }
        SetEvent(gIICEvent);
  InterruptDone(g_IICSysIntr);
  
 }

   
}
 
BOOL WINAPI
DllEntry(HANDLE hinstDLL, DWORD dwReason,
 LPVOID  Reserved)
{
 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;
}

/*******************************************************************************************
函数名称: RFD_Init
描    述: 驱动程序初始化函数
输入参数: DWORD dwContext: 设备管理器传递给本驱动的参数, 通常为流接口驱动在注册表内的位置  
输出参数: 无
返    回: 驱动程序句柄
*******************************************************************************************/
DWORD RFD_Init(DWORD dwContext)

 DWORD threadID; 
 Virtualaddress();// 取得 GPIO 及 IIC 相关寄存器的虚拟地址空间
 InitIOPort();// 使能 IIC 引脚为中断引脚, 并初始化相关寄存器 

  //中断线程句柄
    HANDLE IntThread = CreateThread(NULL, 0,
     (LPTHREAD_START_ROUTINE) RFDProcThread, 0, 0, &threadID);
   if (NULL == IntThread)
   { 
  RETAILMSG(1, (TEXT("ERROR: failed to Create rfid Thread!\r\n")));
  return FALSE;  
   }
    //设置优先级
   SetThreadPriority(IntThread, THREAD_PRIORITY_ABOVE_NORMAL);
      //读写事件
   gIICEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
 
   return (DWORD)IntThread; 
}

/*******************************************************************************************
函数名称: RFD_ReadBlock
描    述: 从 RFID 从机读取块的内容
输入参数: DWORD Handle    : 驱动程序引用事例句柄
          LPVOID pBuffer  : 接收缓冲区
          DWORD dwNumBytes: 要读的字节数
输出参数: 无
返    回: 实际读到字节数
*******************************************************************************************/
DWORD RFD_ReadBlock(DWORD hOpenContext, LPVOID pBuffer, DWORD dwNumBytes)
{
 UINT32 ret;
    uchar *pReadBuffer;
    pReadBuffer = (uchar *)pBuffer;
    if ((pBuffer == NULL) || (dwNumBytes <= 0))
  return 0;
 //以下完成了发送从地址的功能
    I2cStatus = IIC_STATUS_SETADDR;//发送从地址
 StartI2C(IICSla);   
  
 ret = WaitForSingleObject(gIICEvent, INFINITE);  /* 挂起当前线程,直到IIC中断的产生 */  
 ResetEvent(gIICEvent); 
 if ((IICError != I2C_ERROR_NO_ERR) || (ret != WAIT_OBJECT_0))
 {
  RETAILMSG(1, (TEXT("ERROR: IIC_Read: Send Slave Address fail! \r\n")));
  return 0; 
 }
  
 //以下发送命令字0x0A,0x21
 I2cStatus = IIC_STATUS_SENDDAT;
 
 IIC_SendByte(0x0A);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 
 IIC_SendByte(0x21);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 //以下发送密钥标示
 IIC_SendByte(0x0);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 //以下发送想要读取的块号
 IIC_SendByte(0x04);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 //以下发送密钥
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 //密钥发送完毕
 
 
 //以下发送校验字
 I2cStatus=IIC_STATUS_SENDNOR;
 IIC_SendByte(0x0A^0x21^0x0^0x04^0xFF^0xFF^0xFF^0xFF^0xFF^0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 RETAILMSG(1,(TEXT("SENDNOR success..\r\n")));
 ResetEvent(gIICEvent);
 
 v_pIicbus->IICSTAT &=~(1 << 5);//停止写信号  
 v_pIicbus->IICCON &= (~(1 << 4)); //中断清零可以接受后面的中断
 Sleep(1);  
 StartI2C(IICSla | 0x01);
 I2cStatus = IIC_STATUS_SETADDR;//发送从地址 
 
 ret = WaitForSingleObject(gIICEvent, INFINITE);  
 ResetEvent(gIICEvent);  
 if ((IICError != I2C_ERROR_NO_ERR) || (ret != WAIT_OBJECT_0))
 {
  RETAILMSG(1, (TEXT("ERROR: IIC_Read: Send Slave Address fail! \r\n")));
  return 0; 
 }
 
 UINT32 i; 
 I2cStatus = IIC_STATUS_RECEIVE;
 for(i = 0; i < dwNumBytes; i++)
 {
  RETAILMSG(0,(L"Numbers = %d\r\n", dwNumBytes));
  //最后一个字节的情况,要禁止ack
  if ( (dwNumBytes - 1) == i )
  {
   IICCON_DNACK(v_pIicbus->IICCON);
  }
  else
  {
   IICCON_DACK(v_pIicbus->IICCON);
  }
  ret = WaitForSingleObject(gIICEvent, INFINITE);
  ResetEvent(gIICEvent);
  *pReadBuffer = IIC_RecByte();
  pReadBuffer++;
 }
 StopI2C(0);
 return i;
}
/*******************************************************************************************
函数名称: RFD_Read
描    述: 从 RFID 从机读取数据
输入参数: DWORD Handle    : 驱动程序引用事例句柄
          LPVOID pBuffer  : 接收缓冲区
          DWORD dwNumBytes: 要读的字节数
输出参数: 无
返    回: 实际读到字节数
*******************************************************************************************/
DWORD RFD_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD dwNumBytes)
{  
 if (18 == dwNumBytes)
 {
  return RFD_ReadBlock(hOpenContext, pBuffer, dwNumBytes);
 }
 //ret 用于IIC同步
    UINT32 ret;
    uchar *pReadBuffer;
    pReadBuffer = (uchar *)pBuffer;
 
   RETAILMSG(1,(TEXT("RFD_Read..\r\n")));
    if ((pBuffer == NULL) || (dwNumBytes <= 0))
  return 0;
 //以下完成了发送从地址的功能
    I2cStatus = IIC_STATUS_SETADDR;//发送从地址
 StartI2C(IICSla);   
  
 ret = WaitForSingleObject(gIICEvent, INFINITE);  /* 挂起当前线程,直到IIC中断的产生 */  
 ResetEvent(gIICEvent); 
 if ((IICError != I2C_ERROR_NO_ERR) || (ret != WAIT_OBJECT_0))
 {
  RETAILMSG(1, (TEXT("ERROR: IIC_Read: Send Slave Address fail! \r\n")));
  return 0; 
 }
  
 //以下发送数据长度
 I2cStatus = IIC_STATUS_SENDLEN;
 IIC_SendByte(0x03);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 //以下发送命令字
 I2cStatus=IIC_STATUS_SENDCMD; 
 IIC_SendByte(0x20);
 ret=WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 
 //以下发送数据域
 I2cStatus=IIC_STATUS_SENDDAT;
 IIC_SendByte(0x0);
 ret=WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 
 
 //以下发送校验字
 I2cStatus=IIC_STATUS_SENDNOR;
 IIC_SendByte(0x0^0x20^0x03);
 ret=WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 
 //v_pIicbus->IICSTAT &=~(1 << 5);//停止写信号  
 //v_pIicbus->IICCON &= (~(1 << 4)); //中断清零可以接受后面的中断
 Sleep(1);  
 StartI2C(IICSla | 0x01);
 I2cStatus = IIC_STATUS_SETADDR;//发送从地址 
 
 ret = WaitForSingleObject(gIICEvent, INFINITE);  
 ResetEvent(gIICEvent);  
 if ((IICError != I2C_ERROR_NO_ERR) || (ret != WAIT_OBJECT_0))
 {
  RETAILMSG(1, (TEXT("ERROR: IIC_Read: Send Slave Address fail! \r\n")));
  return 0; 
 }
 
 UINT32 i; 
 I2cStatus = IIC_STATUS_RECEIVE;
 for(i = 0; i < dwNumBytes; i++)
 {
  //最后一个字节的情况,要禁止ack
  if ( (dwNumBytes - 1) == i )
  {
   IICCON_DNACK(v_pIicbus->IICCON);
  }
  else
  {
   IICCON_DACK(v_pIicbus->IICCON);
  }
  ret = WaitForSingleObject(gIICEvent, INFINITE);
  ResetEvent(gIICEvent);
  *pReadBuffer = IIC_RecByte();
  pReadBuffer++;
 }
 StopI2C(0);
 return i;
  
}

/*******************************************************************************************
函数名称: IIC_Write
描    述: 向 IIC 从机发送数据
输入参数:
输出参数:
返    回: 成功发送的字节数
*******************************************************************************************/
DWORD RFD_Write(DWORD hOpenContext, LPCVOID pSourceBytes, DWORD NumberOfBytes)
{
 RETAILMSG(1, (TEXT("RFD_Write..\r\n")));
 UINT32 count, ret;
 uchar *pWriteBuffer;
 if ((pSourceBytes == NULL) || (NumberOfBytes <= 0))
  return 0;
 pWriteBuffer = (uchar *)MapPtrToProcess((LPVOID)pSourceBytes, GetCallerProcess());
 //以下完成了发送从地址的功能
    I2cStatus = IIC_STATUS_SETADDR;//发送从地址
 StartI2C(IICSla); 
 ret = WaitForSingleObject(gIICEvent, I2C_RW_TIMEOUT);  /* 挂起当前线程,直到IIC中断的产生 */  
 ResetEvent(gIICEvent); 
 if ((IICError != I2C_ERROR_NO_ERR) || (ret != WAIT_OBJECT_0))
 {
  RETAILMSG(1, (TEXT("ERROR: RFD_Write: send slave address fail! \r\n")));
  return 0; 
 }

 //以下发送命令字0x1A,0x23
 I2cStatus = IIC_STATUS_SENDDAT;
 IIC_SendByte(0x1A);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 
 IIC_SendByte(0x23);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 //以下发送密钥标示
 IIC_SendByte(0x0);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 //以下发送想要读取的块号
 IIC_SendByte(0x04);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 //以下发送密钥
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 IIC_SendByte(0xFF);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 //密钥发送完毕
 
 uchar correctWord = 0x1A^0x23^0x0^0x04^0xFF^0xFF^0xFF^0xFF^0xFF^0xFF;
 for (count = 0; count < NumberOfBytes; count++)
 {
  IIC_SendByte(*pWriteBuffer);
  
  ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
  ResetEvent(gIICEvent); 
  if (ret != WAIT_OBJECT_0)
  {
   RETAILMSG(1, (TEXT("ERROR: IIC write data fail! \r\n")));
   return count;
  }  
  if (IICError != I2C_ERROR_NO_ERR)
  {
   RETAILMSG(1, (TEXT("ERROR: IIC_Write: send data fail! \r\n")));
   break; 
  }
  correctWord ^= *pWriteBuffer;
  pWriteBuffer++;
 }

 IIC_SendByte(correctWord);
 ret = WaitForSingleObject(gIICEvent,I2C_RW_TIMEOUT);
 ResetEvent(gIICEvent);
 
 StopI2C(1);
 return count;
 
  
}

BOOL RFD_IOControl(DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn,
 DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut)
{
 return TRUE;
}
DWORD RFD_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)
{
 return TRUE;
}
BOOL RFD_Close(DWORD hOpenContext)
{
 return TRUE;
}
 
DWORD RFD_Seek(DWORD hOpenContext, long Amount, DWORD Type)
{
 return 0;
}
 
BOOL RFD_Deinit(DWORD hDeviceContext)
{
 RETAILMSG(1, (TEXT(":::IIC: IIC Driver deinit.\r\n")));
 // 通知中断服务线程退出
 g_bKillIST = TRUE;
 PulseEvent(gIICIntrEvent);  
 Sleep(200); 
 // 释放中断资源
 InterruptDone(g_IICSysIntr);
 InterruptDisable(g_IICSysIntr);
 KernelIoControl(IOCTL_HAL_RELEASE_SYSINTR, &g_IICSysIntr, sizeof(UINT32), NULL, 0, NULL);

 /* 释放申请的虚拟内存 */
 if (v_pIOPregs)
  VirtualFree((PVOID)v_pIOPregs, 0, MEM_RELEASE);
 if (v_pIicbus)
  VirtualFree((PVOID)v_pIicbus, 0, MEM_RELEASE);
 // 关闭 I2C 事件句柄
 CloseHandle(gIICIntrEvent);
 return TRUE;
}
void RFD_PowerDown(DWORD hDeviceContext)
{
 
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void RFD_PowerUp(DWORD hDeviceContext)
{
 RETAILMSG(1,(TEXT("USERKEY: INT_PowerUp\r\n")));
}
阅读(1840) | 评论(0) | 转发(0) |
0

上一篇:MapPtrToProcess

下一篇:键盘驱动(wince)

给主人留下些什么吧!~~