cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103VCT6 i2c2 setting trouble...

ssooki
Associate III

Hi

I am trying to communicate using i2c1, i2c2 of STM32F103VCT6.

The pins are set as shown below.

- i2c1: PB6 (SCL), PB7 (SDA)

- i2c2: PB10 (SCL), PB11 (SDA)

i2c1 is communicating correctly, i2c2 is not communicating with i2c1.

When sending data, the busy flag check section normally passes.

but next step, ev5 does not have master mode select and is being processed.

Please check if there is a wrong setting for i2c2 or if there is a problem in the other part.

(Uart3, which uses the same pin, has set remap.)

#define PORT_I2C1 0

#define PORT_I2C2 1

void config(void)

{

....(other setting)

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 | RCC_APB1Periph_I2C2, ENABLE);

​ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |

  RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_ADC1, ENABLE);

GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE);

​}

​void InitPort1(u8 I2Cx_port)

{

  GPIO_InitTypeDef GPIO_InitStructure;

if(I2Cx_port == PORT_I2C1) {

/* Configure I2C1 pins: SCL and SDA */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_Init(GPIOB, &GPIO_InitStructure); 

  GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_7);

  Delay(DELAY5MS);

GPIO_SetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_7);

Delay(DELAY100MS);

}

else if(I2Cx_port == PORT_I2C2){

/* Configure I2C1 pins: SCL and SDA */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;

  GPIO_Init(GPIOB, &GPIO_InitStructure); 

  GPIO_ResetBits(GPIOB, GPIO_Pin_10 | GPIO_Pin_11);

  Delay(DELAY5MS);

GPIO_SetBits(GPIOB, GPIO_Pin_10 | GPIO_Pin_11);

Delay(DELAY100MS);

}

}

void I2C_Configuration1(u8 I2Cx_port)

{

  I2C_InitTypeDef I2C_InitStructure; 

  GPIO_InitTypeDef GPIO_InitStructure;

/* Configure I2C1 pins: SCL and SDA */

if(I2Cx_port == PORT_I2C1) {

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

}

else if(I2Cx_port == PORT_I2C2) {

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

}

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;

  GPIO_Init(GPIOB, &GPIO_InitStructure); 

  /* I2C configuration */

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

  I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;

  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;

   

  if(I2Cx_port == PORT_I2C1) {

 /* Apply I2C configuration after enabling it */

  I2C_Init(I2C1, &I2C_InitStructure);

   /* I2C Peripheral Enable */

  I2C_Cmd(I2C1, ENABLE);

}

else if(I2Cx_port == PORT_I2C2) {

 /* Apply I2C configuration after enabling it */

  I2C_Init(I2C2, &I2C_InitStructure);

   /* I2C Peripheral Enable */

  I2C_Cmd(I2C2, ENABLE);

}

}

byte I2C_BufferWrite_1(u8* pBuffer, u16 NumByteToWrite)

{

   u16 num;

byte Retry = 0;

num = NumByteToWrite;

while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)) {

if(Retry++ > I2C_RETRY_COUNT) {

//printf("\t err1");

sys.i2c_err_flag = 4;

return NG;

}

delay(1);

}

Retry = 0;

  /* Send STRAT condition */

  I2C_GenerateSTART(I2C1, ENABLE);

   

  /* Test on EV5 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) {

if(Retry++ > I2C_RETRY_COUNT) {

I2C_GenerateSTOP(I2C1, ENABLE); 

//printf("\r\n err1"); 

sys.i2c_err_flag = 1; 

return NG;

}

delay(1);

}

Retry = 0;

  /* Send slave address for write */

  I2C_Send7bitAddress(I2C1, I2C1_SLAVE_DEMOD_ADDR, I2C_Direction_Transmitter);

//printf(" Addr = %02x", I2C1_SLAVE_DEMOD_ADDR);

  /* Test on EV6 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) {

if(Retry++ > I2C_RETRY_COUNT) {

I2C_GenerateSTOP(I2C1, ENABLE); 

//printf("\r\n err2"); 

sys.i2c_err_flag = 2; 

return NG;

}

delay(1);

}

Retry = 0;

/* While there is data to be written */

  while(NumByteToWrite--) {

  

    /* Send the current byte */

    I2C_SendData(I2C1, *pBuffer);

    //printf(" [%d]%02x", num-NumByteToWrite, *pBuffer);

    /* Point to the next byte to be written */

    pBuffer++; 

    /* Test on EV8 and clear it */

    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) {

if(Retry++ > I2C_RETRY_COUNT) {

I2C_GenerateSTOP(I2C1, ENABLE); 

//printf("\r\n err4"); 

sys.i2c_err_flag = 3; 

return NG;

}

delay(1);

}

Retry = 0;

  }

  /* Send STOP condition */

  I2C_GenerateSTOP(I2C1, ENABLE);

return OK;

}

byte I2C_BufferWrite_2(u8* pBuffer, u16 NumByteToWrite)

{

   u16 num;

byte Retry = 0;

num = NumByteToWrite;

while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)) {

if(Retry++ > I2C_RETRY_COUNT) {

//printf("\t err1");

sys.i2c_err_flag = 4;

return NG;

}

delay(1);

}

Retry = 0;

  /* Send STRAT condition */

  I2C_GenerateSTART(I2C2, ENABLE);

   

  /* Test on EV5 and clear it */

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)) {

if(Retry++ > I2C_RETRY_COUNT) {

I2C_GenerateSTOP(I2C2, ENABLE); 

//printf("\r\n err1"); 

sys.i2c_err_flag = 1; 

return NG;

}

delay(1);

}

Retry = 0;

  /* Send slave address for write */

  I2C_Send7bitAddress(I2C2, I2C1_SLAVE_DEMOD_ADDR, I2C_Direction_Transmitter);

//printf(" Addr = %02x", I2C1_SLAVE_DEMOD_ADDR);

  /* Test on EV6 and clear it */

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) {

if(Retry++ > I2C_RETRY_COUNT) {

I2C_GenerateSTOP(I2C2, ENABLE); 

//printf("\r\n err2"); 

sys.i2c_err_flag = 2; 

return NG;

}

delay(1);

}

Retry = 0;

/* While there is data to be written */

  while(NumByteToWrite--) {

  

    /* Send the current byte */

    I2C_SendData(I2C2, *pBuffer);

    //printf(" [%d]%02x", num-NumByteToWrite, *pBuffer);

    /* Point to the next byte to be written */

    pBuffer++; 

    /* Test on EV8 and clear it */

    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) {

if(Retry++ > I2C_RETRY_COUNT) {

I2C_GenerateSTOP(I2C2, ENABLE); 

//printf("\r\n err4"); 

sys.i2c_err_flag = 3; 

return NG;

}

delay(1);

}

Retry = 0;

  }

  /* Send STOP condition */

  I2C_GenerateSTOP(I2C2, ENABLE);

return OK;

}

main()

{

char buffer[32];

config();

InitPort1(PORT_I2C1);

InitPort2(PORT_I2C2);

buffer[0] = 0x71;

buffer[1] = 0x03;

I2C_BufferWrite_1(buffer, 2);

I2C_BufferWrite_2(buffer, 2);

}​

1 REPLY 1
fsolt.1
Associate

How am I francisco a question are you using MDK ARM to develop your program?