• AVR Freaks

Helpful ReplyWrite and Read Flash

Author
Jair.Hernandez
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/09/28 11:15:11
  • Location: 0
  • Status: offline
2017/09/28 14:54:39 (permalink)
0

Write and Read Flash

Hello all,
I'm new in MPLAB and I'm trying to write in the flash memory of the pic32mx270f256b. I'm following the steps that provided in the section 5 from the PIC32 FRM. My problem is that I don't know if I'm not writing well or I'm not reading well, when I read what is in the address I read 0xFF and the returns of erase and write in the flash are 0's that means no errors in the WRERR and LVDERR registers. I'm using MPLABX v4.0.0 and XC32 (I don't use harmony or MCC). I post my code and the rest of the files are attached
 
#include "configuracion/configurationBits.h"
#include "configuracion/defines.h"
#include "configuracion/inicializacion.h"
#include "UART/UART1.h"
#include "configuracion/delay.h"
#include <xc.h>
#include <p32xxxx.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/attribs.h>

#define ADDRESS 0x9D03F000

int cuenta = 0;
unsigned int receive_memory;
unsigned char test1 = 60;
int res1;
int res2;

unsigned int NVMUnlock(unsigned int nvmop);
unsigned int NVMWriteWord(void* address, unsigned int data);
unsigned int NVMWriteRow(void* address, void* data);
unsigned int NVMErasePage(void* address);

void __ISR(_TIMER_1_VECTOR, IPL7AUTO) Timer1(void)
{
   cuenta++;
   if (cuenta >= 250) {
      LED_VERDE_INV;
      cuenta = 0;
   }
      IFS0bits.T1IF = 0;
}

void main()
{
    InitMCU();
    Init_Interrupt();
    InitUART1(115200);
    InitDelay();
 
    //NVMErasePage((void *)ADDRESS);
    //NVMWriteWord((void *)ADDRESS, test1);
    //asm("di");
    res1 = NVMErasePage((void *)0x9D03F000);
    res2 = NVMWriteWord((void *)0x9D03F000, 0x33);
    //asm("ei");
 
    Uart1EnviarByte(res1);
    Uart1EnviarByte(res2);
 
    while (1) {
        receive_memory = *(unsigned int *)(ADDRESS);
        if (receive_memory == 0x33) {
           //LED_ROJO_INV;
           Uart1EnviarByte(receive_memory);
           delay_ms(500);
        } else {
           LED_ROJO_INV;
           Uart1EnviarByte(receive_memory);
          delay_ms(500);
        }
  
    }
}

unsigned int NVMUnlock(unsigned int nvmop)
{
    unsigned int status;
    asm volatile ("di %0" : "=r" (status));
    NVMCON = (nvmop | 0x4000);
    NVMKEY = 0x00000000;
    NVMKEY = 0xAA996655;
    NVMKEY = 0x556699AA;
    NVMCONSET = 0x8000;
    while (NVMCON & 0x8000);
    if (status & 0x00000001)
        asm volatile ("ei");
    else
        asm volatile ("di");
    NVMCONCLR = 0x0004000;
    return (NVMCON & 0x3000);
}

unsigned int NVMWriteWord(void* address, unsigned int data)
{
    unsigned int res;
    NVMDATA = data;
    NVMADDR = (unsigned int)address;
    res = NVMUnlock(0x4001);
    return res;
}

unsigned int NVMWriteRow(void* address, void* data)
{
    unsigned int res;
    NVMADDR = (unsigned int)address;
    NVMSRCADDR = (unsigned int)data;
    res = NVMUnlock(0x4003);
    return res;
}

unsigned int NVMErasePage(void* address)
{
    unsigned int res;
    NVMADDR = (unsigned int)address;
    res = NVMUnlock(0x4004);
    return res;
}

#1
Larry.Standage
Super Member
  • Total Posts : 927
  • Reward points : 0
  • Joined: 2011/12/30 09:50:47
  • Location: 0
  • Status: offline
Re: Write and Read Flash 2017/09/28 15:58:00 (permalink)
4.33 (3)
Make sure that you use physical addresses when telling the Flash controller what page to erase, the word to write, etc.
 
So where you have
NVMADDR = (unsigned int)address;

change them to
#include <sys/kmem.h>
...
NVMADDR = KVA_TO_PA((unsigned int)address);

#2
Jair.Hernandez
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/09/28 11:15:11
  • Location: 0
  • Status: offline
Re: Write and Read Flash 2017/09/29 07:00:15 (permalink)
0
Thanks Larry for the help. I tried the changes you wrote, but the program seems to stop running. I don´t watch anything trough the serial port and the leds aren't bliking. Also I tried to see when the program stops turning on a led. The program stops after asm("ei").
 
The part where the program stops

 
void main()
{
    ...
    asm volatile ("di");
    res1 = NVMErasePage((void *)0x9D03F000);
    res2 = NVMWriteWord((void *)0x9D03F000, 0x33);
    asm volatile ("ei");
    LED_ROJO = 1;
    ...
 

 
the changes I made. I added the "KVA_TO_PA" in all the functions where I use NVAMADDR also I added the include you told me.

 
unsigned int NVMWriteWord(void* address, unsigned int data)
{
    unsigned int res;
    NVMDATA = data; 
    NVMADDR = KVA_TO_PA((unsigned int)address);
    res = NVMUnlock(0x4001); 
    return res;
}
 

 
 
#3
Jair.Hernandez
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/09/28 11:15:11
  • Location: 0
  • Status: offline
Re: Write and Read Flash 2017/09/29 12:32:34 (permalink)
3 (1)
After a few tries I change the direction where I write the data in the flash and I could write into the flash but only 1 byte also I coud read it. Now I try to write an row but I got some unexpected results.
 
My code to write a Row

unsigned int ADDRESS = 0x9D03D000;
unsigned char test2[] = "AAABBBCCCDDDEEEFFFGGGHHHJJJIIILLLKKKMMMOOONNNPPPQQQRRRSSSTTTUUUVVVXXXYYYZZZ111333222444555777666888000999ILLLK880009966613366613";
unsigned int receive_memory;
unsigned char recibir[128];
unsigned char j = 0;
void main()
{
    InitMCU();
    Init_Interrupt();
    InitUART1(115200);
    InitDelay();

    NVMErasePage((void *)ADDRESS);
    NVMWriteRow((void *)ADDRESS, (void *)&test2);

    while (1) {
        for(j = 0; j < 128; j++){
            receive_memory = *((unsigned int *)(ADDRESS + j * 4));
            recibir[j] = receive_memory;
        }
        Uart1EnviarTexto(recibir);
        LED_ROJO_INV;
        delay_ms(500);
    }
}

 
What a I read in the serial port is 0xFF 128 times and the string I write in the flash. In the image can be seen what I read in the serial port.

Attached Image(s)

#4
yerpa58
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2005/04/09 09:19:47
  • Location: rpl@reprolabs.com
  • Status: offline
Re: Write and Read Flash 2017/10/03 08:55:42 (permalink)
0
I found it helpful to use Microchip's IPE software tool to view the contents of Flash memory by doing a Read via PICKit3.  IPE can co-exist with MPLAB-X and PICKit3. My first test code would erase one page (4K bytes for PIC32MX350F256H) of Flash memory, then write a simple text string to Flash. I would then read back the entire Flash in IPE and look for the string.  That helped me find out what was actually going on.  I learned a lot about page address requirements this way!
#5
andersm
Super Member
  • Total Posts : 2672
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: offline
Re: Write and Read Flash 2017/10/03 11:25:46 (permalink)
0
Jair.Hernandez
        for(j = 0; j < 128; j++){
            receive_memory = *((unsigned int *)(ADDRESS + j * 4));
            recibir[j] = receive_memory;
        }

This code doesn't look valid at all. Is ADDRESS supposed to point to an array of bytes, or an array of ints?
#6
Hugo Sousa
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2018/10/14 16:23:33
  • Location: 0
  • Status: offline
Re: Write and Read Flash 2018/10/18 04:44:30 (permalink)
0
Hi, i´m also trying to read from flash but setting a pointer to the desired address doesn´t work. I´m working with a pic32mx570f512land using the xc32 compiler.
Here is my code:
 
void main(void)
{

// initialize the device
SYSTEM_Initialize();

// generate data
uint32_t data[128];
int i;

for(i = 0; i < 128; i++)
{
data = i;
}

uint32_t * address = (uint32_t *) 0x1D001000; // define address

uint32_t erro;
erro = NVM_ErasePage( (void*) address);                 // erase a page
erro = NVM_WriteRow( (void*) address, &data );       // write in the address

uint32_t  myvar;
myvar = *(uint32_t *)(0x1D001000);        // read from flash

 
Any idea why is not working?
#7
andersm
Super Member
  • Total Posts : 2672
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: offline
Re: Write and Read Flash 2018/10/18 05:16:48 (permalink) ☄ Helpfulby Jim Nickerson 2018/10/18 05:55:06
3 (2)
Please don't resurrect old threads. Also use code tags when posting code, or it will get completely messed up as seen above.
#8
Jump to:
© 2019 APG vNext Commercial Version 4.5