AnsweredAssumed Answered

I2C reading by old method

Question asked by deokar.mandar on May 5, 2017

Hello ST community,

I had already posted the same query with HAL libraries but I am trying to write and read to/from the EEPROM by using old method but I am stucked in reading from the eeprom.

My code is,

 

uint8_t mem_adr = 0x01;
uint8_t buf = 5;
uint8_t buf_read;

#define I2C_ADDRESS (0x50 << 1)

 

I2C1_Setup();


//I2C write
/* clear CR2 specific bits */
I2C1->CR2 &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));

/* update CR2 */
I2C1->CR2 |= (uint32_t)(((uint32_t)I2C_ADDRESS & I2C_CR2_SADD) | (((uint32_t)1 << 16 ) & I2C_CR2_NBYTES) | \
(uint32_t)I2C_CR2_AUTOEND |(uint32_t)I2C_CR2_START| (uint32_t)I2C_CR2_RD_WRN);

I2C1->TXDR = (uint8_t) mem_adr; /* Send Byte address */


__nop();__nop();__nop();__nop();__nop();
I2C1->ISR |= I2C_ISR_TXE;
I2C1->CR2 |= I2C_CR2_AUTOEND;
if((I2C1->ISR & I2C_ISR_TXE)==I2C_ISR_TXE){

I2C1->CR2 |= I2C_CR2_AUTOEND;
I2C1->TXDR = (uint8_t)buf;

I2C1->CR2 |= I2C_CR2_STOP;
}

 

//I2C read

 

I2C1->CR2 &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));

 

I2C1->CR2 |= (uint32_t)(((uint32_t)I2C_ADDRESS & I2C_CR2_SADD) | (((uint32_t)1 << 16 ) & I2C_CR2_NBYTES) | \
(uint32_t)I2C_CR2_AUTOEND|(uint32_t)I2C_CR2_START) ;

 

I2C1->ISR |= I2C_ISR_TXE;
I2C1->TXDR = (uint8_t) mem_adr; // Send Memory address

__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();

 

//Here TC and RXNE are always 0. The problem must be RXNE is not changing its state to read the data

 

I2C1->CR1|= (uint32_t)(I2C_CR1_RXIE);
I2C1->CR2 |= (uint32_t)((uint32_t)I2C_CR2_RD_WRN |(((uint32_t)I2C_ADDRESS_R & I2C_CR2_SADD)) | (((uint32_t)1 << 16 ) & I2C_CR2_NBYTES) );

 

// As RXNE is 0 program is unable to enter this loop

 

if((I2C1->ISR & I2C_ISR_RXNE)==I2C_ISR_RXNE){

buf_read = I2C1->RXDR;

 

Could anyone please tell me where is the mistake?

 

Thank you in advance.

Regards,

Mandar.

Outcomes