cancel
Showing results for 
Search instead for 
Did you mean: 

Correctly programming I2C1 interface for STM32L151R8T6.

LKuri.1
Associate II

I'm trying to program i2C1 interface of STM32L151 with 3-rd side tools - Algocraft WN programmer.

The sequence of operations attached in the DOC file.

After these operations the START bit does not generated (checked by scope) and MASTER bit doesn't appear in SR1.

What am I doing wrong?

6 REPLIES 6
LKuri.1
Associate II

Looks like I found what's a problem.

After I write to GPIO port mode register (GPIOB_MODER). RST_PANEL(PB5), PB6,PB7 - Alternate function mode 

write -t h40020400 -l 4 --data h0000A800 --mask h0000fc00

lines PB6 and PB7 become LOW, and the BUSY flag is set in I2C1_SR2.

The problem is that the flag remains high even after the reset by setting and release of the SWRST bit in I2C1_CR1.

After I set SWRST bit:

write -t h40005400 -l 4 --data h8000 --mask hbffb

I read the BUSY flag LOW.

But after release SWRST

write -t h40005400 -l 4 --data h0000 --mask hbffb

The flag is HIGH again.

Both lines PB6 and PB7 are in LOW state permanently after Alternate function mode definition, even during SWRST.

There are external pull up resistors on both lines (20K).

Can someone help me?

You need to set also the AFR register in GPIO, read GPIO chapter in Reference Manual (RM) and the AF pin table in Datasheet (DS).

JW

LKuri.1
Associate II

Dear JW,

Thank you.

Now after AFRL register programming at AF4 for PB6, PB7 (0x44000000), both lines are HIGH.

BUSY flag in SR2 is LOW.

But writing into CR2 register (0x00000002) (I'm using MSI clock) doesn't work. I read back 0x0 from it.

What wrong now?

LKuri.1
Associate II

Ok. I found the reason.

Just I put I2C1 in RESET in RCC_APB1RSTR and forgot to release it.

Now I able to program CR2, CCR and TRISE registers.

After that I set PE in CR1 (0x0001) and after that also set START bit (0x0101).

But read back only 0x0001.

LKuri.1
Associate II

Ok. I see. This bit cleared by hardware.

I see Start condition generated and bit SB in CR1 set.

Will continue.

Thank you very much.

I2Cx_CR1.START is self-clearing.

0693W000000W3UNQA0.png

The whole I2C module is tricky, and it requires careful reading and re-reading both the narrative in I2C chapter, and the description of the registers/bits.

JW