yincheng.zhong
2025-02-18 51393af87f6dc09a00e78d71dbff8a15721c1198
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
#include "Flash.h"
#include "HIDO_TypeDef.h"
 
 
/* ¸÷¸öÉÈÇøÊ×µØÖ· */
#define ADDR_FLASH_SECTOR_0     ((HIDO_UINT32)0x08000000) /* Base @ of Sector 0, 16 Kbyte */
#define ADDR_FLASH_SECTOR_1     ((HIDO_UINT32)0x08004000) /* Base @ of Sector 1, 16 Kbyte */
#define ADDR_FLASH_SECTOR_2     ((HIDO_UINT32)0x08008000) /* Base @ of Sector 2, 16 Kbyte */
#define ADDR_FLASH_SECTOR_3     ((HIDO_UINT32)0x0800C000) /* Base @ of Sector 3, 16 Kbyte */
#define ADDR_FLASH_SECTOR_4     ((HIDO_UINT32)0x08010000) /* Base @ of Sector 4, 64 Kbyte */
#define ADDR_FLASH_SECTOR_5     ((HIDO_UINT32)0x08020000) /* Base @ of Sector 5, 128 Kbyte */
#define ADDR_FLASH_SECTOR_6     ((HIDO_UINT32)0x08040000) /* Base @ of Sector 6, 128 Kbyte */
#define ADDR_FLASH_SECTOR_7     ((HIDO_UINT32)0x08060000) /* Base @ of Sector 7, 128 Kbyte */
#define ADDR_FLASH_SECTOR_8     ((HIDO_UINT32)0x08080000) /* Base @ of Sector 8, 128 Kbyte */
#define ADDR_FLASH_SECTOR_9     ((HIDO_UINT32)0x080A0000) /* Base @ of Sector 9, 128 Kbyte */
#define ADDR_FLASH_SECTOR_10    ((HIDO_UINT32)0x080C0000) /* Base @ of Sector 10, 128 Kbyte */
#define ADDR_FLASH_SECTOR_11    ((HIDO_UINT32)0x080E0000) /* Base @ of Sector 11, 128 Kbyte */
 
 
static HIDO_UINT32 MCUFlash_GetSector(HIDO_UINT32 _u32Address)
{
    HIDO_UINT32 u32Sector = 0;
 
    if ((_u32Address < ADDR_FLASH_SECTOR_1) && (_u32Address >= ADDR_FLASH_SECTOR_0))
    {
        u32Sector = FLASH_SECTOR_0;
    }
    else if ((_u32Address < ADDR_FLASH_SECTOR_2) && (_u32Address >= ADDR_FLASH_SECTOR_1))
    {
        u32Sector = FLASH_SECTOR_1;
    }
    else if ((_u32Address < ADDR_FLASH_SECTOR_3) && (_u32Address >= ADDR_FLASH_SECTOR_2))
    {
        u32Sector = FLASH_SECTOR_2;
    }
    else if ((_u32Address < ADDR_FLASH_SECTOR_4) && (_u32Address >= ADDR_FLASH_SECTOR_3))
    {
        u32Sector = FLASH_SECTOR_3;
    }
    else if ((_u32Address < ADDR_FLASH_SECTOR_5) && (_u32Address >= ADDR_FLASH_SECTOR_4))
    {
        u32Sector = FLASH_SECTOR_4;
    }
    else if ((_u32Address < ADDR_FLASH_SECTOR_6) && (_u32Address >= ADDR_FLASH_SECTOR_5))
    {
        u32Sector = FLASH_SECTOR_5;
    }
    
    return u32Sector;
}
 
