STM32F10x_StdPeriph_Examples/FLASH/Dual_Boot/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    FLASH/Dual_Boot/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 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup Dual_Boot
00030   * @{
00031   */
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00035 
00036 /* Private define ------------------------------------------------------------*/
00037 /* Uncomment one of the lines below to select which bank to boot from  */
00038 #if !defined(BOOT_FROM_BANK1) && !defined(BOOT_FROM_BANK2)
00039 //#define BOOT_FROM_BANK1           /* The program will be loaded on Flash Bank1 */
00040 //#define BOOT_FROM_BANK2           /* The program will be loaded on Flash Bank2 */
00041 #endif
00042 
00043 #if defined(BOOT_FROM_BANK1)
00044  #define MESSAGE4   " Running from Bank 1"
00045 #elif defined(BOOT_FROM_BANK2)
00046  #define MESSAGE4   " Running from Bank 2"
00047 #else
00048  #error "Select Boot from Bank1 or Bank2 using defines:BOOT_FROM_BANK1 or BOOT_FROM_BANK2!"
00049 #endif
00050 
00051 #define MESSAGE1   "  STM32 XL Density  "
00052 #define MESSAGE2   " Device running on  "
00053 #define MESSAGE3   "   STM3210E-EVAL    "
00054 
00055 #define MESSAGE5   " Joystick-DOWN: reset BFB2"
00056 #define MESSAGE6   "  bit to Boot from Bank2  "
00057 #define MESSAGE7   " Joystick-UP: set BFB2    "
00058 #define MESSAGE8   "  bit to Boot from Bank1  "
00059 
00060 #define MESSAGE9   " Joystick-SEL: program to "
00061 #define MESSAGE10  " 0x0 the base @ of Bank1/2"
00062 
00063 #define MESSAGE11   "  Operation Failed !"
00064 #define MESSAGE12   "Bank 1/2 base @ -> 0"
00065 
00066 #define BANK1_START_ADDRESS       0x08000000
00067 #define BANK2_START_ADDRESS       0x08080000
00068 
00069 /* Private macro -------------------------------------------------------------*/
00070 /* Private variables ---------------------------------------------------------*/
00071 USART_InitTypeDef USART_InitStructure;
00072 
00073 static __IO uint32_t TimingDelay;
00074 RCC_ClocksTypeDef RCC_Clocks;
00075 
00076 /* Private function prototypes -----------------------------------------------*/
00077 void Delay(__IO uint32_t nTime);
00078 
00079 /* Private functions ---------------------------------------------------------*/
00080 
00081 /**
00082   * @brief   Main program
00083   * @param  None
00084   * @retval None
00085   */
00086 int main(void)
00087 {
00088   /*!< At this stage the microcontroller clock setting is already configured,
00089        this is done through SystemInit() function which is called from startup
00090        file (startup_stm32f10x_xx.s) before to branch to application main.
00091        To reconfigure the default setting of SystemInit() function, refer to
00092        system_stm32f10x.c file
00093      */
00094 
00095   /* Set the vector table address */
00096 #if defined(BOOT_FROM_BANK1)
00097   /* Set the vector table to the Bank1 start address */
00098   NVIC_SetVectorTable(NVIC_VectTab_FLASH, BANK1_START_ADDRESS);
00099 #elif defined(BOOT_FROM_BANK2)
00100   /* Set the vector table to the Bank1 start address */
00101   NVIC_SetVectorTable(NVIC_VectTab_FLASH, BANK2_START_ADDRESS);
00102 #endif /* BOOT_FROM_BANK1 */
00103 
00104   /* Initialize LEDs, Buttons and LCD on STM3210E-EVAL board *****************/
00105   STM_EVAL_LEDInit(LED1);
00106   STM_EVAL_LEDInit(LED2);
00107   STM_EVAL_LEDInit(LED3);
00108   STM_EVAL_LEDInit(LED4);
00109 
00110   /* SysTick end of count event each 10ms */
00111   RCC_GetClocksFreq(&RCC_Clocks);
00112   SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
00113 
00114   /* Configure the Joystick buttons */
00115   STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO);
00116   STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_GPIO);
00117   STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO);
00118   /* Initialize the LCD */
00119   STM3210E_LCD_Init();
00120 
00121   /* Display message on STM3210E-EVAL LCD *************************************/
00122   /* Clear the LCD */
00123   LCD_Clear(LCD_COLOR_WHITE);
00124 
00125   /* Set the LCD Back Color */
00126 #if defined(BOOT_FROM_BANK1)
00127   LCD_SetBackColor(LCD_COLOR_BLUE);
00128 #elif defined(BOOT_FROM_BANK2)
00129   LCD_SetBackColor(LCD_COLOR_RED);
00130 #endif /* BOOT_FROM_BANK1 */
00131 
00132   /* Set the LCD Text Color */
00133   LCD_SetTextColor(LCD_COLOR_WHITE);
00134   LCD_DisplayStringLine(LCD_LINE_0, MESSAGE1);
00135   LCD_DisplayStringLine(LCD_LINE_1, MESSAGE2);
00136   LCD_DisplayStringLine(LCD_LINE_2, MESSAGE3);
00137   LCD_DisplayStringLine(LCD_LINE_4, MESSAGE4);
00138 
00139   LCD_SetFont(&Font12x12);
00140   LCD_DisplayStringLine(LCD_LINE_12, MESSAGE5);
00141   LCD_DisplayStringLine(LCD_LINE_13, MESSAGE6);
00142   LCD_DisplayStringLine(LCD_LINE_15, MESSAGE7);
00143   LCD_DisplayStringLine(LCD_LINE_16, MESSAGE8);
00144   LCD_DisplayStringLine(LCD_LINE_18, MESSAGE9);
00145   LCD_DisplayStringLine(LCD_LINE_19, MESSAGE10);
00146   LCD_SetFont(&Font16x24);
00147 
00148   /* Turn on leds available on STM3210E-EVAL **********************************/
00149   STM_EVAL_LEDOn(LED1);
00150   STM_EVAL_LEDOn(LED2);
00151   STM_EVAL_LEDOn(LED3);
00152   STM_EVAL_LEDOn(LED4);
00153 
00154   /* Infinite loop */
00155   while (1)
00156   {
00157     /*--- If Joystick DOWN button is pushed, reset BFB2 bit to enable boot from Bank2
00158           (active after next reset, w/ Boot pins set in Boot from Flash memory position ---*/
00159     if (STM_EVAL_PBGetState(BUTTON_DOWN) == 0)
00160     {
00161       /* Reset BFB2 bit to enable boot from Flash Bank2 */
00162       FLASH_Unlock();
00163       FLASH_EraseOptionBytes();
00164 
00165       if (FLASH_BootConfig(FLASH_BOOT_Bank2) == FLASH_COMPLETE)
00166       {
00167         /* Generate System Reset to load the new option byte values */
00168         NVIC_SystemReset();
00169       }
00170       else
00171       {
00172         /* Display information */
00173         LCD_DisplayStringLine(LCD_LINE_6, MESSAGE11);
00174       }
00175     }
00176 
00177     /*--- If Joystick UP button is pushed, set BFB2 bit to enable boot from Bank1
00178           (active after next reset, w/ Boot pins set in Boot from Flash memory position ---*/
00179     if (STM_EVAL_PBGetState(BUTTON_UP) == 0)
00180     {
00181       /* Set BFB2 bit to enable boot from Flash Bank2 */
00182       FLASH_Unlock();
00183       FLASH_EraseOptionBytes();
00184       if (FLASH_BootConfig(FLASH_BOOT_Bank1) == FLASH_COMPLETE)
00185       {
00186         /* Generate System Reset to load the new option byte values */
00187         NVIC_SystemReset();
00188       }
00189       else
00190       {
00191         /* Display information */
00192         LCD_DisplayStringLine(LCD_LINE_6, MESSAGE11);
00193       }
00194     }
00195 
00196     /*--- If Joystick UP button is pushed, program the content of address 0x08080000
00197           (base address of Bank2) and 0x08000000(base address of Bank1) to 0x00 --*/
00198     if (STM_EVAL_PBGetState(BUTTON_SEL) == 0)
00199     {
00200       FLASH_Unlock();
00201       /* Erase stack pointer value at Bank 2 start address */
00202       FLASH_ProgramWord(BANK2_START_ADDRESS, 0x00);
00203       /* Erase stack pointer value at Bank 1 start address */
00204       FLASH_ProgramWord(BANK1_START_ADDRESS, 0x00);
00205       FLASH_Lock();
00206 
00207       LCD_ClearLine(LCD_LINE_7);
00208       LCD_ClearLine(LCD_LINE_8);
00209       LCD_ClearLine(LCD_LINE_9);
00210 
00211       /* Check if erase operation is OK */
00212       if ((uint32_t)(*(uint32_t *)BANK2_START_ADDRESS) ==  0x00)
00213       {
00214         if ((uint32_t)(*(uint32_t *)BANK1_START_ADDRESS) !=  0x00)
00215         {
00216           /* Display information */
00217           LCD_DisplayStringLine(LCD_LINE_6, MESSAGE11);
00218         }
00219         else
00220         {
00221           /* Display information */
00222           LCD_DisplayStringLine(LCD_LINE_6, MESSAGE12);
00223         }
00224       }
00225       else
00226       {
00227         /* Display information */
00228         LCD_DisplayStringLine(LCD_LINE_6, MESSAGE11);
00229       }
00230     }
00231 
00232     /* Toggle LD3 */
00233     STM_EVAL_LEDToggle(LED3);
00234 
00235     /* Insert 50 ms delay */
00236     Delay(5);
00237 
00238     /* Toggle LD2 */
00239     STM_EVAL_LEDToggle(LED2);
00240 
00241     /* Insert 100 ms delay */
00242     Delay(10);
00243   }
00244 }
00245 
00246 /**
00247   * @brief  Inserts a delay time.
00248   * @param  nTime: specifies the delay time length, in 10 ms.
00249   * @retval None
00250   */
00251 void Delay(__IO uint32_t nTime)
00252 {
00253   TimingDelay = nTime;
00254 
00255   while (TimingDelay != 0);
00256 }
00257 
00258 /**
00259   * @brief  Decrements the TimingDelay variable.
00260   * @param  None
00261   * @retval None
00262   */
00263 void TimingDelay_Decrement(void)
00264 {
00265   if (TimingDelay != 0x00)
00266   {
00267     TimingDelay--;
00268   }
00269 }
00270 
00271 #ifdef  USE_FULL_ASSERT
00272 /**
00273   * @brief  Reports the name of the source file and the source line number
00274   *         where the assert_param error has occurred.
00275   * @param  file: pointer to the source file name
00276   * @param  line: assert_param error line source number
00277   * @retval None
00278   */
00279 void assert_failed(uint8_t* file, uint32_t line)
00280 {
00281   /* User can add his own implementation to report the file name and line number,
00282      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00283 
00284   while (1)
00285   {}
00286 }
00287 #endif
00288 
00289 /**
00290   * @}
00291   */
00292 
00293 /**
00294   * @}
00295   */
00296 
00297 /******************* (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