• AVR Freaks

Hot!PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave

Author
TechDpt
Starting Member
  • Total Posts : 70
  • Reward points : 0
  • Joined: 2011/06/13 09:29:39
  • Location: 0
  • Status: offline
2020/11/23 11:12:33 (permalink)
0

PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave

Dear all,
this is just to have some confirmations about the usage of this chip in order to do a double I2C communication module, one I2C port will be set as I2C MASTER and one as I2C SLAVE.
The MASTER I2C lines will be used to configure some registers inside power management chip and the SLAVE I2C lines will be used to organize inside the uC registers that can be readed externally e.g. from an Arduino or other type of embedded board.
Following this approach from one side (the I2C SLAVE port) the uC will be configured to perform some task by receiving command on predefinited internal registers and from the other port (the MASTER I2C port) the uC will be able to set some registers inside some dedicated IC to perform specialized functions.
From the datasheet it's seems to me that this mode of operations is permitted, someone have used this device or similar ones with two MSSP modules into the above described way?
Thanks and best regards.
F.
#1

15 Replies Related Threads

    Mysil
    Super Member
    • Total Posts : 4068
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2020/11/23 11:58:03 (permalink)
    0
    Hi,
     
    Yes, what you are described is possible and allowed.
    Just keep separate what code and data belong to the two separate peripherals.
     
    This is usually not difficult, since the logic to handle communication as a slave,
    is quite different from master code.
     
    Code for slave communication is most naturally written as Interrupt Service routine (ISR).
    Master code may be written as blocking code called from main loop in program,
    or using interrupts.
     
    For development of master code, I find it most convient to debug, using a known good hardware slave device,
    a sensor or RTC, a EEPROM memory chip, or the power managment chip you are going to use anyway. 
     
    For testing and debugging slave code, I find it most convient to do this on the same microcontroller,
    making the master talk to the slave I2C peripheral on the same device.
     
    I have not done this on PIC16F18446, but on several other devices in PIC16F, PIC18F, PIC24, PIC32MX and PIC32MZ families.
     
        Mysil
     
    #2
    TechDpt
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2011/06/13 09:29:39
    • Location: 0
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2020/11/24 10:10:23 (permalink)
    0
    Dear @Mysil,
    thank for your helpful answer, this will be a good starting point, about the implementation as master or slave could you suggest some code around here that you know as affidable to perform some initial testing?
     
    Thanks again!
    Best regards.
    F.
    #3
    TechDpt
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2011/06/13 09:29:39
    • Location: 0
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/05 06:04:41 (permalink)
    0
    @Mysil,
    I've bought a PIC16F18446 (DM164144) Curiosity Nano board to perform the testing I'll report some result here.
     
    BR
    F.
    post edited by TechDpt - 2021/01/05 07:00:02
    #4
    TechDpt
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2011/06/13 09:29:39
    • Location: 0
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/13 13:18:41 (permalink)
    0
    Dear all,
    I've get working properly the MSSP2 as I2C2 master, about the MSSP1 as I2C1 slave I've a issue on the address.
     
    Product Revision  :  PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.76
            Device            :  PIC16F18446
            Driver Version    :  2.01
     
    Into the code I've selected inside i2c1.h:
     
    #define I2C1_SLAVE_ADDRESS 0x08 
    #define I2C1_SLAVE_MASK 0x7F

     
    but on the master (a raspberry Pi) using the I2C bus scan tool with the command:
     
    sudo i2cdetect -y 1
     
    The system found the PIC but with address 0x40.
    The address selected was 0x08 => 0000 1000
    The address found by the RPi is 0x40 => 0100 0000
     
    The bit seems swapped in some way... and from the I2C1_Initialize() generated code I can see:
     
    void I2C1_Initialize(void)
    {
        // initialize the hardware
        // SMP High Speed; CKE disabled;
        SSP1STAT = 0x00;
        // SSPEN enabled; CKP disabled; SSPM 7 Bit Polling;
        SSP1CON1 = 0x26; // 0010 0110
        // ACKEN disabled; GCEN disabled; PEN disabled; ACKDT acknowledge; RSEN disabled; RCEN disabled; SEN enabled;
        SSP1CON2 = 0x00; // Era 0x01
        // SBCDE disabled; BOEN disabled; SCIE disabled; PCIE disabled; DHEN disabled; SDAHT 100ns; AHEN disabled;
        SSP1CON3 = 0x00;
        
        // SSPMSK 127;
        SSP1MSK = I2C1_SLAVE_MASK;
        // SSPADD 8;
        i2c_addressLow = (I2C1_SLAVE_ADDRESS & 0x0ff); // value in UI & 0xFF
        i2c_addressUpper = ((I2C1_SLAVE_ADDRESS & 0x300) >> 7 ); // value in ((UI & 0x300) >> 7)
        i2c_upperAddressFlag = true; // SSPADD holds upper address

        SSP1ADD = i2c_addressUpper;
        
        // clear the slave interrupt flag
        PIR3bits.SSP1IF = 0;
        // enable the master interrupt
        PIE3bits.SSP1IE = 1;

    }

     
    Thanks and best regards.
    F.
     
     
     
     
    post edited by TechDpt - 2021/01/13 15:11:49
    #5
    TechDpt
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2011/06/13 09:29:39
    • Location: 0
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/14 01:50:06 (permalink)
    0
    Update
    After looking at the I2C real signals on the bus I've get all working with these I2C1_Initialize() code changes:
     

     
        /*
        // SSPMSK 127;
        SSP1MSK = I2C1_SLAVE_MASK;

        // SSPADD 8;
        i2c_addressLow = (I2C1_SLAVE_ADDRESS & 0x0ff); // value in UI & 0xFF
        i2c_addressUpper = ((I2C1_SLAVE_ADDRESS & 0x300) >> 7 ); // value in ((UI & 0x300) >> 7)
        i2c_upperAddressFlag = true; // SSPADD holds upper address

        SSP1ADD = i2c_addressUpper;
        */
        
        SSP1MSK =0xFF;
        i2c_addressLow = (I2C1_SLAVE_ADDRESS << 1);
        i2c_addressUpper = 0x00;
        i2c_upperAddressFlag = false;

        SSP1ADD = i2c_addressLow;
     

     
    Now the sudo i2cdetect -y 1 command is able to show the address 0x08 for the PIC device on the I2C bus.
     
    root@raspberrypi:~# sudo i2cdetect -y 1
         0 1 2 3 4 5 6 7 8 9 a b c d e f
    00: -- -- -- -- -- 08 -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
    root@raspberrypi:~#

     
    BR
    F.
    post edited by TechDpt - 2021/01/14 02:42:46
    #6
    ric
    Super Member
    • Total Posts : 29474
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/14 01:57:12 (permalink)
    0
    Your initial version was trying to use 10 bit addressing, rather than the usual 7 bit addressing.
    Was that intentional?
    I'm guessing the Raspberry Pi may only support 7 bit addressing.

    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!
    #7
    TechDpt
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2011/06/13 09:29:39
    • Location: 0
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/14 02:52:22 (permalink)
    0
    ric
    Your initial version was trying to use 10 bit addressing, rather than the usual 7 bit addressing.
    Was that intentional?
    I'm guessing the Raspberry Pi may only support 7 bit addressing.


    Hi @Ric,
    yeah, it was a my misunderstanding about the generated code because I've assumed that the 10-bit / 7-bit was automatically managed by the library, just after digging into the code I've discovered the need to perform the code changing.
     
    Yes the RaspberryPi through the i2cdetect utility support the 7 bit addressing.
     
    I'm also looking at the last library release, but again all is changed in a way that is mandatory recode all the sections of my program that is related the I2C stuff... changing is good to get better performance, but is really expensive to follow and perform the change anytime due to the lack of information about the logic used into the new API... it should be better use an OOP approach hence writing a stable API interface and then just update  the internal code or release new API interface for other functionalities, this would be a solution that would allow greater portability of the code without having to completely modify it at each new API release...
    BR
    F.
    #8
    ric
    Super Member
    • Total Posts : 29474
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/14 03:19:04 (permalink)
    +1 (1)
    I'm commonly hearing similar complaints about MCC.
    I've never used it myself.
     

    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!
    #9
    TechDpt
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2011/06/13 09:29:39
    • Location: 0
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/15 10:45:12 (permalink)
    0
    Following just as side note.
     
    Actually I'm using the I2C1 bus between a PIC16F18446 (Vdd is between 3.0 V and 4.2 V) and a RPi4 board that have a not 5 V tolerant GPIO. The SCL/SDA lines of the PIC are wired directly to the corresponding SCL/SDA lines of the Raspberry Pi.
     
    From the RPi schematics there are (built-in on the RPi board) two 1.8 k pull-up resistor between each I2C line and the 3.3 V power supply rail of the RPi.
     
    I think there should be no problems and I can use this straight connection due to the open-drain feature of the I2C bus even if the two systems are effectively working with two different power supply rail, so no need to use any voltage translator from the PIC and the RPi board.
     
    To perform a check of the above, by looking at the I2C signals with a scope, this confirm that the maximum voltage levels on the I2C lines are always equal to 3.3V (the RPi voltage). Also during the PIC poweron all the pins should be set as input so it is state are in high-impedance and again there should be no trouble to the Raspberry Pi system.
     
    B.R.
    F.
    #10
    ric
    Super Member
    • Total Posts : 29474
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/15 13:27:32 (permalink)
    0
    Regarding using 3.3V I2C levels.
    If you are using the specialised I2C pins, the input high voltage is controlled by SSPxSTAT/CKE
    (I2C -> greater than 0.8VDD, SMBUS -> 2.1V)
    If you are using non-dedicated I2C pins, the input level is controlled by the INLVL setting for that pin.

    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!
    #11
    TechDpt
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2011/06/13 09:29:39
    • Location: 0
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/16 07:41:50 (permalink)
    0
    Hi @ric,
    from the PIC16F18446 (2.7V≤VDD≤5.5V) datasheet DS40001985B-page 637 about the I2C electrical level we have (I'm using the specialized pin RB4 and RB6 for the MSSP1 module):
    - parameter D303 I2C input level low (max) 0.3 VDD (valid for 2.7V≤VDD≤5.5V)
    - parameter D323 I2C input level high (min) 0.7 VDD (valid for 2.7V≤VDD≤5.5V)
    so with a Vdd equal to 4.2V we have:
    - Vi_low_max=0.3*4.2=1.26V
    - Vi_hi_min=0.7*4.2=2.94V
    So with a fixed 3.3V from the RPi I'm safe (from a logic level understanding perspective) to properly manage the I2C levels from the PIC also when the VDD voltage of the PIC is at the maximum level.
     
    BR
    F.
    post edited by TechDpt - 2021/01/16 07:54:46
    #12
    Mysil
    Super Member
    • Total Posts : 4068
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/16 17:54:01 (permalink)
    +1 (1)
    Hi,
    Microchip have never understood the concept of creating a defined Application Interface,
    and supporting it for the whole range of microcontrolleers and software tools.
    Especially for I2C, each new line of software tools do driver interfaces in different ways,
    or just deliver building blocks, such that application programmers have to develop their own application interfaces.
    See:  /mcc_generated_files/examples/i2c1_master_example.c 
     
    Some of this may be connected with different application developers have different preferences.
    Some developers want code to be robust and flexible,
    while other developers want code to be bare-bones simple and minimalistic.
    When MCC generated code is neither compact nor robust, it ends up with each developer using their own modifications.
     
    AFIK.  MCC and Harmony do not even agree upon whether to use 7-bit or 8-bit(shifted) representation of 7-bit I2C address values.
     
    The closest I have come, is rewriting the code you have used for I2C from
    Device Library  PIC10 / PIC12 / PIC16 / PIC18 MCUs - v1.77  and earlier.
    Modified routines, I have used on many devices from PIC12  to PIC32MZ___EF.
    See thread here: https://www.microchip.com/forums/FindPost/978822
     
    Lately, I have been making code intended for smaller 8-bit PIC devices,
    to run I2C Master code without interrupts in blocking mode.
    Still with the requirement to function in a multimaster environment,
    and supporting the ability to use 10 bit addressing.
     
    With supply voltage Not higher than 4.2 V, your calculations and signaling seem within range,
    with the restriction that the Pull-up voltage is 3.3 V and not lower.
    However, be aware that the rising edge of signal get more rounded off,
    when signal voltage is closing up to the pull-up voltage, so this will cause a limitation to the highest signaling frequency that will work.
    If possible, you should study the signal vaweform on oscilloscope, to confirm that signal margins are reasonable.
     
    I generally prefer to use SMBus input signal levels, or TTL input signal levels, if SMBus inputs are not available.
     
        Mysil
    post edited by Mysil - 2021/01/16 18:11:29
    #13
    TechDpt
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2011/06/13 09:29:39
    • Location: 0
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/17 10:38:24 (permalink)
    0
    Dear Mysil
    thank for your notes and link about the I2C code.
    Concerning the I2C signal levels the pull-up voltage will stay at 3.3V because is fixed from the RPi side, frquency is 100 kHz and the waveforms looks sharp and clean even that in my prototype that is made with several evaluation boards tied together the I2C wiring are made with flying wires with length about 150 mm :-).
     
    BR
    F.
     
    #14
    BroadwellConsultingInc
    Super Member
    • Total Posts : 99
    • Reward points : 0
    • Joined: 2020/06/09 06:07:55
    • Location: 0
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/17 19:41:18 (permalink)
    +1 (1)
    I've been working with the I2C slave code created by MCC for the last couple of months on the PIC16F15214.  No idea if it generates the same code for the 18446.  At this point I've reworked nearly the entire file.
     
    A couple of things that I found:
    First, the data sheet for the 15214 says that Open Drain configuration for the pins using I2C should be set.  MCC doesn't do this, but it appears that the I2C hardware does it automatically, because I'm not seeing any 5V drive on the pins.  I set them anyway...
     
    Second, the framework doesn't implement any clock stretching.  This is a major item if you're going to hammer your PIC slave with requests from the master, particularly if all of the incoming data parsing / response generation isn't in your interrupt handler or if you've got a bunch of other interrupts going on that could delay I2C interrupt handling.  Be wary of this if you're doing multiple requests from the master without any delay.  
     
    I've found that even using the hardware provisions for clock stretching, the hardware can't consistently handle back-to-back packets when the start of one packet comes in around 10uS after the stop of the prior packet.  Sometimes the I2C module just doesn't stretch the Ack clock on the address byte even when the proper bits are set.  I've given up on this and added a requirement for a 20 uS delay on the host side between I2C transactions.
    #15
    TechDpt
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2011/06/13 09:29:39
    • Location: 0
    • Status: offline
    Re: PIC16F18446: working with two I2C module one set as a I2C master and one as I2C slave 2021/01/18 00:34:49 (permalink)
    0
    Hi @BroadwellConsultingInc,
    concerning MCC I highly recommend to always check the registry settings, I've seen that sometime the registers bits are not properly set despite the options being selected by means of the MCC graphics windows.
     
    BR
    F.
    post edited by TechDpt - 2021/01/18 07:15:00
    #16
    Jump to:
    © 2021 APG vNext Commercial Version 4.5