AnsweredAssumed Answered

Help to interface AT24C256 EEPROM with STM32F051r8

Question asked by Vishnudev K on Apr 7, 2018
Latest reply on Apr 8, 2018 by T J

I am trying to to write/read to at24c256.

Iam new to I2C.

I read about all theory stuffs.

But i tried writing code from the i2c eeprom eg, but am not to able to skip the txis flag while loop.

Please help.

 

 

I commented the data write and all..not even the first line works and generates a acknowledgment. Have i missed some initialization functions?

 

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_i2c.h"
/** @addtogroup STM32F0xx_StdPeriph_Templates
* @{
*/

/* Private typedef -----------------------------------------------------------*/
#define EEPROM_ADDRESS 0x50
#define sEE_WRITE_ADDRESS1 0x01
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{

/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f0xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f0xx.c file
*/

/* Add your application code here
*/
//clocks enable
RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);


RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOC,&GPIO_InitStructure);




GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOC,&GPIO_InitStructure);


I2C_DeInit(I2C2);



GPIO_InitTypeDef I2C_GPIO_CONGIG;
I2C_GPIO_CONGIG.GPIO_Pin=GPIO_Pin_10;
I2C_GPIO_CONGIG.GPIO_Mode=GPIO_Mode_AF;
I2C_GPIO_CONGIG.GPIO_OType=GPIO_OType_OD;
I2C_GPIO_CONGIG.GPIO_PuPd=GPIO_PuPd_NOPULL;
I2C_GPIO_CONGIG.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOB,&I2C_GPIO_CONGIG);
//SCL

I2C_GPIO_CONGIG.GPIO_Pin=GPIO_Pin_11;
GPIO_Init(GPIOB,&I2C_GPIO_CONGIG);
//SDA


GPIO_PinAFConfig(GPIOB,GPIO_Pin_10,GPIO_AF_1);

GPIO_PinAFConfig(GPIOB,GPIO_Pin_11,GPIO_AF_1);

//I2C config
I2C_InitTypeDef I2C_InitStructure;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_Timing = 0x00201D2B;
// I2C_InitStructure.I2C_Timing = 0x1042C3C7;
I2C_Init(I2C2,&I2C_InitStructure);


//I2c enable
I2C_Cmd(I2C2,ENABLE);

while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) == SET)
{}



I2C_TransferHandling(I2C2,EEPROM_ADDRESS,1,I2C_Reload_Mode,I2C_Generate_Start_Write);
/* Infinite loop */



while(I2C_GetFlagStatus(I2C2, I2C_ISR_TXIS) == RESET)
{

GPIO_WriteBit(GPIOC,GPIO_Pin_9,Bit_SET);
}

GPIO_WriteBit(GPIOC,GPIO_Pin_9,Bit_RESET);



I2C_SendData(I2C2, 0x00);

GPIO_WriteBit(GPIOC,GPIO_Pin_8,Bit_SET);
//Ensure that the transfer complete reload flag is
//set, essentially a standard TC flag
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TCR) == RESET){}


//
//
//
// /* Send MSB of memory address */
// I2C_SendData(sEE_I2C, (uint8_t)((sEE_WRITE_ADDRESS1 & 0xFF00) >> 8));
//
// while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TXIS) == RESET)
// {
//
// GPIO_WriteBit(GPIOC,GPIO_Pin_8,Bit_SET);
//
// }
//
//
//
// I2C_SendData(sEE_I2C, (uint8_t)(sEE_WRITE_ADDRESS1 & 0x00FF));
//
// while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TCR) == RESET)
// {
// }

while (1)
{

}
}


#ifdef USE_FULL_ASSERT

/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* Infinite loop */
while (1)
{
}
}
#endif

/**
* @}
*/


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Outcomes