www.pudn.com > ethercat-slave-source-code.rar > fc1100hw.c, change:2013-05-06,size:8931b


/** 
\addtogroup FC1100HW FC1100 Hardware Access 
@{ 
*/ 
 
/** 
\file    fc1100hw.c 
\author EthercatSSC@beckhoff.com 
\brief Implementation 
Hardware access implementation for FC1100 hardware access on Win32 platforms 
 
\version 5.10 
 
<br>Changes to version V5.0:<br> 
V5.01 HW1: Invalid ESC access function was used<br> 
<br>Changes to version V4.42:<br> 
V5.0 ESC4: Save SM disable/Enable. Operation may be pending due to frame handling. 
*/ 
 
/*----------------------------------------------------------------------------------------- 
------ 
------    Includes 
------ 
-----------------------------------------------------------------------------------------*/ 
#ifdef _WIN32 
#include <stdio.h> 
#include <tchar.h> 
#include "TcHelper.h" 
#endif 
 
#include "ecat_def.h" 
#if FC1100_HW 
 
#define _FC1100_ 
#include "fc1100hw.h" 
#undef _FC1100_ 
#include "ecatappl.h" 
 
/*----------------------------------------------------------------------------------------- 
------ 
------    Local Variables 
------ 
-----------------------------------------------------------------------------------------*/ 
 
UINT16 * pFc1100Device; 
HANDLE gDoneEvent; 
HANDLE hTimerQueue; 
 
TSYNCMAN    TmpSyncMan; 
 
UINT16 TimerCnt; 
 
/*----------------------------------------------------------------------------------------- 
------ 
------    Local Functions 
------ 
-----------------------------------------------------------------------------------------*/ 
BOOL InitSystemEvents(void); 
void FreeTimerResources(void); 
 
 
UINT16* GetFc1100Pointer(void) 
{ 
    pFc1100Device = (UINT16*) MapESCPointer(); 
#ifdef _CONSOLE 
    printf("Device pointer:  0x%X.\r\n",pFc1100Device); 
/*ECATCHANGE_START(V5.10)*/ 
    if(pFc1100Device == 0) 
    { 
        DWORD err = GetLastError(); 
        printf("Error:  0x%X.\r\n",err); 
    } 
/*ECATCHANGE_END(V5.10)*/ 
#endif 
    return pFc1100Device; 
} 
 
void FreeFc1100Handle(void) 
{ 
    UnmapESCPointer(); 
    pFc1100Device = NULL; 
} 
 
BOOL WINAPI ConsoleHandler(DWORD CEvent) 
{ 
    switch(CEvent) 
    { 
    case CTRL_CLOSE_EVENT: 
            bRunApplication = FALSE; 
        break; 
    } 
    return TRUE; 
} 
 
VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired) 
{ 
 
#if ECAT_TIMER_INT 
    ECAT_CheckTimer(); 
#endif 
 
    TimerCnt++; 
 
    SetEvent(gDoneEvent); 
} 
 
BOOL InitSystemEvents(void) 
{ 
    BOOL Result =  FALSE; 
    HANDLE hTimer = NULL; 
    hTimerQueue = NULL; 
 
    // Use an event object to track the TimerRoutine execution 
    gDoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 
    if (NULL != gDoneEvent) 
    { 
        // Create the timer queue. 
        hTimerQueue = CreateTimerQueue(); 
        if (NULL != hTimerQueue) 
        { 
            // Set a timer to call the timer routine in 10 seconds. 
            if (CreateTimerQueueTimer( &hTimer, hTimerQueue, (WAITORTIMERCALLBACK)TimerRoutine, NULL , 1, 1, 0)) 
            { 
                Result =  TRUE; 
            } 
        } 
    } 
 
    TimerCnt = 0; 
 
    if(Result == TRUE) 
        Result =  SetConsoleCtrlHandler( (PHANDLER_ROUTINE)ConsoleHandler,TRUE); 
 
 
    return Result; 
} 
 
void FreeTimerResources(void) 
{ 
    CloseHandle(gDoneEvent); 
 
    if(hTimerQueue != NULL) 
    { 
        // Delete all timers in the timer queue. 
        if (!DeleteTimerQueue(hTimerQueue)) 
        { 
#ifdef _CONSOLE 
            printf("DeleteTimerQueue failed (%X)\n", GetLastError()); 
#endif 
        } 
    } 
 
} 
 
///////////////////////////////////////////////////////////////////////////////////////// 
/** 
\return     0 if initialization was successful 
 
 \brief    This function initialize the EtherCAT Slave Interface. 
*//////////////////////////////////////////////////////////////////////////////////////// 
 
UINT16 HW_Init(void) 
{ 
    /* the memory interface to the ESC, the ESC-interrupt and the ECAT-timer for the 
       watchdog monitoring should be initialized here microcontroller specific*/ 
 
    pEsc = (MEM_ADDR ESCMEM *) GetFc1100Pointer(); 
    if(pEsc == NULL) 
    { 
        return 1; 
    } 
    if (InitSystemEvents() == FALSE) 
    { 
        return 1; 
    } 
 
    /* initialize the AL_Event Mask Register */ 
#if ESC_32BIT_ACCESS 
    { 
    UINT32 ResetValue = 0; 
    HW_EscWriteDWord(ResetValue, ESC_AL_EVENTMASK_OFFSET); 
    } 
#else 
    { 
    UINT16 ResetValue = 0; 
    HW_EscWriteWord(ResetValue, ESC_AL_EVENTMASK_OFFSET); 
    } 
#endif  
 
    return 0; 
} 
 
void HW_Release(void) 
{ 
    FreeFc1100Handle(); 
 
    FreeTimerResources(); 
} 
 
