cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Peripheral Control

orhanyilmaz
Associate II
Posted on December 01, 2014 at 12:38

Hello,

I trying use the I2C periph. But, It is not work. Have you a I2C referance manual ? or What should I do? My Example code :

void
I2C_initial(
void
)
{
/*
* Note: Clk is startup enabled.
* for Ex:
* ME.RUNPC[1].R = 0x00000010;//.B.RUN0 = 1;//
*
* ME.PCTL[44].R = 0x01; //I2C0 - RunPC[1]
* //Mode Transition to Enter RUN0 MODE
* ME.MCTL.R = (0x40000000|KEY);//RUN0 MODE & KEY
* ME.MCTL.R = (0x40000000|INVERT_KEY);//RUN0 MODE
*
* //Check Configurations
* while(ME.GS.B.MTRANS);//Global Status Register wait for mode transition to complete
*
* while(ME.GS.B.CURRENTMODE != 4);//Global Status Register verify RUN0 Current mode
*
*/
////////////////////////////////////////////////////////////////
// I2C Pads
////////////////////////////////////////////////////////////////
//CLK
//SIU.PCR[PA11].R = 0x0800; //I2C SCL
SIU.PCR[PB3].R = 0x0800; 
//I2C SCL
//DATA
//SIU.PCR[PA10].R = 0x0800; //I2C SDA
SIU.PCR[PB2].R = 0x0800; 
//I2C SDA
//SDA Select
//SIU.PSMI[30].B.PADSEL = 0; //for PA10
SIU.PSMI[30].B.PADSEL = 1; 
//for PB2
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
I2C_0.IBAD.B.ADR = 0x02;
//Address = 0x01
/*
* IBC7-6 - MUL
* -------------
* 00 -> 1
* 01 -> 2
* 10 -> 4
* 11-Reserved
*
* IBC5-3 scl2start scl2stop scl2tap tap2tap
* ---------------------------------------------------------
* 000 2 7 4 1
* 001 2 7 4 2
* 010 2 9 6 4
* 011 6 9 6 8
* 100 14 17 14 16
* 101 30 33 30 32
* 110 62 65 62 64
* 111 126 129 126 128
*
* IBC2-0 SCL_Tap SDA_Tap
* ------------------------------------
* 000 5 1
* 001 6 1
* 010 7 2
* 011 8 2
* 100 9 3
* 101 10 3
* 110 12 4
* 111 15 4
*
* RM0017 pdf(page 355-357) for more
*/
I2C_0.IBFD.B.IBC = 0;
I2C_0.IBCR.B.MDIS = 0; 
//Module Enable
I2C_0.IBCR.B.IBIE = 0; 
//Bus Interrupt Disabled
I2C_0.IBCR.B.MS = 1; 
//Master mode
I2C_0.IBCR.B.RSTA = 1; 
//Generate repeat start cycle
////////
}
//
void
I2C_Write(U_8 data)
{
I2C_0.IBSR.B.IBIF = 1;
//Flag Clear
I2C_0.IBCR.B.MS = 1; 
//Master mode
I2C_0.IBCR.B.TX = 1;
I2C_0.IBDR.B.DATA = data; 
//( Page: 360 )
I2C_0.IBCR.B.RSTA = 1;
}

1 REPLY 1
Erwan YVIN
ST Employee
Posted on December 03, 2014 at 15:23

Hello Yilmaz ,

Your code seems to be correct

To enable I2C, it is mandatory to set the peripheral clock mode.

if not , it will generate a reset issue when you try to write to the volatile memory I2C_0

cf example below for B512K

/* Sets peripheral clock.*/

  halSPCSetPeripheralClockMode(44, SPC5_ME_PCTL_RUN(1) | SPC5_ME_PCTL_LP(2));

do not forget to handle the interruptions

OSAL_IRQ_HANDLER(vectorXX)

you have an I2C example in chapter 20.5.1 of RM017

  Best regards

            Erwan