static HIDO_UINT32 MCUFlash_GetSectorSize(HIDO_UINT32 _u32Address)
{
    HIDO_UINT32 u32Sector = 0;
 
    if ((_u32Address < ADDR_FLASH_SECTOR_1) && (_u32Address >= ADDR_FLASH_SECTOR_0))
    {
        u32Sector = ADDR_FLASH_SECTOR_1 - ADDR_FLASH_SECTOR_0;
    }
    else if ((_u32Address < ADDR_FLASH_SECTOR_2) && (_u32Address >= ADDR_FLASH_SECTOR_1))
    {
        u32Sector = FLASH_SECTOR_1;
    }
    else if ((_u32Address < ADDR_FLASH_SECTOR_3) && (_u32Address >= ADDR_FLASH_SECTOR_2))
    {
        u32Sector = FLASH_SECTOR_2;
    }
    else if ((_u32Address < ADDR_FLASH_SECTOR_4) && (_u32Address >= ADDR_FLASH_SECTOR_3))
    {
        u32Sector = FLASH_SECTOR_3;
    }
    else if ((_u32Address < ADDR_FLASH_SECTOR_5) && (_u32Address >= ADDR_FLASH_SECTOR_4))
    {
        u32Sector = FLASH_SECTOR_4;
    }
    else if ((_u32Address < ADDR_FLASH_SECTOR_6) && (_u32Address >= ADDR_FLASH_SECTOR_5))
    {
        u32Sector = FLASH_SECTOR_5;
    }
    
    return u32Sector;
}
 
 
/**
  * @brief  Calculate the number of pages
  * @param  Size: The image size,the units of Size is Byte
  * @retval The number of pages or error
  */
unsigned short int STMFLASH_ReadHalfWord(unsigned int faddr)
{
    return *(volatile unsigned short int*)faddr; 
}
 
HIDO_UINT32 FLASH_Pages_Calculate(HIDO_UINT32 Size)
{
    HIDO_UINT32 Flash_Page_Number = 0;
    
    if( Size % PAGE_SIZE != 0)
    {
        Flash_Page_Number = ( Size / PAGE_SIZE) + 1;
    }
    else 
        Flash_Page_Number =   Size / PAGE_SIZE;
    
    if( Flash_Page_Number > MAX_FPAGE_NUM)
        return 0;
    else 
        return Flash_Page_Number;
}
 
 
/**
  * @brief  erase the block before  write Data
  * @param  Address: the start address to erase
            Len:     the length need to erase,uints is Byte
  * @retval 0:error; 1:success
  */
 
 
HIDO_UINT32 FLASH_Prepare(HIDO_UINT32 Address, HIDO_UINT32 Len)      //ÆðʼµØÖ·ºÍ×Ö³¤
{
    FLASH_EraseInitTypeDef stEraseInit;
    HIDO_UINT32 u32EraseError = 0;
 
    HAL_FLASH_Unlock();
    
    stEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
    stEraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3;
    stEraseInit.Banks = FLASH_BANK_1;
    stEraseInit.Sector = MCUFlash_GetSector(Address);
    stEraseInit.NbSectors = MCUFlash_GetSector(Address + Len - 1) - MCUFlash_GetSector(Address) + 1;
 
    if(HAL_FLASHEx_Erase(&stEraseInit, &u32EraseError) != HAL_OK)
    {
        HAL_FLASH_Lock();
        return 0;
    }
 
    HAL_FLASH_Lock();
    return 1;
}
 
/**
  * @brief  read the data from flash
  * @param  Address: the start address to read
            Len:     the length need to read,uints is Byte
  * @retval 
  */
void FLASH_Read( HIDO_UINT32 Address, HIDO_UINT8* Readbuff, HIDO_UINT32 Len)
{
    HIDO_UINT32 ReadCount = 0;
    
    for( ; ReadCount < Len; ReadCount++)
    {
        Readbuff[ ReadCount ] = ((HIDO_UINT8 *)Address)[ ReadCount ];
    }
}
 
/**
  * @brief  write data into flash
  * @param  Address: the start address to write
            Len:     the length need to write,uints is Byte
  * @retval 0:error  ; 1:success
  */
#define DATA_32                 ((HIDO_UINT32)0x12345678)
HIDO_UINT32 FLASH_Write( HIDO_UINT32 Address, const HIDO_UINT8* pData, HIDO_UINT32 Len)
{
    uint64_t WriteCount = 0,temp;
    HIDO_UINT32 FlashDestination = Address;
    HIDO_UINT32 FlashSource = (HIDO_UINT32)pData;
    HAL_StatusTypeDef FLASHStatus = HAL_OK;
        
    HAL_FLASH_Unlock();
    
    for( ; WriteCount < Len; WriteCount+=1)
    {temp=*(uint64_t*)FlashSource;
        FLASHStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, FlashDestination, temp);
        
        if( FLASHStatus != HAL_OK)
        {
            break;
        }
        if (*(HIDO_UINT16*)FlashDestination != *(HIDO_UINT16*)FlashSource)
        {
            //break;
        }
        FlashDestination += 1;
        FlashSource += 1;
    }
    
    HAL_FLASH_Lock();
    
    if( WriteCount < Len)
        return 0;
    else
        return 1;
    
}
 
HIDO_INT32 FLASH_EraseEx(HIDO_UINT32 _u32EraseAddr, HIDO_UINT32 _u32EraseLen)
{
    FLASH_EraseInitTypeDef stEraseInit;
    HIDO_UINT32 u32EraseError = 0;
 
    stEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
    stEraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3;
    stEraseInit.Banks = FLASH_BANK_1;
    stEraseInit.Sector = MCUFlash_GetSector(_u32EraseAddr);
    stEraseInit.NbSectors = MCUFlash_GetSector(_u32EraseAddr + _u32EraseLen - 1) - MCUFlash_GetSector(_u32EraseAddr) + 1;
 
    if(HAL_FLASHEx_Erase(&stEraseInit, &u32EraseError) != HAL_OK)
    {
        return HIDO_ERR;
    }
 
    return HIDO_OK;
}
 
HIDO_INT32 FLASH_ReadEx(HIDO_UINT8 *_pu8ReadData, HIDO_UINT32 _u32ReadAddr, HIDO_UINT32 _u32ReadLen)
{
    memcpy(_pu8ReadData, (HIDO_VOID *)_u32ReadAddr, _u32ReadLen);
 
    return HIDO_OK;
}
 
HIDO_INT32 FLASH_WriteEx(HIDO_UINT32 _u32WriteAddr, HIDO_UINT8 *_pu8WriteData, HIDO_UINT32 _u32WriteLen)
{
    HIDO_UINT32 i = 0;
    HIDO_UINT32 u32TmpLen = 0;
    HIDO_UINT32 u32TmpValue = 0;
//    HIDO_UINT32 u32RertyCnt = 0;
 
    while (_u32WriteLen != 0)
    {
        if (_u32WriteLen < 4)
        {
            u32TmpValue = 0xFFFFFFFF;
            for(i = 0; i < _u32WriteLen; i++)
            {
                ((HIDO_UINT8 *)&u32TmpValue)[i] = _pu8WriteData[i];
            }
            u32TmpLen = _u32WriteLen;
        }
        else
        {
            u32TmpValue = *(HIDO_UINT32 *) _pu8WriteData;
            u32TmpLen = 4;
        }
 
        __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
//    rerty:
        if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, _u32WriteAddr, u32TmpValue) != HAL_OK)
        {
//            if(u32RertyCnt < 3)
//            {
//                u32RertyCnt++;
//                goto rerty;
//            }
//            else
//            {
//                return HIDO_ERR;
//            }
            return HIDO_ERR;
        }
        else
        {
//            u32RertyCnt = 0;
        }
 
        _u32WriteAddr += u32TmpLen;
        _pu8WriteData += u32TmpLen;
        _u32WriteLen -= u32TmpLen;
    }
 
    return HIDO_OK;
}