forked from SZV10X_Software/SZV103_FM33A0xxEV_SiZhu

周巍
2024-04-11 91ef77c00ed797b1048c5187f416e351e646a009
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
 
/******************************************************************************
 * Copyright (C) 2014-2015 HangZhou SiZhu Co.,LTD.
 *
 *-----------------------------------------------------------------------------
 * File:                 w25x40bv.c
 * Description:        FLASH operation code
 * Author:              Lishoujian (867693272@qq.com)
 * Date:                Jan 8, 2015
 *****************************************************************************/
 
/* ----------------------- Platform includes --------------------------------*/
 
#include "w25x40bv.h"
#include "spi.h"
#include "delay.h"
 
 
/******************************************
 * func:    Flash_ReadSR
 * desc:    read 1 byte status data from flash
 * input:   none
 * output:  none
 * return:  1 byte status data read from flash
 *****************************************/
u8 Flash_ReadSR(void)   
{  
    u8 byte=0;   
    FLASH_CS_LOW;                            
    FLASH_ReadWriteByte(FLASH_RDSR);    //send read status register instructon    
    byte=FLASH_ReadWriteByte(0Xff);               
    FLASH_CS_HIGH;                                 
    return byte;   
 
/******************************************
 * func:    FLASH_Write_SR
 * desc:    write 1 byte status data to flash
 * input:   
 *   data  - 1 byte state data to fram status register
 * output:  none
 * return:  none
 *****************************************/
void FLASH_Write_SR(u8 sr)   
{   
    FLASH_Write_Enable();
    FLASH_CS_LOW;                               
    FLASH_ReadWriteByte(FLASH_WRSR);   //send write status register instructon  
    FLASH_ReadWriteByte(sr);               
    FLASH_CS_HIGH;                                         
}   
 
/******************************************
 * func:    FLASH_Write_Enable
 * desc:    write enable flash.
 * input:   none
 * output:  none
 * return:  none
 *****************************************/ 
void FLASH_Write_Enable(void)   
{
    FLASH_CS_LOW;                              
  FLASH_ReadWriteByte(FLASH_WREN);      //send  write enable instruction
    FLASH_CS_HIGH;                                          
 
/******************************************
 * func:    FLASH_Write_Disable
 * desc:    write disable flash
 * input:   none
 * output:  none
 * return:  none
 *****************************************/ 
void FLASH_Write_Disable(void)   
{  
    FLASH_CS_LOW;                            
  FLASH_ReadWriteByte(FLASH_WRDI);     //send  write disable instruction   
    FLASH_CS_HIGH;                                     
}         
 
/******************************************
 * func:    Flash_ReadID
 * desc:    read 16bit ID from flash
 * input:   none
 * output:  none
 * return:  ID - 16bit Manufacturer ID/Device ID
 *****************************************/
u16 Flash_ReadID(void)
{
    u16 ID = 0;      
    FLASH_CS_LOW;                    
    FLASH_ReadWriteByte(FLASH_RDID);//send         
    FLASH_ReadWriteByte(0x00);         
    FLASH_ReadWriteByte(0x00);         
    FLASH_ReadWriteByte(0x00);                     
    ID|=FLASH_ReadWriteByte(0xFF)<<8;  
    ID|=FLASH_ReadWriteByte(0xFF);     
    FLASH_CS_HIGH;                    
    return ID;
}           
 
/******************************************
 * func:    Flash_Read
 * desc:    read one or one more data bytes from flash
 * input:   pBuffer-data read from flash
            ReadAddr-32bit input address,just send the front 24bit
            NumByteToRead-decide how many bytes to read
 * output:  none
 * return:  none
 *****************************************/
void Flash_Read(uint8* pBuffer,uint32 ReadAddr,uint16 NumByteToRead)   
       u16 i;                                                        
      FLASH_CS_LOW;                            
    FLASH_ReadWriteByte(FLASH_READ);            //send read command  
    FLASH_ReadWriteByte((u8)((ReadAddr)>>16));  //send 24bit address   
    FLASH_ReadWriteByte((u8)((ReadAddr)>>8));   
    FLASH_ReadWriteByte((u8)ReadAddr);   
    for(i=0;i<NumByteToRead;i++)
      {  
       pBuffer[i]=FLASH_ReadWriteByte(0XFF);    
    }
    FLASH_CS_HIGH;                                        
}  
 
 
 
/******************************************
 * func:    Flash_Write
 * desc:    write data  into flash,can write more than 256 bytes,and can executed sector erase operation.
 * input:   pBuffer-data write into flash
            ReadAddr-32bit input address,just send the front 24bit
            NumByteToRead-decide how many bytes to write
 * output:  none
 * return:  none
 *****************************************/             
/*u8 FLASH_BUF[4096];
void Flash_Write(uint8* pBuffer,uint32 WriteAddr,uint16 NumByteToWrite)   
    u32 secpos;
    u16 secoff;
    u16 secremain;       
     u16 i;    
 
    secpos=WriteAddr/4096;                                //sector address 0~127 for w25x40bv
    secoff=WriteAddr%4096;                                //sector offset
    secremain=4096-secoff;                                //remaining space of sector 
 
    if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//no more than 4096 bytes
    while(1) 
    {    
        Flash_Read(FLASH_BUF,secpos*4096,4096);             //read all data from sector
        for(i=0;i<secremain;i++)                            //check data
        {
            if(FLASH_BUF[secoff+i]!=0XFF)break;                       
        }
        if(i<secremain)                                     
        {  
            Flash_Erase_Sector(secpos);                       //sector erase
            for(i=0;i<secremain;i++)                            //write data
            {
                FLASH_BUF[i+secoff]=pBuffer[i];      
            }
            Flash_Write_NoCheck(FLASH_BUF,secpos*4096,4096);    //write data into sector  
 
        }
        else Flash_Write_NoCheck(pBuffer,WriteAddr,secremain);                    
        if(NumByteToWrite==secremain)break;                   //write finish
        else                                                  //write unfinish
        {
            secpos++;                                           //sector address add 1
            secoff=0;                                           //sector offset 0      
 
          pBuffer+=secremain;                                 //pointer offset
            WriteAddr+=secremain;                               //write address offset       
             NumByteToWrite-=secremain;                                  //the number of bytes reduce
            if(NumByteToWrite>4096)secremain=4096;                //write operation can't be finished in next sector
            else secremain=NumByteToWrite;                            //write operation can be finished in next sector
        }     
    };          
}*/
 
/******************************************
 * func:    Flash_Write_NoCheck
 * desc:    write data  into flash,can write more than 256 bytes.(we must delete the precious data before write)
 * input:   pBuffer-data write into flash
            ReadAddr-32bit input address,just send the front 24bit
            NumByteToRead-decide how many bytes to write
 * output:  none
 * return:  none
 *****************************************/
void Flash_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)   
{                       
    u16 pageremain;       
    pageremain=256-WriteAddr%256;                           //remaining space of page                 
    if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//no more than 256B
    while(1)
    {       
        Flash_Write_Page(pBuffer,WriteAddr,pageremain);
        if(NumByteToWrite==pageremain)break;                  //write finish
         else //NumByteToWrite>pageremain
        {
            pBuffer+=pageremain;
            WriteAddr+=pageremain;    
 
            NumByteToWrite-=pageremain;                                
            if(NumByteToWrite>256)pageremain=256;               //write operation can't be finished in next page
            else pageremain=NumByteToWrite;                       //write operation can be finished in next page
        }
    }        
}
 
