cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Slave Example

gil23
Associate III
Posted on April 27, 2014 at 19:56

Hi I'

m working on STM32F401 disco EVB with the 

Standard Peripherals Library

,

Where i can find a working example on I2C slave?  

12 REPLIES 12
Posted on April 27, 2014 at 21:10

STM32F4xx_DSP_StdPeriph_Lib_V1.3.0\Project\STM32F4xx_StdPeriph_Examples\I2C\I2C_TwoBoards\I2C_DataExchangeDMA\main.c

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
gil23
Associate III
Posted on April 28, 2014 at 08:02

Thanks,

I have STM32F401-Discovery_FW_V1.0.0 (stsw-stm32136.zip) libraries .

Where can i download STM32F4xx_DSP_StdPeriph_Lib_V1.3.0 ?

gil23
Associate III
Posted on April 28, 2014 at 12:16

Hi,

I define the fololowing:

project config to STM32F40_41xxx .

#define I2C_SLAVE

#define SLAVE_ADDRESS 0xA0 (I tried already 0x50)

I connected SDA to PB6 & SCL to PB9 . I tried with pullups and without.

I debug the project. i performed I2C transaction with a I2C Master that i have and the uC don't response to the I2C address.

frankmeyer9
Associate II
Posted on April 28, 2014 at 14:07

I connected SDA to PB6 & SCL to PB9 . I tried with pullups and without.

 

I2C is open drain, so you MUST have pull-up resistors.

I debug the project. i performed I2C transaction with a I2C Master that i have and the uC don't response to the I2C address.

 

I guess you mean source-level debugging in your IDE.

Have you tried to attach a scope, to see what actually happens on the bus (if at all) ?

You should at least see the slave ACK-ing the address.

gil23
Associate III
Posted on April 28, 2014 at 14:34

Hi,

I have a scope connected.

I definedSLAVE_ADDRESS 0xA0

I attached pictures of write to add 0xA0 and read from address 0xA1 notice that the slave not ACK.

If I definedSLAVE_ADDRESS 0x50

The clock is return the High level at the read picture, I still don't have ACK.

________________

Attachments :

Read_0xA1_.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hzzo&d=%2Fa%2F0X0000000bRu%2FX90BT5xuXLb1.5.FGEe1xZ5MMeWUqQVWO_Bj6TlVmAo&asPdf=false

Write_0xA0_.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hzzj&d=%2Fa%2F0X0000000bRt%2FF.WRyQCKzX9M5zyZxsYBOobBrrCwlIL3FQJI31MFZjQ&asPdf=false
frankmeyer9
Associate II
Posted on April 29, 2014 at 08:45

I attached pictures of write to add 0xA0 and read from address 0xA1 notice that the slave not ACK.

 

According to the image, the slave does acknowledge the read access. So the address seems right.

gil23
Associate III
Posted on April 29, 2014 at 21:07

Hi,

This example hold The SCL at low state when performing a read.

somebody have a working example of I2C slave ?

frankmeyer9
Associate II
Posted on April 30, 2014 at 09:30

This example hold The SCL at low state when performing a read.

 

Not sure what this means.

SCL is controlled by the master, for the purpose of clocking the data in/out.

(The only exception being clock stretching, which shouldn't be the case here).

...somebody have a working example of I2C slave ?

 

Perhaps in the Standard Lib, and in some threads here.

I avoid the I2C peripheral, since ST's implementation (not only according my opinion) is somehow arkward.

munger
Associate II
Posted on May 01, 2014 at 19:36

I've had trouble with this too. Possibly I should create a new thread but this is pretty recent so hopefully a couple people are still monitoring the discussion.

Anyway,I want to interface a discovery board to a ADT7410 temp sensor. We have a prototype board built that brings PB6 and PB7 to the temp sensor. I found a nice example of I2C code here:

http://www.electroons.com/blog/2013/01/hello-world/

and I've modified that for the temp sensor.

My trouble is that it seems I can configure I2C1 to run out of PB8 and PB9 but when I try to move it to PB6 and PB7 things go to hell.

Here is my init code using PB8 and PB9:

I2C_DeInit(I2C1);

// I2C Periph clock enable

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

// I2C_SCL_GPIO_CLK and I2C_SDA_GPIO_CLK Periph clock enable

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

// Configure SCL as alternate function push-pull output

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;

GPIO_Init(GPIOB, &GPIO_InitStruct);

// Configure SDA as alternate function open drain output

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;

GPIO_Init(GPIOB, &GPIO_InitStruct);

// Change mapping of I/O pins to I2C

GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);

// First De-Initialize

I2C_DeInit(I2C1);

// Init I2C to defaults

I2C_StructInit(I2C_InitStruct);

// Set clock speed to 100 kHz

I2C_InitStruct->I2C_ClockSpeed = 100000;

I2C_Init(I2C1, I2C_InitStruct);

// Enable I2C

I2C_Cmd(I2C1, ENABLE);

Enclosed is a couple scope traces showing SDA and SCL when using PB8, PB9 and when using PB6, PB7.

When I switch from PB8, PB9 to PB6, PB7 I believe I just need to change the code in two spots each, right?

When using PB8 and PB9 the signal looks good, I can see the address of the device (0x48) coming out. I don't currently have the sensor hooked up so there is no ACK and the code does nothing else but that's okay.

I've read some posts about the order of the initialization but I believe I am doing it in the suggested order from ST.

With it running on PB8 and PB9 I jumpered it over to PB6 and PB7 with the thought that something else on the board was pulling those lines down but no difference.

So, as usual I'd appreciate any insights anyone can give.

Thanks.

Mike

________________

Attachments :

I2C_analysis.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzdU&d=%2Fa%2F0X0000000bRs%2FXsdcmO6JD372DbIBE8iDq69LWOn5hn7GL7NjMbyGpO4&asPdf=false