write to flash component

Moderator: Benj

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

write to flash component

Post by benp »

Hello,

It would be interesting to have a write/erase/read to flash memory component(not eeprom).
It is then possible to use the free flash memory (with a max 100 000 cycles)

I don't know if this already exist in flowcode for 18fxxxx.
The write to flash macro for 18f877 alredy exist in boost C (see "Microcontroller Systems Engineering).

I did a working erase+write to flash in 2 C code box for 18f2680. If you are interesed to use it to create a component, I can give you a copy.
I think it is possible to use it with all 18fxxxx with minor modifications.
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: write to flash component

Post by Benj »

Hello,

In Flowcode v4 you can manipulate flash directly using these C functions.

value = read_register(address);

write_register(address, value);

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

Thanks guys, I was looking for a solution like this.

Although a Flash Memory component similar to the included EEPROM one would be nice, I can live with the C code version for what I need to do (thanks Ben).
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

Ben,
I'm trying to implement the C-Code you gave into one of my flowcode programs and when I compile I get a failure due to the C-Code (doesn't give specifics).

Here is my Write to Flash:

Code: Select all

/* temp = value to be written
addr = flash memory address
*/
write_register(FCV_ADDR, FCV_TEMP);
And here is my Read from Flash:

Code: Select all

/* temp = value that was read
addr = flash memory address
*/
FCV_TEMP = read_register(FCV_ADDR);
In flowcode I've created 2 variables: temp (as a byte), and addr (as an integer).

So what am I doing wrong?
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: write to flash component

Post by Benj »

Hello

Are the bits shown below done in a caluclation icon or via C code?

addr = flash memory address

If this is being ran as C code then you will need to change to look like this.

FCV_ADDR = address;

May be worth your while attaching your program or sending to me via a PM and I will look into why the compilation is failing.

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

Hi Ben -- the 2 examples I showed were actually 2 independent C Code Blocks in FlowCode. I naturally assumed that the address should be an integer (16-Bits) and that the value to be read or written was limited to 1 byte. So far all I've done is create a macro for each instance (read and write) and placed the appropriate C Code block in each. And as I mentioned, I have also created the variables "temp" and "addr" in the FlowCode variables window.

I don't really understand what you mean by: FCV_ADDR = address? I did show that I am using FCV_ADDR already.
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

Hey Ben -- I PM'ed you a link to my FlowCode Source, hopefully this will both help to clarify what I have done, and also lead to a solution.

Thanks 8)
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

For those that are following this, Ben just sent me some alternative C Code for Reading and Writing to the Flash Memory area on the PIC. Apparently there is a problem with getting the original examples to compile in FlowCode.

Here are the new versions:

Code: Select all

/* Read from Flash Memory
*/
volatile char* register_ptr = (char*)FCV_ADDR;
FCV_TEMP = *register_ptr;

/* Write to Flash Memory
*/
volatile char* register_ptr = (char*)FCV_ADDR;
*register_ptr = FCV_TEMP;
FCV_TEMP = byte to be written into Flash memory, or value that was read from Flash memory (from FlowCode this would be variable "TEMP").
FCV_ADDR = 16-bit integer for Flash memory address where byte will be written to, or read from (from FlowCode this would be variable "ADDR").

This code appears to compile properly, although I have yet to actually try to use it with real hardware.

benp -- you originally started this thread and mentioned writing your own C Code blocks for accessing Flash memory. Is this example similar to what you were doing?
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: write to flash component

Post by Benj »

Brainwave!!!!

The original routines I thought should be working are not working because..... the functions only get included when the ICD is enabled. Doh.

Therefore my second solution is still valid if you are not using ICD :)

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

We all have those days :roll:

Probably a good thing I wasn't using ICD, otherwise the problem and the solution would have been farther down the road. So now you have one less thing to worry about.
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

Re: write to flash component

Post by benp »

No I didn't use write_register(address, value);
On which pic did you test this function?
The write to flash is very different between 16fxxx and 18fxxx

