Flash erase byte routine


Closed Thread
Results 1 to 21 of 21

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Flash erase byte routine

    bit more of an attempt , steve can you check the logic ,cleanup ,


    Code:
    Flash_Byte_Modify:
     ' assumes address , length ( upto 256bytes )
     ' assumes sector 0 is not protected
     ' assumes selectted 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 the changed 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  
         for SDC_Page = 1 to 255                 ' read all pages above 0  of selected sector  
             SDC_Sector  = Flash_Addr_Copy.byte2    ' of read selected sector 
             SDC_byte  = 0                          ' bytes 0 - 255
             Data_Length = 255                      ' all bytes 
             gosub Flash_Read                       ' read 255 bytes at a time 
             SDC_Sector  = 0                        ' use sector 0 aselected sector 
             SDC_byte  = 0                          ' bytes 0 - 255
             Data_Length = 255                      ' all bytes 
             gosub Flash_Write                      ' write  
          next SDC_Page
       endif
    
      if Flash_Addr_Copy.byte1 = 255 then       ' find the pages to copy - copy all pages/ bytes  below given page/ byte of sector  
         for SDC_Page = 0 to 254                 ' read all pages below 255  of selected sector  
             SDC_Sector  = Flash_Addr_Copy.byte2    ' of read selected sector 
             SDC_byte  = 0                          ' bytes 0 - 255
             Data_Length = 255                      ' all bytes 
             gosub Flash_Read                       ' read 255 bytes at a time 
             SDC_Sector  = 0                        ' use sector 0 aselected sector 
             SDC_byte  = 0                          ' bytes 0 - 255
             Data_Length = 255                      ' all bytes 
             gosub Flash_Write                      ' write  
          next SDC_Page
       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  
            
         for SDC_Page = 0 to Flash_Addr_Copy.byte1 - 1   ' read all pages from 0 to selected page -1   
             SDC_Sector  = Flash_Addr_Copy.byte2         ' of read selected sector 
             SDC_byte  = 0                               ' bytes 0 - 255
             Data_Length = 255                           ' all bytes 
             gosub Flash_Read                            ' read 255 bytes at a time 
             SDC_Sector  = 0                             ' use sector 0 aselected sector 
             SDC_byte  = 0                               ' bytes 0 - 255
             Data_Length = 255                           ' all bytes 
             gosub Flash_Write                           ' write  
          next SDC_Page
       endif
     
        for SDC_Page = Flash_Addr_Copy.byte1 + 1 to 255  ' read all pages from selected page + 1  to 255   
             SDC_Sector  = Flash_Addr_Copy.byte2         ' of read selected sector 
             SDC_byte  = 0                               ' bytes 0 - 255
             Data_Length = 255                           ' all bytes 
             gosub Flash_Read                            ' read 255 bytes at a time 
             SDC_Sector  = 0                             ' use sector 0 aselected sector 
             SDC_byte  = 0                               ' bytes 0 - 255
             Data_Length = 255                           ' all bytes 
             gosub Flash_Write                           ' write  
          next SDC_Page
       endif
     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 
       
          SDC_sector = Flash_Addr_Copy.byte2
          gosub Flash_Sec_Erase              ' erase orginal sector 
          
          for SDC_PAGE = 0 to 255                                          '  copy sector 0 pages to selected sector 0 ( 256bytes at a time) 
               SDC_Sector  = 0                                             ' use sector 0 
               SDC_byte    = 0
               Data_Length = 255
               gosub Flash_Read                                             ' read the data  
               SDC_Sector  = Flash_Addr_Copy.byte2                          ' use selected sector  
               SDC_byte    = 0
               Data_Length = 255
          next SDC_PAGE
     
          SDC_sector = 0                     ' clean up sector 0 
          gosub Flash_Sec_Erase              ' erase orginal sector
     return

  2. #2
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: Flash erase byte routine

    I will do! Just need a bit of time to absorb the logic.

Similar Threads

  1. Erase error
    By waynepauly in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 29th June 2012, 02:23
  2. Internal eeprom - erase block
    By ruijc in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 13th February 2008, 11:11
  3. 18F2431 won't program or erase.
    By HenrikOlsson in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 28th September 2006, 07:55
  4. 12F675 won't reprogram or erase
    By modifyit in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 19th May 2006, 05:07
  5. 12F629 won't erase and bandgap save?
    By Keith55555 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 6th April 2004, 01:28

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts