CC2340R5: How to configure I2C slave for CC2340R5 module.

Part Number: CC2340R5

Tool/software:

Hello, 

I'm using CC2340R5, SDK version 7.40, basic_ble project.

I'm interfacing microcontroller with BLE through I2C. 

Controller will be a master and BLE will be a slave. 

My query is, i)how can I configure I2C as A slave in CC2340R5 in given SDK 7.40?

ii)Also, is there any example code for the same? 

I'm not seeing any such API where could configure I2C in slave mode. 

Regards,

Rushikesh.

  • Hello Rushikesh, 

    In order to configure the I2C as a slave in CC2340R5, you will need to override the I2C config done in I2CCC23XX_initHw() i.e. disable master and enable slave. Within the documentation, master is referred to as the controller and slave is referred to as the target. Please see the documentation within the SDK in source\ti\devices\cc23x0r5\driverlib\i2c.h on how to disable i2c controller and enable i2c target. 

    We do not have any example code for this operation. 

    Best Regards, 

    David Chen

  • Hi David,

    Thank you for your suggestions, i will check the API's in i2C.h

    I have another query, 

    how to set I2C slave address? Should this (I2CTargetSetAddress) API work?

    How could we send/receive data on I2C? Does the I2C_transfer() works?

    Regards,

    Rushikesh.

  • Hello Rushikesh, 

    Yes, you may use the I2CTargetSetAddress() API for setting the address. You may also use the I2CTargetInit() API to initialize the target and set its address. 

    For sending / receiving data, please take a look at the I2CTargetPutData() and I2CTargetGetData() API in I2C.h. 

    Best Regards, 

    David Chen

  • Hi David, 

    This is my code below, used for testing. 

    The callback sometimes gets triggered sometimes not and also I2C_send_receive_data( ) function is not getting called here.

    void I2C_Init(void)
    {
        uint32_t status = 0;
    
        I2C_Params_init(&i2cParams);
        i2cParams.bitRate = I2C_400kHz;
        i2c = I2C_open(CONFIG_I2C_0, &i2cParams);
        if (i2c == NULL)
        {
            //Display_printf(display, 0, 0, "Error Initializing I2C\n");
            while (1) {}
        }
        else
        {
            // Display_printf(display, 0, 0, "I2C Initialized!\n");
        }
    
        /*Disabling master mode*/
        I2CControllerDisable(I2C0_BASE);
    
        /*Enable slave mode*/
        I2CTargetEnable(I2C0_BASE);
    
        I2CTargetInit(I2C0_BASE, 0xAA);
        IntRegister(INT_I2C0_IRQ, i2c_callback);
        IntEnable(INT_I2C0_IRQ);
        I2CTargetEnableInt(I2C0_BASE, (I2C_TARGET_INT_STOP | I2C_TARGET_INT_START | I2C_TARGET_INT_DATA));
    
        //I2C_close(i2c);
    }
    
    
    void i2c_callback(void)
    {
        BLEAppUtil_invokeFunctionNoData(I2C_send_receive_data);
    
    }
    
    
    void I2C_send_receive_data()
    {
        uint8_t status = 0;
    
        uint8_t txbuffer = 10;
    
        status = I2CTargetStatus(I2C0_BASE);
    
        if(I2C_TARGET_ACT_RREQ_FBR == status)
        {
            RxBuffer[0] = I2CTargetGetData(I2C0_BASE);
            I2CTargetPutData(I2C0_BASE, txbuffer);
        }
        else if(I2C_TARGET_ACT_RREQ == status)
        {
            RxBuffer[0] = I2CTargetGetData(I2C0_BASE);
            RxBuffer[0] = I2CTargetGetData(I2C0_BASE);
            I2CTargetPutData(I2C0_BASE, txbuffer);
        }
        else if(I2C_TARGET_ACT_TREQ == status)
        {
            RxBuffer[0] = I2CTargetGetData(I2C0_BASE);
            RxBuffer[0] = I2CTargetGetData(I2C0_BASE);
            I2CTargetPutData(I2C0_BASE, txbuffer);
        }
        else
        {
            //Error
        }
    
    }

    Can you please look into the code and help me finding what's wrong?

    Regards, 

    Rushikesh.

  • Hello Rushikesh, 

    Can you provide any screenshots of logic analyzer or debugger / register screen of when the test code is working and when its not? 

    I can provide some general feedback on your test code. 
    Are you creating your own I2C_Init() function as I do not see where you enabling the I2C driver?

    Regarding the code itself: 

    On line 19, when you disable master mode, you will need to disable the master interrupt before, using I2CControllerDisableInt(I2C0_BASE). 
    On line 22, I do not believe you require this function as I2CTargetInit() will also enable the target module. 
    On line 26, I recommend adding I2CTargetClearInt() before calling I2CTargetEnableInt().

    I hope some of these can help with your test code. 

    Regards, 

    David Chen