Mercurial > public > ostc4
annotate Small_CPU/Src/RTE_FlashAccess.c @ 842:c3dd461ca3f9 Evo_2_23
Migrated Sentinel protocol to new UART structure:
The Sentinel protocol had not been migrated to the new UART structure which was introduced with the introduction of the UART MUX. The Sentinel is now supported by autodetection again (development version only)
author | Ideenmodellierer |
---|---|
date | Mon, 15 Jan 2024 21:44:18 +0100 |
parents | 91a8f9893e68 |
children |
rev | line source |
---|---|
38 | 1 /** |
2 ****************************************************************************** | |
3 * @file RTE_FLashAccess.c based on BonexFlashAccess.c based on firmwareEraseProgram.v | |
4 * @author heinrichs weikamp gmbh | |
5 * @version V0.0.1 | |
6 * @date 20-July-2016 | |
7 * @version V0.0.1 | |
8 * @since 20-July-2016 | |
9 * @brief erase and program the STM32F4xx internal FLASH memory for compasss calib etc. | |
10 * based on firmwareEraseProgram.c from OSTC4 | |
11 * | |
12 @verbatim | |
13 ============================================================================== | |
14 ##### How to use ##### | |
15 ============================================================================== | |
16 | |
17 4 x 32 Byte with first block can not be 0xFFFFFFFF | |
18 | |
19 | |
20 | |
21 @endverbatim | |
22 ****************************************************************************** | |
23 * @attention | |
24 * | |
25 * <h2><center>© COPYRIGHT(c) 2015 heinrichs weikamp</center></h2> | |
26 * | |
27 ****************************************************************************** | |
28 */ | |
29 | |
30 /* Includes ------------------------------------------------------------------*/ | |
31 #include "RTE_FlashAccess.h" | |
32 #include "stdio.h" | |
33 | |
34 /** @addtogroup BONEXINFOSYSTEM | |
35 * @{ | |
36 */ | |
37 | |
38 /* Exported variables --------------------------------------------------------*/ | |
39 | |
40 /* Private types -------------------------------------------------------------*/ | |
41 /* taken from | |
42 * C:\Users\hw\STM32Cube\Repository\STM32Cube_FW_F3_V1.2.0\Projects\STM32F3-Discovery\Examples\FLASH\FLASH_EraseProgram | |
43 */ | |
44 | |
45 #define FLASH_SECTOR_SIZE_128KB (0x00020000) | |
46 #define FLASH_USER_START_ADDR (ADDR_FLASH_SECTOR_7) /* Start @ of user Flash area */ | |
47 #define FLASH_USER_END_ADDR (ADDR_FLASH_SECTOR_7 + FLASH_SECTOR_SIZE_128KB) /* End @ of user Flash area */ | |
48 | |
49 /* Base address of the Flash pages */ | |
50 | |
51 #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */ | |
52 #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */ | |
53 #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */ | |
54 #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */ | |
55 #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */ | |
56 #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */ | |
57 #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */ | |
58 #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */ | |
59 | |
60 | |
61 | |
62 | |
63 /* Private variables ---------------------------------------------------------*/ | |
64 | |
65 //static FLASH_EraseInitTypeDef EraseInitStruct; //Variable used for Erase procedure | |
66 | |
67 uint32_t Address = 0; | |
68 uint32_t PageError = 0; | |
69 __IO uint32_t data32 = 0 , MemoryProgramStatus = 0; | |
70 | |
71 /* Private function prototypes -----------------------------------------------*/ | |
72 //uint8_t BFA_eraseSectors(uint32_t SectorAddress, uint32_t NbSectors); | |
73 //uint8_t BFA_eraseSectorsAll(void); | |
74 uint8_t BFA_FindLastDataBlockAndSetAddress(void); | |
75 | |
76 /* Exported functions --------------------------------------------------------*/ | |
77 | |
571
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
78 uint8_t BFA_readLastDataBlock(tfull32 *dataArray4) |
38 | 79 { |
80 uint8_t answer; | |
81 | |
82 answer = BFA_FindLastDataBlockAndSetAddress(); | |
83 if(answer != BFA_OK) | |
84 return answer; | |
85 | |
571
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
86 dataArray4[0].Full32 = *(__IO uint32_t*)(Address + 0); |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
87 dataArray4[1].Full32 = *(__IO uint32_t*)(Address + 4); |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
88 dataArray4[2].Full32 = *(__IO uint32_t*)(Address + 8); |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
89 dataArray4[3].Full32 = *(__IO uint32_t*)(Address + 12); |
38 | 90 return BFA_OK; |
91 } | |
92 | |
93 | |
571
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
94 uint8_t BFA_writeDataBlock(const tfull32 *dataArray4) |
38 | 95 { |
96 uint8_t answer; | |
97 uint32_t dataTest[4]; | |
98 uint32_t StartAddress; | |
99 | |
100 answer = BFA_FindLastDataBlockAndSetAddress(); | |
101 Address = Address + 16; | |
102 | |
103 if((answer == BFA_EMPTY) || (Address >= FLASH_USER_END_ADDR) || (Address < FLASH_USER_START_ADDR)) | |
104 Address = FLASH_USER_START_ADDR; | |
105 | |
106 dataTest[0] = *(__IO uint32_t*)(Address + 0); | |
107 dataTest[1] = *(__IO uint32_t*)(Address + 4); | |
108 dataTest[2] = *(__IO uint32_t*)(Address + 8); | |
109 dataTest[3] = *(__IO uint32_t*)(Address + 12); | |
110 | |
111 for(int i=0;i<4;i++) | |
112 { | |
113 if(dataTest[i] != 0xFFFFFFFF) | |
114 { | |
115 return 0; | |
116 // answer = BFA_eraseSectorsAll(); | |
117 // break; | |
118 } | |
119 else | |
120 answer = BFA_OK; | |
121 } | |
122 | |
123 // can I write? | |
124 if(answer != BFA_OK) | |
125 return answer; | |
126 | |
127 StartAddress = Address; | |
128 HAL_FLASH_Unlock(); | |
129 for(int i=0;i<4;i++) | |
130 { | |
571
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
131 answer = HAL_FLASH_Program(TYPEPROGRAM_WORD, Address, dataArray4[i].Full32); |
38 | 132 Address = Address + 4; |
133 } | |
134 HAL_FLASH_Lock(); | |
135 Address = StartAddress; // back to start of this data set (for reading etc.) | |
136 return answer; | |
137 } | |
571
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
138 uint16_t BFA_calc_Block_Checksum(const tfull32 *dataArray4) |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
139 { |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
140 uint16_t checksum = 0; |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
141 uint8_t loop = 0; |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
142 uint16_t* p_data = (uint16_t*)dataArray4; |
38 | 143 |
571
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
144 for (loop = 0; loop < 6; loop++) // calc checksum across first 6 words */ |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
145 { |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
146 checksum += *p_data; |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
147 } |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
148 return checksum; |
91a8f9893e68
Reactivate compass parameter stored in NVM:
Ideenmodellierer
parents:
38
diff
changeset
|
149 } |
38 | 150 |
151 /* Private functions ---------------------------------------------------------*/ | |
152 /* | |
153 uint8_t BFA_eraseSectorsAll(void) | |
154 { | |
155 return BFA_eraseSectors(FLASH_USER_START_ADDR, (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR)/FLASH_SECTOR_SIZE_128KB); | |
156 } | |
157 | |
158 | |
159 uint8_t BFA_eraseSectors(uint32_t SectorAddress, uint32_t NbSectors) | |
160 { | |
161 if((NbSectors > 1) || (SectorAddress != FLASH_USER_START_ADDR)) | |
162 return 0; | |
163 | |
164 uint8_t answer; | |
165 uint32_t PageError = 0; | |
166 | |
167 HAL_FLASH_Unlock(); | |
168 | |
169 EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;//ERASE_SECTORS; | |
170 EraseInitStruct.Sector = SectorAddress; | |
171 EraseInitStruct.NbSectors = NbSectors; | |
172 | |
173 answer = HAL_FLASHEx_Erase(&EraseInitStruct, &PageError); | |
174 | |
175 HAL_FLASH_Lock(); | |
176 return answer; | |
177 } | |
178 */ | |
179 | |
180 uint8_t BFA_FindLastDataBlockAndSetAddress(void) | |
181 { | |
182 uint32_t StartAddress; | |
183 | |
184 // first part from here to the end | |
185 // there it should be, most likely at Address itself | |
186 if(Address == 0) | |
187 Address = FLASH_USER_END_ADDR - 16; | |
188 else | |
189 Address &= 0xFFFFFFF0; // align with 16Byte | |
190 | |
191 StartAddress = Address; | |
192 while (Address >= FLASH_USER_START_ADDR) | |
193 { | |
194 data32 = *(__IO uint32_t*)Address; | |
195 if(data32 != 0xFFFFFFFF) | |
196 { | |
197 return BFA_OK; | |
198 } | |
199 Address = Address - 16; | |
200 } | |
201 | |
202 // second part from the end to here | |
203 if(StartAddress == FLASH_USER_END_ADDR - 16) | |
204 return BFA_EMPTY; | |
205 | |
206 Address = FLASH_USER_END_ADDR - 16; | |
207 while (Address > StartAddress) | |
208 { | |
209 data32 = *(__IO uint32_t*)Address; | |
210 if(data32 != 0xFFFFFFFF) | |
211 { | |
212 return BFA_OK; | |
213 } | |
214 Address = Address - 16; | |
215 } | |
216 | |
217 // empty flash | |
218 return BFA_EMPTY; | |
219 } | |
220 /** | |
221 * @} | |
222 */ | |
223 | |
224 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ | |
225 | |
226 |