打印

wince的IIC驱动问题,ADS正确,添加到系统不行

[复制链接]
1752|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
mjx91282041|  楼主 | 2012-11-13 20:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在2440下用IIC的IO口的模拟IIC时序,驱动一个外设芯片,ADS调试正确,添加到系统就不行,对驱动的优先级也提升了,还是不正确。跪求大神们帮我看看代码有啥问题啊!这个玩意儿我都搞了几个月了!
volatile IOPreg *s2440IOP = (IOPreg *)IOP_BASE; //volatile INTreg *s2440INT = (INTreg *)INT_BASE; float SHT_Data[2];///tmp HANDLE hDevSHT21; DWORD SHT2x_Option_ThreadFuc(LPVOID Context); void Virtual_Alloc(); // Virtual allocation /////////////////////////////////////////////////////////////////////////// ////////////////////////////////I2C.c source/////////////////////////////////////// #define noACK 0 #define aCk 1 #define SCL_OutEnable s2440IOP->rGPECON =( s2440IOP->rGPECON | 0x30000000 ) & 0xDFFFFFFF //28 config SCL as output #define SDA_OutEnable s2440IOP->rGPECON =( s2440IOP->rGPECON | 0xC0000000 ) & 0x7FFFFFFF //30config SDA as output #define SDA_OutDisable s2440IOP->rGPECON =( s2440IOP->rGPECON & 0x3fffffff)//config SDA as input #define SCL_Set s2440IOP->rGPEDAT |= (1 << 14) //SCL output "1" #define SCL_Clear s2440IOP->rGPEDAT &= ~(1 << 14) //SCL output "0" #define SDA_Set s2440IOP->rGPEDAT |= (1 << 15) //SDA output "1" #define SDA_Clear s2440IOP->rGPEDAT &= ~(1 << 15) void Delay_us(U32 uS) { volatile DWORD dwCounter; dwCounter = 100 * uS; while(dwCounter--); } void I2c_Initialize () { // put setup code here SCL_OutEnable; // set gpio6->SCL as output SCL_Set; // set SCL = "1" SDA_OutDisable; // set gpio7->SDA as input SDA_Set; Delay_us(100); // let I2C device ready } //============================================================================== void I2c_StartCondition () //============================================================================== { SDA_OutEnable; // set SDA as output //SCL_OutEnable; //!! SDA_Set; SCL_Set; Delay_us(1); SDA_Clear; Delay_us(5); SCL_Clear; Delay_us(5); } //============================================================================== void I2c_StopCondition () //============================================================================== { SDA_OutEnable; // set SDA as output //SCL_OutEnable; //!! SDA_Clear; SCL_Clear; Delay_us(1); SCL_Set; Delay_us(5); SDA_Set; Delay_us(5); } //---------------------------------------------------------------------------------- unsigned char I2c_WriteByte(unsigned char value) // writes a byte on the Sensibus and checks the acknowledge //---------------------------------------------------------------------------------- { // dwThreadID = GetCurrentThread(); //dwThreadPri = CeGetThreadPriority(dwThreadID); //dwThreadQuantum = CeGetThreadQuantum(dwThreadID); //CeSetThreadPriority(dwThreadID, 0);/ //CeSetThreadQuantum(dwThreadID, 0); // //。。。。。。。 (此部分是操作i2c的函数) // //CeSetThreadQuantum(dwThreadID, dwThreadQuantum); //CeSetThreadPriority(dwThreadID, dwThreadPri); HANDLE dwThreadID = GetCurrentThread(); DWORD dwThreadPri = CeGetThreadPriority(dwThreadID); DWORD dwThreadQuantum = CeGetThreadQuantum(dwThreadID); CeSetThreadPriority(dwThreadID, 0); CeSetThreadQuantum(dwThreadID, 0); int error=0, i1=0, i2=0; for(i2 = 0; i2 < 8; i2++ ) { // setup data onto SDA if(value & 0x80){ SDA_Set; //SetSDA( 1 ); } else{ SDA_Clear; //SetSDA( 0 ); } Delay_us(1); //I2C timing: data setup time > 200ns // issue a clock SCL_Set; //SetSCL( 1 ); Delay_us(1); //I2C timing: SCL high time > 0.6us SCL_Clear; //SetSCL( 0 ); Delay_us(2); //I2C timing: SCL low time > 1.3us value= value<< 1; } //check ACK(Low state) from I2C device SDA_OutDisable; //set SDA as input Delay_us(1); //I2C timing: data(from I2C device) setup time > 200ns SCL_Set; //SetSCL( 1 ); // issue a clock Delay_us(1); //I2C timing: SCL high time > 0.6us if(s2440IOP->rGPEDAT>>15) //read ACK_BIT issued from I2C device { // NO ACK, so setup STOP condition SCL_Set; //SetSCL( 1 ); Delay_us(1); //I2C timing: stop setup timing > 0.6us SDA_Set; //SetSDA( 1 ); SDA is in input status! return -(i1+1); // no acknowledgement -> failed } SCL_Clear; //SetSCL( 0 ); Delay_us(2); //I2C timing: SCL low time > 1.3us //check ACK passed, config SDA as output for further outputing SDA_OutEnable; // backup the thread priority; CeSetThreadQuantum(dwThreadID, dwThreadQuantum); CeSetThreadPriority(dwThreadID, dwThreadPri); return 0; } //---------------------------------------------------------------------------------- unsigned char I2c_ReadByte(unsigned char ack) //---------------------------------------------------------------------------------- // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" { HANDLE dwThreadID = GetCurrentThread(); DWORD dwThreadPri = CeGetThreadPriority(dwThreadID); DWORD dwThreadQuantum = CeGetThreadQuantum(dwThreadID); CeSetThreadPriority(dwThreadID, 0); CeSetThreadQuantum(dwThreadID, 0); unsigned char i,val=0; int i2=0,ub[1]; SDA_OutDisable; //SetSDADir( 0 ); // set SDA as input // then read a abyte for(i2 = 0; i2 < 8; i2++) { ub[0] = ub[0] << 1; // issue a clock SCL_Set; //SetSCL( 1 ); //DelayInuSec(1); //I2C timing: SCL high time > 0.6us if(s2440IOP->rGPEDAT>>15) // read SDA { ub[0] = ub[0] | 0x01; } SCL_Clear; //SetSCL( 0 ); Delay_us(5); //I2C timing: SCL low time > 1.3us } //issue ACK condition properly SDA_OutEnable; if (ack) { SDA_Clear; } else { SDA_Set; } Delay_us(1); //I2C timing: data setup time > 200ns SCL_Set; //SetSCL( 1 ); Delay_us(1); //I2C timing: SCL high time > 0.6us SCL_Clear; Delay_us(2); //I2C timing: SCL low time > 1.3us // backup the thread priority; CeSetThreadQuantum(dwThreadID, dwThreadQuantum); CeSetThreadPriority(dwThreadID, dwThreadPri); return ub[0]; } ///////////////////////////// SHT2x.c source ////////////////////////////////////// //=========================================================================== unsigned char SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, unsigned int *pMeasurand) //=========================================================================== { unsigned char checksum; //checksum unsigned char data[2]; //data array for checksum verification unsigned char error=0; //error variable unsigned int i; //counting variable //-- write I2C sensor address and command -- I2c_StartCondition(); error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr 0x80 RETAILMSG(1,(TEXT(" ...I2c_WriteByte (I2C_ADR_W ) error=%d \r\n"),error)); error = 0; switch(eSHT2xMeasureType) { case HUMIDITY: error |= I2c_WriteByte (TRIG_RH_MEASUREMENT_HM); break; //0xe5 case TEMP : error |= I2c_WriteByte (TRIG_T_MEASUREMENT_HM); break; //0xe3 default: break; } //-- wait until hold master is released -- RETAILMSG(1,(TEXT(" ...The MEASUREMENT error=%d \r\n"),error)); error = 0; I2c_StartCondition(); error |= I2c_WriteByte (I2C_ADR_R);//0x81 RETAILMSG(1,(TEXT(" ...I2c_WriteByte (I2C_ADR_R) error =%d \r\n"),error)); error = 0; Delay_us(2400);//covert time 90ms //-- read two data bytes and one checksum byte -- data[0] = I2c_ReadByte(ACK); data[1] = I2c_ReadByte(ACK); checksum=I2c_ReadByte(NO_ACK); RETAILMSG(1,(TEXT(" ...the MSB is 0x%x\r\n"), data[0])); RETAILMSG(1,(TEXT(" ...the LSB is 0x%x\r\n"), data[1])); *pMeasurand=data[0]; *pMeasurand<<=8; *pMeasurand+=data[1]; //-- verify checksum -- //error |= SHT2x_CheckCrc (data,2,checksum); I2c_StopCondition(); return error; } //=========================================================================== u8 SHT2x_SoftReset() //=========================================================================== { u8 error=0; //error variable I2c_StartCondition(); error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr 0x80 error |= I2c_WriteByte (SOFT_RESET); //0xfe // Command I2c_StopCondition(); //Delay_us(400); // wait till sensor has restarted Sleep(15); return error; } //void SHT2x_Option(unsigned int pValue[] ) DWORD SHT2x_Option_ThreadFuc(LPVOID Context) { int error=0; unsigned int sRH,sT; float fHumiValue,fTempeValue; LPCRITICAL_SECTION RegCS; // Register CS while(1) { // EnterCriticalSection(RegCS); I2c_Initialize( ); error |= SHT2x_SoftReset(); RETAILMSG(1,(TEXT("\r\n0.rest error0= %d \r\n"),error)); error=0; // --- measure humidity with "Hold Master Mode (HM)" --- error |= SHT2x_MeasureHM(TEMP, &sT); RETAILMSG(1,(TEXT("1.TEMP messure error= %d \r\n"),error)); error=0; // --- measure temperature with "Polling Mode" (no hold master) --- // error |= SHT2x_MeasureHM(HUMIDITY, &sRH); // RETAILMSG(1,(TEXT("2.HUMIDITY messure error= %d \r\n"),error)); // error=0; // --- calculate humidity and temperature -- RETAILMSG(1,(TEXT("3.Temperature is %x ℃\r\n"),sT)); // RETAILMSG(1,(TEXT("4.humidity is %x'H \r\n"),sRH)); fTempeValue = SHT2x_CalcTemperatureC(sT); // fHumiValue = SHT2x_CalcRH(sRH); RETAILMSG(1,(TEXT("5.Temperature is %.2f℃\ \r\n"),fTempeValue)); // RETAILMSG(1,(TEXT("6.humidity is %.2f 'H \r\n"),fHumiValue)); SHT_Data[0] = fTempeValue ; // SHT_Data[1] = fHumiValue ; // LeaveCriticalSection(RegCS); Sleep(1); } return 0; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- BOOL SHT_IOControl(DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut) { //SHT2x_Option(tmp); memcpy(pBufOut,SHT_Data,2);//output dwLenOut = 2;// RETAILMSG(0,(TEXT("SHT21: Ioctl code = 0x%x\r\n"), dwCode)); return TRUE; } 






DWORD SHT_Init(DWORD dwContext)
{
      
    RETAILMSG(1,(TEXT("++++++ SHT21_Init ++++++\r\n")));
  
    // 1. Virtual Alloc
    //Virtual_Alloc();
      
    I2c_Initialize();// analog I2C GPIOE14,GPIOE15;
  
    hDevSHT21 =CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SHT2x_Option_ThreadFuc, NULL, 0, NULL);
      
    return TRUE;
}

相关帖子

沙发
mjx91282041|  楼主 | 2012-11-13 20:31 | 只看该作者
代码怎么这个样子呀,可以看这个链接:http://bbs.csdn.net/topics/390278816?page=1#post-392904645

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

12

主题

54

帖子

1

粉丝