AnsweredHot!Not able to write data to the flash memory location in dspic33ep32gp502

Page: 12 > Showing page 1 of 2
Author
rajatkalyan
New Member
  • Total Posts : 29
  • Reward points : 0
  • Joined: 2018/01/17 21:30:45
  • Location: 0
  • Status: offline
2018/03/01 04:00:09 (permalink)
0

Not able to write data to the flash memory location in dspic33ep32gp502

Since there's no EEPROM available in dspic33ep32gp502, so to store data permanently i have to use flash memory(as per my knowledge)
I want to store an integer value  ,let's say int x=1; 
I want to store this value in the flash memory without disturbing my programme/reseting my code.
 
Here's what i have tried so far
  
#define pagelength 128
#define rowlength 128
int RamBuffer[pagelength*2];


char table __attribute__ ((space(prog), address(0x27FE)));
int main (void)
{
while(1)
{
read_flash();
erase_flash();
write_flash();
}
}
void read_flash(void)
{
    int offset,i;
    TBLPAG=__builtin_tblpage(&table);
    offset=__builtin_tbloffset(&table);
    offset=offset&0xF800;
    for(i=0;i<pagelength*2;i++)
    {
        RamBuffer[i++]=__builtin_tblrdl(offset);
        RamBuffer[i]=__builtin_tblrdh(offset);
        offset+=2;
    }
}

void write_flash(void)
{
   int offset,i;
   TBLPAG=0xFA;
   
   offset=0;
   for(i=0;i<rowlength*2;i++)
   {
       __builtin_tblwtl(offset,RamBuffer[i++]);
       __builtin_tblwth(offset,RamBuffer[i]);
       offset+=2;
    }
   NVMADRU=__builtin_tblpage(&table);
   offset=__builtin_tbloffset(&table);
   NVMADR=(offset&0xF800);
   
   NVMCON=0x4002;
   
   __builtin_disi(6);
   __builtin_write_NVM();
}

void erase_flash(void)
{

    int offset;
    NVMADRU=__builtin_tblpage(&table);
    offset=__builtin_tbloffset(&table);
    NVMADR=(offset&0xF800);
    NVMCON=0x4003;
    __builtin_disi(6);
    __builtin_write_NVM();
}

 
#1
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2645
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/01 10:21:45 (permalink) ☄ Helpfulby rajatkalyan 2018/03/11 21:31:05
0
Don't put the address in there, that will cause major problems .
__prog__ char __attribute__((space(prog),section("Gort"),fillupper(0xa5))) Mars[]="Klattu Barada Nikto";

You have to read the page into sram that contains Mars by getting the index, modify it and then write the page back.
 
An external eeprom is a better solution.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#2
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2645
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/01 10:24:11 (permalink)
0
I've got some code that does that, it'll dig it up later.
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#3
Isaac_Sewell
Senior Member
  • Total Posts : 160
  • Reward points : 0
  • Joined: 2003/11/25 06:26:50
  • Location: Sheffield, England
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/01 12:58:06 (permalink)
0
Apologies if this is a repeated post: I replied half an hour ago and it disapeared!
I am concerned about the while(1) loop. I’m certain that you can only write to flash a few thousand times before you break it.
Cheers, Isaac
#4
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/01 13:48:06 (permalink) ☄ Helpfulby rajatkalyan 2018/03/11 21:31:18
0
Isaac_Sewell
Apologies if this is a repeated post: I replied half an hour ago and it disapeared!
I am concerned about the while(1) loop. I’m certain that you can only write to flash a few thousand times before you break it.

Yes, you have a limited number of erase/write cycles, so why do you have it in a repeating loop?
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#5
rajatkalyan
New Member
  • Total Posts : 29
  • Reward points : 0
  • Joined: 2018/01/17 21:30:45
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 03:17:09 (permalink)
0
Thanks for all the replies.
I will try again for some 10-20 values. 
Pardon me,but no one told whether my code is right or wrong!!?
Thanks :)
#6
rodims
Super Member
  • Total Posts : 1337
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 03:44:27 (permalink)
5 (1)
It's a bit of work to review the code for problems, if the problem description is so vague.
You original post contains some information, but the only relevant information for your assumed problem is the "title" = "not able to write data".
You did  not mention:
- whether your erase does what it should
- whether your read does what it should
- and the most important: how do you know that it does not write, i.e. how do you exactly test
Of course it's easier for you, if someone tells you that your code in line xyz is wrong, but it's easier to help if you describe what you have already done to narrow the problem. 
post edited by rodims - 2018/03/02 03:46:12
#7
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2645
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 08:14:32 (permalink) ☄ Helpfulby rajatkalyan 2018/03/11 21:31:38
0
Did your erase func. work?
The way to test that is to create a constant page aligned address the size of 512 instructions. (1,536 bytes inc. upper)   Fill with 0x123456.
 
