STM32F10x_StdPeriph_Examples/BKP/Tamper/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    BKP/Tamper/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 "main.h"
00023 
00024 /** @addtogroup STM32F10x_StdPeriph_Examples
00025   * @{
00026   */
00027 
00028 /** @addtogroup BKP_Tamper
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 #if defined USE_STM3210B_EVAL || defined USE_STM32100B_EVAL
00035   #define BKP_DR_NUMBER              10   
00036 #else
00037   #define BKP_DR_NUMBER              42
00038 #endif /* USE_STM3210B_EVAL or USE_STM32100B_EVAL */
00039 
00040 /* Private macro -------------------------------------------------------------*/
00041 /* Private variables ---------------------------------------------------------*/
00042 #if defined USE_STM3210B_EVAL || defined USE_STM32100B_EVAL
00043 uint16_t BKPDataReg[BKP_DR_NUMBER] =
00044   {
00045     BKP_DR1, BKP_DR2, BKP_DR3, BKP_DR4, BKP_DR5, BKP_DR6, BKP_DR7, BKP_DR8,
00046     BKP_DR9, BKP_DR10
00047   };
00048 #else
00049 uint16_t BKPDataReg[BKP_DR_NUMBER] =
00050   {
00051     BKP_DR1, BKP_DR2, BKP_DR3, BKP_DR4, BKP_DR5, BKP_DR6, BKP_DR7, BKP_DR8,
00052     BKP_DR9, BKP_DR10, BKP_DR11, BKP_DR12, BKP_DR13, BKP_DR14, BKP_DR15, BKP_DR16,
00053     BKP_DR17, BKP_DR18, BKP_DR19, BKP_DR20, BKP_DR21, BKP_DR22, BKP_DR23, BKP_DR24,
00054     BKP_DR25, BKP_DR26, BKP_DR27, BKP_DR28, BKP_DR29, BKP_DR30, BKP_DR31, BKP_DR32,
00055     BKP_DR33, BKP_DR34, BKP_DR35, BKP_DR36, BKP_DR37, BKP_DR38, BKP_DR39, BKP_DR40,
00056     BKP_DR41, BKP_DR42
00057   };  
00058 #endif /* USE_STM3210B_EVAL or USE_STM32100B_EVAL */
00059 
00060 /* Private function prototypes -----------------------------------------------*/
00061 void GPIO_Configuration(void);
00062 void NVIC_Configuration(void);
00063 void WriteToBackupReg(uint16_t FirstBackupData);
00064 uint32_t CheckBackupReg(uint16_t FirstBackupData);
00065 
00066 /* Private functions ---------------------------------------------------------*/
00067 
00068 /**
00069   * @brief   Main program.
00070   * @param  None
00071   * @retval None
00072   */
00073 int main(void)
00074 {
00075   /*!< At this stage the microcontroller clock setting is already configured, 
00076        this is done through SystemInit() function which is called from startup
00077        file (startup_stm32f10x_xx.s) before to branch to application main.
00078        To reconfigure the default setting of SystemInit() function, refer to
00079        system_stm32f10x.c file
00080      */     
00081        
00082   /* NVIC configuration */
00083   NVIC_Configuration();
00084     
00085   /* Initialize Leds mounted on STM3210X-EVAL board */
00086   STM_EVAL_LEDInit(LED1);
00087   STM_EVAL_LEDInit(LED2);
00088   STM_EVAL_LEDInit(LED3);
00089   STM_EVAL_LEDInit(LED4);
00090 
00091   /* Enable PWR and BKP clock */
00092   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
00093 
00094   /* Enable write access to Backup domain */
00095   PWR_BackupAccessCmd(ENABLE);
00096 
00097   /* Disable Tamper pin */
00098   BKP_TamperPinCmd(DISABLE);
00099   
00100   /* Disable Tamper interrupt */
00101   BKP_ITConfig(DISABLE);
00102 
00103   /* Tamper pin active on low level */
00104   BKP_TamperPinLevelConfig(BKP_TamperPinLevel_Low);
00105 
00106   /* Clear Tamper pin Event(TE) pending flag */
00107   BKP_ClearFlag();
00108 
00109   /* Enable Tamper interrupt */
00110   BKP_ITConfig(ENABLE);
00111 
00112   /* Enable Tamper pin */
00113   BKP_TamperPinCmd(ENABLE);
00114    
00115   /* Write data to Backup DRx registers */
00116   WriteToBackupReg(0xA53C);
00117   
00118   /* Check if the written data are correct */
00119   if(CheckBackupReg(0xA53C) == 0x00)
00120   {
00121     /* Turn on LED1 */
00122     STM_EVAL_LEDOn(LED1);
00123   }
00124   else
00125   {
00126     /* Turn on LED2 */
00127     STM_EVAL_LEDOn(LED2);
00128   }
00129         
00130   while (1)
00131   {    
00132   }
00133 }
00134 
00135 /**
00136   * @brief  Configures NVIC and Vector Table base location.
00137   * @param  None
00138   * @retval None
00139   */
00140 void NVIC_Configuration(void)
00141 {
00142   NVIC_InitTypeDef NVIC_InitStructure;
00143 
00144   /* Enable TAMPER IRQChannel */
00145   NVIC_InitStructure.NVIC_IRQChannel = TAMPER_IRQn;
00146   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00147   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00148   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00149   NVIC_Init(&NVIC_InitStructure);
00150 }
00151 
00152 /**
00153   * @brief  Writes data Backup DRx registers.
00154   * @param  FirstBackupData: data to be written to Backup data registers.
00155   * @retval None
00156   */
00157 void WriteToBackupReg(uint16_t FirstBackupData)
00158 {
00159   uint32_t index = 0;
00160 
00161   for (index = 0; index < BKP_DR_NUMBER; index++)
00162   {
00163     BKP_WriteBackupRegister(BKPDataReg[index], FirstBackupData + (index * 0x5A));
00164   }  
00165 }
00166 
00167 /**
00168   * @brief  Checks if the Backup DRx registers values are correct or not.
00169   * @param  FirstBackupData: data to be compared with Backup data registers.
00170   * @retval 
00171   *         - 0: All Backup DRx registers values are correct
00172   *         - Value different from 0: Number of the first Backup register
00173   *           which value is not correct
00174   */
00175 uint32_t CheckBackupReg(uint16_t FirstBackupData)
00176 {
00177   uint32_t index = 0;
00178 
00179   for (index = 0; index < BKP_DR_NUMBER; index++)
00180   {
00181     if (BKP_ReadBackupRegister(BKPDataReg[index]) != (FirstBackupData + (index * 0x5A)))
00182     {
00183       return (index + 1);
00184     }
00185   }
00186 
00187   return 0;  
00188 }
00189 
00190 /**
00191   * @brief  Checks if the Backup DRx registers are reset or not.
00192   * @param  None
00193   * @retval 
00194   *          - 0: All Backup DRx registers are reset
00195   *          - Value different from 0: Number of the first Backup register
00196   *            not reset
00197   */
00198 uint32_t IsBackupRegReset(void)
00199 {
00200   uint32_t index = 0;
00201 
00202   for (index = 0; index < BKP_DR_NUMBER; index++)
00203   {
00204     if (BKP_ReadBackupRegister(BKPDataReg[index]) != 0x0000)
00205     {
00206       return (index + 1);
00207     }
00208   }
00209 
00210   return 0;  
00211 }
00212 
00213 #ifdef  USE_FULL_ASSERT
00214 
00215 /**
00216   * @brief  Reports the name of the source file and the source line number
00217   *         where the assert_param error has occurred.
00218   * @param  file: pointer to the source file name
00219   * @param  line: assert_param error line source number
00220   * @retval None
00221   */
00222 void assert_failed(uint8_t* file, uint32_t line)
00223 { 
00224   /* User can add his own implementation to report the file name and line number,
00225      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00226 
00227   /* Infinite loop */
00228   while (1)
00229   {
00230   }
00231 }
00232 #endif
00233 
00234 /**
00235   * @}
00236   */ 
00237 
00238 /**
00239   * @}
00240   */ 
00241 
00242 /******************* (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