/** * This function contains the actual functionality for the XMutex TestApp * example. he idea is to have this application running on two processors which * share the same bus and have access to the Mutex hardware. CPU 0 is considered * the primary CPU since it is the one which is expected to initially lock the * Mutex. * * @param MutexDeviceID is the device id to be initialized and used. * * @return * - XST_SUCCESS if successful * - XST_FAILURE if unsuccessful * * @note None * ******************************************************************************/ int MutexExample(u16 MutexDeviceID) { XMutex_Config *ConfigPtr; XStatus Status; u32 TimeoutCount = 0; /* * Lookup configuration data in the device configuration table. * Use this configuration info down below when initializing this * driver instance. */ ConfigPtr = XMutex_LookupConfig(MutexDeviceID); if (ConfigPtr == (XMutex_Config *)NULL) { return XST_FAILURE; } /* * Perform the rest of the initialization. */ Status = XMutex_CfgInitialize(&Mutex, ConfigPtr, ConfigPtr->BaseAddress); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Depending on which CPU this is running on, either lock the * Mutex or test the Mutex to see if it is locked. */ if (XPAR_CPU_ID == 0) { XMutex_Lock(&Mutex, MUTEX_NUM); /* Acquire lock */ XMutex_Unlock(&Mutex, MUTEX_NUM); /* Release lock */ } else { /* * Try to acquire lock, other CPU should have it so it * should fail. */ while ((XMutex_Trylock(&Mutex, MUTEX_NUM)) != XST_SUCCESS) { if (TimeoutCount++ > LOCK_TIMEOUT) { return XST_FAILURE; } } XMutex_Unlock(&Mutex, MUTEX_NUM); /* Release lock */ } return XST_SUCCESS; }
/** * * Selftest a particular Mutex hardware core. * * @param InstancePtr is a pointer to the XMutex instance to be worked on. * * @return * - XST_SUCCESS if test was successful. * - XST_FAILURE if test was not successful. * * @note * * This test is destructive. It will fail if the Mutex is currently being used. * This is also a blocking call, if there is another process which has the * Mutex, the first _lock will hand the test until the other process releases * it. * ******************************************************************************/ int XMutex_SelfTest(XMutex *InstancePtr) { int Status; u32 Locked; u32 Owner; int Index; for (Index = 0; Index < InstancePtr->Config.NumMutex; Index++) { /* Lock Mutex blocking call*/ XMutex_Lock(InstancePtr, Index); /* Get Status and verify if Status matches */ XMutex_GetStatus(InstancePtr, Index, &Locked, &Owner); if (Owner != XPAR_CPU_ID) { return XST_FAILURE; } if (Locked != LOCKED_BIT) { return XST_FAILURE; } /* Verify that the Mutex is locked */ if (XMutex_IsLocked(InstancePtr, Index) != TRUE) { return XST_FAILURE; } /* Unlock Mutex */ Status = XMutex_Unlock(InstancePtr, Index); if (Status != XST_SUCCESS) { return Status; } /* Get Status and verify if Status matches */ XMutex_GetStatus(InstancePtr, Index, &Locked, &Owner); if (Owner != 0) { return XST_FAILURE; } if (Locked != 0) { return XST_FAILURE; } } return XST_SUCCESS; }