Mercurial > ~darius > hgwebdir.cgi > stm32temp
comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/FLASH/Write_Protection/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/Write_Protection/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 "stm32f10x.h" | |
24 | |
25 /** @addtogroup STM32F10x_StdPeriph_Examples | |
26 * @{ | |
27 */ | |
28 | |
29 /** @addtogroup FLASH_Write_Protection | |
30 * @{ | |
31 */ | |
32 | |
33 /* Private typedef -----------------------------------------------------------*/ | |
34 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; | |
35 | |
36 /* Private define ------------------------------------------------------------*/ | |
37 /* Define the STM32F10x FLASH Page Size depending on the used device */ | |
38 #if defined (STM32F10X_HD) || defined (STM32F10X_HD_VL) || (STM32F10X_CL) || defined (STM32F10X_XL) | |
39 #define FLASH_PAGE_SIZE ((uint16_t)0x800) | |
40 #define FLASH_PAGES_TO_BE_PROTECTED (FLASH_WRProt_Pages12to13 | FLASH_WRProt_Pages14to15) | |
41 #else | |
42 #define FLASH_PAGE_SIZE ((uint16_t)0x400) | |
43 #define FLASH_PAGES_TO_BE_PROTECTED (FLASH_WRProt_Pages24to27 | FLASH_WRProt_Pages28to31) | |
44 #endif | |
45 | |
46 #define BANK1_WRITE_START_ADDR ((uint32_t)0x08006000) | |
47 #define BANK1_WRITE_END_ADDR ((uint32_t)0x08008000) | |
48 | |
49 /* Uncomment this line to program the Falsh pages */ | |
50 //#define FLASH_PAGE_PROGRAM | |
51 /* Uncomment this line to Enable Write Protection */ | |
52 //#define WRITE_PROTECTION_ENABLE | |
53 /* Uncomment this line to Disable Write Protection */ | |
54 #define WRITE_PROTECTION_DISABLE | |
55 | |
56 /* Private macro -------------------------------------------------------------*/ | |
57 /* Private variables ---------------------------------------------------------*/ | |
58 uint32_t EraseCounter = 0x0, Address = 0x0; | |
59 uint16_t Data = 0x1753; | |
60 uint32_t WRPR_Value = 0xFFFFFFFF, ProtectedPages = 0x0; | |
61 uint32_t NbrOfPage; | |
62 volatile FLASH_Status FLASHStatus = FLASH_COMPLETE; | |
63 volatile TestStatus MemoryProgramStatus = PASSED; | |
64 | |
65 /* Private function prototypes -----------------------------------------------*/ | |
66 /* Private functions ---------------------------------------------------------*/ | |
67 | |
68 /** | |
69 * @brief Main program | |
70 * @param None | |
71 * @retval None | |
72 */ | |
73 int main(void) | |
74 { | |
75 /*!< At this stage the microcontroller clock setting is already configured, | |
76 this is done through SystemInit() function which is called from startup | |
77 file (startup_stm32f10x_xx.s) before to branch to application main. | |
78 To reconfigure the default setting of SystemInit() function, refer to | |
79 system_stm32f10x.c file | |
80 */ | |
81 | |
82 /* Unlock the Flash Program Erase controller */ | |
83 FLASH_Unlock(); | |
84 | |
85 /* Get pages write protection status */ | |
86 WRPR_Value = FLASH_GetWriteProtectionOptionByte(); | |
87 | |
88 #ifdef WRITE_PROTECTION_DISABLE | |
89 | |
90 /* Get pages already write protected */ | |
91 ProtectedPages = ~(WRPR_Value | FLASH_PAGES_TO_BE_PROTECTED); | |
92 | |
93 /* Check if desired pages are already write protected */ | |
94 if((WRPR_Value | (~FLASH_PAGES_TO_BE_PROTECTED)) != 0xFFFFFFFF ) | |
95 { | |
96 /* Erase all the option Bytes */ | |
97 FLASHStatus = FLASH_EraseOptionBytes(); | |
98 | |
99 /* Check if there is write protected pages */ | |
100 if(ProtectedPages != 0x0) | |
101 { | |
102 /* Restore write protected pages */ | |
103 FLASHStatus = FLASH_EnableWriteProtection(ProtectedPages); | |
104 } | |
105 /* Generate System Reset to load the new option byte values */ | |
106 NVIC_SystemReset(); | |
107 } | |
108 | |
109 #elif defined WRITE_PROTECTION_ENABLE | |
110 | |
111 /* Get current write protected pages and the new pages to be protected */ | |
112 ProtectedPages = (~WRPR_Value) | FLASH_PAGES_TO_BE_PROTECTED; | |
113 | |
114 /* Check if desired pages are not yet write protected */ | |
115 if(((~WRPR_Value) & FLASH_PAGES_TO_BE_PROTECTED )!= FLASH_PAGES_TO_BE_PROTECTED) | |
116 { | |
117 | |
118 /* Erase all the option Bytes because if a program operation is | |
119 performed on a protected page, the Flash memory returns a | |
120 protection error */ | |
121 FLASHStatus = FLASH_EraseOptionBytes(); | |
122 | |
123 /* Enable the pages write protection */ | |
124 FLASHStatus = FLASH_EnableWriteProtection(ProtectedPages); | |
125 | |
126 /* Generate System Reset to load the new option byte values */ | |
127 NVIC_SystemReset(); | |
128 } | |
129 | |
130 #endif | |
131 | |
132 #ifdef FLASH_PAGE_PROGRAM | |
133 /* Get the number of pages to be erased */ | |
134 NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE; | |
135 | |
136 /* The selected pages are not write protected */ | |
137 if ( (WRPR_Value & FLASH_PAGES_TO_BE_PROTECTED) != 0x00) | |
138 { | |
139 /* Clear All pending flags */ | |
140 FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR); | |
141 | |
142 /* erase the FLASH pages */ | |
143 for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) | |
144 { | |
145 FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)); | |
146 } | |
147 | |
148 /* FLASH Half Word program of data 0x1753 at addresses defined by BANK1_WRITE_START_ADDR and BANK1_WRITE_END_ADDR */ | |
149 Address = BANK1_WRITE_START_ADDR; | |
150 | |
151 while((Address < BANK1_WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE)) | |
152 { | |
153 FLASHStatus = FLASH_ProgramHalfWord(Address, Data); | |
154 Address = Address + 2; | |
155 } | |
156 | |
157 /* Check the correctness of written data */ | |
158 Address = BANK1_WRITE_START_ADDR; | |
159 | |
160 while((Address < BANK1_WRITE_END_ADDR) && (MemoryProgramStatus != FAILED)) | |
161 { | |
162 if((*(__IO uint16_t*) Address) != Data) | |
163 { | |
164 MemoryProgramStatus = FAILED; | |
165 } | |
166 Address += 2; | |
167 } | |
168 } | |
169 else | |
170 { | |
171 /* Error to program the flash : The desired pages are write protected */ | |
172 MemoryProgramStatus = FAILED; | |
173 } | |
174 | |
175 #endif | |
176 | |
177 while (1) | |
178 { | |
179 } | |
180 } | |
181 | |
182 #ifdef USE_FULL_ASSERT | |
183 | |
184 /** | |
185 * @brief Reports the name of the source file and the source line number | |
186 * where the assert_param error has occurred. | |
187 * @param file: pointer to the source file name | |
188 * @param line: assert_param error line source number | |
189 * @retval None | |
190 */ | |
191 void assert_failed(uint8_t* file, uint32_t line) | |
192 { | |
193 /* User can add his own implementation to report the file name and line number, | |
194 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ | |
195 | |
196 while (1) | |
197 { | |
198 } | |
199 } | |
200 | |
201 #endif | |
202 | |
203 /** | |
204 * @} | |
205 */ | |
206 | |
207 /** | |
208 * @} | |
209 */ | |
210 | |
211 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ |