Aditional Flash Mem Library

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
yo2lio
Posts: 39
Joined: Tue Sep 25, 2007 6:52 pm
Location: Arad, Romania
Contact:

Aditional Flash Mem Library

Post by yo2lio » Tue Sep 25, 2007 7:22 pm

This is a implementations of Flash Library for PIC18F MCU, writed by me.

Explanation of procedures and functions :

Variables :

Code: Select all

Public Dim data_8(8) As Byte
Public Dim data_64(64) As Byte
Public Dim data_1024(1024) As Byte
' Erase ...( depends of MCU ) block at address ... (64 or 1024 bytes, see datasheet of MCU)'
' For PIC18F452,Pic18F4520, ..., PIC18FxxJxx '

Code: Select all

sub Flash_Mem_Erase(address as longword)
' Write 8 bytes block located in data_8, at address ... '
' Only for PIC18Fxx2 series, see datasheet of MCU '

Code: Select all

sub Flash_Mem_Write_8(address as longword)
' Write 64 bytes block located in data_64, at address ... '
' Only for PIC18F4520, ..., PIC18FxxJxx, see datasheet of MCU '

Code: Select all

sub Flash_Mem_Write_64(address as longword)
' Erase and write 64 bytes 8x8 block located in data_64, at address ... '
' Only for PIC18Fxx2 series, see datasheet of MCU '

Code: Select all

sub Flash_Mem_Erase_Write_64(address as longword)
' Erase and write 64 bytes block located in data_64, at address ... '
' Only for PIC18F4520 series, see datasheet of MCU '

Code: Select all

sub Flash_Mem_Erase_Write_64A(address as longword)
' Erase and write 1024 bytes block located in data_1024, at address ... '
' Only for PIC18FxxJxx, see datasheet of MCU '

Code: Select all

sub Flash_Mem_Erase_Write_1024(address as longword)
' Read 1 byte at address ... '
' For all type of MCU '

Code: Select all

function Flash_Mem_Read(address as longword) as byte
' Read N bytes (64) at address ... and put them in data var... '
' For all type of MCU '

Code: Select all

sub Flash_Mem_Read_N_Bytes(address as longword, N as byte)
User must choose between this procedures and functions depending of MCU.

Code: Select all

