EEPROM - Read/Write

Discuss the Firewing development environment

EEPROM - Read/Write

Postby skartalov » Fri Dec 19, 2014 7:56 am

I am using a device with EEPROM -> 24FV16KM102

How to read and write in it?
skartalov
 
Posts: 69
Joined: Sun Sep 15, 2013 1:12 pm

Re: EEPROM - Read/Write

Postby Coccoliso » Fri Dec 19, 2014 11:46 am

Hello,
try to have a look here http://www.microchip.com/forums/m592252.aspx
the start address of the EEPROM is this &H7FFE00 as it says in the article.
User avatar
Coccoliso
 
Posts: 178
Joined: Sat Sep 27, 2014 10:02 am

Re: EEPROM - Read/Write

Postby Jerry Messina » Fri Dec 19, 2014 2:46 pm

I've been playing around with accessing the __builtin functions of xc16.
Maybe this helps some? These are all pretty much untested (except to compile)

file: builtins.bas
Code: Select all
// builtins.bas
// fw interface to XC16 __builtin_xxxxx functions
// v1.00

// examples of accessing the XC16 _builtin functions
module builtins

imports "builtins.c"

public external sub _builtin_write_OSCCONL(value as byte) "fw_builtin_write_OSCCONL"
public external sub _builtin_write_OSCCONH(value as byte) "fw_builtin_write_OSCCONH"

public external function _builtin_tblrdh(offset as ushort) as ushort "fw_builtin_tblrdh"
public external function _builtin_tblrdl(offset as ushort) as ushort "fw_builtin_tblrdl"
public external sub _builtin_tblwth(offset as ushort, data as ushort) "fw_builtin_tblwth"
public external sub _builtin_tblwtl(offset as ushort, data as ushort) "fw_builtin_tblwtl"

public external sub _builtin_write_NVM() "fw_builtin_write_NVM"

end module


file: builtins.c
Code: Select all
//
// builtins.c
// fw interface to XC16 __builtin_xxxxx functions (and builtins.bas)
// (see Appendix G in the MPLAB XC16 C Compilers Users Guide)
// v1.00
//

/*
//-------------------------------------------------------
__builtin_write_OSCCONL
Description:
    Unlocks and writes its argument to OSCCONL.
    Interrupts may need to be disable for proper operation.
    This builtin function can be used as a part of a complex sequence discussed in the data
    sheet or family reference manual.
    See this documentation for more information.
Prototype:
    void __builtin_write_OSCCONL(unsigned char value);
Argument:
    value character to be written
Return Value:
    None.
Assembler Operator/Machine Instruction:
    mov #0x46, w0
    mov #0x57, w1
    mov #_OSCCON, w2
    mov.b w0, [w2]
    mov.b w1, [w2]
    mov.b value, [w2]
Error Messages None.
//-------------------------------------------------------
*/
void fw_builtin_write_OSCCONL(unsigned char value)
{
    __builtin_write_OSCCONL(value);
}


/*
//-------------------------------------------------------
__builtin_write_OSCCONH
Description:
    Unlocks and writes its argument to OSCCONH.
    Interrupts may need to be disable for proper operation.
    This builtin function can be used as a part of a complex sequence discussed in the data
    sheet or family reference manual.
    See this documentation for more information.
Prototype:
    void __builtin_write_OSCCONH(unsigned char value);
Argument:
    value character to be written
Return Value:
    None.
Assembler Operator/ Machine Instruction:
    mov #0x78, w0
    mov #0x9A, w1
    mov #_OSCCON+1, w2
    mov.b w0, [w2]
    mov.b w1, [w2]
    mov.b value, [w2]
Error Messages None.
//-------------------------------------------------------
*/
void fw_builtin_write_OSCCONH(unsigned char value)
{
    __builtin_write_OSCCONH(value);
}

/*
//-------------------------------------------------------
__builtin_tblrdh
Description:
    Issues the tblrdh.w instruction to read a word from Flash or EEDATA memory. You must
    set up the TBLPAG to point to the appropriate page. To do this, you may make use of
    __builtin_tbloffset() and __builtin_tblpage().
    Please refer to the data sheet or dsPIC Family Reference Manual for complete details
    regarding reading and writing program Flash.
Prototype:
    unsigned int __builtin_tblrdh(unsigned int offset);
Argument:
    offset desired memory offset
Return Value:
    Contents of the memory address in Flash or EEDATA memory.
Assembler Operator/ Machine Instruction:
    tblrdh
Error Messages None.
//-------------------------------------------------------
*/
unsigned int fw_builtin_tblrdh(unsigned int offset)
{
    return __builtin_tblrdh(offset);
}

