cancel
Showing results for 
Search instead for 
Did you mean: 

I2C peripheral not working with CubeMx generated code (HAL driver)

Jane Sk
Associate
Posted on June 28, 2017 at 22:16

I am using CubeMx (HAL driver)for initialization code generation and using HAL libraries/functions after that. The MCU is

https://community.st.com/message/97161?sr=search&searchId=101014d1-ce3e-4537-930e-04009467924a&searchIndex=2

I am having problem with the

mailto:I@C

peripheral. It is a custom board and for the time being, no external device is attached to the I2C pins. The pins are just pulled up using pull-up resistors. I am trying to use it in master transmit/receive mode. After MX_I2C1_Init() is executed : -

Observations: Using an oscilloscope, i can see that there is no clock signal on SCL pin. The voltage at the SCL pin is always LOW. The voltage on the SDA pin is always HIGH. And I2C1 -> ISR-> BUSY flag is SET.

Below I have copied portion of the code from main() function:

int main(void)

{

  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_CAN_Init();

  MX_SPI1_Init();

  MX_USART1_UART_Init();

   MX_I2C1_Init();

accl_buff[0] = 'a';              //       transmit buffer for I2C

  while (1)

  {

    HAL_I2C_Master_Transmit(&hi2c1, 0x18, accl_buff, 1, 10 );         // 0x18 is slave address

    

    accl_buff[0] = accl_buff[0]+1;         //    update I2C transmit buffer

       

    for(i=0;i<10000;i++);      //       delay function

   }

   }

All I have done is add HAL_I2C_Master_Transmit() to the while loop in main(). Otherwise, the code is as generated by CubeMx. But the I2C peripheral DOES NOT generate any clock signal on SCL pins(always held LOW) & neither give out any data on the SDA pins(held HIGH).

Where am I mistaken ?

I think my problem is similar to

https://community.st.com/message/152756-i2c-generated-cubemx

, though a different MCU. I did not find any I2C example for this MCU to test. I have also tried generating code with LowLevel Driver from CubeMx, and the BUSY flag is in reset condition after initialization.

Thanks in advance.

#stm32f0-hal-driver-i2c #stm32f042 #stm32f0-i2c #cubemx-i2c
7 REPLIES 7
Tilen MAJERLE
ST Employee
Posted on June 29, 2017 at 11:50

Hello

Jana.Saikat

‌,

can you please share with us configuration file (MSP file).

Since board is custom, can you verify you are checking correct pins (the same used in CubeMX for init) and that device is properly soldered?

Best regards,

Tilen

john doe
Lead
Posted on June 29, 2017 at 12:34

>HAL_I2C_Master_Transmit(&hi2c1, 0x18, accl_buff, 1, 10 ); 

HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)(0x18<<1), accl_buff, 1,10);

andy b
Senior
Posted on June 29, 2017 at 16:27

Hi Jane

Here is the document that tells you the steps to follow to use the I2C on stm32f0 chips.

http://www.st.com/content/ccc/resource/technical/document/user_manual/2f/77/25/0f/5c/38/48/80/DM00122015.pdf/files/DM001…

 

The part that you should go take a look at are pages 216 to 221.The document seems well made just make sure that you read the section corresponding the type of communication you are looking for. ex: by polling,dma,etc.

You mentionned that the ISR flag is always busy.To start I would try to get it working by polling that means that you can disable all I2C interrupts.When thats working move on to interrupt.Also if you have many peripherals working with interrupts remember that you may run out of stack.You can increase the stack either in project->settings if you are using cubeMX or in your config files in your project.(not sure in what file exacly)

Good luck

-Andy

Posted on June 29, 2017 at 14:54

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6si&d=%2Fa%2F0X0000000bxA%2FHTTOau1qREUI0nup_QwOYKde4Gqt2EQuhXtdGKVpCJw&asPdf=false
Posted on June 29, 2017 at 16:31

Ps:I would  highly recommend that you use a logic analyser if you have acces to one.Or you can buy a chinese replica thats works fine (have one and it works great) for about 10$ and it works with Saleae's software which is free.

Yuri Fedkin
Associate
Posted on March 05, 2018 at 21:07

Hi!

If you look at the CubeMX generated code, you will not see the initialization of the I2C port pins. Possible it is a CubeMX bug. 

Adding the missing code should help you.

    GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

     .......

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

If you soldered pullup resistors use 

GPIO_MODE_AF_OD value in mode field.

Good luck!

Dean Neumann
Associate II
Posted on July 15, 2018 at 18:04

The OP said the problem was that there is no I2C clock signal.  Perhaps the problem is that the clock source has not been configured.

Per stm32l4xx_hal_rcc.c  :

After reset the device is running from Multiple Speed Internal oscillator

(4 MHz) with Flash 0 wait state. Flash prefetch buffer, D-Cache

and I-Cache are disabled, and all peripherals are off except internal

SRAM, Flash and JTAG.

(+) There is no prescaler on High speed (AHBs) and Low speed (APBs) busses:

all peripherals mapped on these busses are running at MSI speed.

(

+) The clock for all peripherals is switched off,

except the SRAM and FLASH.

(+) All GPIOs are in analog mode, except the JTAG pins which

are assigned to be used for debug purpose.

[..]

Once the device started from reset, the user application has to:

(+) Configure the clock source to be used to drive the System clock

(if the application needs higher frequency/performance)

(+) Configure the System clock frequency and Flash settings

(+) Configure the AHB and APB busses prescalers

(+) Enable the clock for the peripheral(s) to be used

(+) Configure the clock source(s) for peripherals which clocks are not

derived from the System clock (SAIx, RTC, ADC, USB OTG FS/SDMMC1/RNG)