📄 owirepdd.c
字号:
}
}
LeaveCriticalSection(&g_hOwireLock);
OWIRE_FUNCTION_EXIT();
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: OwireReadData
//
// Read count number of bytes from DS2502.
// This function calls OwireReadByte to read one byte at
// a time into readBuf.
//
// Parameters:
// readBuf
// [out] Buffer for bytes read from DS2502
// count
// [in] Number of bytes to read into readBuf
//
// Returns:
// TRUE if read sequence successful
// FALSE if read sequence failed
//
//------------------------------------------------------------------------------
BOOL OwireReadData(BYTE* readBuf, DWORD count)
{
UINT32 i;
OWIRE_FUNCTION_ENTRY();
EnterCriticalSection(&g_hOwireLock);
// Read one byte at a time from the DS2502
for (i = 0; i < count; i++)
{
if (!OwireReadByte(&readBuf[i]))
{
return FALSE;
}
DEBUGMSG(ZONE_READWRITE, (TEXT("Read byte[%d] = %x\r\n"), i, readBuf[i]));
}
LeaveCriticalSection(&g_hOwireLock);
OWIRE_FUNCTION_EXIT();
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: OwireWriteByte
//
// Sends 8 bits of data to DS2502.
// This function calls WriteBit to send the 8-bit data one bit at a time.
//
// Parameters:
// UINT8 byte - 8-bit data to be sent to DS2502
//
// Returns:
// FALSE if writing sequence failed
// TRUE if writing sequence successful
//
//------------------------------------------------------------------------------
BOOL OwireWriteByte(UINT8 byte)
{
UINT8 byte_bit;
UINT8 count;
OWIRE_FUNCTION_ENTRY();
DEBUGMSG(ZONE_READWRITE, (TEXT("Writing byte %x\r\n"), byte));
byte_bit = byte;
for (count = 0; count < 8; count++)
{
if (!OwireWriteBit(byte_bit & 0x1))
{
DEBUGMSG(ZONE_ERROR, (TEXT("Writing bit[%d] = %d failed!\r\n"), count, byte_bit & 0x1));
return FALSE;
}
byte_bit >>= 1;
}
OWIRE_FUNCTION_EXIT();
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: OwireReadByte
//
// Read 8 bits of data from DS2502.
// This function calls ReadBit to read one bit at
// a time and reconstruct the byte of information.
//
// Parameters:
// by
// [out] Pointer to byte where 8-bit data
// from the DS2502 is stored
//
// Returns:
// TRUE if read sequence successful
// FALSE if read sequence failed
//
//------------------------------------------------------------------------------
BOOL OwireReadByte(BYTE *byte)
{
BYTE result;
UINT8 count;
OWIRE_FUNCTION_ENTRY();
// Clear byte. We will build this byte from
// the eight bits we read.
*byte = 0;
for (count = 0; count < 8; count++)
{
result = OwireReadBit();
if (result == ERROR_READ_FAULT)
{
DEBUGMSG(ZONE_ERROR, (TEXT("Reading byte[%d] failed! Data invalid.\r\n"), count));
return FALSE;
}
DEBUGMSG(ZONE_READWRITE, (TEXT("Read bit %x\r\n"), result));
*byte |= ((result & 0x1) << count);
}
DEBUGMSG(ZONE_READWRITE, (TEXT("Read byte %x\r\n"), *byte));
OWIRE_FUNCTION_EXIT();
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: OwireWriteBit
//
// This is a function to write a bit, 0 or 1, to DS2502.
//
// Parameters:
// UINT8 bit
// bit is 0 : Write 0 (set 1 to WR0 bit of OWIRE_CR)
// 1 : Write 1 (set 1 to WR1 bit of OWIRE_CR)
//
// Returns:
// TRUE if writing sequence successful
// FALSE if writing sequence failed
//
// WR0
// ____a _____b__________
// | / :
// | A | B :
// |_______________| :
// *****************
// ----------------------------------------------------
// a: Set WR0 b:WR0 auto cleared
// A: 100us B:17us
//
// WR1
// ____a _________________b__________
// | / :
// | A | B :
// |___| :
// *****
// ----------------------------------------------------
// a: Set WR1 b:WR1 auto cleared
// A: 5us B:112us
//
// *: Master Transmits
//
//------------------------------------------------------------------------------
BOOL OwireWriteBit(UINT8 bit)
{
OWIRE_FUNCTION_ENTRY();
switch(bit)
{
case 0:
INSREG16(&g_pOWIRE->CR, CSP_BITFMASK(OWIRE_CR_WR0),
CSP_BITFVAL(OWIRE_CR_WR0, OWIRE_CR_WR0_WRITE));
// Need to delay here for 117uSec while
// the write 0 operation is acknowledged.
// Try to give remaining timeslice to another
// thread while waiting.
while (EXTREG16(&g_pOWIRE->CR, CSP_BITFMASK(OWIRE_CR_WR0), OWIRE_CR_WR0_LSH))
Sleep(0);
break;
case 1:
INSREG16(&g_pOWIRE->CR, CSP_BITFMASK(OWIRE_CR_WR1RD),
CSP_BITFVAL(OWIRE_CR_WR1RD, OWIRE_CR_WR1RD_WRITE));
// Need to delay here for 117uSec while
// the write 1 operation is acknowledged.
// Try to give remaining timeslice to another
// thread while waiting.
while (EXTREG16(&g_pOWIRE->CR, CSP_BITFMASK(OWIRE_CR_WR1RD), OWIRE_CR_WR1RD_LSH))
Sleep(0);
break;
default:
return FALSE;
}
OWIRE_FUNCTION_EXIT();
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: OwireReadBit
//
// This is a function to read a bit, 0 or 1, from DS2502.
//
// Parameters:
// None.
//
// Returns:
// 0 - Read 0 bit (RDST bit is 0).
// 1 - Read 1 bit (RDST bit is 1).
// ERROR_READ_FAULT - Failed (WR1/RD bit wasn't cleared)
//
// Read 0
// ____a _____b__________
// | / :
// | A | B :
// |_______________| :
// *****
// ----------------------------------------------------
// a: Set WR1/RD b:WR1/RD auto cleared
// A: 45us A+B:117us
//
// Read 1
// ____a _________________b__________
// | / :
// | A | B :
// |___| :
// *****
// ----------------------------------------------------
// a: Set WR1/RD b:WR1/RD auto cleared
// A: 5us A+B:117us
//
// *: Master Transmits
//
//------------------------------------------------------------------------------
BYTE OwireReadBit(void)
{
BYTE bit = 0;
OWIRE_FUNCTION_ENTRY();
// Write to WR1RD bit in order to perform a read
INSREG16(&g_pOWIRE->CR, CSP_BITFMASK(OWIRE_CR_WR1RD),
CSP_BITFVAL(OWIRE_CR_WR1RD, OWIRE_CR_WR1RD_WRITE));
// Need to delay here for 117uSec before
// reading bit from RDST field.
// Try to give remaining timeslice to another
// thread while waiting.
while (EXTREG16(&g_pOWIRE->CR, CSP_BITFMASK(OWIRE_CR_WR1RD), OWIRE_CR_WR1RD_LSH))
Sleep(0);
if (EXTREG16(&g_pOWIRE->CR, CSP_BITFMASK(OWIRE_CR_WR1RD), OWIRE_CR_WR1RD_LSH))
{
DEBUGMSG(ZONE_ERROR, (TEXT("Reading failed!\r\n")));
return ERROR_READ_FAULT;
}
// Read bit from RDST and return
bit = EXTREG16(&g_pOWIRE->CR, CSP_BITFMASK(OWIRE_CR_RDST), OWIRE_CR_RDST_LSH);
OWIRE_FUNCTION_EXIT();
return bit;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -