-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLoader_Src.c
333 lines (260 loc) · 8.66 KB
/
Loader_Src.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*********************************************
* @file Dev_Inf.c
* @brief modified by Mauro linking W25Qxxx library
* @date: 02 april 2024
* @version V.2.0.0
*
*********************************************
* Creating an new External loader you have to
* follow the below step 1 to 3 and set the
* first field in the struct of Dev_Inf.c
*********************************************/
#include "main.h"
#include "crc.h"
#include "spi.h"
#include "gpio.h"
#include "z_flash_W25QXXX.h"
/* PLEASE NOTE: the below EXT_FLASH_ADDR_MASK is used to "convert" the "uC mapped address"
* received to the "flash memory address" to read/write. That mask is used,
* in below functions, in an "and" filter supposing the flash is mapped on 0x90000000.
* In case you are using a different address you could need to change this filter
* or even use another way to "extract" internal flash address from the uC memory address.
*/
#define EXT_FLASH_ADDR_MASK 0x0FFFFFFFUL
#define EXT_FLASH_ROUNDING_ADDR_MASK 0xFFFFFFFCUL //address is rounded to values multiple of 4
#define LED_VERIFY 1
#define LED_CHKSUM 2
#define LED_SECTERAS 4
#define LED_FULLERAS 8
#define LED_WRITE 16
#define LED_READ 32
#define LED_INIT 64
#define LED_ANY 127
/* !!! EXTERNAL LOADER CONFIGURAZION !!!!!!!! */
/* STEP 1 *************************************
* remove comment in below "#define IS_LED" if you have
* a led for the External Loader.
* On CubeMX, led's GPIO pin label must be LED
* ********************************************/
#define IS_LED
#define LED_LEVEL_SHOWN ((uint8_t) (LED_ANY)) //set here functions that have to blink e.g. ((uint8_t) (LED_READ+LED_WRITE))
/* STEP 2 *************************************
* change the below #define assigning Pin name
* and port you used for led,
* specify also pin level to turn on led
* ********************************************/
#ifdef IS_LED
#define LED_PIN_ON GPIO_PIN_SET //this is the GPIO level turning on led
#endif //IS_LED
/* STEP 3 *************************************
* change the init function to call as per SPI
* port used (hint: that's the function
* available in spi.c, check it)
* ********************************************/
void LOC_SPI_Init(){
MX_SPI1_Init(); // !!! this line needs to be aligned to the SPI port used! Check this function: it is defined in spi.c !!!
}
/* END OF EXTERNAL LOADER CONFIGURAZION !!!!! */
extern void SystemClock_Config(void);
void LOC_LedOn(uint8_t level){
#ifdef IS_LED
if (level & LED_LEVEL_SHOWN)
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, LED_PIN_ON);
#endif //IS_LED
}
void LOC_LedOff(uint8_t level){
#ifdef IS_LED
if (level & LED_LEVEL_SHOWN)
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, !LED_PIN_ON);
#endif //IS_LED
}
int Init (void){
uint8_t result=3;
uint8_t k;
// *(uint32_t*)0xE000EDF0 = 0xA05F0000; //enable interrupts in debug
SystemInit();
/* ADAPTATION TO THE DEVICE
*
* change VTOR setting for H7 device
* SCB->VTOR = 0x24000000 | 0x200;
*
* change VTOR setting for other devices
* SCB->VTOR = 0x20000000 | 0x200;
*
*/
SCB->VTOR = 0x20000000 | 0x200;
HAL_DeInit();
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
LOC_SPI_Init();
MX_CRC_Init();
__set_PRIMASK(0); //enable interrupts
LOC_LedOn(LED_INIT);
for (k=0;((k<10) && (result));k++) { // retry 10 times init if retirning error (result!=0)
Flash_Init(&result);
}
__HAL_CRC_DR_RESET(&hcrc); //reset the CRC counter
LOC_LedOff(LED_INIT);
__set_PRIMASK(1); //disable interrupts
return (!result); //return 1 (OK) if no errors (result==0)
}
/**
* Description :
* Read data from the device
* Inputs :
* Address : Write location
* Size : Length in bytes
* buffer : Address where to get the data to write
* outputs :
* R0 : "1" : Operation succeeded
* "0" : Operation failure
* Note: Mandatory for all types except SRAM and PSRAM
*/
int Read (uint32_t Address, uint32_t Size, uint8_t* buffer){
__set_PRIMASK(0); //enable interrupts
Address = Address & EXT_FLASH_ADDR_MASK;
LOC_LedOn(LED_READ);
Flash_Read(Address, buffer, Size);
LOC_LedOff(LED_READ);
__set_PRIMASK(1); //disable interrupts
return 1;
}
/**
* Description :
* Write data to the device
* Inputs :
* Address : Write location
* Size : Length in bytes
* buffer : Address where to get the data to write
* outputs :
* R0 : "1" : Operation succeeded
* "0" : Operation failure
* Note: Mandatory for all types except SRAM and PSRAM
*/
int Write (uint32_t Address, uint32_t Size, uint8_t* buffer){
__set_PRIMASK(0); //enable interrupts
Address = Address & EXT_FLASH_ADDR_MASK;
LOC_LedOn(LED_WRITE);
Flash_Write(Address, buffer, Size);
LOC_LedOff(LED_WRITE);
__set_PRIMASK(1); //disable interrupts
return 1;
}
/**
* Description :
* Erase the full chip
* Inputs :
* None
* outputs :
* R0 : "1" : Operation succeeded
* "0" : Operation failure
* Note: Not Mandatory for SRAM PSRAM and NOR_FLASH
*/
int MassErase (void){
__set_PRIMASK(0); //enable interrupts
LOC_LedOn(LED_FULLERAS);
Flash_ChipErase();
LOC_LedOff(LED_FULLERAS);
__set_PRIMASK(1); //disable interrupts
return 1;
}
/**
* Description :
* Erase a full sector in the device
* Inputs :
* SectrorAddress : Start of sector
* Size : Size (in WORD)
* InitVal : Initial CRC value
* outputs :
* R0 : "1" : Operation succeeded
* "0" : Operation failure
* Note: Not Mandatory for SRAM PSRAM and NOR_FLASH
*/
int SectorErase (uint32_t EraseStartAddress ,uint32_t EraseEndAddress) {
EraseStartAddress = EraseStartAddress & EXT_FLASH_ADDR_MASK;
EraseEndAddress = EraseEndAddress & EXT_FLASH_ADDR_MASK;
EraseStartAddress = (EraseStartAddress - (EraseStartAddress % EXT_FLASH_SECTOR_SIZE));
while (EraseEndAddress>=EraseStartAddress) {
__set_PRIMASK(0); //enable interrupts
LOC_LedOn(LED_SECTERAS);
Flash_SErase4k(EraseStartAddress);
LOC_LedOff(LED_SECTERAS);
EraseStartAddress += EXT_FLASH_SECTOR_SIZE;
__set_PRIMASK(1); //disable interrupts
}
return 1;
}
/**
* Description :
* Calculates checksum value of the memory zone
* Inputs :
* StartAddress : Flash start address
* Size : Size (in WORD)
* InitVal : Initial CRC value
* outputs :
* R0 : Checksum value
* Note - Optional for all types of device
* NOTE - keeping original ST algorithm: not verified and optimized
*/
uint32_t CheckSum(uint32_t StartAddress, uint32_t Size, uint32_t InitVal) {
uint32_t Val;
StartAddress = StartAddress & EXT_FLASH_ADDR_MASK;
uint32_t EndAddress = StartAddress+Size;
StartAddress = StartAddress & EXT_FLASH_ROUNDING_ADDR_MASK;
__set_PRIMASK(0); //enable interrupts
LOC_LedOn(LED_CHKSUM);
for( StartAddress=StartAddress ; StartAddress<EndAddress; StartAddress+=4) {
Flash_WaitForWritingComplete();
Flash_Read(StartAddress,(uint8_t *) &Val, 4);
Val=__REV(Val);
InitVal=HAL_CRC_Accumulate(&hcrc, &Val, 1);
}
LOC_LedOff(LED_CHKSUM);
__set_PRIMASK(1); //disable interrupts
return (InitVal);
}
/**
* Description :
* Verify flash memory with RAM buffer and calculates checksum value of
* the programmed memory
* Inputs :
* FlashAddr : Flash address
* RAMBufferAddr : RAM buffer address
* Size : Size (in WORD)
* InitVal : Initial CRC value
* outputs :
* R0 : Operation failed (address of failure)
* R1 : Checksum value
* Note: Optional for all types of device
* NOTE - not verified!
**/
/*
uint64_t Verify (uint32_t MemoryAddr, uint32_t RAMBufferAddr, uint32_t Size, uint32_t missalignement){
#define BUF_SIZE 16
uint32_t InitVal = 0;
uint32_t VerifiedData = 0;
uint64_t checksum;
Size*=4;
uint8_t Buffer[BUF_SIZE];
uint32_t LocAddr = MemoryAddr & EXT_FLASH_ADDR_MASK;
uint32_t posBuf;
checksum = CheckSum((uint32_t)LocAddr + (missalignement & 0xf), Size - ((missalignement >> 16) & 0xF), InitVal);
while (Size>VerifiedData)
{
__set_PRIMASK(0); //enable interrupts
LOC_LedOn(LED_VERIFY);
Flash_Read(MemoryAddr+VerifiedData, Buffer, BUF_SIZE);
LOC_LedOff(LED_VERIFY);
__set_PRIMASK(1); //disable interrupts
posBuf=0;
while ((Size>VerifiedData) && (posBuf<BUF_SIZE)) {
if (Buffer[posBuf] != *((uint8_t*)RAMBufferAddr+VerifiedData))
return ((checksum<<32) + ((uint64_t)MemoryAddr+VerifiedData));
posBuf++;
VerifiedData++;
}
}
return (checksum<<32);
}
*/