• AVR Freaks

Helpful ReplyHot!PIC32MK EEPROM write operation

Author
p_stoyanoff
Senior Member
  • Total Posts : 146
  • Reward points : 0
  • Joined: 2011/06/30 00:23:55
  • Location: 0
  • Status: offline
2018/07/13 10:03:33 (permalink)
0

PIC32MK EEPROM write operation

Hi! It seems the problem with UART ports(the last theme) is caused by Harmony library. So I want to remove it, but still I have to access the PIC32MK1024GPD064`s EEPROM. Using the Harmony library EEPROM read and write operations are working just fine. I`m trying to write similar functions. The read operation is working fine, but I can`t write to the eerpom. 
Here is my code:

#define EEPROM_NOT_READY 8
 
unsigned int data_EEPROM_write(unsigned int ee_addr, uint32_t ee_data)
{
if (EECONbits.RDY==1) // If Data EEPROM to be ready
{
if (EECONbits.RW==0) // If no operation underway
{ // Execute Write Command

EEADDR = ee_addr<<2;
EEDATA = ee_data;
EECONSET = _EECON_WREN_MASK;
EECONCLR = _EECON_CMD_MASK;
EECONSET=_EECON_WREN_MASK;
EEKEY = 0xED87; // Write unlock sequence
EEKEY = 0x1248;
EECONbits.RW = 1; // Start the write cycle
while (EECONbits.RW==1);
return EECONbits.ERR;
}
}
return EEPROM_NOT_READY;
}

I`m executing this code using Harmony EEPROM_init function and using my init function(based on Harmony). And nothing!
I`ve noticed in the Harmony lib write function has been executed 4 times(extra 3 with 0s). I`ve tried the same - no luck.
Is there anything else? What am I missing?!
Thanks!
 
#1
cvm
Super Member
  • Total Posts : 264
  • Reward points : 0
  • Joined: 2011/09/16 05:16:15
  • Location: 0
  • Status: offline
Re: PIC32MK EEPROM write operation 2018/07/13 15:22:38 (permalink)
0
EEADDR = ee_addr<<2;
&~3 would make more sense
I would assume you are trying to align on 32bits


#2
p_stoyanoff
Senior Member
  • Total Posts : 146
  • Reward points : 0
  • Joined: 2011/06/30 00:23:55
  • Location: 0
  • Status: offline
