工具配置
基本就是默认配置就行,就是在这6个状态里面跳转,重点就是前面2个状态.这个是芯片的电源管理,跟产品的电源管理是两回事。
生成代码
在Generated_Code/pwrMan1.c 里面,对应刚才配置的信息,一共有6个状态。
/* ************************************************************************** Configuration structure for Power Manager Configuration 0* ************************************************************************* */
/*! @brief User Configuration structure power_managerCfg_0 */
power_manager_user_config_t pwrMan1_InitConfig0 = {.powerMode = POWER_MANAGER_HSRUN, /*!< Power manager mode */.sleepOnExitValue = false, /*!< Sleep on exit value */
};
/* ************************************************************************** Configuration structure for Power Manager Configuration 1* ************************************************************************* */
/*! @brief User Configuration structure power_managerCfg_1 */
power_manager_user_config_t pwrMan1_InitConfig1 = {.powerMode = POWER_MANAGER_RUN, /*!< Power manager mode */.sleepOnExitValue = false, /*!< Sleep on exit value */
};
/* ************************************************************************** Configuration structure for Power Manager Configuration 2* ************************************************************************* */
/*! @brief User Configuration structure power_managerCfg_2 */
power_manager_user_config_t pwrMan1_InitConfig2 = {.powerMode = POWER_MANAGER_VLPR, /*!< Power manager mode */.sleepOnExitValue = false, /*!< Sleep on exit value */
};
/* ************************************************************************** Configuration structure for Power Manager Configuration 3* ************************************************************************* */
/*! @brief User Configuration structure power_managerCfg_3 */
power_manager_user_config_t pwrMan1_InitConfig3 = {.powerMode = POWER_MANAGER_STOP1, /*!< Power manager mode */.sleepOnExitValue = false, /*!< Sleep on exit value */
};
/* ************************************************************************** Configuration structure for Power Manager Configuration 4* ************************************************************************* */
/*! @brief User Configuration structure power_managerCfg_4 */
power_manager_user_config_t pwrMan1_InitConfig4 = {.powerMode = POWER_MANAGER_STOP2, /*!< Power manager mode */.sleepOnExitValue = false, /*!< Sleep on exit value */
};
/* ************************************************************************** Configuration structure for Power Manager Configuration 5* ************************************************************************* */
/*! @brief User Configuration structure power_managerCfg_5 */
power_manager_user_config_t pwrMan1_InitConfig5 = {.powerMode = POWER_MANAGER_VLPS, /*!< Power manager mode */.sleepOnExitValue = false, /*!< Sleep on exit value */
};
统一放在powerConfigsArr数组里面,初始化的时候用。
/*! @brief Array of pointers to User configuration structures */
power_manager_user_config_t * powerConfigsArr[] = {&pwrMan1_InitConfig0,&pwrMan1_InitConfig1,&pwrMan1_InitConfig2,&pwrMan1_InitConfig3,&pwrMan1_InitConfig4,&pwrMan1_InitConfig5
};
另外会有一个powerStaticCallbacksConfigsArr结构体数组,把初始化之后的信息接出来,每个结构体就是三个函数,回调函数、回调类型和回调数据,一个结构体对应一个状态。
/*!* @brief callback configuration structure** This structure holds configuration of callbacks passed* to the Power manager during its initialization.* Structures 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* Implements power_manager_callback_user_config_t_Class*/
typedef struct
{power_manager_callback_t callbackFunction;power_manager_callback_type_t callbackType;power_manager_callback_data_t * callbackData;
} power_manager_callback_user_config_t;/*! @brief Array of pointers to User defined Callbacks configuration structures */
power_manager_callback_user_config_t * powerStaticCallbacksConfigsArr[] = {(void *)0};
接口使用
POWER_SYS_Init
初始化接口,传入配置结构体powerConfigsArr,用powerStaticCallbacksConfigsArr获取。
/*FUNCTION************************************************************************ Function Name : POWER_SYS_Init* Description : Initializes the Power manager for operation.* This function initializes the Power manager and its run-time state structure.* Reference to an array of Power mode configuration structures has to be passed* as parameter along with parameter specifying its size. At least one power mode* configuration is required. Optionally, reference to array of predefined* call-backs can be passed with its size parameter.* For details about call-backs refer to the power_manager_callback_user_config_t.* As Power manager stores only references to array of these structures they have* to exist while Power manager is used.** Implements POWER_SYS_Init_Activity*END**************************************************************************/
status_t POWER_SYS_Init(power_manager_user_config_t * (*powerConfigsPtr)[],uint8_t configsNumber,power_manager_callback_user_config_t * (*callbacksPtr)[],uint8_t callbacksNumber)
{DEV_ASSERT(powerConfigsPtr != NULL); /* Reference to the power configurations is valid. */DEV_ASSERT(configsNumber != 0U); /* Power configuration index is valid. */DEV_ASSERT(gPowerManagerState.configs == NULL); /* Driver is not initialized, reference to configuration is not valid. */DEV_ASSERT(gPowerManagerState.configsNumber == 0U); /* Driver is not initialized, number of configurations is zero. *//* Store references to user-defined power mode configurations */gPowerManagerState.configs = (power_manager_user_config_t * (*)[])powerConfigsPtr;gPowerManagerState.configsNumber = configsNumber;gPowerManagerState.currentConfig = 0U;/* Store references to user-defined callback configurations and increment call-back handle counter */if (callbacksPtr != NULL){gPowerManagerState.staticCallbacks = (power_manager_callback_user_config_t * (*)[])callbacksPtr;gPowerManagerState.staticCallbacksNumber = callbacksNumber;/* Default value of handle of last call-back that returned error */gPowerManagerState.errorCallbackIndex = callbacksNumber;}else{gPowerManagerState.staticCallbacks = NULL;gPowerManagerState.staticCallbacksNumber = 0U;gPowerManagerState.errorCallbackIndex = 0U;}return POWER_SYS_DoInit();
}
举个例子,PWR_MAX是状态个数,一般就是6
POWER_SYS_Init(&powerConfigsArr, PWR_MAX, &powerStaticCallbacksConfigsArr, 0);
POWER_SYS_Deinit
逆初始化
/*FUNCTION************************************************************************ Function Name : POWER_SYS_Deinit* Description : De-initializes the Power manager.** Implements POWER_SYS_Deinit_Activity*END**************************************************************************/
status_t POWER_SYS_Deinit(void)
{gPowerManagerState.configs = NULL;gPowerManagerState.configsNumber = 0U;gPowerManagerState.staticCallbacks = NULL;gPowerManagerState.staticCallbacksNumber = 0U;return POWER_SYS_DoDeinit();
}
POWER_SYS_SetMode
这个是最常用的,切换模式。
/*FUNCTION************************************************************************ Function Name : POWER_SYS_SetMode* Description : Configures the power mode.** This function switches to one of the defined power modes. Requested mode number is passed* as an input parameter. This function notifies all registered callback functions before* the mode change (using POWER_MANAGER_CALLBACK_BEFORE set as callback type parameter),* sets specific power options defined in the power mode configuration and enters the specified* mode. In case of success switch, this function also invokes all registered callbacks after* the mode change (using POWER_MANAGER_CALLBACK_AFTER).* The actual mode switch is performed by POWER_SYS_DoSetMode in the specific implementation.* Callbacks are invoked in the following order: All registered callbacks are notified* ordered by index in the callbacks array (see callbacksPtr parameter of POWER_SYS_Init()).* The same order is used for before and after switch notifications.* The notifications before the power mode switch can be used to obtain confirmation about* the change from registered callbacks. If any registered callback denies the power* mode change, further execution of this function depends on mode change policy: the mode* change is either forced(POWER_MANAGER_POLICY_FORCIBLE) or exited(POWER_MANAGER_POLICY_AGREEMENT).* When mode 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* with POWER_MANAGER_CALLBACK_AFTER set as callback type parameter. 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 POWER_SYS_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 POWER_SYS_SetMode() exits.* It is possible to enter any mode supported by the processor. Refer to the chip reference manual* for list of available power modes. If it is necessary to switch into intermediate power mode prior to* entering requested mode, then the intermediate mode is entered without invoking the callback mechanism.** Implements POWER_SYS_SetMode_Activity*END**************************************************************************/
status_t POWER_SYS_SetMode(uint8_t powerModeIndex,power_manager_policy_t policy)
{power_manager_user_config_t * configPtr; /* Local pointer to the requested user-defined power mode configuration */status_t returnCode; /* Function return */status_t errorCode;bool successfulSwitch; /* Power mode switch is successful or not */uint8_t currentStaticCallback = 0U; /* Index to array of statically registered call-backs */power_manager_notify_struct_t notifyStruct; /* Callback notification structure *//* Driver is already initialized. */DEV_ASSERT(gPowerManagerState.configs != NULL);DEV_ASSERT(gPowerManagerState.configsNumber != 0U);/* Power mode index is valid. */DEV_ASSERT(powerModeIndex < gPowerManagerState.configsNumber);/* Initialization of local pointer to the requested user-defined power mode configuration */configPtr = (*gPowerManagerState.configs)[powerModeIndex];/* Reference to the requested user-defined power mode configuration is valid. */DEV_ASSERT(configPtr != NULL);/* Default value of handle of last call-back that returned error */gPowerManagerState.errorCallbackIndex = gPowerManagerState.staticCallbacksNumber;/* Set the transaction policy in the notification structure */notifyStruct.policy = policy;/* Set the target power mode configuration in the notification structure */notifyStruct.targetPowerConfigIndex = powerModeIndex;notifyStruct.targetPowerConfigPtr = configPtr;/* Notify those which asked to be called before the power mode change */notifyStruct.notifyType = POWER_MANAGER_NOTIFY_BEFORE;returnCode = POWER_SYS_CallbacksManagement(¬ifyStruct, ¤tStaticCallback, policy);/* Power mode switch *//* In case that any call-back returned error code and policy doesn't force the mode switch go to after switch call-backs */if ((policy == POWER_MANAGER_POLICY_FORCIBLE) || (returnCode == STATUS_SUCCESS)){returnCode = POWER_SYS_DoSetMode(configPtr);successfulSwitch = (STATUS_SUCCESS == returnCode);}else{/* Unsuccessful switch */successfulSwitch = false;}if (successfulSwitch){/* End of successful switch *//* Update current configuration index */gPowerManagerState.currentConfig = powerModeIndex;/* Notify those which asked to be called after the power mode change */notifyStruct.notifyType = POWER_MANAGER_NOTIFY_AFTER;returnCode = POWER_SYS_CallbacksManagement(¬ifyStruct, ¤tStaticCallback, POWER_MANAGER_POLICY_FORCIBLE);}else{/* End of unsuccessful switch *//* Notify those which have been called before the power mode change */notifyStruct.notifyType = POWER_MANAGER_NOTIFY_RECOVER;errorCode = POWER_SYS_CallbacksManagement(¬ifyStruct, ¤tStaticCallback, POWER_MANAGER_POLICY_FORCIBLE);(void)(errorCode);}return returnCode;
}
入参有两个,一个是模式,就是上面那6个的其中一个,顺序对应配置界面从0-5。在power_manager_S32K1xx.h文件里面,把相应的枚举复制过去就行。
/*!* @brief Power modes enumeration.** Defines power modes. Used in the power mode configuration structure* (power_manager_user_config_t). From ARM core perspective, Power modes* can be generally divided into run modes (High speed run, Run and* Very low power run), sleep (Wait and Very low power wait) and deep sleep modes* (all Stop modes).* List of power modes supported by specific chip along with requirements for entering* and exiting of these modes can be found in chip documentation.* List of all supported power modes:\n* \li POWER_MANAGER_HSRUN - High speed run mode.* \li POWER_MANAGER_RUN - Run mode.* \li POWER_MANAGER_VLPR - Very low power run mode.* \li POWER_MANAGER_WAIT - Wait mode.* \li POWER_MANAGER_VLPW - Very low power wait mode.* \li POWER_MANAGER_PSTOP1 - Partial stop 1 mode.* \li POWER_MANAGER_PSTOP2 - Partial stop 2 mode.* \li POWER_MANAGER_PSTOP1 - Stop 1 mode.* \li POWER_MANAGER_PSTOP2 - Stop 2 mode.* \li POWER_MANAGER_VLPS - Very low power stop mode.* Implements power_manager_modes_t_Class*/
typedef enum
{
#if FEATURE_SMC_HAS_HIGH_SPEED_RUN_MODEPOWER_MANAGER_HSRUN, /*!< High speed run mode. */
#endifPOWER_MANAGER_RUN, /*!< Run mode. */POWER_MANAGER_VLPR, /*!< Very low power run mode. */
#if FEATURE_SMC_HAS_WAIT_VLPWPOWER_MANAGER_WAIT, /*!< Wait mode. */POWER_MANAGER_VLPW, /*!< Very low power wait mode. */
#endif
#if FEATURE_SMC_HAS_PSTOPOPOWER_MANAGER_PSTOP1, /*!< Partial stop 1 mode. */POWER_MANAGER_PSTOP2, /*!< Partial stop 2 mode. */
#endif
#if FEATURE_SMC_HAS_STOPOPOWER_MANAGER_STOP1, /*!< Stop 1 mode. */POWER_MANAGER_STOP2, /*!< Stop 2 mode. */
#endifPOWER_MANAGER_VLPS, /*!< Very low power stop mode. */POWER_MANAGER_MAX
} power_manager_modes_t;
第二个入参是切换模式时候的方法,需要所有回调成功就用POWER_MANAGER_POLICY_AGREEMENT,我们一般用这个。不需要看回调结果就用POWER_MANAGER_POLICY_FORCIBLE。
/*!* @brief Power manager policies.** Defines whether the mode switch initiated by the POWER_SYS_SetMode() is agreed upon* (depending on the result of notification callbacks), or forced.* For POWER_MANAGER_POLICY_FORCIBLE the power mode is changed* regardless of the callback results, while for POWER_MANAGER_POLICY_AGREEMENT policy* any error code returned by one of the callbacks aborts the mode change.* See also POWER_SYS_SetMode() description.* Implements power_manager_policy_t_Class*/
typedef enum
{POWER_MANAGER_POLICY_AGREEMENT, /*!< Power mode is changed if all of the callbacks return success. */POWER_MANAGER_POLICY_FORCIBLE /*!< Power mode is changed regardless of the result of callbacks. */
} power_manager_policy_t;
POWER_SYS_GetLastMode
获取切换前上次的模式
/*FUNCTION************************************************************************ Function Name : POWER_SYS_GetLastMode* Description : This function returns power mode set as the last one.** This function returns index of power mode which was set using POWER_SYS_SetMode() as the last one.* If the power mode was entered although some of the registered call-back denied the mode change* or if any of the call-backs invoked after the entering/restoring run mode failed then the return* code of this function has STATUS_ERROR value.* value.** Implements POWER_SYS_GetLastMode_Activity*END**************************************************************************/
status_t POWER_SYS_GetLastMode(uint8_t * powerModeIndexPtr)
{status_t returnCode; /* Function return *//* Pass index of user-defined configuration structure of currently running power mode */*powerModeIndexPtr = gPowerManagerState.currentConfig;/* Return whether all call-backs executed without error */if (gPowerManagerState.errorCallbackIndex == gPowerManagerState.staticCallbacksNumber){returnCode = STATUS_SUCCESS;}else{returnCode = STATUS_ERROR;}return returnCode;
}
POWER_SYS_GetLastModeConfig
获取上次设置模式的配置内容
/*FUNCTION************************************************************************ Function Name : POWER_SYS_GetLastModeConfig* Description : This function returns user configuration structure of power mode set as the last one.** This function returns reference to configuration structure which was set using POWER_SYS_SetMode()* as the last one. If the current power mode was entered although some of the registered call-back denied* the mode change or if any of the call-backs invoked after the entering/restoring run mode failed then* the return code of this function has STATUS_ERROR value.** Implements POWER_SYS_GetLastModeConfig_Activity*END**************************************************************************/
status_t POWER_SYS_GetLastModeConfig(power_manager_user_config_t ** powerModePtr)
{status_t returnCode; /* Function return *//* Pass reference to user-defined configuration structure of currently running power mode */*powerModePtr = (*gPowerManagerState.configs)[gPowerManagerState.currentConfig];/* Return whether all call-backs executed without error */if (gPowerManagerState.errorCallbackIndex == gPowerManagerState.staticCallbacksNumber){returnCode = STATUS_SUCCESS;}else{returnCode = STATUS_ERROR;}return returnCode;
}
POWER_SYS_GetCurrentMode
获取当前模式
/*FUNCTION************************************************************************ Function Name : POWER_SYS_GetCurrentMode* Description : Returns currently running power mode.** Implements POWER_SYS_GetCurrentMode_Activity**END**************************************************************************/
power_manager_modes_t POWER_SYS_GetCurrentMode(void)
{power_manager_modes_t retVal;switch (SMC_GetPowerModeStatus(SMC)){
#if FEATURE_SMC_HAS_HIGH_SPEED_RUN_MODE/* High speed run mode */case STAT_HSRUN:retVal = POWER_MANAGER_HSRUN;break;
#endif/* Run mode */case STAT_RUN:retVal = POWER_MANAGER_RUN;break;/* Very low power run mode */case STAT_VLPR:retVal = POWER_MANAGER_VLPR;break;/* This should never happen - core has to be in some run mode to execute code */default:retVal = POWER_MANAGER_MAX;break;}return retVal;
}
POWER_SYS_GetErrorCallbackIndex
返回错误信息索引的回调函数,一般用不上。
/*FUNCTION************************************************************************ Function Name : POWER_SYS_GetErrorCallbackIndex* Description : Returns the last failed notification callback.** This function returns index of the last call-back that failed during the power mode switch while* the last POWER_SYS_SetMode() was called. If the last POWER_SYS_SetMode() call ended successfully* value equal to callbacks number is returned. Returned value represents index in the array of* static call-backs.** Implements POWER_SYS_GetErrorCallbackIndex_Activity*END**************************************************************************/
uint8_t POWER_SYS_GetErrorCallbackIndex(void)
{return gPowerManagerState.errorCallbackIndex;
}
POWER_SYS_GetErrorCallback
获取错误的回调函数
/*FUNCTION************************************************************************ Function Name : POWER_SYS_GetErrorCallback* Description : Get the callback which returns error in last mode switch.** Implements POWER_SYS_GetErrorCallback_Activity*END**************************************************************************/
power_manager_callback_user_config_t * POWER_SYS_GetErrorCallback(void)
{/* If all callbacks return success. */return (gPowerManagerState.errorCallbackIndex >=gPowerManagerState.staticCallbacksNumber) ? NULL : (*gPowerManagerState.staticCallbacks)[gPowerManagerState.errorCallbackIndex];
}
POWER_SYS_GetDefaultConfig
获取默认配置
/*FUNCTION************************************************************************ Function Name : POWER_SYS_GetDefaultConfig* Description : Initializes the power_manager configuration structure.* This function returns a pointer of the power_manager configuration structure.* All structure members have default value when CPU is default power mode.** Implements : POWER_SYS_GetDefaultConfig_Activity*END**************************************************************************/
void POWER_SYS_GetDefaultConfig(power_manager_user_config_t * const config)
{POWER_SYS_DoGetDefaultConfig(config);
}
POWER_SYS_GetResetSrcStatusCmd
获取指定源的当前复位源状态
/*FUNCTION************************************************************************ Function Name : POWER_SYS_GetResetSrcStatusCmd* Description : This function will get the current reset source status for specified source** Implements POWER_SYS_GetResetSrcStatusCmd_Activity**END**************************************************************************/
bool POWER_SYS_GetResetSrcStatusCmd(const RCM_Type * const baseAddr , const rcm_source_names_t srcName)
{return RCM_GetSrcStatusCmd(baseAddr , srcName);
}
第一个入参是RCM寄存器基地址,S32K146.h里面有,
#define RCM ((RCM_Type *)RCM_BASE)
第二个入参是唤醒源,在power_manager_S32K1xx.h里面。
/*!* @brief System Reset Source Name definitions* Implements rcm_source_names_t_Class*/
typedef enum
{RCM_LOW_VOLT_DETECT = 1U, /*!< Low voltage detect reset */RCM_LOSS_OF_CLK = 2U, /*!< Loss of clock reset */RCM_LOSS_OF_LOCK = 3U, /*!< Loss of lock reset */
#if FEATURE_RCM_HAS_CMU_LOSS_OF_CLOCKRCM_CMU_LOC = 4U, /*!< CMU Loss of lock reset */
#endifRCM_WATCH_DOG = 5U, /*!< Watch dog reset */RCM_EXTERNAL_PIN = 6U, /*!< External pin reset */RCM_POWER_ON = 7U, /*!< Power on reset */RCM_SJTAG = 8U, /*!< JTAG generated reset */RCM_CORE_LOCKUP = 9U, /*!< core lockup reset */RCM_SOFTWARE = 10U, /*!< Software reset */RCM_SMDM_AP = 11U, /*!< MDM-AP system reset */RCM_STOP_MODE_ACK_ERR = 13U, /*!< Stop mode ack error reset */RCM_SRC_NAME_MAX
} rcm_source_names_t;