{
*****************************************************************************
*  Name    : Flash_Mem_Library.BAS                                          *
*  Author  : Medrea Florin ANdrei                                           *
*  Notice  : Copyright (c) 2007 V1.1                                        *
*          : All Rights Reserved                                            *
*  Date    : 9/25/2007                                                      *
*  Version : 1.1                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Module Flash_Mem_Library

Public Dim data_8(8) As Byte
Public Dim data_64(64) As Byte
Public Dim data_1024(1024) As Byte

Dim GIE_ As INTCON.7
Dim FREE_ As EECON1.4
Dim WREN_ As EECON1.2
Dim WR_ As EECON1.1
Dim RD_ As EECON1.0

Public Sub Flash_Mem_Erase(address As LongWord)
  Dim flag As Byte     ' Erase ...( depends of MCU ) block at address ... (64 or 1024 bytes, see datasheet of MCU)
  flag = INTCON And %10000000   ' save state of GIE bit
  GIE_ = 0           ' disable interrupts
  TBLPTRL = address.byte0
  TBLPTRH = address.byte1
  TBLPTRU = address.byte2
  EECON1 = EECON1 Or %10000000   ' for compatibility J series with standard MCU
  EECON1 = EECON1 And %10111111  ' for compatibility J series with standard MCU
  WREN_ = 1
  FREE_ = 1
  EECON2 = $55
  EECON2 = $AA
  WR_ = 1
  WREN_ = 0
  INTCON = INTCON Or flag   ' enable interrupts with first state of GIE bit
End Sub

Public Sub Flash_Mem_Write_8(address As LongWord)    'only for PIC18Fxx2 series, see datasheet of MCU
  Dim i,flag As Byte     ' write 8 bytes block at address ...
  flag = INTCON And %10000000   ' save state of GIE bit
  GIE_ = 0           ' disable interrupts
  TBLPTRL = address.byte0
  TBLPTRH = address.byte1
  TBLPTRU = address.byte2
  FSR2 = @data_8
  i = 0
  While i < 8
    TABLAT  = POSTINC2
    ASM
      nop
      TBLWT+*
      nop
    End ASM
    Inc(i)
  Wend
  EECON1 = EECON1 Or %10000000
  EECON1 = EECON1 And %10111111
  WREN_ = 1
  EECON2 = $55
  EECON2 = $AA
  WR_ = 1
  WREN_ = 0
  INTCON = INTCON Or flag   ' enable interrupts with first state of GIE bit
End Sub

Public Sub Flash_Mem_Write_64(address As LongWord)  'only for PIC18F4520 and J series, see datasheet of MCU
  Dim i,flag As Byte     ' write 64 bytes block at address ...
  flag = INTCON And %10000000   ' save state of GIE bit
  GIE_ = 0           ' disable interrupts
  TBLPTRL = address.byte0
  TBLPTRH = address.byte1
  TBLPTRU = address.byte2
  FSR2 = @data_64
  i = 0
  While i < 64
    TABLAT  = POSTINC2
    ASM
      nop
      TBLWT+*
      nop
    End ASM
    Inc(i)
  Wend
  EECON1 = EECON1 Or %10000000
  EECON1 = EECON1 And %10111111
  WREN_ = 1
  EECON2 = $55
  EECON2 = $AA
  WR_ = 1
  WREN_ = 0
  INTCON = INTCON Or flag   ' enable interrupts with first state of GIE bit
End Sub

Public Sub Flash_Mem_Erase_Write_64(address As LongWord)  'only for PIC18Fxx2 series, see datasheet of MCU
  Dim i,j,flag As Byte     ' erase and write 64 bytes 8x8 block at address ...
  flag = INTCON And %10000000   ' save state of GIE bit
  GIE_ = 0           ' disable interrupts
  TBLPTRL = address.byte0
  TBLPTRH = address.byte1
  TBLPTRU = address.byte2
  EECON1 = EECON1 Or %10000000   ' for compatibility J series with standard MCU
  EECON1 = EECON1 And %10111111  ' for compatibility J series with standard MCU
  WREN_ = 1
  FREE_ = 1
  EECON2 = $55
  EECON2 = $AA
  WR_ = 1
  ASM
    nop
    TBLRD*-
    nop
  End ASM
  j = 0
  FSR2 = @data_64
  While j < 8
    i = 0
    While i < 8
      TABLAT  = POSTINC2
      ASM
        nop
        TBLWT+*
        nop
      End ASM
      Inc(i)
    Wend
    EECON1 = EECON1 Or %10000000
    EECON1 = EECON1 And %10111111
    WREN_ = 1
    EECON2 = $55
    EECON2 = $AA
    WR_ = 1
    Inc(j)
  Wend
  WREN_ = 0
  INTCON = INTCON Or flag   ' enable interrupts with first state of GIE bit
End Sub

Public Sub Flash_Mem_Erase_Write_64A(address As LongWord)  'only for PIC18F4520 series, see datasheet of MCU
  Dim i,flag As Byte     ' erase and write 64 bytes block at address ...
  flag = INTCON And %10000000   ' save state of GIE bit
  GIE_ = 0           ' disable interrupts
  TBLPTRL = address.byte0
  TBLPTRH = address.byte1
  TBLPTRU = address.byte2
  EECON1 = EECON1 Or %10000000   ' for compatibility J series with standard MCU
  EECON1 = EECON1 And %10111111  ' for compatibility J series with standard MCU
  WREN_ = 1
  FREE_ = 1
  EECON2 = $55
  EECON2 = $AA
  WR_ = 1
  ASM
    nop
    TBLRD*-
    nop
  End ASM
  FSR2 = @data_64
  i = 0
  While i < 64
    TABLAT  = POSTINC2
    ASM
      nop
      TBLWT+*
      nop
    End ASM
    Inc(i)
  Wend
  EECON1 = EECON1 Or %10000000
  EECON1 = EECON1 And %10111111
  WREN_ = 1
  EECON2 = $55
  EECON2 = $AA
  WR_ = 1
  WREN_ = 0
  INTCON = INTCON Or flag   ' enable interrupts with first state of GIE bit
End Sub

Public Sub Flash_Mem_Erase_Write_1024(address As LongWord)  ' only for J series
  Dim i,j,flag As Byte     ' erase and write 1024 bytes block at address ...
  flag = INTCON And %10000000   ' save state of GIE bit
  GIE_ = 0           ' disable interrupts
  TBLPTRL = address.byte0
  TBLPTRH = address.byte1
  TBLPTRU = address.byte2
  EECON1 = EECON1 Or %10000000   ' for compatibility J series with standard MCU
  EECON1 = EECON1 And %10111111  ' for compatibility J series with standard MCU
  WREN_ = 1
  FREE_ = 1
  EECON2 = $55
  EECON2 = $AA
  WR_ = 1
  ASM
    nop
    TBLRD*-
    nop
  End ASM
  j = 0
  FSR2 = @data_1024
  While j < 16
    i = 0
    While i < 64
      TABLAT  = POSTINC2
      ASM
        nop
        TBLWT+*
        nop
      End ASM
      Inc(i)
    Wend
    EECON1 = EECON1 Or %10000000
    EECON1 = EECON1 And %10111111
    WREN_ = 1
    EECON2 = $55
    EECON2 = $AA
    WR_ = 1
    Inc(j)
  Wend
  WREN_ = 0
  INTCON = INTCON Or flag   ' enable interrupts with first state of GIE bit
End Sub

Public Function Flash_Mem_Read(address As LongWord) As Byte
  TBLPTRL = address.byte0
  TBLPTRH = address.byte1
  TBLPTRU = address.byte2
  ASM
    TBLRD*
  End ASM
  result = TABLAT
End Function

Public Sub Flash_Mem_Read_N_Bytes(address As LongWord, N As Byte)
  Dim i As Byte                ' read N bytes (user can define this) at address ... and put them in data dim.
  GIE_ = 0           ' disable interrupts
  TBLPTRL = address.byte0
  TBLPTRH = address.byte1
  TBLPTRU = address.byte2
  i = 0
  While i < N
    ASM
      TBLRD*+
    End ASM
    data_64(i) = TABLAT
    Inc(i)
  Wend
End Sub
Enjoy
Best regards, Florin Medrea
My UserLibrary Folder http://www.microelemente.ro/Swordfish/UserLibrary/

User avatar
David Barker
Swordfish Developer
Posts: 1194
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Sep 25, 2007 8:29 pm

Thank you very much for sharing your code. I'm quite happy for people to post code like this on the forum. However, it can get 'lost' over time - and that would be a real shame.

Perhaps you would consider posting on the wiki?

http://www.sfcompiler.co.uk/wiki/pmwiki ... er.Modules

Just press 'edit' at the bottom of the screen. You will need

Code: Select all

 username : user
 password : swordfish
Thanks again for your post ;-)

yo2lio
Posts: 39
Joined: Tue Sep 25, 2007 6:52 pm
Location: Arad, Romania
Contact:

Post by yo2lio » Wed Sep 26, 2007 6:39 am

I'll will write a short description and I will put it on the wiki.
Best regards, Florin Medrea
My UserLibrary Folder http://www.microelemente.ro/Swordfish/UserLibrary/

rmteo
Posts: 237
Joined: Fri Feb 29, 2008 7:02 pm
Location: Colorado, USA

Question for Florin

Post by rmteo » Thu Apr 03, 2008 6:14 pm

Here is a question for Florin. I am using a PIC18F4620.

Code: Select all

Flash_Mem_Write_64(buff,pageaddr)
The above does not write to Flash.

Code: Select all

Flash_Mem_Erase_Write_64A(buff,pageaddr)
This one works and writes to flash.

Code: Select all

Flash_Mem_Erase(pageaddr)
Flash_Mem_Write_64(buff,pageaddr)
I have also tried this but it does NOT write to Flash. Any comments?

Post Reply