STM32F10x_StdPeriph_Examples/BKP/Backup_Data/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    BKP/Backup_Data/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body.
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x.h"
00023 #include "stm32_eval.h"
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup BKP_Backup_Data
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 /* Private define ------------------------------------------------------------*/
00035 #if defined USE_STM3210B_EVAL || defined USE_STM32100B_EVAL
00036   #define BKP_DR_NUMBER              10   
00037 #else
00038   #define BKP_DR_NUMBER              42
00039 #endif /* USE_STM3210B_EVAL or USE_STM32100B_EVAL */
00040 
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 #if defined USE_STM3210B_EVAL || defined USE_STM32100B_EVAL
00044 uint16_t BKPDataReg[BKP_DR_NUMBER] =
00045   {
00046     BKP_DR1, BKP_DR2, BKP_DR3, BKP_DR4, BKP_DR5, BKP_DR6, BKP_DR7, BKP_DR8,
00047     BKP_DR9, BKP_DR10
00048   };
00049 #else
00050 uint16_t BKPDataReg[BKP_DR_NUMBER] =
00051   {
00052     BKP_DR1, BKP_DR2, BKP_DR3, BKP_DR4, BKP_DR5, BKP_DR6, BKP_DR7, BKP_DR8,
00053     BKP_DR9, BKP_DR10, BKP_DR11, BKP_DR12, BKP_DR13, BKP_DR14, BKP_DR15, BKP_DR16,
00054     BKP_DR17, BKP_DR18, BKP_DR19, BKP_DR20, BKP_DR21, BKP_DR22, BKP_DR23, BKP_DR24,
00055     BKP_DR25, BKP_DR26, BKP_DR27, BKP_DR28, BKP_DR29, BKP_DR30, BKP_DR31, BKP_DR32,
00056     BKP_DR33, BKP_DR34, BKP_DR35, BKP_DR36, BKP_DR37, BKP_DR38, BKP_DR39, BKP_DR40,
00057     BKP_DR41, BKP_DR42
00058   };  
00059 #endif /* USE_STM3210B_EVAL or USE_STM32100B_EVAL */
00060 
00061 /* Private function prototypes -----------------------------------------------*/
00062 void WriteToBackupReg(uint16_t FirstBackupData);
00063 uint8_t CheckBackupReg(uint16_t FirstBackupData);
00064 
00065 /* Private functions ---------------------------------------------------------*/
00066 
00067 /**
00068   * @brief   Main program.
00069   * @param  None
00070   * @retval None
00071   */
00072 int main(void)
00073 {
00074   /*!< At this stage the microcontroller clock setting is already configured, 
00075        this is done through SystemInit() function which is called from startup
00076        file (startup_stm32f10x_xx.s) before to branch to application main.
00077        To reconfigure the default setting of SystemInit() function, refer to
00078        system_stm32f10x.c file
00079      */     
00080        
00081   /* Initialize Leds mounted on STM3210X-EVAL board */
00082   STM_EVAL_LEDInit(LED1);
00083   STM_EVAL_LEDInit(LED2);
00084   STM_EVAL_LEDInit(LED3);
00085   STM_EVAL_LEDInit(LED4);
00086   
00087   /* Enable PWR and BKP clock */
00088   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
00089 
00090   /* Enable write access to Backup domain */
00091   PWR_BackupAccessCmd(ENABLE);
00092 
00093   /* Clear Tamper pin Event(TE) pending flag */
00094   BKP_ClearFlag();
00095 
00096   /* Check if the Power On Reset flag is set */
00097   if(RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
00098   {
00099     /* Clear reset flags */
00100     RCC_ClearFlag();
00101 
00102     /* Turn on LED3 */
00103     STM_EVAL_LEDOn(LED3);
00104    
00105     /* Check if Backup data registers are programmed */
00106     if(CheckBackupReg(0x3210) == 0x00)
00107     { /* Backup data registers values are correct */
00108 
00109       /* Turn on LED1 */
00110       STM_EVAL_LEDOn(LED1);
00111     }
00112     else
00113     { /* Backup data registers values are not correct or they are not yet
00114          programmed (when the first time the program is executed) */
00115 
00116       /* Write data to Backup data registers */
00117       WriteToBackupReg(0x3210);
00118 
00119       /* Turn on LED2 */
00120       STM_EVAL_LEDOn(LED2);
00121     }
00122   }
00123 
00124   /* Turn on LED4 */
00125   STM_EVAL_LEDOn(LED4);
00126        
00127   while (1)
00128   {    
00129   }
00130 }
00131 
00132 /**
00133   * @brief  Writes data Backup DRx registers.
00134   * @param  FirstBackupData: data to be written to Backup data registers.
00135   * @retval None
00136   */
00137 void WriteToBackupReg(uint16_t FirstBackupData)
00138 {
00139   uint32_t index = 0;
00140 
00141   for (index = 0; index < BKP_DR_NUMBER; index++)
00142   {
00143     BKP_WriteBackupRegister(BKPDataReg[index], FirstBackupData + (index * 0x5A));
00144   }  
00145 }
00146 
00147 /**
00148   * @brief  Checks if the Backup DRx registers values are correct or not.
00149   * @param  FirstBackupData: data to be compared with Backup data registers.
00150   * @retval 
00151   *          - 0: All Backup DRx registers values are correct
00152   *          - Value different from 0: Number of the first Backup register
00153   *            which value is not correct
00154   */
00155 uint8_t CheckBackupReg(uint16_t FirstBackupData)
00156 {
00157   uint32_t index = 0;
00158 
00159   for (index = 0; index < BKP_DR_NUMBER; index++)
00160   {
00161     if (BKP_ReadBackupRegister(BKPDataReg[index]) != (FirstBackupData + (index * 0x5A)))
00162     {
00163       return (index + 1);
00164     }
00165   }
00166 
00167   return 0;
00168 }
00169 
00170 #ifdef  USE_FULL_ASSERT
00171 
00172 /**
00173   * @brief  Reports the name of the source file and the source line number
00174   *         where the assert_param error has occurred.
00175   * @param  file: pointer to the source file name
00176   * @param  line: assert_param error line source number
00177   * @retval None
00178   */
00179 void assert_failed(uint8_t* file, uint32_t line)
00180 { 
00181   /* User can add his own implementation to report the file name and line number,
00182      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00183 
00184   /* Infinite loop */
00185   while (1)
00186   {
00187   }
00188 }
00189 #endif
00190 
00191 /**
00192   * @}
00193   */ 
00194 
00195 /**
00196   * @}
00197   */ 
00198 
00199 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32