Not every DRV_LOCKED is not only returned when the mutex is locked, but also occurs unwanted when the DRV_MUTEX_TRYLOCK function was interrupted between load exclusive and store exclusive (see below). Code Block |
---|
| /*!
* \brief Try to lock the given mutex
* \details Checks if the mutex is locked and acquires it by executing an exclusive LDREX, STREX cycle.
* \memberof DRV_MUTEX_T
* \param [in] ptMutex ptMutex to be locked
* \return DRV_LOCKED The mutex was locked
* DRV_OK The mutex is locked
*/
__STATIC_FORCEINLINE DRV_STATUS_E DRV_MUTEX_TRYLOCK(DRV_MUTEX_T* ptMutex)
{
uint32_t ret;
ret = __LDREXW((volatile uint32_t*) &ptMutex->eState);
if(ret == (uint32_t) DRV_MUTEX_STATE_LOCKED)
{
return DRV_LOCKED;
}
else
{
ret = __STREXW(DRV_MUTEX_STATE_LOCKED, (volatile uint32_t*) &ptMutex->eState);
__DMB();
if(0 == ret)
{
return DRV_OK;
}
else
{
return DRV_LOCKED;
}
}
} |
When using the mutex API, it is advisible to free mutexes after a certain time or a certain amount of trylocks in order to prevent deadlocks. The following code snipped can help:STATE_E differing from DRV_OK is an error. A good example might be the DRV_LOCKED state, which might occur because an interrupt interrupted the allocation of the locking mutex. If this has happened one has to try again locking it. However, the API might also be locked and another task is using it. So it is feasible to log the time or count how often this happens and decide at which point this might be an error. DRV_LOCKED is not an error condition, just the notification that the API was not free at that time or the trylock function was interrupted. Fell free to implement your own mutex in the netx_drv_config.h. The following code snipped is an example code on how to make use of the returned state.
View file |
---|
name | SPIFaultHandlingExample.c |
---|
height | 150 |
---|
|
|