📄 i2script.cpp
字号:
FAIL;
i2cAccessBlock.Flags = I2C_FLAGS_ACK;
i2cAccessBlock.Command = I2C_COMMAND_READ;
for( nIndex = 0; nIndex < pI2CPacket->cbReadCount; nIndex ++)
{
// read the data to the buffer
if( nIndex == ( UINT)( pI2CPacket->cbReadCount - 1))
{
// don't apply ACK at the last read - read operation termination
i2cAccessBlock.Flags &= ~I2C_FLAGS_ACK;
// also apply STOP condition for the last byte
i2cAccessBlock.Flags |= I2C_FLAGS_STOP;
}
if( AccessI2CProvider( m_pdoDriver, &i2cAccessBlock) != I2C_STATUS_NOERROR)
break;
pI2CPacket->puchReadBuffer[nIndex] = i2cAccessBlock.Data;
}
if( nIndex != pI2CPacket->cbReadCount)
FAIL;
/* // STOP condition is applied with the last byte to be read
// apply stop condition as the end of read operation
i2cAccessBlock.Flags = I2C_FLAGS_STOP;
i2cAccessBlock.Command = I2C_COMMAND_NULL;
m_i2cProviderInterface.i2cAccess( m_pdoDriver, &i2cAccessBlock);
*/
if( pI2CPacket->usFlags & I2COPERATION_READWRITE)
{
// write operation should be taken care again, the last byte in the pbyWriteBuffer
// should be constructed from the value read back and the binary operations OR and AND
// with the values specified in the packet
uchValue = pI2CPacket->puchReadBuffer[pI2CPacket->cbReadCount - 1];
uchValue &= pI2CPacket->uchANDValue;
pI2CPacket->puchWriteBuffer[pI2CPacket->cbWriteCount - 1] = uchValue | pI2CPacket->uchORValue;
if( pI2CPacket->cbWriteCount)
{
// implement a write request
// apply START condition with the I2C chip address first
i2cAccessBlock.Flags = I2C_FLAGS_START | I2C_FLAGS_ACK;
i2cAccessBlock.Command = I2C_COMMAND_WRITE;
i2cAccessBlock.Data = pI2CPacket->uchChipAddress & 0xFE;
if( AccessI2CProvider( m_pdoDriver, &i2cAccessBlock) != I2C_STATUS_NOERROR)
FAIL;
i2cAccessBlock.Flags = I2C_FLAGS_ACK;
for( nIndex = 0; nIndex < pI2CPacket->cbWriteCount; nIndex ++)
{
// write the data from the buffer
i2cAccessBlock.Data = pI2CPacket->puchWriteBuffer[nIndex];
if( nIndex == ( UINT)( pI2CPacket->cbWriteCount - 1))
// the last byte to write - apply STOP condition
i2cAccessBlock.Flags |= I2C_FLAGS_STOP;
if( AccessI2CProvider( m_pdoDriver, &i2cAccessBlock) != I2C_STATUS_NOERROR)
break;
}
if( nIndex != pI2CPacket->cbWriteCount)
FAIL;
/* // STOP condition is applied withe the last byte to be written
// apply stop condition as the end of write operation
i2cAccessBlock.Flags = I2C_FLAGS_STOP;
i2cAccessBlock.Command = I2C_COMMAND_NULL;
m_i2cProviderInterface.i2cAccess( m_pdoDriver, &i2cAccessBlock);
*/
}
}
}
uchI2CResult = I2C_STATUS_NOERROR;
} END_ENSURE;
if( uchI2CResult == I2C_STATUS_ERROR)
{
// there was an error during accessing I2C - issue Reset command
i2cAccessBlock.Command = I2C_COMMAND_RESET;
AccessI2CProvider( m_pdoDriver, &i2cAccessBlock);
}
pI2CPacket->uchI2CResult = uchI2CResult;
return( TRUE);
} END_ENSURE;
OutputDebugTrace(( "CI2CScript:ExecuteI2CPacket() nError = %x", nError));
return( FALSE);
}
/*^^*
* CheckI2CScriptPacket()
* Purpose : checks integrity of the I2C control package
*
* Inputs : PI2CPacket pI2CPacket : pointer to I2C access packet
*
* Outputs : BOOL : returns TRUE, if I2C control package is a valid one
*
* Author : IKLEBANOV
*^^*/
UINT CI2CScript::CheckI2CScriptPacket( IN PI2CPacket pI2CPacket)
{
UINT nPacketError;
ENSURE
{
if(( m_i2cProviderInterface.i2cOpen == NULL) ||
( m_i2cProviderInterface.i2cAccess == NULL) ||
( m_pdoDriver == NULL))
{
// the I2CProvider was not found
nPacketError = I2CSCRIPT_ERROR_NOPROVIDER;
FAIL;
}
if(( !pI2CPacket->cbWriteCount) && ( !pI2CPacket->cbReadCount))
{
// nothing to do
nPacketError = I2CSCRIPT_ERROR_NODATA;
FAIL;
}
if((( pI2CPacket->cbWriteCount) && ( pI2CPacket->puchWriteBuffer == NULL))
|| (( pI2CPacket->cbReadCount) && ( pI2CPacket->puchReadBuffer == NULL)))
{
// NULL pointer, when the data is specified
nPacketError = I2CSCRIPT_ERROR_NOBUFFER;
FAIL;
}
if(( pI2CPacket->usFlags & I2COPERATION_READWRITE) && ( !pI2CPacket->cbWriteCount))
{
// if Read-Modify-Write is specified, the Write data should be present
nPacketError = I2CSCRIPT_ERROR_READWRITE;
FAIL;
}
nPacketError = I2CSCRIPT_NOERROR;
} END_ENSURE;
return( nPacketError);
}
/*^^*
* ClearScript()
* Purpose : clears I2CScript to the NULL state - no I2C operations are on hold.
*
* Inputs : none
*
* Outputs : none
* Author : IKLEBANOV
*^^*/
void CI2CScript::ClearScript( void)
{
m_nExecutionIndex = 0;
m_nScriptLength = 0;
m_pfnReturnWhenDone = NULL;
m_bExecutionInProcess = FALSE;
}
/*^^*
* AppendToScript()
* Purpose : appends a I2CPacket to the bottom of the I2CScript.
* The 16 bits emulation is not implemented at this time.
*
* Inputs : PI2CPacket pI2CPacket - pointer to the I2C packet to append
*
* Outputs : BOOL : returns TRUE, if the packet was successfully appended.
* FALSE might happend if the I2CPacket is a bad one, or overflow occurs
* Author : IKLEBANOV
*^^*/
BOOL CI2CScript::AppendToScript( PI2CPacket pI2CPacket)
{
UINT nError, nScriptIndex;
UINT nIndex, cbCount;
ENSURE
{
PI2CScriptPrimitive pI2CPrimitive;
if(( nError = CheckI2CScriptPacket( pI2CPacket)) != I2CSCRIPT_NOERROR)
FAIL;
nError = I2CSCRIPT_ERROR_OVERFLOW;
// m_nExecutionIndex is used as a Script build index. We will work with a local copy of it
// first to ensure we have no overflow
nScriptIndex = m_nExecutionIndex;
pI2CPrimitive = &m_i2cScript[nScriptIndex];
// We assume the last byte in the buffer belongs to the Write operation
// after Read-Modify, is specified.
cbCount = ( pI2CPacket->usFlags & I2COPERATION_READWRITE) ? \
( pI2CPacket->cbWriteCount - 1) : ( pI2CPacket->cbWriteCount);
if( cbCount)
{
// I2C Chip address should be taken care of first
pI2CPrimitive->ulCommand = I2C_COMMAND_WRITE;
pI2CPrimitive->byData = pI2CPacket->uchChipAddress;
pI2CPrimitive->byANDData = 0xFE;
pI2CPrimitive->byORData = 0x00;
pI2CPrimitive->ulProviderFlags = I2C_FLAGS_START | I2C_FLAGS_ACK;
pI2CPrimitive->byFlags = 0x0;
// check the Script length
if( ++ nScriptIndex >= I2CSCRIPT_LENGTH_MAXIMUM)
FAIL;
pI2CPrimitive ++;
// I2C write buffer should be taken care of.
for( nIndex = 0; nIndex < cbCount; nIndex ++)
{
pI2CPrimitive->ulCommand = I2C_COMMAND_WRITE;
pI2CPrimitive->byData = pI2CPacket->puchWriteBuffer[nIndex];
pI2CPrimitive->byORData = 0x00;
pI2CPrimitive->byANDData = 0xFF;
pI2CPrimitive->ulProviderFlags = I2C_FLAGS_ACK;
pI2CPrimitive->byFlags = 0x0;
if( nIndex == cbCount - 1)
// this is the last byte to be written - apply STOP
pI2CPrimitive->ulProviderFlags |= I2C_FLAGS_STOP;
// check the Script length
if( ++ nScriptIndex >= I2CSCRIPT_LENGTH_MAXIMUM)
break;
pI2CPrimitive ++;
}
// check the Script length
if( nScriptIndex >= I2CSCRIPT_LENGTH_MAXIMUM)
FAIL;
/*
// Stop condition is applied with the last byte to be written
// We finished Write portion here, whether it's a Write only, Read-Modify-Write operation
pI2CPrimitive->ulCommand = I2C_COMMAND_NULL;
pI2CPrimitive->ulProviderFlags = I2C_FLAGS_STOP;
pI2CPrimitive->byFlags = 0x0;
// check the Script length
if( ++ nScriptIndex >= I2CSCRIPT_LENGTH_MAXIMUM)
FAIL;
pI2CPrimitive ++;
*/
}
// We have to see, if there is a Read operation involved
if( pI2CPacket->cbReadCount)
{
// I2C Chip address should be taken care of first
pI2CPrimitive->ulCommand = I2C_COMMAND_WRITE;
pI2CPrimitive->byData = pI2CPacket->uchChipAddress;
pI2CPrimitive->byANDData = 0xFE;
pI2CPrimitive->byORData = 0x01;
pI2CPrimitive->ulProviderFlags = I2C_FLAGS_START | I2C_FLAGS_ACK;
pI2CPrimitive->byFlags = 0x0;
// check the Script length
if( ++ nScriptIndex >= I2CSCRIPT_LENGTH_MAXIMUM)
FAIL;
pI2CPrimitive ++;
// I2C read buffer should be taken care of. We assume the last byte in the buffer belongs to
// the Write operation after Read-Modify, is specified.
for( nIndex = 0; nIndex < pI2CPacket->cbReadCount; nIndex ++)
{
pI2CPrimitive->ulCommand = I2C_COMMAND_READ;
if( nIndex == ( UINT)( pI2CPacket->cbReadCount - 1))
{
pI2CPrimitive->ulProviderFlags = I2C_FLAGS_STOP;
pI2CPrimitive->byFlags = pI2CPacket->usFlags & I2COPERATION_READWRITE;
}
else
{
pI2CPrimitive->ulProviderFlags = I2C_FLAGS_ACK;
pI2CPrimitive->byFlags = 0x0;
}
// check the Script length
if( ++ nScriptIndex >= I2CSCRIPT_LENGTH_MAXIMUM)
break;
pI2CPrimitive ++;
}
// check the Script length
if( nScriptIndex >= I2CSCRIPT_LENGTH_MAXIMUM)
FAIL;
/* // Stop condition is applied with the last byte to be read
// We finished Read portion here, whether it's a Read only, Read-Modify-Write operation
pI2CPrimitive->ulCommand = I2C_COMMAND_NULL;
pI2CPrimitive->ulProviderFlags = I2C_FLAGS_STOP;
pI2CPrimitive->byFlags = 0x0;
// check the Script length
if( ++ nScriptIndex >= I2CSCRIPT_LENGTH_MAXIMUM)
FAIL;
pI2CPrimitive ++;
*/
}
// the last thing left to do, is to implement Write after Read-Modify, if specified
if( pI2CPacket->usFlags & I2COPERATION_READWRITE)
{
// I2C Chip address should be taken care of first
pI2CPrimitive->ulCommand = I2C_COMMAND_WRITE;
pI2CPrimitive->byData = pI2CPacket->uchChipAddress;
pI2CPrimitive->byANDData = 0xFE;
pI2CPrimitive->byORData = 0x00;
pI2CPrimitive->ulProviderFlags = I2C_FLAGS_START | I2C_FLAGS_ACK;
pI2CPrimitive->byFlags = 0x0;
// check the Script length
if( ++ nScriptIndex >= I2CSCRIPT_LENGTH_MAXIMUM)
FAIL;
pI2CPrimitive ++;
// I2C write buffer should be taken care of.
for( nIndex = 0; nIndex < pI2CPacket->cbWriteCount; nIndex ++)
{
pI2CPrimitive->ulCommand = I2C_COMMAND_WRITE;
pI2CPrimitive->byData = pI2CPacket->puchWriteBuffer[nIndex];
pI2CPrimitive->ulProviderFlags = I2C_FLAGS_ACK;
if( nIndex == ( UINT)( pI2CPacket->cbWriteCount - 1))
{
// it's time to write the byte modified after the Read operation
pI2CPrimitive->byORData = pI2CPacket->uchORValue;
pI2CPrimitive->byANDData = pI2CPacket->uchANDValue;
pI2CPrimitive->byFlags = I2COPERATION_READWRITE;
// apply STOP condition with the last byte to be read
pI2CPrimitive->ulProviderFlags |= I2C_FLAGS_STOP;
}
else
{
pI2CPrimitive->byORData = 0x00;
pI2CPrimitive->byANDData = 0xFF;
pI2CPrimitive->byFlags = 0x0;
}
// check the Script length
if( ++ nScriptIndex >= I2CSCRIPT_LENGTH_MAXIMUM)
break;
pI2CPrimitive ++;
}
// check the Script length
if( nScriptIndex >= I2CSCRIPT_LENGTH_MAXIMUM)
FAIL;
/* // Stop condition is applied with the last byte to be written
// We finished Write portion here, whether it's a Write only, Read-Modify-Write operation
pI2CPrimitive->ulCommand = I2C_COMMAND_NULL;
pI2CPrimitive->ulProviderFlags = I2C_FLAGS_STOP;
pI2CPrimitive->byFlags = 0x0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -