94 lines
2.1 KiB
C
94 lines
2.1 KiB
C
#include "stm32f4xx_hal.h"
|
|
#include "user_power_mgmt.h"
|
|
|
|
typedef struct {
|
|
RCC_OscInitTypeDef RCC_OscInitStruct;
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct;
|
|
uint32_t Flash_Latency;
|
|
uint32_t Voltage_Scale;
|
|
} user_pm_vfs_t;
|
|
|
|
#include "user_power_mgmt_profiles.h"
|
|
|
|
static void user_pm_error_handler(void) {
|
|
while(1) {
|
|
//
|
|
}
|
|
}
|
|
|
|
static void user_pm_enter_sleep(void) {
|
|
//
|
|
}
|
|
|
|
static void user_pm_exit_sleep(void) {
|
|
//
|
|
}
|
|
|
|
/**
|
|
* @brief Set VFS mode, bad things happens if wrong sequence applied.
|
|
*
|
|
* @param vfs
|
|
* @return HAL_StatusTypeDef
|
|
*/
|
|
static HAL_StatusTypeDef user_pm_set_vfs_mode(user_pm_vfs_t *vfs) {
|
|
|
|
//Temporary raise voltage to SCALE 1
|
|
if(HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) {
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
// Set oscillators and PLLs.
|
|
if(HAL_RCC_OscConfig(&vfs->RCC_OscInitStruct) != HAL_OK) {
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
// Apply system clock settings and bus clock settings.
|
|
if(HAL_RCC_ClockConfig(&vfs->RCC_ClkInitStruct, vfs->Flash_Latency) != HAL_OK) {
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
// Set correct voltage scale.
|
|
if(HAL_PWREx_ControlVoltageScaling(vfs->Voltage_Scale) != HAL_OK) {
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief Scale system frequency to specified preset
|
|
*
|
|
* @param preset: Target VFS preset
|
|
* @return Status
|
|
*/
|
|
HAL_StatusTypeDef user_pm_scale_vfs(user_pm_vfs_preset_t preset) {
|
|
|
|
if(preset > USER_PM_VFS_END) return HAL_ERROR;
|
|
|
|
// Transition system to a stable state.
|
|
if(user_pm_set_vfs_mode(&s_user_vfs_table[USER_PM_VFS_SAFE]) != HAL_OK) {
|
|
// Something bad happened.
|
|
user_pm_error_handler();
|
|
}
|
|
|
|
// Apply new frequency settings.
|
|
if(user_pm_set_vfs_mode(&s_user_vfs_table[preset]) != HAL_OK) {
|
|
// Another bad thing happened.
|
|
user_pm_error_handler();
|
|
}
|
|
|
|
// Update global variables
|
|
SystemCoreClockUpdate();
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief Sleep for specific timeout
|
|
*
|
|
* @param sleep_msec: Time to sleep
|
|
* @return Time actually in sleep mode
|
|
*/
|
|
uint32_t user_pm_idle_timeout(uint32_t sleep_msec) {
|
|
return 0;
|
|
} |