I did my write to flash from the 18f2680 datasheet (see p103-6.5).
I am not sure your method can work on 18fxxx because erase fonction work on 64 bytes or 32 bytes.
The adress must be erased before writing (this means FF before writing) and you can only erase 64 bytes on 18f2680.

It will work if the function can do this:
-read 64 bytes flash to ram
-erase 64 bytes flash
-modify 1 byte ram
-write 64 bytes from ram to flash

It is possible write direct to flash if the value in flash is FF alredy. This means that you can do a direct write to flash if you write only one time in the flash in an erased block (for example in not used memory after a reprogramming).
Did somebody test on hardware?

Here is my test program on 18f2680-hardware tested:
flash write-okv5.fcf
(9.5 KiB) Downloaded 448 times
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

Hi benp --- thanks for uploading your code (I'll be looking it over after this post).

I am using an ECIO-40P which has a PIC18F4455 on board.

Well I am now testing benj's flash read/write examples in hardware and I am seeing something rather unusual.
So here is what is happening: My program at first appears to be properly updating individual flash memory locations, which can be verified by playing it back (program spits it out sequentially via a USB HID keyboard routine). However if I either reset the ECIO or power it down, the resulting flash playback is not the same as what I originally put in there.

What I have also noticed, is that the data that appears after a reset, is very consistent (although not what I thought I had written into the memory cells). So I am somewhat confused. Its as if the flash memory is being treated more like standard RAM, and remains accurate only while the program is running and uninterrupted (no reset, or power down). I wouldn't think this is possible.

Anyway assuming your explanation holds true for the chip I am using, this could possibly explain the problem I am seeing.
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

Re: write to flash component

Post by benp »

It is possible to see the content of the flash with ppp.
It sould be at:
C:\Program Files\Matrix Multimedia\Flowcode V4\Tools\PPP
Do View/View pic micro
Then you can see the content of your eeprom and flash.

Here is My C code for write with no erase.
I wrote it from the 18f2680 datasheet. I tried to translate as much asm to C but some line are still in asm. I don't know how to translate. Maybe Benj know howto do it...

You can adapt to your pic. Just have a look to your datasheet.

Code: Select all

char counter;
tblptru=0x00;//load TBLPTR with the base address of the memory block
tblptrh=0x10;
tblptrl=0x00;
//write to buffer begin
counter=64;
while(counter>0)
                 {
                 tablat=0x99;//present 99 to table latch
                 asm tblwt+*  //write data, perform a short write to internal tblwt holding register.
                 asm nop
				 --counter;
                 }
//write to buffer end
tblptru=0x00;//load TBLPTR with the base address of the memory block
tblptrh=0x10;
tblptrl=0x00;
//write to flash bebin
set_bit(eecon1,EEPGD); //point to flash program memory
clear_bit(eecon1,CFGS); //access flash program memory
set_bit(eecon1,WREN); //enable write to memory
clear_bit(intcon,GIE);//disable interrupts
//Required sentence 
eecon2=0x55;
eecon2=0xaa;
set_bit(eecon1,WR);//wite to flash
asm nop
//fin required
set_bit(intcon,GIE);
clear_bit(eecon1,WREN);//disable write to memory
// write to flash end
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

Will PPP work with an ECIO module? When I installed FlowCode I didn't select the option of installing PPP, can it be done after the fact?

Thanks for the code benp, but it certainly is much more complicated then what benj was suggesting. I'm thinking that benj's example must be utilizing some hidden aspect of BoostC to try an achieve the same thing, but with far fewer lines of code. As I said, benj's example appears to work, but just doesn't stick after hitting reset. So my thinking is that perhaps as you suggested (and the data sheet does as well), single bytes can be written to flash so long as no bit needs to change from a zero to a one (need to start with 0xFF in the flash memory cell). With this in mind, then first erasing all the flash that I wish to modify under program control should do the trick (at least I think so). So if this idea works any suggestions on erasing a block of memory from 0x4000 - 0x5FFF using a BoostC method?

In my application I already implemented a "Clear" button for some of the EEPROM that is tracking the number of data logged events. essentially it is the index into how many rows of data are stored in flash. originally my concept was to never have to erase the flash, but to simply overwrite after clearing the index (kinda similar to how a hard drive works when you delete files). So if erasing the flash will make everything work properly, I can simple add this to my index "Clear" routine.
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: write to flash component

Post by Benj »

Hello,

The ECIO devices have their own programming tool which is located in the "Flowcode v4/Tools/ECIO" directory. When you select the ECIO as a target device this will automatically select its own programming tool and also predefine the correct configuration settings. This also goes for the formula flowcode, MIAC, etc.

I have seen other bootloaders and flash manipulation using the lines of assembler you mentioned so I wouldn't worry about keeping them in your program.

It is a surprise to me that the BoostC functions I provided are being reset upon device reset. Could it be variables being initialised on startup or something similar that is causing this problem?

PPP will be installed anyway even if you selected not to use it. If you want to specify it for use then you will have to go to the chip -> compiler options window and change the settings to match the following.

Programmer Location

C:\Program Files\Matrix Multimedia\Flowcode V4\tools\PPP\PPPv3.exe

Programmer Parameters

-cs 2 -chip PIC%p -nogui "%f.hex"

Use external program to set config - ticked.

Location

C:\Program Files\Matrix Multimedia\Flowcode V4\Tools\PPP\PPPv3.exe

Parameters

-cs 2 -chip PIC%p -config

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

Hi benj,
The ECIO devices have their own programming tool which is located in the "Flowcode v4/Tools/ECIO" directory. When you select the ECIO as a target device this will automatically select its own programming tool and also predefine the correct configuration settings.
Yep, that is how my system is set-up and everything appears to work just fine from a programming point of view. What benp was suggesting, was to use the PPP tools for peering into the flash memory locations to get a better clue as to what is going wrong.
It is a surprise to me that the BoostC functions I provided are being reset upon device reset. Could it be variables being initialised on startup or something similar that is causing this problem?
Yeah I was thinking the same thing, that perhaps one of my pointers had changed after coming through reset, and prior to entering my main loop. But so far I don't see anything in my code to account for this. I even tried just writing a single location, reading it and sending it out through the USB HID routine to verify. Then I reset the ECIO, and initiated a new read just to discover that the value had changed from what I had put in. As I said before, what ever I read after the reset, is consistent, and will always remain the same after subsequent resets. It is just what I write that doesn't appear to stick.

Is there a way to easily view the ECIO's flash memory via its programming software?
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: write to flash component

Post by Benj »

Hello,

If you use the ECIO programming tool located inside the "Flowcode v4/Tools/ECIOprog" directory then you will be able to read back the flash as a hex file. Then if you use a program such as winhex you can explore the hex file in a nice graphical way. PPP will also do a nice job of allowing you to view a hex file.

What address are you begging your writing at? Could it be possible clashing with your program running on the device? Alternativly you could be accessing a peripheral control register which would explain why the value was resetting on a restart.

ECIO memory locations 0x5000 - 0x5FFF should hopefully be untouched by your program unless it is very large and hopefully should not change value on a reset.

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

Hi benj,
Well to eliminate any doubt that my program was inadvertently messing something up after reset, I wrote a very very simple FlowCode program to write a single byte (0x55) into a single location (0x5000). I then compiled this to chip via FlowCode, and let it run. Then I ran the ECIOprog.exe file, pressed the reset button on the ECIO, and when the ECIOprog found it and gave me the "Ready" prompt, I had it read the chip and saved the hex file. Then I ran my MElabs Programmer application, opened the hex file, and scanned through the file. Except for the small amount of code at the beginning, the rest of the file contained 0xFF's, including location 0x5000.

So it appears that the BoostC Code to write to Flash is not working... any ideas?
Attachments
Program as viewed in FlowCode
Program as viewed in FlowCode
FlowCode.gif (8.94 KiB) Viewed 20582 times
ECIO.hex
Hex code read from ECIO-40P after running FlashWriteTest program
(61.33 KiB) Downloaded 322 times
FlashWriteTest.fcf
As simple as it gets write 0x55 to Flash location 0x5000
(4.5 KiB) Downloaded 318 times
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

I've been Googling for some examples of using BoostC to Read, Write, and Erase flash memory in a PIC18F series chip, and haven't had much luck (just keep finding references to PIC16F examples). Does anyone know where I can find specific information for utilizing BoostC for these functions as they relate to a PIC18F series chip?

Basically I am most interested in something that really defines this information very clearly, because to be quite frank, I don't really fully understand Benj's C Code examples, and how they truly work.
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

Re: write to flash component

Post by benp »

I looked for that information in boostC pdf which should be at:
C:\Program Files\Matrix Multimedia\Flowcode V4\boostc
I could find anything there or on the web that's why I wrote from the datasheet.

If you find a working solution with a short code, I am interested.

It is important to know what the boostC macro do exactly because you must take care not to overswrite on flash. Only 100 000 cycles are permitted.
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

It is important to know what the boostC macro do exactly because you must take care not to overwrite on flash. Only 100 000 cycles are permitted.
Yes I totally agree on having a good understanding of the macros involved and how they work. Although for my application, 100,000 write cycles is way more than enough. I figure that worst case scenario for what I need would be an average of one data write per day, per memory cell. So with that in mind, it would take over 200 years to exceed the flash memory write limits.
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

For my next test, I decided to also see if the Read-from-Flash example that Benj used was actually working properly.

C Code -- Read from Flash

Code: Select all

volatile char* register_ptr = (char*)FCV_ADDR;
FCV_TEMP = *register_ptr;
So I wrote this simple FlowCode program to test it:
FlowCode Read Flash Test
FlowCode Read Flash Test
ReadFlash.gif (9.01 KiB) Viewed 20522 times
EEPROM Macro Call Configuration
EEPROM Macro Call Configuration
EEPROMconfig.gif (12.2 KiB) Viewed 20522 times
Not having any display device attached to my ECIO-40P, I opted to send the results of my Flash Read to the EEPROM component, and write into one of the EEPROM's memory cells. At first I wrote into the first cell at location 0x00. And then later I wrote into location 0x0F. The value in both cases was coming from Flash address 0x5000, which held a default value of 0xFF.

Anyway after each test run I read the ECIO's memory and scanned through it. On the first run I would get a 0x0F in the EEPROM cell being written to. On subsequent runs I would get 0x00. Very odd, and not what I expected to see.

So Benj I await your response and explanation as to what I am seeing.
Attachments
FlashReadTest.fcf
Read from Flash (Addr = 0x5000) & write value to EEPROM (Addr = 0x0F)
(5 KiB) Downloaded 335 times
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: write to flash component

Post by Benj »

Hello,

Im not sure why the value is being stored and then reset upon a device restart.

Page 87, section 6.5 of the device datasheet contains the instructions for writing to Flash memory and includes an example assembler program. You could copy the assembler and see if this helps to solve the problem.

I will have a bit of a dig and see if I can find out why the current solution is not remaining after a reset.

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: write to flash component

Post by mytekcontrols »

I will have a bit of a dig and see if I can find out why the current solution is not remaining after a reset.
Sounds good Benj. I'll hold off for a bit on substituting the assembly version, to give you some time to perhaps come up with a working BoostC version.

BTW where did you get your BoostC code for Flash Read/Write from? If there was a documented example and explanation I would love to look it over.
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

Re: write to flash component

Post by benp »

I did the boostC for erase/write to flash from the 18f2680 datasheet. It is in asm and I translated in boostC (with light modifications: a nop added) . You should be able to do it with your from your pic datasheet and my BoostC.
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

Post Reply