AnsweredAssumed Answered

ADDR bit is not set in I2C

Question asked by Sage on Apr 20, 2015
Hi, I am trying to configure STM32f3discovery board as slave and arduino board as master. I tried to transmit data from stm32f3discovery board to arduino. However, the problem is the ADDR bit is never set. Can anyone tell me how can i solve this problem?
#include <stddef.h>
#include "stm32f30x.h"
#include "stm32f30x_conf.h"
 
/* Private typedef */
typedef enum {
    Error =  0, Success =!Error
} Status;
GPIO_InitTypeDef GPIO_InitStruct;
I2C_InitTypeDef I2C_InitStruct;
RCC_ClocksTypeDef RCC_Clocks;
__IO uint32_t TimingDelay = 0;
uint8_t buff[2] = {'a','b'};
 
Status i2c_slavetransmit(I2C_TypeDef* I2Cx, uint8_t *buf, uint32_t nbuf);
 
 
int main(){
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
     /*I2C SCL and SDA configuration*/
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;    //PB6 SCL PB7 SDA
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_OD ;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
     /*Connect SCL*/
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_4);
     /*Connect SDA*/
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_4);
    RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
    RCC_GetClocksFreq(&RCC_Clocks);
    SysTick_Config(RCC_Clocks.I2C1CLK_Frequency / 100);
    /*configure I2C */
     I2C_DeInit(I2C1);
     I2C_SoftwareResetCmd(I2C1);
 
    I2C_Cmd(I2C1,ENABLE);
    I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_InitStruct.I2C_AnalogFilter =  I2C_AnalogFilter_Disable;
    I2C_InitStruct.I2C_DigitalFilter = 0x00;
    I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStruct.I2C_Timing = 0xA061191F;
    I2C_InitStruct.I2C_OwnAddress1 = 0x08;
    I2C_Init(I2C1,&I2C_InitStruct);
        while(1){
       }
}
 
Status i2c_slavetransmit(I2C_TypeDef* I2Cx, uint8_t *buf, uint32_t nbuf){
    uint32_t timeout = 0xffff;
    I2C_ITConfig(I2C1,I2C_IT_ADDRI,ENABLE);
    //  if(nbuf){
 
            while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY)!=RESET){
                if (timeout-- == 0)
                    GPIOE->ODR |= 1L << 9;
                    return Error;
            }
 
            while(I2C_GetFlagStatus(I2Cx,I2C_FLAG_ADDR) ==RESET);
            GPIOE->ODR |= 1L << 14;
            while(I2C_GetFlagStatus(I2Cx,I2C_ISR_DIR) != RESET){
                if (timeout-- == 0)
                GPIOE->ODR |= 1L << 11;
                return Error;
            }
 
            while(I2C_GetFlagStatus(I2Cx,I2C_FLAG_TXE) == RESET);
 
            while(nbuf){
                I2C_SendData(I2Cx,*buf++);
            while(I2C_GetFlagStatus(I2Cx,I2C_FLAG_TXE) == RESET);
 
            GPIOE->ODR |= 1L << 12;
            nbuf-- ;
            }
 
            GPIOE->ODR |= 1L << 13;
            return Success;
}
 
void SysTick_Handler(void)
{
 
    i2c_slavetransmit(I2C1,buff,2);
 
    if (TimingDelay != 0x00){
        TimingDelay--;
    }
}

Outcomes