/******************************************
 * func:    Flash_Write_Page
 * desc:    write data less than 256 bytes into flash.(we must delete the precious data before write)
 * input:   pBuffer-data write into flash
            ReadAddr-32bit input address,just send the front 24bit
            NumByteToRead-decide how many bytes to write
 * output:  none
 * return:  none
 *****************************************/
void Flash_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
       u16 i;  
    FLASH_Write_Enable();                       //write enable 
      FLASH_CS_LOW;                                  
    FLASH_ReadWriteByte(FLASH_WRITE);           //send write command
    FLASH_ReadWriteByte((u8)((WriteAddr)>>16)); //send 24bit address   
    FLASH_ReadWriteByte((u8)((WriteAddr)>>8));   
    FLASH_ReadWriteByte((u8)WriteAddr);   
    for(i=0;i<NumByteToWrite;i++)
    FLASH_ReadWriteByte(pBuffer[i]);
    FLASH_CS_HIGH;                            
    Flash_Wait_Busy();                                      //wait write finish
 
 
/******************************************
 * func:    Flash_Erase_Chip
 * desc:    erase all data.
 * input:   none
 * output:  none
 * return:  none
 *****************************************/ 
void Flash_Erase_Chip(void)   
{                                             
    FLASH_Write_Enable();                   
    Flash_Wait_Busy();   
      FLASH_CS_LOW;                              
    FLASH_ReadWriteByte(FLASH_ChipErase);        //send chip erase instruction 
      FLASH_CS_HIGH;                                          
      Flash_Wait_Busy();                                    //wait erase finish
}   
 
 
/******************************************
 * func:    Flash_Erase_Sector
 * desc:    sector erase operation.
 * input:   Dst_Addr - sector address.
 * output:  none
 * return:  none
 *****************************************/ 
void Flash_Erase_Sector(u32 Dst_Addr)   
{   
      Dst_Addr*=4096;
    FLASH_Write_Enable();                        //write enable 
    Flash_Wait_Busy();   
      FLASH_CS_LOW;                            
    FLASH_ReadWriteByte(FLASH_SectorErase);      //send sector erase instruction
    FLASH_ReadWriteByte((u8)((Dst_Addr)>>16));      
    FLASH_ReadWriteByte((u8)((Dst_Addr)>>8));   
    FLASH_ReadWriteByte((u8)Dst_Addr);  
      FLASH_CS_HIGH;                                         
    Flash_Wait_Busy();                      
}  
 
 
/******************************************
 * func:    Flash_Wait_Busy
 * desc:    wait  write or erase operation finish.
 * input:   none
 * output:  none
 * return:  none
 *****************************************/ 
void Flash_Wait_Busy(void)   
{   
    while ((Flash_ReadSR()&0x01)==0x01);   //wait BUSY=0
}  
 
 
/******************************************
 * func:    Flash_PowerDown
 * desc:    take flash into power-down mode.
 * input:   none
 * output:  none
 * return:  none
 *****************************************/ 
void Flash_PowerDown(void)   
      FLASH_CS_LOW;                            
    FLASH_ReadWriteByte(FLASH_POWERDOWN);      //send power-down instruction
      FLASH_CS_HIGH;                                          
    delay_us(3);                               //wait TPD  
}   
 
 
/******************************************
 * func:    Flash_WAKEUP
 * desc:    release flash from power-down mode.
 * input:   none
 * output:  none
 * return:  none
 *****************************************/ 
void Flash_WAKEUP(void)   
{  
      FLASH_CS_LOW;                              
    FLASH_ReadWriteByte(FLASH_WKUP);         //send wake up command 0xAB    
      FLASH_CS_HIGH;                                          
    delay_us(3);                             //wait TRES1
}