• AVR Freaks

Hot!I2C EEPROM Lib in MCC

Author
Wesley_88
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2018/11/26 17:19:06
  • Location: 0
  • Status: offline
2019/10/30 09:49:29 (permalink)
0

I2C EEPROM Lib in MCC

I have difficulty using the I2C EEPROM code generate by MCC
All I am trying to do is using PIC18F47K42 I2C2 to write a byte of data on to an external EEPROM 
 
The code that MCC generate :
I2Cx_MasterWrite(I2C_EEPROM_T * eepromInstance, uint8_t *data, uint8_t size, uint8_t busAddress, ERR_STATUS_T *pstatus)
 
Anybody know what is the "I2C_EEPROM_T * eepromInstance" and "ERR_STATUS_T *pstatus" mean or what should these 2 be replace with ?
 
when the MCC generated the code I immediately found 2 error in both the i2c_eeprom_app.h and i2c_eeprom_app.c

Attached Image(s)

#1

16 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3225
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/10/30 13:03:01 (permalink)
    5 (1)

    ... what is the ...

    Risk an eye on the headers ! (Especially I2Cx.h) Should be understandable. (e.g. *pstatus is the pointer to a status enum)
     
    Re i2c_eeprom_app.*
    Is this the eeprom driver that has been added quite recently?
    The I2C read/write calls look somewhat "weird". (At least the sequence of arguments differs from the read/write function I got recently on a dsPIC32EV. And I assume these to be constant on all devices.)
    So start with checking the I2C read/write sequences of i2c_eeprom* vs. the implementations in i2cx.*

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    Wesley_88
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2018/11/26 17:19:06
    • Location: 0
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/10/30 14:20:52 (permalink)
    0
    Yes This is the newly added EEPROM driver to MCC
    see attachment for the eeprom driver header and c file 
    my I2C master is PIC18F47K42 and my I2C slave EEPROM is 24LC024H 
    #3
    Wesley_88
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2018/11/26 17:19:06
    • Location: 0
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/10/30 14:27:53 (permalink)
    0
    The control code for the I2C EEPROM = '1010'
    The chip select bit for the I2C EEPROM A2,A1,A0 = '110'
    The EEPROM register address = '0x00'
    The data = '0x31'
     
    So given the MCC generated code from my post above:
    I2Cx_MasterWrite(I2C_EEPROM_T * eepromInstance, uint8_t *data, uint8_t size, uint8_t busAddress, ERR_STATUS_T *pstatus);
     
    What I am still trying to figure out is what goes on to the BOLD portion of the code below:
    I2Cx_MasterWrite(I2C_EEPROM_T * eepromInstance, 0x31, 1, 0x00, ERR_STATUS_T *pstatus);
    #4
    ric
    Super Member
    • Total Posts : 24540
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/10/30 14:32:01 (permalink)
    5 (1)
    It's passing pointers to structures that the call uses to maintain the state of the I2C engine, and return the status.
    Presumably this is to allow it to be used as part of a state machine.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    du00000001
    Just Some Member
    • Total Posts : 3225
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/10/30 14:54:42 (permalink)
    0
    From my dsPIC33EV experience:
    the I2C drivers are next to magic (although I exprienced some issue I circumvented). But the calls to the I2Cx (from i2c_eeprom_app) do not seem to be compatible with the functions implemented in I2Cx.c.
     
    The whole I2C communication is realized in interrupt mode. Thus the status pointer is the means to report the progress of operation back to the application program. Quite capable.
    But you have to ensure this status variable is persistent for the whole duration of the I2C transfer! And be aware that the status enum is volatile in terms of the XC compilers! (The other data is buffered in structures within the functions called.)

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #6
    Mysil
    Super Member
    • Total Posts : 3464
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/10/30 16:46:31 (permalink)
    0
    Hi,
    In PIC18FxxK42 and PIC18FxxK83 devices, there is a new I2C peripheral that is entirely different from any previous PIC16  or PIC18 devices. 
     
    I am not at all convinced that there is a I2C driver in MCC for these devices,
    that provide the API interface used by the  EEPROM library code.
     
    I have a PIC18F46K42 on a development board, so may do some experiments,
    but it will take a day or two.
     
    On the product homepage for PIC18F47K42 there are links to a couple of application notes with description and example code for the new peripheral in Master and Slave mode.
     
        Mysil
     
     
    #7
    Mysil
    Super Member
    • Total Posts : 3464
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/10/31 08:53:22 (permalink)
    5 (1)
    Hi,
    Are you posting duplicate threads about the same subject ?
    https://www.microchip.com/forums/FindPost/1117073
     
    There is some example code in this thread,
    https://www.microchip.com/forums/FindPost/1094883
    It is based on the code published and described in TB3191 by Chris Best
    https://www.microchip.com/wwwAppNotes/AppNotes.aspx?appnote=en605150
    with corrections and updates to work with MPLAB X v5.15 and XC8 v2.05 and using vectored interrupts.
     
    The I2C peripheral in PIC18...K42 microcontrollers, is a complicated peripheral with a lot of registers and options.
    It seem to be designed to be used with DMA and Interrupts, but this will require careful software support.
     
    Demo code is running Master code and Slave code by connecting 2  separate I2C peripherals in the same chip.
    This may be useful in a demo or development case.
    Document claim to demonstrate a simulate a I2C EEPROM device, 
    but the code have several discrepancies from a hardware I2C EEPROM.
    There is only 1 EEPROM address byte, while a real memory device may typically have 2 address bytes.
    Demo code run in a loop, repeatedly writing and reading the same addresses.
    This is a bad use if a real EEPROM device is used.
    Also page boundaries are not observed, this is a task for the EEPROM library.
     
    If you are doing only small writes to the 128 byte device,
    then you may well use the master driver library code directly.
     
    There seem to be a new version of MCC PIC16, PIC18 device libraries beeing released,
    I have not tried it.
     
        Mysil
    #8
    Wesley_88
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2018/11/26 17:19:06
    • Location: 0
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/10/31 12:10:02 (permalink)
    0
    Initially I tried several attempt by using the typical I2C2 code that was generated by MCC for PIC18F47K42 to write to an external I2C EEPROM 24LC024H with the code below but no success
     
    i2c2_write1ByteRegister(uint8_t address, uint8_t reg, uint8_t data)
     
    I contacted the tech support and they told me by using the I2C EEPROM code generate by MCC below will work
     
    I2Cx_MasterWrite(I2C_EEPROM_T * eepromInstance, uint8_t *data, uint8_t size, uint8_t busAddress, ERR_STATUS_T *pstatus);
     
    But once I generate the code, there were 2 error being generated by MCC on the i2c_eeprom_app.c which I have no idea how to correct, and because of the error I was not able to compile the code.
    #9
    Wesley_88
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2018/11/26 17:19:06
    • Location: 0
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/10/31 12:13:34 (permalink)
    0
    Hi Mysil,
     
    Thank you or the link above, I will check it out and report back if it resolve my concern. 
    Ya I was posting the same issue on 2 other forums. 
     
    #10
    mlp
    boots too small
    • Total Posts : 825
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/11/01 12:49:49 (permalink)
    5 (2)
    ekwong@hotmail.com
    Initially I tried several attempt

    Show us your code.
    Tell us what you expect to see happen.
    Tell us what you actually see happen.
    (all described in such a manner that we may replicate your experiments and duplicate your results, please)
     

    But once I generate the code, there were 2 error being generated by MCC

    What were those errors?
     

    Ya I was posting the same issue on 2 other forums. 

    That's a quick way to annoy at least 3 groups of people.
    Instead, post your detailed query (see above for details of what you really should include) in just one place.
    Then, in each other place post a short message with a link to the single place where your detailed question and all of the responses and discussion reside together.
     
    This is elementary stuff, but nobody reads before they post nowadays so they don't learn from others' mistakes.
    Uphill in the snow both ways.

    Mark (this opinion available for hire)
    #11
    Mysil
    Super Member
    • Total Posts : 3464
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/11/03 01:33:04 (permalink)
    0
    Well,
    The cause for the problem observed, is that code described by Christopher Best in TB3191,
    and generated by MCC Device library for PIC18 MCUs  version 1.77.0
    provide this family of I2C driver functions:  
    void i2c1_write1ByteRegister(uint8_t address, uint8_t reg, uint8_t data);
    void i2c1_write2ByteRegister(uint8_t address, uint8_t reg, uint16_t data);
    void i2c1_writeNBytes(uint8_t address, void * data, uint8_t len);
    uint8_t i2c1_read1ByteRegister(uint8_t address, uint8_t reg);
    uint16_t i2c1_read2ByteRegister(uint8_t address, uint8_t reg);
    void i2c1_readDataBlock(uint8_t address, uint8_t reg, char *data, uint8_t len);
    void i2c1_readNBytes(uint8_t address, char* data, uint8_t len);

    While MCC's I2C_EEPROM library version 1.0  and Temperature sensor library version 1.0
    expect driver interface functions in this family:
    void I2C1_MasterWrite( uint8_t *pdata,  uint8_t  length,  uint16_t address,  I2C_MESSAGE_STATUS  *pstatus);
    void I2C1_MasterRead( uint8_t *pdata,  uint8_t  length,  uint16_t address,   I2C_MESSAGE_STATUS  *pstatus);
    ...

    So until someone come up with the nessesary glue logic ...
     
    In the Microchip MCC website, there is listed ...PIC18 device library version 1.78.0
    and I2C_EEPROM library version 2.1.0, but downloads doesn't work, and they do not show up in MCC Versions panel.
     
        Mysil
     
    #12
    du00000001
    Just Some Member
    • Total Posts : 3225
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/11/03 02:44:53 (permalink)
    0
    Repeating what I stated above:
    the EEPROM calls look like a fit for the interrupt-driven I2Cx drivers of the dsPIC33E? (not absolutely sure about the sequence of the call parameters).
     
    So one might either port these drivers to the PIC18 (not sure about feasibility - I try to avoid PIC18s) - which would result in a really powerful solution. Or adapt the EEPROM library to use the PIC18s' non-interrupt calls, which might require major modifications to what MCC generated.
    Either way not a fast track.
     
    What I consider "hardly feasible": writing some glue logic. The reason: this would combine the drawbacks of both implementations: non-interrupt (PIC18), and high(er) RAM consumption (from all the control structures of the dsPIC services made to support interrupt-driven operation).

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #13
    Mysil
    Super Member
    • Total Posts : 3464
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/11/03 06:52:45 (permalink)
    0
    Hi,
    I have on some time ago published updated drivers for PIC24/dsPIC devices, PIC32MX,
    and PIC18xxx/PIC16xx devices, based on I2C driver code in MCC device libraries.
    See this thread: https://www.microchip.com/forums/FindPost/978822
    Yes, they all support the same application interface functions, and the same argument lists,
    on PIC16, PIC18, PIC24, dsPIC33, PIC32MX... 
    There are however some differences in organization of status code values and sequence.
     
    It is fully possible to write a blocking and non-interrupt driver
    with the same application interface as the blocking drivers. 
    Especially for PIC16 devices with only 1 interrupt priority level,
    a non-interrupt driver for I2C make a lot of sense, smaller memory, both code and ram,
    and no interference with time-critical code running in interrupts.
    I may publish a example project soon.
     
    There is blocking bitbanging code for PIC32MM devices in the same thread:
    https://www.microchip.com/forums/FindPost/989704 
    since the first generation of those devices didn't have functional I2C peripheral.
     
    Sorry that du00000001 do not dare to use PIC18 devices, they are in my opinion excellent devices,
    and the product line of PIC18 devices have undergone a big upgrade during the  last years.
     
    The I2C peripheral in PIC18FxxK42 and PIC18FxxK83 families is however completely different from MSSP peripheral in previous PIC18 and PIC16 devices.
    The I2C peripheral  in PIC18FxxK42 is clearly designed to be running with DMA in a Interrupt driver, using Vectored interrupts. There are separate interrupt vectors for I2C Read transfer, I2C Write transfer, I2C control operations, and error conditions. With Interrupt flags that are reset by hardware when registers are read or written, or SFR bits are cleared.
    Using this peripheral in non-interrupt code is somewhat awkward, there is a lot of if's and but's to take care of.
    The code published with TB3191 and in MCC seem mostly like a stop-gap solution.
     
    Writing a driver to fully use the potential in PIC18FxxK42 and family, is a task that need some careful coding, and a lot of testing. 
     
     MCC Foundation Services seem to have mostly the same application interface routines as PIC18 device library v1.77.0, but I haven't tried those on PIC18F46K42 yet.
     
        Mysil
     
    #14
    du00000001
    Just Some Member
    • Total Posts : 3225
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/11/03 07:04:05 (permalink)
    0
    @ Mysil
    Until now, I had no need to explore the PIC18 family:
    • Fancy small applications run well on PIC16s (including e.g. a SENT decoder and a SENT transmitter).
    • When it comes to more challenging applications, I prefer the PIC16/dsPIC family: 16-Bit math and "quite some speed".
    PIC18s would come handy for 5 V systems if the EV family's peripherals won't suffice. It's just that I haven't yet had such an application  :)

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #15
    mbrowning
    USNA79
    • Total Posts : 1564
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/11/03 11:56:19 (permalink)
    0
    Writing a driver to fully use the potential in PIC18FxxK42 and family, is a task that need some careful coding, and a lot of testing.
    Perhaps because I’d never done I2C on a PIC before and had nothing to unlearn, I found K42 I2C (master) to be really easy. haven’t tried slave. I started with the TB but immediately converted it to state machine with read and write command circular buffers.

    I keep thinking an interrupt version would be a little more efficient but I can’t imagine the saved overhead would amount to enough to be worth the effort.

    Go Navy! Beat Army!
    #16
    Wesley_88
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2018/11/26 17:19:06
    • Location: 0
    • Status: offline
    Re: I2C EEPROM Lib in MCC 2019/11/07 16:55:34 (permalink)
    0
    I finally resolve my own issue.
     
    Apparently, it is easier to use the below generic I2C command to write to an external EEPROM
     
    i2c1_write1ByteRegister(uint8_t address, uint8_t reg, uint8_t data);
     
    Previously it did not work because I enter one extra bit for the slave address. Instead of 7 bits, I included the last write bit and make it 8 bits.
     
    Thank you for everyone help. 
    #17
    Jump to:
    © 2019 APG vNext Commercial Version 4.5