Mercurial > ~darius > hgwebdir.cgi > stm32temp
comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/FLASH/Dual_Boot/main.c @ 0:c59513fd84fb
Initial commit of STM32 test code.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 03 Oct 2011 21:19:15 +1030 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c59513fd84fb |
---|---|
1 /** | |
2 ****************************************************************************** | |
3 * @file FLASH/Dual_Boot/main.c | |
4 * @author MCD Application Team | |
5 * @version V3.5.0 | |
6 * @date 08-April-2011 | |
7 * @brief Main program body | |
8 ****************************************************************************** | |
9 * @attention | |
10 * | |
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS | |
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE | |
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY | |
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING | |
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE | |
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. | |
17 * | |
18 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> | |
19 ****************************************************************************** | |
20 */ | |
21 | |
22 /* Includes ------------------------------------------------------------------*/ | |
23 #include "main.h" | |
24 | |
25 | |
26 /** @addtogroup STM32F10x_StdPeriph_Examples | |
27 * @{ | |
28 */ | |
29 | |
30 /** @addtogroup Dual_Boot | |
31 * @{ | |
32 */ | |
33 | |
34 /* Private typedef -----------------------------------------------------------*/ | |
35 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; | |
36 | |
37 /* Private define ------------------------------------------------------------*/ | |
38 /* Uncomment one of the lines below to select which bank to boot from */ | |
39 #if !defined(BOOT_FROM_BANK1) && !defined(BOOT_FROM_BANK2) | |
40 //#define BOOT_FROM_BANK1 /* The program will be loaded on Flash Bank1 */ | |
41 //#define BOOT_FROM_BANK2 /* The program will be loaded on Flash Bank2 */ | |
42 #endif | |
43 | |
44 #if defined(BOOT_FROM_BANK1) | |
45 #define MESSAGE4 " Running from Bank 1" | |
46 #elif defined(BOOT_FROM_BANK2) | |
47 #define MESSAGE4 " Running from Bank 2" | |
48 #else | |
49 #error "Select Boot from Bank1 or Bank2 using defines:BOOT_FROM_BANK1 or BOOT_FROM_BANK2!" | |
50 #endif | |
51 | |
52 #define MESSAGE1 " STM32 XL Density " | |
53 #define MESSAGE2 " Device running on " | |
54 #define MESSAGE3 " STM3210E-EVAL " | |
55 | |
56 #define MESSAGE5 " Joystick-DOWN: reset BFB2" | |
57 #define MESSAGE6 " bit to Boot from Bank2 " | |
58 #define MESSAGE7 " Joystick-UP: set BFB2 " | |
59 #define MESSAGE8 " bit to Boot from Bank1 " | |
60 | |
61 #define MESSAGE9 " Joystick-SEL: program to " | |
62 #define MESSAGE10 " 0x0 the base @ of Bank1/2" | |
63 | |
64 #define MESSAGE11 " Operation Failed !" | |
65 #define MESSAGE12 "Bank 1/2 base @ -> 0" | |
66 | |
67 #define BANK1_START_ADDRESS 0x08000000 | |
68 #define BANK2_START_ADDRESS 0x08080000 | |
69 | |
70 /* Private macro -------------------------------------------------------------*/ | |
71 /* Private variables ---------------------------------------------------------*/ | |
72 USART_InitTypeDef USART_InitStructure; | |
73 | |
74 static __IO uint32_t TimingDelay; | |
75 RCC_ClocksTypeDef RCC_Clocks; | |
76 | |
77 /* Private function prototypes -----------------------------------------------*/ | |
78 void Delay(__IO uint32_t nTime); | |
79 | |
80 /* Private functions ---------------------------------------------------------*/ | |
81 | |
82 /** | |
83 * @brief Main program | |
84 * @param None | |
85 * @retval None | |
86 */ | |
87 int main(void) | |
88 { | |
89 /*!< At this stage the microcontroller clock setting is already configured, | |
90 this is done through SystemInit() function which is called from startup | |
91 file (startup_stm32f10x_xx.s) before to branch to application main. | |
92 To reconfigure the default setting of SystemInit() function, refer to | |
93 system_stm32f10x.c file | |
94 */ | |
95 | |
96 /* Set the vector table address */ | |
97 #if defined(BOOT_FROM_BANK1) | |
98 /* Set the vector table to the Bank1 start address */ | |
99 NVIC_SetVectorTable(NVIC_VectTab_FLASH, BANK1_START_ADDRESS); | |
100 #elif defined(BOOT_FROM_BANK2) | |
101 /* Set the vector table to the Bank1 start address */ | |
102 NVIC_SetVectorTable(NVIC_VectTab_FLASH, BANK2_START_ADDRESS); | |
103 #endif /* BOOT_FROM_BANK1 */ | |
104 | |
105 /* Initialize LEDs, Buttons and LCD on STM3210E-EVAL board *****************/ | |
106 STM_EVAL_LEDInit(LED1); | |
107 STM_EVAL_LEDInit(LED2); | |
108 STM_EVAL_LEDInit(LED3); | |
109 STM_EVAL_LEDInit(LED4); | |
110 | |
111 /* SysTick end of count event each 10ms */ | |
112 RCC_GetClocksFreq(&RCC_Clocks); | |
113 SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); | |
114 | |
115 /* Configure the Joystick buttons */ | |
116 STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO); | |
117 STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_GPIO); | |
118 STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO); | |
119 /* Initialize the LCD */ | |
120 STM3210E_LCD_Init(); | |
121 | |
122 /* Display message on STM3210E-EVAL LCD *************************************/ | |
123 /* Clear the LCD */ | |
124 LCD_Clear(LCD_COLOR_WHITE); | |
125 | |
126 /* Set the LCD Back Color */ | |
127 #if defined(BOOT_FROM_BANK1) | |
128 LCD_SetBackColor(LCD_COLOR_BLUE); | |
129 #elif defined(BOOT_FROM_BANK2) | |
130 LCD_SetBackColor(LCD_COLOR_RED); | |
131 #endif /* BOOT_FROM_BANK1 */ | |
132 | |
133 /* Set the LCD Text Color */ | |
134 LCD_SetTextColor(LCD_COLOR_WHITE); | |
135 LCD_DisplayStringLine(LCD_LINE_0, MESSAGE1); | |
136 LCD_DisplayStringLine(LCD_LINE_1, MESSAGE2); | |
137 LCD_DisplayStringLine(LCD_LINE_2, MESSAGE3); | |
138 LCD_DisplayStringLine(LCD_LINE_4, MESSAGE4); | |
139 | |
140 LCD_SetFont(&Font12x12); | |
141 LCD_DisplayStringLine(LCD_LINE_12, MESSAGE5); | |
142 LCD_DisplayStringLine(LCD_LINE_13, MESSAGE6); | |
143 LCD_DisplayStringLine(LCD_LINE_15, MESSAGE7); | |
144 LCD_DisplayStringLine(LCD_LINE_16, MESSAGE8); | |
145 LCD_DisplayStringLine(LCD_LINE_18, MESSAGE9); | |
146 LCD_DisplayStringLine(LCD_LINE_19, MESSAGE10); | |
147 LCD_SetFont(&Font16x24); | |
148 | |
149 /* Turn on leds available on STM3210E-EVAL **********************************/ | |
150 STM_EVAL_LEDOn(LED1); | |
151 STM_EVAL_LEDOn(LED2); | |
152 STM_EVAL_LEDOn(LED3); | |
153 STM_EVAL_LEDOn(LED4); | |
154 | |
155 /* Infinite loop */ | |
156 while (1) | |
157 { | |
158 /*--- If Joystick DOWN button is pushed, reset BFB2 bit to enable boot from Bank2 | |
159 (active after next reset, w/ Boot pins set in Boot from Flash memory position ---*/ | |
160 if (STM_EVAL_PBGetState(BUTTON_DOWN) == 0) | |
161 { | |
162 /* Reset BFB2 bit to enable boot from Flash Bank2 */ | |
163 FLASH_Unlock(); | |
164 FLASH_EraseOptionBytes(); | |
165 | |
166 if (FLASH_BootConfig(FLASH_BOOT_Bank2) == FLASH_COMPLETE) | |
167 { | |
168 /* Generate System Reset to load the new option byte values */ | |
169 NVIC_SystemReset(); | |
170 } | |
171 else | |
172 { | |
173 /* Display information */ | |
174 LCD_DisplayStringLine(LCD_LINE_6, MESSAGE11); | |
175 } | |
176 } | |
177 | |
178 /*--- If Joystick UP button is pushed, set BFB2 bit to enable boot from Bank1 | |
179 (active after next reset, w/ Boot pins set in Boot from Flash memory position ---*/ | |
180 if (STM_EVAL_PBGetState(BUTTON_UP) == 0) | |
181 { | |
182 /* Set BFB2 bit to enable boot from Flash Bank2 */ | |
183 FLASH_Unlock(); | |
184 FLASH_EraseOptionBytes(); | |
185 if (FLASH_BootConfig(FLASH_BOOT_Bank1) == FLASH_COMPLETE) | |
186 { | |
187 /* Generate System Reset to load the new option byte values */ | |
188 NVIC_SystemReset(); | |
189 } | |
190 else | |
191 { | |
192 /* Display information */ | |
193 LCD_DisplayStringLine(LCD_LINE_6, MESSAGE11); | |
194 } | |
195 } | |
196 | |
197 /*--- If Joystick UP button is pushed, program the content of address 0x08080000 | |
198 (base address of Bank2) and 0x08000000(base address of Bank1) to 0x00 --*/ | |
199 if (STM_EVAL_PBGetState(BUTTON_SEL) == 0) | |
200 { | |
201 FLASH_Unlock(); | |
202 /* Erase stack pointer value at Bank 2 start address */ | |
203 FLASH_ProgramWord(BANK2_START_ADDRESS, 0x00); | |
204 /* Erase stack pointer value at Bank 1 start address */ | |
205 FLASH_ProgramWord(BANK1_START_ADDRESS, 0x00); | |
206 FLASH_Lock(); | |
207 | |
208 LCD_ClearLine(LCD_LINE_7); | |
209 LCD_ClearLine(LCD_LINE_8); | |
210 LCD_ClearLine(LCD_LINE_9); | |
211 | |
212 /* Check if erase operation is OK */ | |
213 if ((uint32_t)(*(uint32_t *)BANK2_START_ADDRESS) == 0x00) | |
214 { | |
215 if ((uint32_t)(*(uint32_t *)BANK1_START_ADDRESS) != 0x00) | |
216 { | |
217 /* Display information */ | |
218 LCD_DisplayStringLine(LCD_LINE_6, MESSAGE11); | |
219 } | |
220 else | |
221 { | |
222 /* Display information */ | |
223 LCD_DisplayStringLine(LCD_LINE_6, MESSAGE12); | |
224 } | |
225 } | |
226 else | |
227 { | |
228 /* Display information */ | |
229 LCD_DisplayStringLine(LCD_LINE_6, MESSAGE11); | |
230 } | |
231 } | |
232 | |
233 /* Toggle LD3 */ | |
234 STM_EVAL_LEDToggle(LED3); | |
235 | |
236 /* Insert 50 ms delay */ | |
237 Delay(5); | |
238 | |
239 /* Toggle LD2 */ | |
240 STM_EVAL_LEDToggle(LED2); | |
241 | |
242 /* Insert 100 ms delay */ | |
243 Delay(10); | |
244 } | |
245 } | |
246 | |
247 /** | |
248 * @brief Inserts a delay time. | |
249 * @param nTime: specifies the delay time length, in 10 ms. | |
250 * @retval None | |
251 */ | |
252 void Delay(__IO uint32_t nTime) | |
253 { | |
254 TimingDelay = nTime; | |
255 | |
256 while (TimingDelay != 0); | |
257 } | |
258 | |
259 /** | |
260 * @brief Decrements the TimingDelay variable. | |
261 * @param None | |
262 * @retval None | |
263 */ | |
264 void TimingDelay_Decrement(void) | |
265 { | |
266 if (TimingDelay != 0x00) | |
267 { | |
268 TimingDelay--; | |
269 } | |
270 } | |
271 | |
272 #ifdef USE_FULL_ASSERT | |
273 | |
274 /** | |
275 * @brief Reports the name of the source file and the source line number | |
276 * where the assert_param error has occurred. | |
277 * @param file: pointer to the source file name | |
278 * @param line: assert_param error line source number | |
279 * @retval None | |
280 */ | |
281 void assert_failed(uint8_t* file, uint32_t line) | |
282 { | |
283 /* User can add his own implementation to report the file name and line number, | |
284 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ | |
285 | |
286 while (1) | |
287 {} | |
288 } | |
289 | |
290 #endif | |
291 | |
292 /** | |
293 * @} | |
294 */ | |
295 | |
296 /** | |
297 * @} | |
298 */ | |
299 | |
300 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ |