• AVR Freaks

Hot!I2C application using Harmony

Author
Mohammed_Hassan
Starting Member
  • Total Posts : 31
  • Reward points : 0
  • Joined: 2019/03/13 06:05:20
  • Location: 0
  • Status: offline
2019/07/31 04:39:19 (permalink)
0

I2C application using Harmony

Hello All, 
I want to develop an application using I2C communication as a master. 
I configured a harmony project with these configurations attached below.
 
and this is the code: 
typedef struct
{
    /* The application's current state */
    APP_STATES state;

    /* I2C Driver Handle */
    APP_I2C_DRIVER_HANDLE drvI2CHandle_Master;
    /* TODO: Define any additional data used by the application. */
    APP_I2C_BUFFER_HANDLE drvI2CTxRxBufferHandle[MAX_NUMBER_OF_BUFFERS];

} APP_DATA;

APP_DATA appData;
 

DRV_I2C_BUFFER_EVENT i2cOpStatus;
 

// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************


void I2CMasterOpStatusCb ( DRV_I2C_BUFFER_EVENT event,
                           DRV_I2C_BUFFER_HANDLE bufferHandle,
                           uintptr_t context);
 
 
 
            appData.drvI2CHandle_Master = DRV_I2C_Open(DRV_I2C_INDEX_0, DRV_IO_INTENT_READWRITE);
            
            /* event-handler set up receive callback from DRV_I2C_Tasks */
            DRV_I2C_BufferEventHandlerSet(appData.drvI2CHandle_Master, I2CMasterOpStatusCb, i2cOpStatus );
            
            if(appData.drvI2CHandle_Master != (DRV_HANDLE) NULL)
            {
                appData.state = APP_WRITE_READ_DATA;
            }
            else
            {
                appData.state = APP_STATE_ERROR;
            }
            break;

 
and always it is going to ERROR state as the return of DRV_I2C_Open is always Null! 
 
I tried a lot to find the issue but I have no idea what is going on.
 
Thanks,
post edited by Mohammed_Hassan - 2019/07/31 04:47:51

Attached Image(s)

#1