/*
//-------------------------------------------------------
__builtin_tblrdl
Description:
    Issues the tblrdl.w instruction to read a word from Flash or EEDATA memory. You must
    set up the TBLPAG to point to the appropriate page. To do this, you may make use of
    __builtin_tbloffset() and__builtin_tblpage().
    Please refer to the data sheet or “dsPIC30F Family Reference Manual” (DS70046) for com-
    plete details regarding reading and writing program Flash.
Prototype:
    unsigned int __builtin_tblrdl(unsigned int offset);
Argument:
    offset desired memory offset
Return Value:
    Contents of the memory address in Flash or EEDATA memory.
Assembler Operator/ Machine Instruction:
    tblrdl
Error Messages None.
//-------------------------------------------------------
*/
unsigned int fw_builtin_tblrdl(unsigned int offset)
{
    return __builtin_tblrdl(offset);
}

/*
//-------------------------------------------------------
__builtin_tblwth
Description:
    Issues the tblwth.w instruction to write a word to Flash or EEDATA memory. You must set
    up the TBLPAG to point to the appropriate page. To do this, you may make use of
    __builtin_tbloffset() and __builtin_tblpage().
    Please refer to the data sheet or “dsPIC30F Family Reference Manual” (DS70046) for com-
    plete details regarding reading and writing program Flash.
Prototype:
    void __builtin_tblwth(unsigned int offset, unsigned int data);
Argument:
    offset desired memory offset
    data data to be written
Return Value:
    None.
Assembler Operator/ Machine Instruction:
    tblwth
Error Messages None.
//-------------------------------------------------------
*/
void fw_builtin_tblwth(unsigned int offset, unsigned int data)
{
    __builtin_tblwth(offset, data);
}

/*
//-------------------------------------------------------
__builtin_tblwtl
Description:
    Issues the tblrdl.w instruction to write a word to Flash or EEDATA memory. You must set
    up the TBLPAG to point to the appropriate page. To do this, you may make use of
    __builtin_tbloffset() and __builtin_tblpage().
    Please refer to the data sheet or “dsPIC30F Family Reference Manual” (DS70046) for com-
    plete details regarding reading and writing program Flash.
Prototype:
    void __builtin_tblwtl(unsigned int offset, unsigned int data);
Argument:
    offset desired memory offset
    data data to be written
Return Value:
    None.
Assembler Operator/ Machine Instruction:
    tblwtl
Error Messages None.
//-------------------------------------------------------
*/
void fw_builtin_tblwtl(unsigned int offset, unsigned int data)
{
    __builtin_tblwtl(offset, data);
}

/*
//-------------------------------------------------------
__builtin_write_NVM
Description:
    Enables the Flash for writing by issuing the correct unlock sequence and enabling the Write
    bit of the NVMCON register.
    Interrupts may need to be disable for proper operation.
    This builtin function can be used as a part of a complex sequence discussed in the data
    sheet or family reference manual.
    See this documentation for more information.
Prototype:
    void __builtin_write_NVM(void);
Argument:
    None.
Return Value:
    None.
Assembler Operator/ Machine Instruction:
    mov #0x55, Wn
    mov Wn, _NVMKEY
    mov #0xAA, Wn
    mov Wn, _NVMKEY
    bset _NVMCON, #15
    nop
    nop
Error Messages None.
//-------------------------------------------------------
*/
void fw_builtin_write_NVM(void)
{
    __builtin_write_NVM();
}


examples
Code: Select all
imports builtins

dim offs as ushort
dim data as ushort

Sub Main()

    offs = _builtin_tblrdh(offs)
    offs = _builtin_tblrdl(offs)
    _builtin_tblwth(offs, data)
    _builtin_tblwtl(offs, data)
   
   _builtin_write_NVM()

   // from XC pps.h
   // #define  PPSUnLock  __builtin_write_OSCCONL(OSCCON & 0xbf)
   // unlock the Pin assignment registers
   _builtin_write_OSCCONL(OSCCON and $BF)        // to clear IOLOCK

   // #define  PPSLock   __builtin_write_OSCCONL(OSCCON | 0x40)
   // Relock the Pin assignment registers
   _builtin_write_OSCCONL(OSCCON or $40)        // to set IOLOCK
 