Re: PIC32MK EEPROM write operation 2018/07/14 00:51:12 (permalink)
0
This doesn`t work, too!
#3
cvm
Super Member
  • Total Posts : 264
  • Reward points : 0
  • Joined: 2011/09/16 05:16:15
  • Location: 0
  • Status: offline
Re: PIC32MK EEPROM write operation 2018/07/14 09:03:25 (permalink)
4 (1)
I'm not assuming that's the whole problem, I'm just pointing out you are probably setting EEADDR to something you do not want as you are multiplying the incoming address by 4. Either you do want to multiply by 4 for some reason, or you more than likely intended to align on 32bits but used the wrong method to do it. One or the other, with completely different results. Which is it?
 
step 2.
 
EEKEY = 0xED87; // Write unlock sequence
should be 0xEDB7 ?
 
datasheet register description shows EDB7
example code shows ED87- http://ww1.microchip.com/...eviceDoc/60001326A.pdf
(and it looks like you are somewhat following the example, which is why its a little strange you were using ee_addr<<2 - if you use the examples, why not copy verbatim?)

which makes more sense for unlock keys?
EDB7 = 1110 1101 1011 0111
1248 = 0001 0010 0100 1000

ED87 = 1110 1101 1000 0111
1248 = 0001 0010 0100 1000

my money is on EDB7 (example code writers didn't have their glasses on it seems)
#4
Mysil
Super Member
  • Total Posts : 3321
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: PIC32MK EEPROM write operation 2018/07/14 16:51:15 (permalink)
0
Hi,
In addition to confusion about Unlock code that in FRM manual section 58,
documentation of EEKEY register, is different from value used in some examples, as explained by cvm.
 
There is also a mistake in code shown in message #1,
that  WREN bit is given the same value two times,
and CMD field is Cleared,
but Not given a value to specify that Write operation is to be performed.
You may try:
    ...
        EECONCLR = _EECON_CMD_MASK; /* Clear CMD field  */
        EECONSET = 1;                /* Write word command value. */
    ...  

 
Shifting the address argument 2 bits left,
will convert a 32 bit word array index into a aligned byte array index, so is a valid translation,
and since interpretation of address argument is not documented neither in FRM example code,
nor in this thread, both alternatives are valid C code.
 
   Mysil
#5
rutiger15
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2013/07/06 06:40:00
  • Location: 0
  • Status: offline
Re: PIC32MK EEPROM write operation 2018/08/12 14:48:16 (permalink) ☄ Helpfulby Wituch 2019/04/26 13:56:18
4.33 (3)
After looking at the forums for a while I figured out why everyone is having problems with the EEPROM module. The first issue is the correct key values are 0xEDB7 and 0x1248. Not 0xED87 like in several examples. The second issue is ECONbits.RW = 1; does not work. You must use EECONSET = _EECON_RW_MASK;
 
 
 
#define EEKEY1 0xEDB7
#define EEKEY2 0x1248

void EEInitialize(void) // Basic EEPROM enable and initialization
{
    __builtin_disable_interrupts();
    
    CFGCON2bits.EEWS = 7;// 8 = for system clocks < 75MHz
    
    EECONbits.ON = 1;

    while (EECONbits.RDY == 0);// Wait until EEPROM is ready (~125 us)

    EECONbits.WREN = 1;// Enable writing to the EEPROM
    EECONbits.CMD = 0b100;// Set the command to Configuration Write

    EEADDR = 0x00;// Addr 0x00 = DEVEE1;
    EEDATA = DEVEE0;
    EEKEY = EEKEY1; // Unlock the EEPROM to enable writing
    EEKEY = EEKEY2;
    EECONSET = _EECON_RW_MASK;
    while (EECONbits.RW); // desired

    EEADDR = 0x04;// Addr 0x04 = DEVEE2;
    EEDATA = DEVEE1;
    EEKEY = EEKEY1; // Unlock the EEPROM to enable writing
    EEKEY = EEKEY2;
    EECONSET = _EECON_RW_MASK;
    while (EECONbits.RW); // desired

    EEADDR = 0x08;// Addr 0x08 = DEVEE3;
    EEDATA = DEVEE2;
    EEKEY = EEKEY1; // Unlock the EEPROM to enable writing
    EEKEY = EEKEY2;
    EECONSET = _EECON_RW_MASK;
    while (EECONbits.RW); // desired

    EEADDR = 0x0C;// Addr 0x08 = DEVEE3;
    EEDATA = DEVEE3;
    EEKEY = EEKEY1; // Unlock the EEPROM to enable writing
    EEKEY = EEKEY2;
    EECONSET = _EECON_RW_MASK;
    while (EECONbits.RW); // desired

    EECONbits.WREN = 0; // Turn off writes.
    
    __builtin_enable_interrupts();
    
}

uint32_t data_EEPROM_read(uint32_t ee_addr)
{
    uint32_t Data;
    
    EEADDR = ee_addr & 0xFFC; // Set address on 32-bit boundary
    EECONbits.CMD = 0; // Load CMD<2:0> with
    // Data EEPROM read command
    EECONbits.WREN = 0; // Access for read

    __builtin_disable_interrupts();
    EEKEY = EEKEY1; // Write unlock sequence
    EEKEY = EEKEY2;
    EECONSET = _EECON_RW_MASK; // Start the operation
    __builtin_enable_interrupts();

    while (EECONbits.RW==1); // Wait until read is complete
    Data = EEDATA; // Read the data
    
    return Data;
}


void data_EEPROM_write(uint32_t ee_addr, uint32_t ee_data)
{
    EECONbits.CMD = 1; // Load CMD<2:0> with write command
    EECONbits.WREN = 1; // Access for write

    EEADDR = ee_addr & 0xFFC; // Load address on a 32-bit boundary
    EEDATA = ee_data;

    __builtin_disable_interrupts();
    EEKEY = EEKEY1; // Write unlock sequence
    EEKEY = EEKEY2;
    EECONSET = _EECON_RW_MASK;
    __builtin_enable_interrupts();

    while (EECONbits.RW == 1);
}

 
I'm still trying to get the interrupt to work since it is not documented anywhere and even when I clear the flag it is still set and triggering the interrupt.
#6
Jump to:
© 2019 APG vNext Commercial Version 4.5