AnsweredAssumed Answered

STM32F100RB will not communicate with EEPROM

Question asked by cole.tom on Sep 27, 2013
Hello guys , I'm trying to write then read to the AT24C256 EEPROM.(http://www.atmel.com/Images/doc0670.pdf)datasheet.

After debugging the code gets stuck at the flag check.
(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))

The I2C is outputting what i want on the oscilloscope and the address pins on the EEPROM and Write Protect have been tied to ground.

If anyone could help that would be great.
here is the code:

001.#include <stm32f10x_i2c.h>
002.#include <stm32f10x_gpio.h>
003.#include <stm32f10x_rcc.h>
004.#include "stm32F10x.h"
005. 
006. 
007. 
008.#define I2C             I2C2
009.#define I2C_EE_CLK      RCC_APB1Periph_I2C2
010.#define I2C_GPIO        GPIOB
011.#define I2C_GPIO_CLK    RCC_APB2Periph_GPIOB
012.#define I2C_SCL         GPIO_Pin_10
013.#define I2C_SDA         GPIO_Pin_11
014. 
015. 
016. 
017. 
018. 
019. void Delay(__IO uint32_t nCount)
020.{
021.  for(; nCount != 0; nCount--);
022.}
023. 
024. uint8_t I2C_read_ack(I2C_TypeDef* I2Cx){
025.    // enable acknowledge of recieved data
026.    I2C_AcknowledgeConfig(I2C2, ENABLE);
027.    // wait until one byte has been received
028.    while( !I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED) );
029.    // read data from I2C data register and return data byte
030.    uint8_t data = I2C_ReceiveData(I2C2);
031.    return data;
032. }
033. 
034. 
035. 
036.int main(void)
037.{
038.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2 , ENABLE);
039.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
040.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
041. 
042. 
043. 
044. ////////////////////////////////////////////////////////////////////////////////
045.  //---------------------------I2C----------------------------------------------
046.  //??????????????????????????initiation///////////////////////////////////////
047. 
048. 
049.//set-up pins
050.  GPIO_InitTypeDef GPIO_InitStructure;
051.  GPIO_InitStructure.GPIO_Pin =  I2C_SCL | I2C_SDA; // sda =7 scl = 6
052.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
053.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
054.  GPIO_Init(I2C_GPIO , &GPIO_InitStructure);
055. 
056.  I2C_Cmd(I2C2, ENABLE);
057.  I2C_InitTypeDef I2C_InitStructure;
058. 
059.  I2C_StructInit (& I2C_InitStructure);
060.  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
061.  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
062.  I2C_InitStructure.I2C_OwnAddress1 = 0xAE;
063.  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
064.  I2C_InitStructure.I2C_AcknowledgedAddress =
065.  I2C_AcknowledgedAddress_7bit;
066.  I2C_InitStructure.I2C_ClockSpeed = 100000;
067.  I2C_Init(I2C2 , &I2C_InitStructure);
068. 
069.//int i = 0;
070.uint8_t EEPROMaddress =  (0b1010000)<<1;
071. 
072.while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));
073.I2C_GenerateSTART(I2C2, ENABLE);
074.while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
075.asm("NOP");
076.I2C_Send7bitAddress(I2C2, 0xA0, I2C_Direction_Receiver);
077.while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
078. 
079. 
080.asm("NOP");
081. 
082.I2C_SendData(I2C2, 0x00);
083.while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
084.I2C_SendData(I2C2, 0x00);
085.while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
086.I2C_SendData(I2C2, 0x00);
087.while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
088. 
089.I2C_GenerateSTOP(I2C2, ENABLE);
090.asm("NOP");
091. 
092. 
093.I2C_GenerateSTART(I2C2, ENABLE);
094.while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
095.I2C_Send7bitAddress(I2C2, 0xA0, I2C_Direction_Transmitter);
096.while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
097.I2C_SendData(I2C2, 0x00);
098.while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
099.I2C_SendData(I2C2, 0x00);
100.while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
101.I2C_Send7bitAddress(I2C2, 0xA0, I2C_Direction_Receiver);
102.while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
103.int testbyte = I2C_ReceiveData(I2C2);
104.I2C_GenerateSTOP(I2C2, ENABLE);
105. 
106.asm("NOP");
107. 
108. 
109. }

thanks for taking the time to read. 

Outcomes