What is the correct synchronization mechanism in ISR

what is the correct synchronization mechanism in ISR context? mutex, CriticalSectionLock, or any other?

Hello t_huang,

The related mbed documentation says:

Mutexes should not be used within interrupt service routines (ISRs). The correct way to work with ISRs is not to let them do any real processing when responding to an interrupt. Instead, interrupts should send an event or message to a thread; the interrupt then completes, the thread is rescheduled and, subsequently, it is the thread that will perform the processing. This allows the thread to acquire the mutex safely when it needs it for processing.

The RTOS provides several mechanisms to move interrupt processing onto a thread. These include, but are not limited to:

Note: … if you acquire a mutex lock in an interrupt, you can break the thread safety mechanisms and introduce race conditions into an otherwise safe piece of code.

Mbed OS provides atomic functions to make code interrupt safe. If you must modify an object or data structure from interrupts, you can use these atomic functions to synchronize the access.

Critical sections

Critical sections disable interrupts to provide uninterrupted access to a resource, so you can use critical sections to make code interrupt safe. However, you should avoid using critical sections if you can, because they must execute quickly, or they will cause system instability.

Notes:

  • Do not perform time consuming operations inside critical sections. This will negatively affect the timing of the entire system, because all interrupts are disabled during critical sections.
  • Do not invoke any standard lib or RTOS functions within a critical section; it could result in a hard fault because RTOS performs SVC calls.

Mbed OS RTOS is based on (inludes) CMSIS-RTOS. There is a very nice CMSIS-RTOS tutorial available for free. The RTOS Interrup Handling section provides additional info on the topic.

Best regards,

Zoltan

what does the message blow mean:
Error Message: Mutex: 0x20008D64, Not allowed in ISR context
I try for debugging for a long time,but cann’t solute it.

According to the related mbed documentation

Mutex methods cannot be called from interrupt service routines (ISR). In the current version of Mbed OS, if you attempt to use a mutex from within an ISR, it will treat that as a fatal system error.

So I think the error message reported above is telling you that a mutex method was called in ISR context, which is not allowed.