The appropriate approach depends on the Application. The goal is to understand the pros and cons, then determine the correct methods. As can see in Cortex-M4 instruction the LDREX/ STREX requires 2 cycles to be executed and to disable/ enable interrupts 1 or 2 cycles. The mentioned disadvantage of disabling interrupts is that the critical interrupts are delayed for that period of time. A different approach is the use of "DRV_MUTEX_LOCK" function by modifying the existing #define DRV_LOCK(__HANDLE__) (in netx_drv_conf.h file) to #define DRV_LOCK(__HANDLE__) DRV_MUTEX_LOCK(&(__HANDLE__)→tLock). Here, the system waits until an event occurs by using the following function: Code Block |
---|
| /**
\brief Wait For Event
\details Wait For Event is a hint instruction that permits the processor to enter
a low-power state until one of a number of events occurs.
*/
#define __WFE() __ASM volatile ("wfe") |
Of course, you can implement your own function to lock the mutex. Depending on the application, DRV_MUTEX can be converted into a simple lock. See the following example: Note: If an interrupt interrupted this function shortly before ptMutex->eState = DRV_MUTEX_STATE_LOCKED; and set the eState to LOCKED, the application does not notice this. Code Block |
---|
| #define DRV_LOCK(__HANDLE__) DRV_SIMPLE_LOCK(&(__HANDLE__)→tLock)
__STATIC_FORCEINLINE DRV_STATUS_E DRV_SIMPLE_LOCK(DRV_MUTEX_T* ptMutex)
{
if(ptMutex->eState == DRV_MUTEX_STATE_LOCKED)
{
return DRV_LOCKED;
}
else if(ptMutex->eState == DRV_MUTEX_STATE_UNLOCKED)
{
ptMutex->eState = DRV_MUTEX_STATE_LOCKED;
return DRV_OK;
}
else
{
return DRV_ERROR; /* please check the initialization of ptDriver->tLock = DRV_LOCK_INITIALIZER; */
}
} |
|