cancel
Showing results for 
Search instead for 
Did you mean: 

stm32lxx i2c slave, what am I doing wrong

dpampouktsis
Associate
Posted on May 19, 2015 at 22:05

I have a stm32l00rb that I have set up as a slave. I have a SoC with embedded linux that I am using as the master. I am trying to use i2cdetect on the master (SoC) to detect the slave (stm32lxx), but I am getting no response. 

My logic is i2cdetect writes 0x00 - 0x77 to the i2c bus and if the slave has the corresponding address it will reply. I set the slave address to 0x0A which is within the range, but with no success. 

void

init_I2C1(

void

){

 

GPIO_InitTypeDef

GPIO_InitStruct;

 

I2C_InitTypeDef

I2C_InitStruct;

 

NVIC_InitTypeDef

  NVIC_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,

ENABLE

);

  GPIO_InitStruct.

GPIO_Pin

= GPIO_Pin_11 | GPIO_Pin_10;

  GPIO_InitStruct.

GPIO_Mode

=

GPIO_Mode_AF

;

  GPIO_InitStruct.

GPIO_Speed

=

GPIO_Speed_40MHz

;

  GPIO_InitStruct.

GPIO_OType

=

GPIO_OType_OD

;

  GPIO_InitStruct.

GPIO_PuPd

=

GPIO_PuPd_UP

;

  GPIO_Init(GPIOB, &GPIO_InitStruct);

 

// Connect I2C1 pins to AF

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C1);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C1);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,

ENABLE

);

  I2C_InitStruct.

I2C_ClockSpeed

= 400000;

//10000; // 10KHz

  I2C_InitStruct.

I2C_Mode

= I2C_Mode_I2C;

  I2C_InitStruct.

I2C_DutyCycle

= I2C_DutyCycle_2;

  I2C_InitStruct.

I2C_OwnAddress1

= 0x0A;

  I2C_InitStruct.

I2C_Ack

= I2C_Ack_Enable;

//I2C_Ack_Disable;

  I2C_InitStruct.

I2C_AcknowledgedAddress

= I2C_AcknowledgedAddress_7bit;

  I2C_Init(I2C1, &I2C_InitStruct);

  I2C_Cmd(I2C1,

ENABLE

);

 

// I2C1 interrupts

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

  NVIC_InitStructure.

NVIC_IRQChannel

=

I2C1_EV_IRQn

;

  NVIC_InitStructure.

NVIC_IRQChannelPreemptionPriority

= 1;

  NVIC_InitStructure.

NVIC_IRQChannelSubPriority

= 0;

  NVIC_InitStructure.

NVIC_IRQChannelCmd

=

ENABLE

;

  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.

NVIC_IRQChannel

=

I2C1_ER_IRQn

;

  NVIC_Init(&NVIC_InitStructure);

  I2C_ITConfig(I2C1, I2C_IT_EVT | I2C_IT_ERR,

ENABLE

);

}

void

I2C1_EV_IRQHandler(

void

){

    

volatile

uint32_t

temp;

    

switch

(I2C_GetLastEvent(I2C1)){

    

case

I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:

        I2C_SendData(I2C1, 0x0A);

       

break

;

    

case

I2C_EVENT_SLAVE_BYTE_RECEIVED:

     I2C_SendData(I2C1, 0x0A);

       

//I2C_InputBuffer[I2C_InputBufferIndex++] = I2C_ReceiveData(I2C1);

       

break

;

    

case

I2C_EVENT_SLAVE_STOP_DETECTED:

       

break

;

     }

   

// ADDR & STOPF cleaning

   

while

((I2C1->

SR1

& I2C_SR1_ADDR) == I2C_SR1_ADDR)

    {

        temp=I2C1->

SR1

;

        temp=I2C1->

SR2

;

      }

     

while

((I2C1->

SR1

&I2C_SR1_STOPF) == I2C_SR1_STOPF)

      {

        temp=I2C1->

SR1

;

        I2C1->

CR1

|= 0x1;

      }

}

/* ********* MAIN ************* */

int

main(

void

)

{

init_I2C1();

 

while

(1){

  I2C1_EV_IRQHandler();

  }

}

#stm32 #i2c #slave
0 REPLIES 0