📄 owirepdd.c
字号:
//-----------------------------------------------------------------------------
//
// Copyright (C) 2004, Motorola Inc. All Rights Reserved
//
//-----------------------------------------------------------------------------
//
/*---------------------------------------------------------------------------
* Copyright (C) 2004,2006, Freescale Semiconductor, Inc. All Rights Reserved.
* THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
* AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
*--------------------------------------------------------------------------*/
//-----------------------------------------------------------------------------
//
// File: owirepdd.c
//
// Implementation of One-Wire Driver Platform Device Driver
//
//-----------------------------------------------------------------------------
#include <windows.h>
#include <nkintr.h>
#include <ceddk.h>
#include "csp.h"
#include "owire.h"
#include "owire_priv.h"
//-----------------------------------------------------------------------------
// External Functions
extern UINT32 BSPGetDivider();
//-----------------------------------------------------------------------------
// External Variables
//-----------------------------------------------------------------------------
// Defines
//-----------------------------------------------------------------------------
// Types
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
// Local Variables
static PCSP_OWIRE_REGS g_pOWIRE;
static CRITICAL_SECTION g_hOwireLock;
//------------------------------------------------------------------------------
// Local Functions
BOOL OwireWriteByte(UINT8);
BOOL OwireReadByte(BYTE*);
BOOL OwireWriteBit(UINT8);
BYTE OwireReadBit();
//------------------------------------------------------------------------------
//
// Function: OwireDeinit
//
// Frees up the register space and deletes critical
// section for deinitialization.
//
// Parameters:
// None
//
// Returns:
// None
//
//------------------------------------------------------------------------------
void OwireDeinit(void)
{
OWIRE_FUNCTION_ENTRY();
// free the virtual space allocated for OWIRE memory map
if (g_pOWIRE != NULL)
{
VirtualFree((void *) g_pOWIRE, 0, MEM_RELEASE);
g_pOWIRE = NULL;
}
// Delete the critical section
DeleteCriticalSection(&g_hOwireLock);
OWIRE_FUNCTION_EXIT();
return;
}
//------------------------------------------------------------------------------
//
// Function: OwireSetDevider
//
// This function sets the time divider register.
//
// Parameters:
// None
//
// Returns:
// None
//
//------------------------------------------------------------------------------
void OwireSetDevider(void)
{
UINT16 dividerVal;
dividerVal = BSPGetDivider();
// set time divider value to get as close as possible to a 1MHz clock
OUTREG16(&g_pOWIRE->TD, dividerVal);
DEBUGMSG (ZONE_INFO, (TEXT("OWIRE regs - ctrl: %x divider: %x reset: %x\r\n"),
INREG16(&g_pOWIRE->CR), INREG16(&g_pOWIRE->TD), INREG16(&g_pOWIRE->RST)));
}
//------------------------------------------------------------------------------
//
// Function: OwireInitialize
//
// This function is the initialization routine for the one-wire. It
// allocates memory for the one-wire registers and sets the
// time divider register. If there is a failure in any of the
// initialization steps, OwireRelease will be called to clean up.
//
// Parameters:
// None
//
// Returns:
// TRUE - If success
//
// FALSE - If failure
//
//------------------------------------------------------------------------------
BOOL OwireInitialize(void)
{
PHYSICAL_ADDRESS phyAddr;
OWIRE_FUNCTION_ENTRY();
phyAddr.QuadPart = CSP_BASE_REG_PA_OWIRE;
g_pOWIRE = (PCSP_OWIRE_REGS) MmMapIoSpace(phyAddr, sizeof(CSP_OWIRE_REGS), FALSE);
// check if Map Virtual Address failed
if (g_pOWIRE == NULL)
{
DEBUGMSG(ZONE_ERROR, (TEXT("OwireInitialize: MmMapIoSpace failed!\r\n")));
goto Error;
}
OwireSetDevider();
OWIRE_FUNCTION_EXIT();
return TRUE;
Error:
//Cleaning up
OwireDeinit();
return FALSE;
}
//------------------------------------------------------------------------------
//
// Function: OwireSoftReset
//
// Software resets the OneWire module. First set the RESET register
// to 1 and then set it to 0.
//
// Parameters:
// None.
//
// Returns:
// None.
//
//------------------------------------------------------------------------------
void OwireSoftReset(void)
{
OWIRE_FUNCTION_ENTRY();
EnterCriticalSection(&g_hOwireLock);
// We must manually assert reset and then deassert.
OUTREG16(&g_pOWIRE->RST, CSP_BITFVAL(OWIRE_RST_RST, OWIRE_RST_RST_RESET));
OUTREG16(&g_pOWIRE->RST, CSP_BITFVAL(OWIRE_RST_RST, OWIRE_RST_RST_ENDRESET));
LeaveCriticalSection(&g_hOwireLock);
OWIRE_FUNCTION_EXIT();
return;
}
//------------------------------------------------------------------------------
//
// Function: OwireSetResetPresencePulse
//
// This function is used to detect the presence of DS2502.
// This function should always be called before any
// write or read sequence.
//
// Parameters:
// None.
//
// Returns:
// TRUE if Detection sequence failed
// FALSE if DS2502 exists, presence detected.
//
//------------------------------------------------------------------------------
BOOL OwireSetResetPresencePulse(void)
{
DWORD startTime = GetTickCount();
OWIRE_FUNCTION_ENTRY();
EnterCriticalSection(&g_hOwireLock);
INSREG16(&g_pOWIRE->CR, CSP_BITFMASK(OWIRE_CR_RPP),
CSP_BITFVAL(OWIRE_CR_RPP, OWIRE_CR_RPP_RESET));
// Need to delay here for 579uSec for onewire
// module to finish sampling the line and set PST.
while (!EXTREG16(&g_pOWIRE->CR, CSP_BITFMASK(OWIRE_CR_PST), OWIRE_CR_PST_LSH))
{
// If more than one tick count has elapsed,
// we have waited longer than 1ms and the
// DS2520 must not be present
if ((GetTickCount() - startTime) > 1)
{
DEBUGMSG(ZONE_ERROR, (TEXT("time waited (ms): %x\r\n"), (GetTickCount() - startTime)));
DEBUGMSG(ZONE_ERROR, (TEXT("PST bit wasn't set!\r\n")));
}
}
// If we are here we have received the presence bit.
// Now we need to delay here for another 444uSec for
// onewire module to AutoClear RPP bit.
// Try to give remaining timeslice to another
// thread while waiting.
while (EXTREG16(&g_pOWIRE->CR, CSP_BITFMASK(OWIRE_CR_RPP), OWIRE_CR_RPP_LSH))
Sleep(0);
LeaveCriticalSection(&g_hOwireLock);
OWIRE_FUNCTION_EXIT();
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: OwireWriteData
//
// Write count bytes of data to DS2502.
// This function calls OwireWriteByte to write one byte at
// a time from writeBuf.
//
// Parameters:
// writeBuf
// [in] Buffer containing bytes to write to DS2502
// count
// [in] Number of bytes to write from writeBuf
//
// Returns:
// TRUE if write sequence successful
// FALSE if write sequence failed
//
//------------------------------------------------------------------------------
BOOL OwireWriteData(BYTE* writeBuf, DWORD count)
{
UINT32 i;
OWIRE_FUNCTION_ENTRY();
EnterCriticalSection(&g_hOwireLock);
// Write one byte at a time to the DS2502
for (i = 0; i < count; i++)
{
if (!OwireWriteByte(writeBuf[i]))
{
return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -