WXK
2024-01-25 88da33b0d531f61e1b75d27e18b6f244ad2c3224
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
#include "Flash.h"
 
/**
  * @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; 
}
uint32_t FLASH_Pages_Calculate(uint32_t Size)
{
    uint32_t 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
  */
 
extern uint16_t testflag;
uint32_t FLASH_Prepare(uint32_t Address, uint32_t Len)      //ÆðʼµØÖ·ºÍ×Ö³¤
{uint32_t PageError = 0;
    FLASH_EraseInitTypeDef EraseInitStruct;
     HAL_FLASH_Unlock();
    EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
  EraseInitStruct.PageAddress = Address;
  EraseInitStruct.NbPages     = Len >>7;
    // HAL_FLASH_Lock(); 
     if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
  {     
      testflag=1;
        HAL_FLASH_Lock();
        return 0;
      
    }else{
        testflag=0;
        HAL_FLASH_Lock();
        return 1;
    }
 
//    uint32_t NbrOFPage = 0;
//    uint32_t EraseCount = 0;
//     HAL_StatusTypeDef status = HAL_OK; 
//    
//    HAL_FLASH_Unlock();
//    NbrOFPage = FLASH_Pages_Calculate( Len );
//    
//    for(; EraseCount < NbrOFPage; EraseCount++)
//    {
//    FLASH_PageErase( Address + ( PAGE_SIZE * EraseCount) );
//         status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
//        
//        if( status != HAL_OK)
//            break;
//    }
//    
//    HAL_FLASH_Lock();
 
//    if( EraseCount != NbrOFPage)
//        return 0;
//    else
//        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( uint32_t Address, uint8_t* Readbuff, uint32_t Len)
{
    uint32_t ReadCount = 0;
    
    for( ; ReadCount < Len; ReadCount++)
    {
        Readbuff[ ReadCount ] = ((uint8_t *)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                 ((uint32_t)0x12345678)
uint32_t temp21;
uint32_t FLASH_Write( uint32_t Address, const uint8_t* pData, uint32_t Len)
{
    uint32_t WriteCount = 0;
    uint32_t FlashDestination = Address;
    uint32_t FlashSource = (uint32_t)pData;
    HAL_StatusTypeDef FLASHStatus = HAL_OK;
        
    HAL_FLASH_Unlock();
    
    for( ; WriteCount < Len; WriteCount+=4)
    {temp21=*(uint32_t*)FlashSource;
        FLASHStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAMDATA_WORD, FlashDestination, temp21);
        
        if( FLASHStatus != HAL_OK)
        {
            break;
        }
        if (*(uint16_t*)FlashDestination != *(uint16_t*)FlashSource)
        {
            //break;
        }
        FlashDestination += 4;
        FlashSource += 4;
    }
    
    HAL_FLASH_Lock();
    
    if( WriteCount < Len)
        return 0;
    else
        return 1;
    
}
 
///**
//  * @brief  Disable the write protection of desired pages
//  * @param  None
//  * @retval None
//  */
//void FLASH_Dis_WriteProt_Pages(void)
//{
//    uint32_t UserOptionByte = 0, WRPR = 0;
//    uint16_t Var1 = OB_IWDG_SW, Var2 = OB_STOP_NoRST, Var3 = OB_STDBY_NoRST;
//    FLASH_Status Status = FLASH_BUSY;
//    static uint32_t UserMemoryMask = 0;
 
//    WRPR = FLASH_GetWriteProtectionOptionByte();
 
//      /* Test if user memory is write protected */
//      if ((WRPR & UserMemoryMask) != UserMemoryMask)
//      {
//        UserOptionByte = FLASH_GetUserOptionByte();
 
//        UserMemoryMask |= WRPR;
 
//        Status = FLASH_EraseOptionBytes();   //½â³ý±£»¤
 
//        if (UserMemoryMask != 0xFFFFFFFF)
//        {
//          Status = FLASH_EnableWriteProtection((uint32_t)~UserMemoryMask);
//        }
 
//        /* Test if user Option Bytes are programmed */
//        if ((UserOptionByte & 0x07) != 0x07)
//        { 
//          /* Restore user Option Bytes */
//          if ((UserOptionByte & 0x01) == 0x0)
//          {
//            Var1 = OB_IWDG_HW;
//          }
//          if ((UserOptionByte & 0x02) == 0x0)
//          {
//            Var2 = OB_STOP_RST;
//          }
//          if ((UserOptionByte & 0x04) == 0x0)
//          {
//            Var3 = OB_STDBY_RST;
//          }
 
//          FLASH_UserOptionByteConfig(Var1, Var2, Var3);
//        }
//        
//      }
 
//}