Reading FLASH

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
CLinquist
Posts: 4
Joined: Sun Oct 29, 2006 6:09 pm
Location: Campbell, California

Reading FLASH

Post by CLinquist » Thu Dec 07, 2006 3:08 am

I'm finally getting to use Swordfish, after long using PBP. My problem is that I need to do a checksum across the entire FLASH memory of an 8722.
I can't find anything that would lead me in the proper direction.

Can someone tell me where to start?

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

Post by David Barker » Thu Dec 07, 2006 1:33 pm

In order to calculate a checksum, you will need to read each byte in ROM. Here is some code which will read a byte from any address location

Code: Select all

function ReadByte(pAddress as TABLEPTR) as byte
   asm-
      TBLRD *+
      movff TABLAT, result
   end asm
end function

function ReadByte(pAddress as longword) as byte
   TBLPTRU = pAddress.Byte2
   TBLPTRH = pAddress.Byte1
   TBLPTRL = pAddress.Byte0
   asm-
      TBLRD *+
      movff TABLAT, result
   end asm
end function  
The above are overloaded functions. If you pass a word size variable (which will access 64K) then the first routine is called. If you want to access above 64K, you will need to use a longword address - in which case the second routine is called.

It might be an idea to put these routines in a module so you can re-use. If you are reading contiguous bytes, you could write a more effecient routine that doesn't set the address each time. However, Swordfish uses table reads all the time (for example, reading and writing strings) so the routines above are safe.

Here is some test code which will read the first 1000 ROM locations - notice that address is longword, so the second overloaded function will be called...

Code: Select all

include "usart.bas"
include "convert.bas"

dim Address as longword

SetBaudrate(br19200)
Address = 0
while Address < 1000
   Write("$", HexToStr(Address,4)," = $", HexToStr(ReadByte(Address),2), 13, 10)
   inc(Address)
wend
Hope this helps...

CLinquist
Posts: 4
Joined: Sun Oct 29, 2006 6:09 pm
Location: Campbell, California

Post by CLinquist » Thu Dec 07, 2006 3:20 pm

Please bear with me as I'm trying to get started, but when I try to compile the following code, I get errors. What am I doing wrong?



Device = 18F8720
Clock = 20

Config

WDT = off

Include "usart.bas"
Include "convert.bas"

Dim Address As LongWord
SetBaudrate(br9600)

Address = 0
While Address < 1000
Write("$", HexToStr(Address,4)," = $", HexToStr(ReadByte(Address),2), 13, 10)
Inc(Address)
Wend
End



Function ReadByte(pAddress As TABLEPTR) As Byte
ASM-
TBLRD *+
movff TABLAT, result
End ASM
End Function

Function ReadByte(pAddress As LongWord) As Byte
TBLPTRU = pAddress.Byte2
TBLPTRH = pAddress.Byte1
TBLPTRL = pAddress.Byte0
ASM-
TBLRD *+
movff TABLAT, result
End ASM
End Function

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Thu Dec 07, 2006 3:43 pm

Hello Charles

The errors is due to you declaring the functions after you use them.

Move them under your includes and you will be on your way :)

CLinquist
Posts: 4
Joined: Sun Oct 29, 2006 6:09 pm
Location: Campbell, California

Post by CLinquist » Thu Dec 07, 2006 5:57 pm

Closer - The following compiles, but I don't seem to get the correct answer. Even though CSUM is a word, and I have a "pVal" of "4",I only get two HEX chars as a displayed result. What is wrong now?
----------------------------------------------------------------------------------

Device = 18F8720
Clock = 20

Config

WDT = off

Include "usart.bas"
Include "convert.bas"

Dim result As Byte
Dim CSUM As Word

Function ReadByte(pAddress As LongWord) As Byte
TBLPTRU = pAddress.Byte2
TBLPTRH = pAddress.Byte1
TBLPTRL = pAddress.Byte0
ASM-
TBLRD *+
movff TABLAT, result
End ASM
End Function


Dim Address As LongWord
SetBaudrate(br9600)

Address = 0
CSUM = 0

While Address < 131071
Inc(Address)
result = ReadByte(Address)
CSUM = CSUM + result
Wend

Write ("$",HexToStr(CSUM,4))
End

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

Post by David Barker » Thu Dec 07, 2006 6:11 pm

The MCU has gone to sleep (END) before the USART has time to output all characters. Try putting a delay before END. Alternatively,

Code: Select all

Write ("$",HexToStr(CSUM,4), 13, 10) 
should do the trick...

CLinquist
Posts: 4
Joined: Sun Oct 29, 2006 6:09 pm
Location: Campbell, California

Post by CLinquist » Thu Dec 07, 2006 6:17 pm

Thanks David! That did it.

I should have known... The same thing would have happened with the "old" compiler.

Bruce
Posts: 16
Joined: Sun Oct 29, 2006 5:54 pm
Contact:

Post by Bruce » Fri Dec 08, 2006 5:44 pm

Not sure if this will help, but MPLAB will show a checksum for .HEX files loaded if you right click on the toolbar, and place a check mark by Checksum.

The checksum displays on the toolbar for the file you have loaded.
Regards,

Bruce
http://www.rentron.com

Post Reply