This section describes the programming interface of the Notifier driver.
Notifier Overview
The Notifier provides a configuration dynamic change service. Based on this service, applications can switch between pre-defined configurations. The Notifier enables drivers and applications to register callback functions to this framework. Each time that the configuration is changed, drivers and applications receive a notification and change their settings. To simplify, the Notifier only supports the static callback registration. This means that, for applications, all callback functions are collected into a static table and passed to the Notifier.
The configuration transition includes 3 steps:
- Before configuration transition, the Notifier sends a "BEFORE" message to the callback table. When this message is received, IP drivers should check whether any current processes can be stopped and stop them. If the processes cannot be stopped, the callback function returns an error.
The Notifier supports two types of transition policies, a graceful policy and a forceful policy. When the graceful policy is used, if some callbacks return an error while sending "BEFORE" message, the configuration transition stops and the Notifier sends a "RECOVER" message to all drivers that have stopped. Then, these drivers can recover the previous status and continue to work. When the forceful policy is used, drivers are stopped forcefully.
- After the "BEFORE" message is processed successfully, the system changes to the new configuration.
- After the configuration changes, the Notifier sends an "AFTER" message to the callback table to notify drivers that the configuration transition is finished.
This is an example to use the Notifier in the Power Manager application:
~~~~~{.c}
#include "fsl_notifier.h"
/* Definition of the Power Manager callback
status_t callback0(notifier_notification_block_t *notify, void *data)
{
status_t ret = kStatus_Success;
...
...
...
return ret;
}
/* Definition of the Power Manager user function
status_t APP_PowerModeSwitch(notifier_user_config_t *targetConfig, void *userData)
{
...
...
...
}
...
...
...
...
...
/* Main function
int main(void)
{
/* Define a notifier handle
notifier_handle_t powerModeHandle;
/* Callback configuration
user_callback_data_t callbackData0;
notifier_callback_config_t callbackCfg0 = {callback0,
kNOTIFIER_CallbackBeforeAfter,
(void *)&callbackData0};
notifier_callback_config_t callbacks[] = {callbackCfg0};
/* Power mode configurations
power_user_config_t vlprConfig;
power_user_config_t stopConfig;
notifier_user_config_t *powerConfigs[] = {&vlprConfig, &stopConfig};
/* Definition of a transition to and out the power modes
vlprConfig.mode = kAPP_PowerModeVlpr;
vlprConfig.enableLowPowerWakeUpOnInterrupt = false;
stopConfig = vlprConfig;
stopConfig.mode = kAPP_PowerModeStop;
/* Create Notifier handle
NOTIFIER_CreateHandle(&powerModeHandle, powerConfigs, 2U, callbacks, 1U, APP_PowerModeSwitch, NULL);
...
...
/* Power mode switch
NOTIFIER_switchConfig(&powerModeHandle, targetConfigIndex, kNOTIFIER_PolicyAgreement);
}
~~~~~{.c}
struct notifier_notification_block_t |
struct notifier_callback_config_t |
This structure holds configuration of callbacks. Callbacks of this type are expected to be statically allocated. This structure contains following application-defined data: callback - pointer to the callback function callbackType - specifies when the callback is called callbackData - pointer to the data passed to the callback.
void* notifier_callback_config_t::callbackData |
Notifier handle structure. Contains data necessary for Notifier proper function. Stores references to registered configurations, callbacks, information about their numbers, user function, user data and other internal data. NOTIFIER_CreateHandle() must be called to intialize this handle.
uint8_t notifier_handle_t::configsNumber |
uint8_t notifier_handle_t::callbacksNumber |
uint8_t notifier_handle_t::errorCallbackIndex |
uint8_t notifier_handle_t::currentConfigIndex |
void* notifier_handle_t::userData |
Reference of user defined configuration is stored in an array, notifer switch between these configurations based on this array.
Before and after this function execution, different notification will be sent to registered callbacks. If this function returns any error code, NOTIFIER_SwitchConfig() will exit.
- Parameters
-
targetConfig | target Configuration. |
userData | Refers to other specific data passed to user function. |
- Returns
- An error code or kStatus_Success.
Declaration of callback. It is common for registered callbacks. Reference to function of this type is part of notifier_callback_config_t callback configuration structure. Depending on callback type, function of this prototype is called (see NOTIFIER_SwitchConfig()) before configuration switch, after it or in both cases to notify about the switch progress (see notifier_callback_type_t). When called, type of the notification is passed as parameter along with reference to the target configuration structure (see notifier_notification_block_t) and any data passed during the callback registration. When notified before configuration switch, depending on the configuration switch policy (see notifier_policy_t) the callback may deny the execution of user function by returning any error code different from kStatus_Success (see NOTIFIER_SwitchConfig()).
- Parameters
-
notify | Notification block. |
data | Callback data. Refers to the data passed during callback registration. Intended to pass any driver or application data such as internal state information. |
- Returns
- An error code or kStatus_Success.
Used as return value of Notifier functions.
Enumerator |
---|
kStatus_NOTIFIER_ErrorNotificationBefore |
Error occurs during send "BEFORE" notification.
|
kStatus_NOTIFIER_ErrorNotificationAfter |
Error occurs during send "AFTER" notification.
|
Defines whether user function execution is forced or not. For kNOTIFIER_PolicyForcible, the user function is executed regardless of the callback results, while kNOTIFIER_PolicyAgreement policy is used to exit NOTIFIER_SwitchConfig() when any of the callbacks returns error code. See also NOTIFIER_SwitchConfig() description.
Enumerator |
---|
kNOTIFIER_PolicyAgreement |
NOTIFIER_SwitchConfig() method is exited when any of the callbacks returns error code.
|
kNOTIFIER_PolicyForcible |
user function is executed regardless of the results.
|
Used to notify registered callbacks
Enumerator |
---|
kNOTIFIER_NotifyRecover |
Notify IP to recover to previous work state.
|
kNOTIFIER_NotifyBefore |
Notify IP that configuration setting is going to change.
|
kNOTIFIER_NotifyAfter |
Notify IP that configuration setting has been changed.
|
Used in the callback configuration structure (notifier_callback_config_t) to specify when the registered callback is called during configuration switch initiated by NOTIFIER_SwitchConfig(). Callback can be invoked in following situations:
- before the configuration switch (Callback return value can affect NOTIFIER_SwitchConfig() execution. Refer to the NOTIFIER_SwitchConfig() and notifier_policy_t documentation).
- after unsuccessful attempt to switch configuration
- after sucecessful configuration switch
Enumerator |
---|
kNOTIFIER_CallbackBefore |
Callback handles BEFORE notification.
|
kNOTIFIER_CallbackAfter |
Callback handles AFTER notification.
|
kNOTIFIER_CallbackBeforeAfter |
Callback handles BEFORE and AFTER notification.
|
- Parameters
-
notifierHandle | A pointer to notifier handle |
configs | A pointer to an array with references to all configurations which is handled by the Notifier. |
configsNumber | Number of configurations. Size of configs array. |
callbacks | A pointer to an array of callback configurations. If there are no callbacks to register during Notifier initialization, use NULL value. |
callbacksNumber | Number of registered callbacks. Size of callbacks array. |
userFunction | user function. |
userData | user data passed to user function. |
- Returns
- An error code or kStatus_Success.
This function sets the system to the target configuration. Before transition, the Notifier sends notifications to all callbacks registered to the callback table. Callbacks are invoked in the following order: All registered callbacks are notified ordered by index in the callbacks array. The same order is used for before and after switch notifications. The notifications before the configuration switch can be used to obtain confirmation about the change from registered callbacks. If any registered callback denies the configuration change, further execution of this function depends on the notifier policy: the configuration change is either forced (kNOTIFIER_PolicyForcible) or exited (kNOTIFIER_PolicyAgreement). When configuration change is forced, the result of the before switch notifications are ignored. If agreement is required, if any callback returns an error code then further notifications before switch notifications are cancelled and all already notified callbacks are re-invoked The index of the callback which returned error code during pre-switch notifications is stored (any error codes during callbacks re-invocation are ignored) and NOTIFIER_GetErrorCallback() can be used to get it. Regardless of the policies, if any callback returned an error code, an error code denoting in which phase the error occurred is returned when NOTIFIER_SwitchConfig() exits.
- Parameters
-
notifierHandle | pointer to notifier handle |
configIndex | Index of the target configuration. |
policy | Transaction policy, kNOTIFIER_PolicyAgreement or kNOTIFIER_PolicyForcible. |
- Returns
- An error code or kStatus_Success.
This function returns index of the last callback that failed during the configuration switch while the last NOTIFIER_SwitchConfig() was called. If the last NOTIFIER_SwitchConfig() call ended successfully value equal to callbacks number is returned. Returned value represents index in the array of static call-backs.
- Parameters
-
notifierHandle | pointer to notifier handle |
- Returns
- Callback index of last failed callback or value equal to callbacks count.