cancel
Showing results for 
Search instead for 
Did you mean: 

Help to interface AT24C256 EEPROM with STM32F051r8

Vishnudev  K
Associate II
Posted on April 07, 2018 at 17:42

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****/
7 REPLIES 7
T J
Lead
Posted on April 08, 2018 at 01:03

do you have a nucleo board?

can you try some examples first, see how they work.

The cube has many examples, there should be one for your processor and IIC.

IOt is a very steep learning curve   after 3 years you become self reliant

Posted on April 08, 2018 at 01:24

GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_1); // Use PinSource index for AF config

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Graham Sayers
Associate
Posted on April 08, 2018 at 05:08

This is not a direct answer, but if you picked up an mbed compatible nucleo or disco board you could get this running in a couple minutes.  I've used this eeprom library with 24Cxx chips before with great success.

https://os.mbed.com/users/bborredon/code/eeprom/

 
Posted on April 08, 2018 at 05:54

I dont have . got only stm32f051 discovery board

Posted on April 08, 2018 at 05:55

woww..thankyou...i never noticed that....You think it makes a differenc??Let me try and tell you

Posted on April 08, 2018 at 05:56

I dont wont entire library function. I want to learn.. So i need to use basic I2C function and do it

Posted on April 08, 2018 at 06:02

Here is a hint from Vasile,

If you use HAL, look here

/external-link.jspa?url=http%3A%2F%2Fstm32f4-discovery.net%2F2015%2F07%2Fhal-library-16-i2c-for-stm32fxxx-devices%2F

If you use SPL, look here

/external-link.jspa?url=http%3A%2F%2Fstm32f4-discovery.net%2F2014%2F05%2Flibrary-09-i2c-for-stm32f4xx%2F