End Sub
Attachments
builtins.zip
(1.59 KiB) Downloaded 1551 times
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: EEPROM - Read/Write

Postby Coccoliso » Fri Dec 19, 2014 6:13 pm

Hello Jerry,
You have divination skills :o !
I just encountered a problem on __builtin that I was not able to solve .. you have made a contribution and a valuable hint with External and example posted.
The call that I was not able to solve is this:

Code: Select all
__builtin_dmaoffset(_ram_protect_a)


.. and now I'm much closer to the solution :D .
I just have to find where this damn __builtin_dmaoffset.
User avatar
Coccoliso
 
Posts: 178
Joined: Sat Sep 27, 2014 10:02 am

Re: EEPROM - Read/Write

Postby Jerry Messina » Fri Dec 19, 2014 7:56 pm

This is getting a little off-topic, but
Code: Select all
__builtin_dmaoffset(_ram_protect_a)

probably won't work, and I don't think it's really needed anyway.

In XC16, you reserve a block of memory for DMA use by doing something like
Code: Select all
char buffer[256] __attribute__((space(dma)));

Since it's in a different space, you need to use "__builtin_dmaoffset(&buffer)" to get the offset address of 'buffer' to use with the DMA peripheral.

In FW, you reserve a block using "_ram_protect_a". It doesn't get a special attribute that I'm aware of. I haven't tried this yet, but I would think you should just be able to use the address of the object you've located @ _ram_protect_a for the DMA address.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: EEPROM - Read/Write

Postby skartalov » Sat Dec 20, 2014 6:52 am

So you are saying that EEPROM read/write is not implemented in FIREWING, so I have to figure it out by myself how to do that?

Why is the word "EEPROM" reserved?
How is it used? What syntax? When? Where?

In my case I need just to store few bytes in EEPROM, is that have to be that hard??
skartalov
 
Posts: 69
Joined: Sun Sep 15, 2013 1:12 pm

Re: EEPROM - Read/Write

Postby Coccoliso » Sat Dec 20, 2014 9:33 am

Hello Skartalov,
I try to give an explanation of what I understand ..
FW PIC24 is builds around a PIC24HJ128GP502 and how much of MPU of this family does not have the EEPROM so if you use a different MPU must write the module it takes.
With FW R2 hardware the workaround for a pseudo EEPROM is the use of the module ROM.BAS that allows you to write directly into MPU allocating space in the programming area.
This module is used to store the calibration of the FT800 and then you could have a look .. if there are few bytes then maybe you better waiting for a "clean thing"

In case of FW PIC18 exists the possibility of using EEPROM since almost all MPU have the EEPROM and also the 18F25K22 have it therefore the keyword is used:

Code: Select all
Const EEAddr as Byte = &H0
EEPROM(EEAddr)    = {15 As Byte} 
...
Dim bValue as Byte = Eprom.ReadByte(EEAddr)
...
Eprom.Write(EEAddr,bValue)
User avatar
Coccoliso
 
Posts: 178
Joined: Sat Sep 27, 2014 10:02 am

Re: EEPROM - Read/Write

Postby skartalov » Sat Dec 20, 2014 10:43 am

I have used the ROM elsewhere
but for this device now, the ROM routines doesn't work.
I have no idea why. I cannot compile.
skartalov
 
Posts: 69
Joined: Sun Sep 15, 2013 1:12 pm

Re: EEPROM - Read/Write

Postby Jerry Messina » Sat Dec 20, 2014 12:16 pm

the ROM routines doesn't work

That's because ROM.bas doesn't support the 24FV16KM family since it's not a standard Firewing device.

You would have to change the #options to match the 24FV
Code: Select all
// 1-row (96 bytes), 2-row (192 bytes) and 4-row (384 bytes) erase blocks
// selection must match NVMCON setting
#option ROM_BLOCK_ERASE = 384   ' was 1536
#option ROM_BLOCK_WRITE = 96     ' was 192


But you'd also have to change the WriteBuffer() routine since the NVMCON register is different for that chip.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am


Return to Development Environment

Who is online

Users browsing this forum: No registered users and 2 guests

x