3.5.2.2 Application Using Callback

In the non-blocking method, instead of status polling, the callback mechanism can also be used to check the transfer status. Follow these steps to use the callback mechanism:
  1. Register a callback function to the PLIB using a dedicated API given by the PLIB.
  2. Define the function which is registered.
  3. Make the transfer request.
  4. Do other application tasks. Whenever a transfer completes, the registered callback function which is defined, will be called by the PLIB.

Because callbacks are called from the interrupt context, the following guidelines must be followed while defining the callback functions in the application:

  • Must be treated like an ISR.
  • Must be short.
  • Must not call application functions that are not interrupt safe.
  • Use volatile keywords for the variables which are accessed both inside of callback function and outside of callback function.

The code example using a callback mechanism is not shown as MPLAB Harmony v2 PLIBs did not have this feature, hence nothing to compare. The user can refer to the PLIB demonstrations in the csp/apps folder in the csp repository for many PLIB callback examples.