AnsweredAssumed Answered

STM32F103 Problem with reading EEPROM

Question asked by Jafari.Alex on Jul 18, 2016
Latest reply on Nov 13, 2016 by Jafari.Alex
Hey friends,

I am trying to initialize I2C to read the content of 24AA02E48T EEPROM but after initializing the I2C busy_flag stays set. the code gets stock at while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY )); in I2C_burst_read(). 
void RCC_Configuration(void)
{
    ErrorStatus HSEStartUpStatus;
 
    /* RCC system reset(for debug purpose) */
    RCC_DeInit();
 
    HSEStartUpStatus = SUCCESS;
    RCC_HSICmd(ENABLE);
 
    if(HSEStartUpStatus == SUCCESS)
    {
        /* Enable Prefetch Buffer */
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
 
        /* Flash 2 wait state */
        FLASH_SetLatency(FLASH_Latency_2);
 
        /* HCLK = SYSCLK */
        RCC_HCLKConfig(RCC_SYSCLK_Div1);
 
        /* PCLK2 = HCLK */
        RCC_PCLK2Config(RCC_HCLK_Div1);
 
        /* PCLK1 = HCLK/2 */
        RCC_PCLK1Config(RCC_HCLK_Div2);
 
        /* PLLCLK = 8MHz * 9 = 72 MHz */
        RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_6);
        //    RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
 
        /* Enable PLL */
        RCC_PLLCmd(ENABLE);
 
        /* Wait till PLL is ready */
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
        {
        }
 
        /* Select PLL as system clock source */
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
 
        /* Wait till PLL is used as system clock source */
        while(RCC_GetSYSCLKSource() != 0x08)
        {
        }
 
        /* TIM2 clock enable */
        //  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3|RCC_APB1Periph_USART2, ENABLE);
        //RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
 
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_SPI2, ENABLE);
 
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
                | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1 | RCC_APB2Periph_SPI1, ENABLE);
    }
}
 
//===============================
void I2C_init(I2C_TypeDef* I2Cx, uint32_t speed)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    I2C_InitTypeDef I2C_InitStructure;
 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;//GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
 
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE);
 
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_ClockSpeed = speed;
    I2C_InitStructure.I2C_OwnAddress1 = 0x00;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_Init(I2Cx, &I2C_InitStructure);
    I2C_Cmd(I2Cx, ENABLE);
}
//===============================
void I2C_burst_read(I2C_TypeDef* I2Cx, uint8_t HW_address, uint8_t addr, uint8_t n_data, uint8_t *data)
{
        while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY ));
    I2C_GenerateSTART(I2Cx, ENABLE);
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(I2Cx, HW_address, I2C_Direction_Transmitter);
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    I2C_SendData(I2Cx, addr);
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_GenerateSTOP(I2Cx, ENABLE);
    I2C_GenerateSTART(I2Cx, ENABLE);
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(I2Cx, HW_address, I2C_Direction_Receiver);
        while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    I2C_AcknowledgeConfig(I2Cx, ENABLE);
    while(n_data--) {
        if(!n_data) I2C_AcknowledgeConfig(I2Cx, DISABLE);
            while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));
        *data++ = I2C_ReceiveData(I2Cx);
    }
    I2C_AcknowledgeConfig(I2Cx, DISABLE);
    I2C_GenerateSTOP(I2Cx, ENABLE);
        while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
}
//===============================
int main(int argc, char* argv[])
{
    int ret;
    uint8_t EE_Content[6] = { 0 };
    RCC_Configuration();
 
    I2C_init(I2C1, 100000);
    I2C_burst_read(I2C1, 0xA0, 0xFA, 6, EE_Content);
    while (1) {
        ;
        //infinite loop
    }
}

I read several posts on internet but I could not find anything that helps. I would really appreciate any help. 

Outcomes