read write program memory

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
cluster
Posts: 5
Joined: Thu Nov 24, 2011 7:58 pm

read write program memory

Post by cluster » Sat Nov 26, 2011 6:20 pm

hello,

can any one tell me how to write and read program memory?
i am working on 18f46j50. i need to store some data coming from usart so instead of using external eeprom i wanted it to write into program memory

thank you

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Sat Nov 26, 2011 9:47 pm


cluster
Posts: 5
Joined: Thu Nov 24, 2011 7:58 pm

Post by cluster » Sun Nov 27, 2011 4:42 pm

thanks for the link octal. but how do i write to address? like if i wanted to write 0 to address from $1000-$1400 then how to do that?
i saw in ROM module there is a variable "FROMAddress", i tried accessing it ROM.FROMAddress = $1000 but compiler returned error.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Sun Nov 27, 2011 10:03 pm

if you need so much granularity, check also
http://www.sfcompiler.co.uk/wiki/pmwiki ... moryAccess

cluster
Posts: 5
Joined: Thu Nov 24, 2011 7:58 pm

Post by cluster » Mon Nov 28, 2011 1:20 pm

octal wrote:if you need so much granularity, check also
http://www.sfcompiler.co.uk/wiki/pmwiki ... moryAccess
Hi octal,
thank you for the help, but still i am unable to get it working... :(
as example given in the link i made a simple program as below, its not working properly. I am trying to write 64 bytes as mentioned in datasheet but for some reason its writing 128 byte. can you please take a look at the code and memory dump, i would really appreciate it.

thank you

Code: Select all

Program RWT

Device = 18F46J50
Clock = 48
Config OSC = HSPLL
Config PLLDIV = 5 'PLL division

#option USB_SERVICE = False 

#option ROM_BLOCK_SIZE = 64

Include "usbhid.bas"
Include "string.bas"
include "MemoryAccess.BAS"

Dim Address As Word
Dim index As byte

Structure TTXReport
   Message As String
End Structure

Structure TRXReport
   Message As String
End Structure 

Dim TXReport As TTXReport Absolute TXReportRAM 
Dim RXReport As TRXReport Absolute RXReportRAM
Dim Service As PORTA.0


Clear(HID.Buffer)
High(Service)

While true
    
    HID.Service
    If HID.Attached Then
        Low(Service)
        If DataAvailable Then
            ReadReport
                Select RXReport.Message
                    Case "0"
                        Address = $800
                        EraseBlock(Address)
                        For index = 0 To 63 
                            WriteAt(Address,index) 
                            Inc (Address) 
                        Next 
                        CloseWrite
                        TXReport.Message = "Written"                                                
                    Case "1"
                        TXReport.Message = "Resetting Device..."
                        UCON = 0                             
                        UIE = 0                                 
                        UCON.3 = 0
                        DelayMS(100)
                        ASM
                            RESET
                        End ASM
                    Else
                        TXReport.Message = RXReport.Message
                EndSelect
            WriteReport
        EndIf
    Else
        High(Service)
    EndIf
   
Wend
 
End Program 
memory dump, left most column are address

Code: Select all


07F0	FFFF	FFFF	FFFF	FFFF	FFFF	FFFF	FFFF	 FFFF
0800	0100	0302	0504	0706	0908	0B0A	0D0C    0F0E
0810	1110	1312	1514	1716	1918	1B1A	1D1C    1F1E
0820	2120	2322	2524	2726	2928	2B2A	2D2C    2F2E
0830	3130	3332	3534	3736	3938	3B3A	3D3C    F13E
0840	0100	0302	0504	0706	0908	0B0A	0D0C    0F0E
0850	1110	1312	1514	1716	1918	1B1A	1D1C    1F1E
0860	2120	2322	2524	2726	2928	2B2A	2D2C    2F2E
0870	3130	3332	3534	3736	3938	3B3A	3D3C    3F3E
0880	FFFF	FFFF	FFFF	FFFF	FFFF	FFFF	FFFF	 FFFF

cluster
Posts: 5
Joined: Thu Nov 24, 2011 7:58 pm

Post by cluster » Mon Nov 28, 2011 1:46 pm

ok got it working for the 64byte block, just commented out CloseWrite
but still its not writing for the last 64th byte, it always write F1 there
what i am doing wrong... :cry: i have also tried changing index 0 to 64 still its not writing 64th byte..

Code: Select all

07F0	FFFF	FFFF	FFFF	FFFF	FFFF	FFFF	FFFF	FFFF
0800	0000	0000	0000	0000	0000	0000	0000	0000
0810	0000	0000	0000	0000	0000	0000	0000	0000
0820	0000	0000	0000	0000	0000	0000	0000	0000
0830	0000	0000	0000	0000	0000	0000	0000	F100 <-----
0840	FFFF	FFFF	FFFF	FFFF	FFFF	FFFF	FFFF	FFFF

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Mon Nov 28, 2011 2:41 pm

I don't think you're doing anything wrong. The code in MemoryAccess.bas doesn't look correct. It seems that WriteItem() is going to call CloseWrite() one byte too early, and that matches what you're seeing.

WriteItem() uses the following to actually write a byte. It uses the asm table write instruction that automatically increments the TABLEPTR register after it writes.

Code: Select all

// table write
    Inline Sub TableWrite()
      ASM
      TBLWT*+
      End ASM
    End Sub
But, WriteItem also increments the TABLEPTR register

Code: Select all

{
****************************************************************************
* Name    : WriteItem                                                      *
* Purpose : Write a byte to program memory                                 *
****************************************************************************
}
    Public Sub WriteItem (pValue As TABLAT)
      TableWrite       
      If (WriteBlockSize And Byte(TBLPTRL+1)) = 0 Then
         CloseWrite
      EndIf
      Inc(TABLEPTR)
    End Sub

If the ROM_BLOCK_SIZE=64, then the constant WriteBlockSize=63, and CloseWrite() will be called when WriteItem() is called with TABLEPTR = 62.

Try changing TableWrite() to this and see if it works better

Code: Select all

// table write
    Inline Sub TableWrite()
      ASM
      TBLWT*
      End ASM
    End Sub

cluster
Posts: 5
Joined: Thu Nov 24, 2011 7:58 pm

Post by cluster » Tue Nov 29, 2011 9:38 am

:D thank you very much Jerry and Octal its working now...

Mikeclx
Posts: 20
Joined: Mon Feb 09, 2009 5:36 am
Location: New Zealand

Post by Mikeclx » Wed Mar 07, 2012 1:46 am

Thanks Jerry,
That little + cost me hours of time to work around last year and now i understand why it was all not working correctly...

Regards,

Mike

User avatar
Jason
Posts: 50
Joined: Mon Mar 10, 2008 1:10 pm
Location: Australia

Post by Jason » Wed Mar 14, 2012 3:00 pm

Love this post... I tried to get program memory access working a while ago and never quite did. I think it's time to dive back in there and own it.

Post Reply