#if BOOTSTRAPMODE_SUPPORTED 
///////////////////////////////////////////////////////////////////////////////////////// 
/** 
 
 \brief    This function resets the hardware 
*//////////////////////////////////////////////////////////////////////////////////////// 
 
void     HW_RestartTarget(void) 
{ 
} 
#endif /* BOOTSTRAPMODE_SUPPORTED */ 
 
 
///////////////////////////////////////////////////////////////////////////////////////// 
/** 
 \param     channel        Sync Manager channel 
 
 \return     pPdSyncMan        Pointer to the settings of requested SYNC Manager channel 
 
 \brief    This function is called to read the SYNC Manager channel descriptions of the 
             process data SYNC Managers. 
*//////////////////////////////////////////////////////////////////////////////////////// 
 
TSYNCMAN ESCMEM * HW_GetSyncMan( UINT8 channel ) 
{ 
    HW_EscRead((MEM_ADDR *)&TmpSyncMan, ESC_SYNCMAN_REG_OFFSET + (channel * SIZEOF_SM_REGISTER), SIZEOF_SM_REGISTER ); 
 
    return &TmpSyncMan; 
} 
 
///////////////////////////////////////////////////////////////////////////////////////// 
/** 
 \param     channel        Sync Manager channel 
 
 \brief    This function disables a Sync Manager channel 
*//////////////////////////////////////////////////////////////////////////////////////// 
void HW_DisableSyncManChannel(UINT8 channel) 
{ 
    UINT16 Offset; 
#if ESC_32BIT_ACCESS 
    //The registers from 0x804 to 0x806 are only readable from PDI => writing 0 for all registers is valid 
    UINT32 smStatus = SM_SETTING_PDI_DISABLE; 
    Offset = (ESC_SYNCMAN_CONTROL_OFFSET + (SIZEOF_SM_REGISTER*channel)); 
 
    HW_EscWriteDWord(smStatus,Offset); 
 
    /*wait until SyncManager is disabled*/ 
    do 
    { 
        HW_EscReadDWord(smStatus, Offset); 
    }while(!(smStatus & SM_SETTING_PDI_DISABLE)); 
#elif ESC_16BIT_ACCESS 
    //The register 0x806 is only readable from PDI => writing 0 is valid 
    UINT16 smStatus = SM_SETTING_PDI_DISABLE; 
    Offset = (ESC_SYNCMAN_ACTIVE_OFFSET + (SIZEOF_SM_REGISTER*channel)); 
 
    HW_EscWriteWord(smStatus,Offset); 
     
    /*wait until SyncManager is disabled*/ 
    do 
    { 
        HW_EscReadWord(smStatus, Offset); 
    }while(!(smStatus & SM_SETTING_PDI_DISABLE)); 
#else 
    UINT8 smStatus = SM_SETTING_PDI_DISABLE; 
    Offset = (ESC_SM_PDICONTROL_OFFSET + (SIZEOF_SM_REGISTER*channel)); 
 
    HW_EscWriteByte(smStatus,Offset); 
     
    /*wait until SyncManager is disabled*/ 
    do 
    { 
        HW_EscReadByte(smStatus, Offset); 
    }while(!(smStatus & SM_SETTING_PDI_DISABLE)); 
#endif 
} 
 
///////////////////////////////////////////////////////////////////////////////////////// 
/** 
 \param     channel        Sync Manager channel 
 
 \brief    This function enables a Sync Manager channel 
*//////////////////////////////////////////////////////////////////////////////////////// 
void HW_EnableSyncManChannel(UINT8 channel) 
{ 
    UINT16 Offset; 
#if ESC_32BIT_ACCESS 
    //The registers from 0x804 to 0x806 are only readable from PDI => writing 0 for all registers is valid 
    UINT32 smStatus = 0x00000000; 
    Offset = (ESC_SYNCMAN_CONTROL_OFFSET + (SIZEOF_SM_REGISTER*channel)); 
 
    HW_EscWriteDWord(smStatus,Offset); 
 
    /*wait until SyncManager is enabled*/ 
    do 
    { 
        HW_EscReadDWord(smStatus,Offset); 
    }while((smStatus & SM_SETTING_PDI_DISABLE)); 
#elif ESC_16BIT_ACCESS 
    //The register 0x806 is only readable from PDI => writing 0 is valid 
    UINT16 smStatus = 0x0000; 
    Offset = (ESC_SYNCMAN_ACTIVE_OFFSET + (SIZEOF_SM_REGISTER*channel)); 
 
    HW_EscWriteWord(smStatus,Offset); 
     
    /*wait until SyncManager is enabled*/ 
    do 
    { 
        HW_EscReadWord(smStatus,Offset); 
    }while((smStatus & SM_SETTING_PDI_DISABLE)); 
#else 
    UINT8 smStatus = 0x00; 
    Offset = (ESC_SM_PDICONTROL_OFFSET + (SIZEOF_SM_REGISTER*channel)); 
 
    HW_EscWriteByte(smStatus,Offset); 
     
    /*wait until SyncManager is enabled*/ 
    do 
    { 
        HW_EscReadByte(smStatus,Offset); 
    }while((smStatus & SM_SETTING_PDI_DISABLE)); 
#endif 
} 
 
 
UINT16 HW_GetTimer(void) 
{ 
    return TimerCnt; 
} 
 
void HW_ClearTimer(void) 
{ 
    TimerCnt = 0; 
} 
 
 
#if UC_SET_ECAT_LED 
void HW_SetLed(BOOL RunLed,BOOL ErrLed) 
{ 
 
} 
#endif //#if UC_SET_ECAT_LED 
 
#endif //#if FC1100_HW 
 
/** @} */