Reading FLASH
Moderators: David Barker, Jerry Messina
Reading FLASH
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?
I can't find anything that would lead me in the proper direction.
Can someone tell me where to start?
- David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
- Contact:
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
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...
Hope this helps...
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
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
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
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
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
----------------------------------------------------------------------------------
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
- David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
- Contact:
The MCU has gone to sleep (END) before the USART has time to output all characters. Try putting a delay before END. Alternatively,
should do the trick...
Code: Select all
Write ("$",HexToStr(CSUM,4), 13, 10)