Check the address is aligned by and'ing with 0x3ff, you can use that to get the offset into the page for the area you want to modify which could be, int x.
 
Erase that page, then read it back to see if it was set.
 
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#8
rajatkalyan
New Member
  • Total Posts : 29
  • Reward points : 0
  • Joined: 2018/01/17 21:30:45
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 08:27:50 (permalink)
0
Yes erase function did work,but it seems its erasing all of my programm data too.
My programm stops working too(its a CAN programm which reads from and writes to different CAN devices)
#9
rajatkalyan
New Member
  • Total Posts : 29
  • Reward points : 0
  • Joined: 2018/01/17 21:30:45
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 09:39:33 (permalink)
0
I really need your help people.
I am following this guide from microchip developer page. Kindly open the link and see the code , 
http://microchipdeveloper.com/16bit:flash-write
 
I am not able to understand few things in their code:
 
1. What is the variable &myDataInFlash? They have not declared it anywhere. Is it the address pointing to flash memory location where i want to store data. If yes,then how to declare it properly.
2. What is this RamBuffer2Write? Again,no declaration. Is it the variable which will store my data in Flash Memory.
 
Thanks to everyone who replied.
I really really need your help.
 
 
#10
rodims
Super Member
  • Total Posts : 1337
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 10:54:35 (permalink) ☼ Best Answerby rajatkalyan 2018/03/11 21:31:54
0
rajatkalyan
Yes erase function did work,but it seems its erasing all of my programm data too.
My programm stops working too(its a CAN programm which reads from and writes to different CAN devices)


I think then the erase function did NOT work.
You did not answer the other questions. If your erase function kills your program then obviously all further testing is questionable. 
 
rajatkalyan
I am following this guide from microchip developer page. Kindly open the link and see the code , 
http://microchipdeveloper.com/16bit:flash-write

 
This is an excellent guide, but you cannot rely on that it fits for your dspic33ep32gp502.
This will not be sufficient to solve your problem, I guess, you have to read more.  Not all 16-bit PICs are the same.
 
E.g. I conclude from  DS70663D-page 11
http://ww1.microchip.com/downloads/en/DeviceDoc/70663D.pdf
that your PIC only supports
NVCOM = 0x4001 -> Double-word program operation.
They do not list the NVMCON=0x4002, which you use.
 
It's still unclear what you are doing and you did not answer the questions.
#11
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2645
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 11:37:15 (permalink)
0
Align the constant to a page and set the size to a page.
That way you can do what ever you want with that section of memory and an erase will not affect your program code.
 
__prog__ char __attribute__((  space(prog), section("Gort"), fillupper(0xa5), align(1024)  )) Mars[1024]="Klattu Barada Nikto";
 
Total size is 1536 and 1024 addresses, boundry 1024.
 
