/**
******************************************************************************
* @file PWR/PWR_BOR/Src/main.c
* @author MCD Application Team
* @brief This sample code shows how to use STM32F4xx PWR HAL API to manage the
* Programmable Voltage Detector (PVD).
******************************************************************************
* @attention
*
*
© Copyright (c) 2017 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/** @addtogroup STM32F4xx_HAL_Examples
* @{
*/
/** @addtogroup PWR_BOR
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Uncomment the corresponding line to select the BOR level*/
//#define BOR_LEVEL OB_BOR_OFF /*!< BOR Reset threshold levels for 1.64V - 2.10V VDD power supply */
//#define BOR_LEVEL OB_BOR_LEVEL1 /*!< BOR Reset threshold levels for 2.10V - 2.40V VDD power supply */
//#define BOR_LEVEL OB_BOR_LEVEL2 /*!< BOR Reset threshold levels for 2.40V - 2.70V VDD power supply */
#define BOR_LEVEL OB_BOR_LEVEL3 /*!< BOR Reset threshold levels for 2.70V - 3.00V VDD power supply */
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* OB Program structure */
FLASH_OBProgramInitTypeDef FLASH_OBInitStruct;
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static void Error_Handler(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/* STM32F4xx HAL library initialization:
- Configure the Flash prefetch, instruction and Data caches
- Configure the Systick to generate an interrupt each 1 msec
- Set NVIC Group Priority to 4
- Global MSP (MCU Support Package) initialization
*/
HAL_Init();
/* Configure LED1 and LED3 */
BSP_LED_Init(LED1);
BSP_LED_Init(LED3);
/* Configure the system clock to 180 MHz */
SystemClock_Config();
/* Configure Key Button */
BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
/* Test if Key push-button on EVAL Board is pressed */
if(BSP_PB_GetState(BUTTON_KEY) == 0x00)
{
HAL_FLASHEx_OBGetConfig(&FLASH_OBInitStruct);
/* Get BOR Option Bytes */
if((FLASH_OBInitStruct.BORLevel & 0x0C) != BOR_LEVEL)
{
/* Unlocks the option bytes block access */
HAL_FLASH_OB_Unlock();
/* Select the desired V(BOR) Level */
FLASH_OBInitStruct.OptionType = OPTIONBYTE_BOR;
FLASH_OBInitStruct.BORLevel = BOR_LEVEL;
HAL_FLASHEx_OBProgram(&FLASH_OBInitStruct);
/* Launch the option byte loading */
HAL_FLASH_OB_Launch();
/* Locks the option bytes block access */
HAL_FLASH_OB_Lock();
}
}
/* Infinite loop */
while (1)
{
/* Toggle LED1 */
BSP_LED_Toggle(LED1);
/* Insert 50 ms delay */
HAL_Delay(50);
}
}
/**
* @brief System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 180000000
* HCLK(Hz) = 180000000
* AHB Prescaler = 1
* APB1 Prescaler = 4
* APB2 Prescaler = 2
* HSE Frequency(Hz) = 25000000
* PLL_M = 25
* PLL_N = 360
* PLL_P = 2
* PLL_Q = 7
* VDD(V) = 3.3
* Main regulator output voltage = Scale1 mode
* Flash Latency(WS) = 5
* @param None
* @retval None
*/
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();
/* The voltage scaling allows optimizing the power consumption when the device is
clocked below the maximum system frequency, to update the voltage scaling value
regarding system frequency refer to product datasheet. */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/* Activate the Over Drive feature (available only for STM32F42xxx/43xxx devices)*/
if(HAL_PWREx_EnableOverDrive() != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
static void Error_Handler(void)
{
/* Turn LED3 on */
BSP_LED_On(LED3);
while(1)
{
}
}
/**
* @brief SYSTICK callback
* @param None
* @retval None
*/
void HAL_SYSTICK_Callback(void)
{
HAL_IncTick();
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/