well approach seems to work , here is a rundown , the real key is USE A FLASH CHIP THAT SUPPORTS SMALL SECTOR ERASE SIZE AND USE THAT SETTING
3 new routines - to be added to examples i left
hope it save others time in the future
Regards
Sheldon
1. Flash_byte_modfy - use - change 1- 255 bytes of a selected sector/page ,
uses sector 0 as scratchpad and places changed code to sector 0 ,
copies all other pages / bytes not changed of the selected sector to sector 0
then erases the origin sector and copy complete sector 0 to origin
how to use
Code:
'---------- FLASH_MODIFY_BYTE TEST - example
Data_Length = 10 ' changed data length
For SDC_index = 0 to Data_Length
SDC_buffer[SDC_index] = SDC_index ' fill the changed data into buffer with values
next SDC_index
SDC_Sector = 1 ' sector start address for where data is to be placed
SDC_Page = 3 ' page start address for where data is to be placed
SDC_Byte = 16 ' byte start addresss for where data is to be placed
gosub Flash_Byte_Modify
Code:
Flash_Byte_Modify:
' assumes address , length ( upto 256bytes )
' asusmes bytes changed are in the same sector and no boundry cross
' assumes sector 0 is not protected
' assumes selected sector is not protected
Flash_Addr_Copy.byte3 = Data_Length ' save the selected address & length
Flash_Addr_Copy.byte2 = SDC_Sector
Flash_Addr_Copy.byte1 = SDC_Page
Flash_Addr_Copy.byte0 = SDC_Byte
SDC_sector = 0
gosub Flash_Sec_Erase ' erase sector 0
SDC_sector = 0 ' use sector 0,instead of given sector , but use given page, byte length
gosub Flash_write ' Write the data to sector 0
if Flash_Addr_Copy.byte1 = 0 then ' find the pages to copy - copy all pages/ bytes above 0 for given sector
Flash_Page_Orgin =1 ' start page of orgin
Flash_Page_length = 255 ' how many pages
Flash_Sec_Orgin = Flash_Addr_Copy.byte2 ' sector
Flash_Sec_Dest = 0
Flash_Page_Dest = 1
gosub Flash_Page_Copy
endif
if Flash_Addr_Copy.byte1 = 255 then ' find the pages to copy - copy all pages/ bytes below given page/ byte of sector
Flash_Page_Orgin =0 ' start page of orgin
Flash_Page_length = 254 ' how many pages
Flash_Sec_Orgin = Flash_Addr_Copy.byte2 ' sector
Flash_Sec_Dest = 0
Flash_Page_Dest = 0
gosub Flash_Page_Copy
endif
if Flash_Addr_Copy.byte1 >0 and Flash_Addr_Copy.byte1 < 254 then ' find the pages to copy - copy all pages/ bytes below given page/ byte of sector
Flash_Page_Orgin =0 ' start page of orgin
Flash_Page_length = Flash_Addr_Copy.byte1 - 1 ' how many pages
Flash_Sec_Orgin = Flash_Addr_Copy.byte2 ' sector
Flash_Sec_Dest = 0 ' start at same page value as orgin
Flash_Page_Dest = 0
gosub Flash_Page_Copy
Flash_Page_Orgin = Flash_Addr_Copy.byte1 + 1 ' start page of orgin
Flash_Page_length = 255 - Flash_Page_Orgin ' panges to copy = 255 - start address +1
Flash_Sec_Orgin = Flash_Addr_Copy.byte2 ' sector
Flash_Sec_Dest = 0
Flash_Page_Dest = Flash_Addr_Copy.byte1 + 1
gosub Flash_Page_Copy
endif
if Flash_Addr_Copy.byte0 + Flash_Addr_Copy.byte3 < 255 then ' if the start byte + data length of the data changed is < 255 bytes ,
' then copy bytes below the selected changed data
SDC_Sector = Flash_Addr_Copy.byte2 ' read selected sector
SDC_PAGE = Flash_Addr_Copy.byte1 ' of selected page
SDC_byte = 0 ' start address bytes 0 to
Data_Length = Flash_Addr_Copy.byte0 - 1 ' selected byte - 1 ( length)
gosub Flash_Read ' read
SDC_Sector = 0 ' use sector 0 aselected sector
SDC_PAGE = Flash_Addr_Copy.byte1 ' of selected page
SDC_byte = 0 ' start address bytes 0 to
Data_Length = Flash_Addr_Copy.byte0 - 1 ' selected byte - 1 ( length)
gosub Flash_Write ' write
' then copy bytes above the selected changed data
SDC_Sector = Flash_Addr_Copy.byte2 ' read selected sector
SDC_PAGE = Flash_Addr_Copy.byte1 ' of selected page
SDC_byte = Flash_Addr_Copy.byte0 + Flash_Addr_Copy.byte3 ' start address bytes above given address and length
Data_Length = 255 - SDC_byte ' data above changed bytes ( length = 255 bytes - given addrees + given length )
gosub Flash_Read ' read the data
SDC_Sector = 0 ' use sector 0
SDC_PAGE = Flash_Addr_Copy.byte1 ' of selected page
SDC_byte = Flash_Addr_Copy.byte0 + Flash_Addr_Copy.byte3 ' start address bytes above given address and length
Data_Length = 255 - SDC_byte ' data above changed bytes ( length = 255 bytes - given addrees + given length )
gosub Flash_Write ' write the data
endif
' Sector 0 should now have a complete copy of the data + changed data from the orginal selected sector
Flash_Sec_Orgin = 0 ' copy sector 0
Flash_Sec_dest = Flash_Addr_Copy.byte2 ' erase destination and copy data from flash orgin
Flash_clear = 1 ' set flag erase orgin sector 0 after copy
gosub Flash_Sec_Copy ' copy the sector
return
2. Sector copy routine - - this routine copy the data from the origin sector to destination sector
- will erase destination sector prior to copy
- option flag " Flash_clear " when set will Erase orgin sector after copy
- assumes no copy protect on destination or origin
how to use
Code:
Flash_Sec_Orgin = 0 ' sector origin - sector to copy
Flash_Sec_dest = 1 ' sector destination - will be erased then copy data from sector origin
Flash_clear = 1 ' option flag to erase origin sector after copy
gosub Flash_Sec_Copy ' copy the sector
Code:
Flash_Sec_Copy:
' copys data from 1 sector to another
' erases destination sector - optional clean of orginal sector
' assumes Flash_Sect_Orgin,Flash_Sect_Dest Flash_clear
' Erases wont work on sectors that are write protected
' sector erase take 600ms per instruction
SDC_Sector = Flash_Sec_Dest ' clear destination sector
gosub Flash_Sec_Erase ' use SDC_sector info to erase destination sector
for SDC_PAGE = 0 to 255 ' copy sector 0 pages to selected sector 0 ( 256bytes at a time)
SDC_Sector = Flash_Sec_Orgin ' sector to copy
SDC_byte = 0
Data_Length = 255
gosub Flash_Read ' read the data to buffer
SDC_Sector = Flash_Sec_Dest ' sector to copy to
SDC_byte = 0 ' use sector details for destination
Data_Length = 255
gosub Flash_Write ' write the data
next SDC_PAGE
if Flash_clear = 1 then
SDC_sector = Flash_Sec_Orgin ' clean up orgin sector
gosub Flash_Sec_Erase
endif
return
3. Page copy routine - this routine will copy pages 0-255 of origin to destination
- no sector boundry cross supported
- assumes destination is all "1" - no erase is required
- sector is not protected
how to use
Code:
' example copy sector 0/ page 0 50 pages to sector 0 , start page 125 ( page size number of bytes depends on chip)
Flash_Page_Orgin = 0 ' start address page of origin
Flash_Page_length = 50 ' number of pages to copy
Flash_Sec_Orgin = 0 ' sector origin address
Flash_Sec_Dest = 0 ' Sector destination
Flash_Page_Dest = 125 ' Page destination
gosub Flash_Page_Copy
Code:
Flash_Page_Copy:
' copys data from pages 0 -255 from orgin or destination sector
' assumes destination sector pages are Erased
' assumes pages orgin and pages destination do not cross sector boundrys
' assumes Flash_Page_Orgin,Flash_Page_dest ,Flash_Page_length, Flash_Sec_orgin ,Flash_Sec_dest
' writes wont work on sectors that are write protected
for Flash_tmp2 = Flash_Page_Orgin to Flash_Page_length ' copy sector 0 pages to selected sector 0 ( 256bytes at a time)
SDC_Sector = Flash_Sec_Orgin ' sector to copy
SDC_PAGE = Flash_tmp2
SDC_byte = 0
Data_Length = 255
gosub Flash_Read ' read the data to buffer
SDC_Sector = Flash_Sec_Dest ' sector to copy to
SDC_PAGE = Flash_Page_Dest ' page to copy to
SDC_byte = 0
Data_Length = 255
gosub Flash_Write ' write the data
Flash_Page_Dest = Flash_Page_Dest + 1 ' increment destination page
next Flash_tmp2
return
Bookmarks