R/W internal flash. How to point to a memo placeholder ?

General discussion on mikroBasic PRO for PIC32.
Post Reply
Author
Message
giovaniluigi
Posts: 92
Joined: 01 Oct 2007 03:57
Location: Canada/Brazil

R/W internal flash. How to point to a memo placeholder ?

#1 Post by giovaniluigi » 09 Aug 2014 22:41

What is wrong with the following way to store and read PIC's flash memory:

Code: Select all

'Placeholder for the data:
Const ConfigAddress as longword[10] = (0,0,0,0,0,0,0,0,0,0) org $9D001000

'Reading from flash:
ReadValue = ConfigAddress[0]

'Writing to flash:
Flash_Write_Word(@ConfigAddress[0],ValueToBeSaved)

I'm trying to keep away from pointers...
I assume that @ConfigAddress[0] is ok.

Am I right ?
giovaniluigi.com

giovaniluigi
Posts: 92
Joined: 01 Oct 2007 03:57
Location: Canada/Brazil

Re: R/W internal flash. How to point to a memo placeholder ?

#2 Post by giovaniluigi » 10 Aug 2014 00:07

Its only me, or the Flash_Write_Word() routine is not working in mikroBASIC PRO for PIC32 ?
giovaniluigi.com

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: R/W internal flash. How to point to a memo placeholder ?

#3 Post by marina.petrovic » 11 Aug 2014 13:51

Hi,

Please, can you explain me a little bit more where the problem occurs?
Did you tested simple Flash writing example from compiler adjusted to use pointers?

Code: Select all

program Flash
Const ConfigAddress as longword[10] = (0,0,0,0,0,0,0,0,0,0) org $9D001000

dim rowbuff as longword[128]
dim i as longword
dim ptr as ^longword

main:
  AD1PCFG = 0xFFFF
  LATB = 0
  TRISB = 0
  LATD = 0
  TRISD = 0

  for i = 0 to 127
    rowbuff[i] = i + (i << 16)
  next i

  Flash_Write_Row(@ConfigAddress,@rowbuff)

  ptr = @ConfigAddress
  for i = 0 to 127
    LATB = ptr^
    LATD = ptr^ >> 16
    Inc(ptr)
    Delay_ms(50)
  next i

  Delay_ms(1000)

  Flash_Erase_Page(@ConfigAddress)
  Flash_Write_Word(@ConfigAddress[0],0xAAAAAAAA)

  ptr = @ConfigAddress
  LATB = ptr^
  LATD = ptr^ >> 16
end.
Best regards,
Marina

giovaniluigi
Posts: 92
Joined: 01 Oct 2007 03:57
Location: Canada/Brazil

Re: R/W internal flash. How to point to a memo placeholder ?

#4 Post by giovaniluigi » 11 Aug 2014 18:18

Hi Marina,

Well the circuit board that I'm testing is my own, so I can't just load the demo.
However I have the PIC32 board from you and then I can test. Just need to pick it up and that need 1 day for me.

I tested the code using the address directly. Not as variable or a constant but as a literal...
I just wrote to the address 0x9D001000 then I read from it, and the value remains the same as before I write.
In the case of my code, I'm starting the IC with the value '1' in that address. I write '2' and it remains = '1'.
I have a LED and I'm making the LED lit If (value = 1)...

So, for me the conclusion is that for some reason the value is not being written to the specified location.

I'm using a PIC32MX795F512.

The result from my own board should not be diferente from the result when running at your dev. board.
Anyway sometimes I saw that your compiler just gets "confused" in some occasions then I'll try the demo code.
I'll return to you the result once I have it.
giovaniluigi.com

giovaniluigi
Posts: 92
Joined: 01 Oct 2007 03:57
Location: Canada/Brazil

Re: R/W internal flash. How to point to a memo placeholder ?

#5 Post by giovaniluigi » 12 Aug 2014 03:43

Just tested the code.

Result:

When I test the code to write and read the internal flash in a blank new Project it Works on MikroE board. (PIC Fusion)
When I test EXACTLY the same piece of code in my project that contains many modules and libs, it doesn't work.

When I have ruled out any code problem and HW issue, it turns out that is everything about the compiler/linker.
Its not the first time that I'm having that kind of weird problem that solves if I copy and paste it in a new blank and unique (no modules) project file.
Check for my last post on this forum and you will find out another weird thing.
How I solved ? Just wrote the same code in a new Project file without spliting the code into modules.

For this one I can't join the modules in a single file because it is not a small project.
giovaniluigi.com

giovaniluigi
Posts: 92
Joined: 01 Oct 2007 03:57
Location: Canada/Brazil

Re: R/W internal flash. How to point to a memo placeholder ?

#6 Post by giovaniluigi » 12 Aug 2014 05:53

hmm this world is so mean...
:?

Here is the code:

Code: Select all

program Flash

Const ConfigAddress as longword[4096] = (0) org $9D001000 'reserve 1 entire memo page to be written.

dim ptr as ^longword
dim i as longword

main:
  AD1PCFG = 0xFFFF
  LATB = 0
  TRISB = 0
  LATD = 0
  TRISD = 0

  i = ConfigAddress[0] 'just in case if linker try to optmize the code removing the placeholder

  Flash_Erase_Page(@ConfigAddress)
  Flash_Write_Word(@ConfigAddress[0],0x0000AAAA)

  ptr = @ConfigAddress
  LATB = ptr^
  
end.
Beautiful, neat and simple.
The code above writes the value 0x0000AAAA to the flash and then read the flash and output the value in port B.
It works!

BUT... it stops working if you remove the line

Code: Select all

Flash_Erase_Page(@ConfigAddress)
then the value on port B is 0x00000000.

Why :?:
giovaniluigi.com

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: R/W internal flash. How to point to a memo placeholder ?

#7 Post by marina.petrovic » 12 Aug 2014 12:38

Hi,

In compiler library Flash_Write_Word() routine, erase isn't implemented before write.
Each Flash partition is divided into pages, which represent the smallest block of memory that can be erased.
You can find detailed information in microcontroller datasheet.

Best regards,
Marina

giovaniluigi
Posts: 92
Joined: 01 Oct 2007 03:57
Location: Canada/Brazil

Re: R/W internal flash. How to point to a memo placeholder ?

#8 Post by giovaniluigi » 12 Aug 2014 17:17

Yeah I know, for this MCU we have 4096 bytes on each page.
That is why I can't erase one entire page if I'm using only 10 bytes...
I had to reserve a whole page to store 10 bytes...

Well ok but why I need to erase for it work ?
I'm not sure if this is a procedure needed by the MCU.
Is there any reason for that ?
giovaniluigi.com

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: R/W internal flash. How to point to a memo placeholder ?

#9 Post by marina.petrovic » 13 Aug 2014 13:07

Hi,

Always before writing into MCU flash you need to erase the flash (procedure needed by the MCU)
and like I mentioned in my previous answer you only can erase the whole page.

Best regards,
Marina

giovaniluigi
Posts: 92
Joined: 01 Oct 2007 03:57
Location: Canada/Brazil

Re: R/W internal flash. How to point to a memo placeholder ?

#10 Post by giovaniluigi » 13 Aug 2014 18:05

Ok.

Well please I ask you to request for your R&D team to put a note in the help files about this.

As you're offering the solution into a lib, many people (like me) will use it not considering many details in datasheet, as the lib should simplify all the job.
giovaniluigi.com

Post Reply

Return to “mikroBasic PRO for PIC32 General”