16 Replies Related Threads

    Paul PortSol
    Super Member
    • Total Posts : 493
    • Reward points : 0
    • Joined: 2015/07/03 11:52:03
    • Location: Newfoundland, Canada
    • Status: offline
    Re: I2C application using Harmony 2019/07/31 06:33:18 (permalink)
    0
    - Need more info:
    Harmony Version?
     PIC Part Number?
    Your I2C circuit?
    Pullups on SDA/SCL lines?
    Did you try any of the MHC Demo Code, or set MHC to Generate "I2C Application" Code, or just write it yourself from scratch?
     
    #2
    Mohammed_Hassan
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2019/03/13 06:05:20
    • Location: 0
    • Status: offline
    Re: I2C application using Harmony 2019/07/31 06:38:10 (permalink)
    0
    Paul PortSol
    - Need more info:
    Harmony Version?
     PIC Part Number?
    Your I2C circuit?
    Pullups on SDA/SCL lines?
    Did you try any of the MHC Demo Code, or set MHC to Generate "I2C Application" Code, or just write it yourself from scratch?
     


    - Harmony V2_06
    - PIC Part Number:  PIC32MZ0512EFF064 
    - what is the meaning if I2C circuit? 
    - there is no external pull ups for SDA and SCL on my board, Thats why I enabled them internally.
    - I set the MHC to generate the static driver of I2C and then write my application code inside app.c file
     
    Thanks
    #3
    Paul PortSol
    Super Member
    • Total Posts : 493
    • Reward points : 0
    • Joined: 2015/07/03 11:52:03
    • Location: Newfoundland, Canada
    • Status: offline
    Re: I2C application using Harmony 2019/07/31 06:50:05 (permalink)
    0
    The internal pullups are quite large, so you'd have to run at a low datarate to give time for the circuit capacitance to pullup (try 1Kbps~10kbps). 
    I'd recommend external pullups appropriate to your target datarate, maybe 10K, maybe 2K.
     
    (PIC + I2C Pullups + slave ICs = I2C circuit)(Every device and bit of copper on each trace will add a bit of capacitance that needs to be charged through I2C pullups, I2C isn't push-pull, instead I2C only drives low and relies on pullups as a way of avoiding damage if data clashes from multiple ICs)
     
    Paul
    #4
    Paul PortSol
    Super Member
    • Total Posts : 493
    • Reward points : 0
    • Joined: 2015/07/03 11:52:03
    • Location: Newfoundland, Canada
    • Status: offline
    Re: I2C application using Harmony 2019/07/31 06:52:14 (permalink)
    0
    Use an oscilloscope on SDA & SCL to ensure the signals have risen completely in time for your chosen datarate, otherwise either slow datarate or change pullups.
     
    See wikipedia for I2C description and waveforms.
    #5
    Mohammed_Hassan
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2019/03/13 06:05:20
    • Location: 0
    • Status: offline
    Re: I2C application using Harmony 2019/07/31 06:55:51 (permalink)
    0
    Paul PortSol
    The internal pullups are quite large, so you'd have to run at a low datarate to give time for the circuit capacitance to pullup (try 1Kbps~10kbps). 
    I'd recommend external pullups appropriate to your target datarate, maybe 10K, maybe 2K.
     
    (PIC + I2C Pullups + slave ICs = I2C circuit)(Every device and bit of copper on each trace will add a bit of capacitance that needs to be charged through I2C pullups, I2C isn't push-pull, instead I2C only drives low and relies on pullups as a way of avoiding damage if data clashes from multiple ICs)
     
    Paul


    Thanks for the clarifications (Y) 
    Actually I am trying to use 100kbps, I will try lower rates.
     
    for the slaves, my project will deal with 3 ICs with I2C communications .. magnetic field sensor, external EEPROM and digital potentiometer .. right now it is connected only with the magnetic field sensor (but I want to set the I2c up now just seeing the address message on scope)
    Thanks
    #6
    Paul PortSol
    Super Member
    • Total Posts : 493
    • Reward points : 0
    • Joined: 2015/07/03 11:52:03
    • Location: Newfoundland, Canada
    • Status: offline
    Re: I2C application using Harmony 2019/07/31 07:17:40 (permalink)
    0
    Depending on each IC's input capacitance on SCL/SDA you  may need "stronger" pullups as you add more ICs.
    (Values on Datasheet, but best to check with scope, and add safety margins for temperature effects, resistor tolerances, etc. Safety margin: make ensure timing rises 20-50% before needed, i.e. if bit needed at clock change then make sure bit rose to 90% at 1/4 clock before change needed = half way between clock edges)
    #7
    Mohammed_Hassan
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2019/03/13 06:05:20
    • Location: 0
    • Status: offline
    Re: I2C application using Harmony 2019/07/31 07:35:18 (permalink)
    0
    Thanks for the valuable info .. 
    But could you help why always the DRV_I2C_Open function return Null ?? 
    #8
    Paul PortSol
    Super Member
    • Total Posts : 493
    • Reward points : 0
    • Joined: 2015/07/03 11:52:03
    • Location: Newfoundland, Canada
    • Status: offline
    Re: I2C application using Harmony 2019/07/31 08:05:19 (permalink)
    0
    a) MHC should have generated code to define and Init I2C first in system_init.c, yes/no?
    - const DRV_I2C_INIT drvI2C0InitData = ...
    - sysObj.drvI2C0 = DRV_I2C_Initialize(DRV_I2C_INDEX_0, (SYS_MODULE_INIT *)&drvI2C0InitData);
     
    b) Extract from my code for I2C Open:
                sI2C.hI2C0 = DRV_I2C_Open(D_I2C_INDEX, DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_NONBLOCKING|DRV_IO_INTENT_EXCLUSIVE );//Fail = -1 = DRV_HANDLE_INVALID 
                if(DRV_HANDLE_INVALID == sI2C.hI2C0)
                {
                    D_PRINTFn0_LogD_ERROR("I2C Init Failed");
                    sI2C.eState = I2C_InitERROR; sI2C.uErrCycle++;
                    Nop();//For Breakpoint when optimization makes other lines "unbreakable"
                    vI2C_State();
                    break;
                }//else Opened OK

     
    c) Useful:  MHC_CreatingProjectWithoutBSP_v00PR.txt
    Especially: "
    Option: I have found it useful to create dummy projects with minimal definitions, and use the MHC - Application Configuration - Application 0 Configuration - Generate Application Code. I've found "Generate Application Code" often creates code better matched to the Harmony version than some of the demonstrations and samples elsewhere. I do it in a dummy project as it can change settings elsewhere in MHC-Options, and it is easier to see those changes when not in your full project. i.e. I've had a BSP randomly enabled by this process and there isn't one for my PCB. Selecting a different PIC may generate different code (i.e. PIC32MX vs PIC32MZ differs in generated Interrupt code, ADC code, and more).
    After doing MHC-Generate go through these and pick out what you need for your own project:
    - the generated App code (app.h, app.c),
    -- Tools like "Beyond Compare" are quite useful in compare whole folder structures and contents of files. So if you copy folder, then add MHC options, generate, then do Beyond Compare, the exact changes by MHC can be quickly seen (You still have to pick through to find what you need).
    - check for any changed settings in modules of MHC the "Generate Application Code" is expected to use (i.e. Driver I2C for adding I2C code), and also
    - check things like SystemServices-Timer as timers are used for many other things."
     
    Paul
     
    #9
    arpananand
    Super Member
    • Total Posts : 417
    • Reward points : 0
    • Joined: 2009/11/18 04:35:42
    • Location: 0
    • Status: offline
    Re: I2C application using Harmony 2019/07/31 08:07:46 (permalink)
    0
    can you debug going inside Open function and see why/where is it returning, that may give some idea why is it returning NULL.
     
    #10
    Mohammed_Hassan
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2019/03/13 06:05:20
    • Location: 0
    • Status: offline
    Re: I2C application using Harmony 2019/08/01 05:01:20 (permalink)
    0
    Paul PortSol
    Depending on each IC's input capacitance on SCL/SDA you  may need "stronger" pullups as you add more ICs.
    (Values on Datasheet, but best to check with scope, and add safety margins for temperature effects, resistor tolerances, etc. Safety margin: make ensure timing rises 20-50% before needed, i.e. if bit needed at clock change then make sure bit rose to 90% at 1/4 clock before change needed = half way between clock edges)


    Sorry Paul, 
    I have a question and it may seems basics but I need to be sure.. 
    If there is no slave 'means that the master will still wait for ACK for the first address message', I can see on scope at least the clock and the first message containing the slave address on SDA, am i right? 
    because I cannot see the address message but the application is waiting the ACK.
     
    Thanks in advance
    #11
    ric
    Super Member
    • Total Posts : 23855
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: I2C application using Harmony 2019/08/01 05:16:29 (permalink)
    0
    Mohammed_Hassan
    If there is no slave 'means that the master will still wait for ACK for the first address message',

    A Master should never "wait" for ACK.
    The ACK/NAK occurs on a specific clock pulse. If the ACK is not there, then it is a NAK, and the Master should immediately abort and report an error.
     

    I can see on scope at least the clock and the first message containing the slave address on SDA, am i right?

    I'm not sure what you are saying.
    What exactly are you seeing?
    The START cycle, and the first address byte?
    What does the SDA signal do during the 9th clock pulse?
    Then what happens?
     

    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!
    #12
    Mohammed_Hassan
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2019/03/13 06:05:20
    • Location: 0
    • Status: offline
    Re: I2C application using Harmony 2019/08/01 05:24:06 (permalink)
    0
    I cannot see any thing on the SDA also the SCL see just few pulses. 
     
    Juts an important information: I do not have external Pull ups for SDA and SCL .. I am using the internal ones.
     
    and here you can find attached what exactly the application does. (it is not mine it is an example code)
     
    Thanks

    Attached Image(s)

    #13
    ric
    Super Member
    • Total Posts : 23855
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: I2C application using Harmony 2019/08/01 05:35:53 (permalink)
    0
    Mohammed_Hassan
    Juts an important information: I do not have external Pull ups for SDA and SCL .. I am using the internal ones.

    Why?
    The internal pullups are not nearly strong enough. That's just silly.
     

    and here you can find attached what exactly the application does. (it is not mine it is an example code)
     

    I guarantee your clock pulses are not that square with only the internal pullup.
    Do you have a scope?
    Can you take a photo of what you are really getting?
     

    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!
    #14
    Mohammed_Hassan
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2019/03/13 06:05:20
    • Location: 0
    • Status: offline
    Re: I2C application using Harmony 2019/08/01 05:45:21 (permalink)
    0
    I have a logic analyzer not scope .. 
    ric
    Why?
    The internal pullups are not nearly strong enough. That's just silly.
     

    this is the board that I have right now, but I can solder them .
    Do you think that this is the problem? 
     
    ric
     
    I guarantee your clock pulses are not that square with only the internal pullup.
    Do you have a scope?
    Can you take a photo of what you are really getting?
     



    I have logic analyzer not a scope and this is what I get from it whecn connecting it to SDA1 and SCL1 pins
    #15
    ric
    Super Member
    • Total Posts : 23855
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: I2C application using Harmony 2019/08/01 13:12:57 (permalink)
    0
    I don't know if the resistors are the actual problem, but they are certainly A problem.
    Debugging is the process of removing all the possible problems.
     

    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!
    #16
    DominusT
    Super Member
    • Total Posts : 1328
    • Reward points : 0
    • Joined: 2005/07/22 08:31:18
    • Status: offline
    Re: I2C application using Harmony 2019/08/01 13:32:22 (permalink)
    0
    Bit Bang mode is used for PIC32MZ. I managed to communicate with an EERAM without problems.
     

    post edited by DominusT - 2019/08/01 13:35:07

    Attached Image(s)

    #17
    Jump to:
    © 2019 APG vNext Commercial Version 4.5