It is also possible to change 1 byte but you need to save 1024 addresses or 1536 if you modify the high word, low byte.
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#12
rajatkalyan
New Member
  • Total Posts : 29
  • Reward points : 0
  • Joined: 2018/01/17 21:30:45
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 11:42:09 (permalink)
0
Thanks for reply.
Here's i have chnged code , i have written it to just erase the data present in flash memeory.
Is it right,How can i check if its erasing the data or not :(
 
__prog__ char __attribute__(( space(prog), section("Gort"), fillupper(0xa5), align(1024) )) Mars[1024]="Klattu Barada Nikto";
int main(void)
{
    int i;
    for(i=0;i<10;i++)
    {
  NVMCON=0x4003;
  
  NVMKEY=0x55;
  NVMKEY=0xAA;
  NVMCONbits.WREN=1;
  NVMCONbits.WR=1;
 while(NVMCONbits.WR==1);
    
    
    TRISBbits.TRISB13=0;
    LATBbits.LATB13=1;
    }
}

#13
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2645
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 11:44:58 (permalink)
0
You can set 24bit constants in assembly, write to the high word, low byte.
;----------------------------------------------------
KeyWords:
.pascii "PS",<0>        ;serial prescaler
.pascii "T",<0>,<0>        ;wait time between operations and E time
.pascii "SPI"            ;serial module 1-4
.pascii "SER"            ;serial module 1-4
.pascii "SDO"            ;serial date out
.pascii "SCK"            ;serial clock
.pascii "CS",<0>        ;chip select
.pascii "RS",<0>        ;register select
.pascii "RW",<0>        ;not read/write
.pascii "E",<0>,<0>        ;enable trigger
.pascii "BUS"            ;4/8bit bus
.pascii <0>
;----------------------------------------------------

 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#14
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2645
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 11:49:38 (permalink)
0
Magic words: NVMCON=0x4003;
 
#ifndef FLASH_ERASE_CODE
#define FLASH_ERASE_CODE                    0x4042
#define FLASH_ERASE_GENERAL_SEGMENT_CODE    0x404D
#define FLASH_ERASE_SECURE_SEGMENT_CODE     0x404C
#define FLASH_WRITE_ROW_CODE                0x4001
#define FLASH_WRITE_WORD_CODE               0x4003
#endif
//----------------------------------------------------

For the FJ but get the values for your chip and set the macros.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#15
rajatkalyan
New Member
  • Total Posts : 29
  • Reward points : 0
  • Joined: 2018/01/17 21:30:45
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 11:49:54 (permalink)
0
The code which i have written ,will that do the erase memeory job for me?
Thanks for bearing with my idiotic questions, I am noobie into this field. 
Pardon me ,if i am bugging you too much.
thanks for every bit of help
#16
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2645
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 12:01:56 (permalink)

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#17
rodims
Super Member
  • Total Posts : 1337
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/02 12:54:36 (permalink)
0
rajatkalyan
Thanks for reply.
Here's i have changed code , i have written it to just erase the data present in flash memeory.
Is it right,How can i check if its erasing the data or not :(
 
__prog__ char __attribute__(( space(prog), section("Gort"), fillupper(0xa5), align(1024) )) Mars[1024]="Klattu Barada Nikto";
int main(void)
{
  int i;
  for(i=0;i<10;i++)
    {
    NVMCON=0x4003;
  
    NVMKEY=0x55;
    NVMKEY=0xAA;
    NVMCONbits.WREN=1;
    NVMCONbits.WR=1;
    while(NVMCONbits.WR==1);
   
    TRISBbits.TRISB13=0;
    LATBbits.LATB13=1;
    }
}



Sorry, I will not manage to guide you, at least not before next week; just a few comments. 
So, - No, the code is not sufficient for erase, may be your first version for erase was better.
Your "for 10x loop" does not make much sense, since you don't vary anything inside. Also you don't specify an address for the page(s) to erase.
 
On the other hand it looks as if you are simply exiting your main function, or you did not show all the code.
You must not exit your main function, put a while(1);  loop at the end, if main would otherwise return.
 
You do not specify WHERE (which page) to erase, so it might erase something, somewhere.
Erasing means that flash memory locations are set to 0xff.  If nothing was in there before, you cannot verify whether it has been erased, because it would not change. 

If the answers so far do not help, try googling within the forum. E.g.
site:www.microchip.com\forums flash erase 4003
This restricts the search to the microchip forum and then you specify your keywords as usual.
 
Remember that the  NVMCON keywords like 4003 or 4001 are different for various 16 bit PICs. You have to look them up for your PIC, I posted the reference already. (so 4003 indeed is erase for your pic)
 
#18
rajatkalyan
New Member
  • Total Posts : 29
  • Reward points : 0
  • Joined: 2018/01/17 21:30:45
  • Location: 0
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/11 21:30:51 (permalink)
4 (2)
Thanks everyone for sharing the knowledge.
I have solved the issue of erasing,writing and reading from the flash memory.
 
Regards
Rajat Kalyan
#19
Aussie Susan
Super Member
  • Total Posts : 3306
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: Not able to write data to the flash memory location in dspic33ep32gp502 2018/03/12 18:34:41 (permalink)
0
For the sake of others who may be having the same issue, can you please tell us what was wrong, how you fund it an how you fixed it?
Susan
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2018 APG vNext Commercial Version 4.5