Author Topic: I2C on STM32F4 [SOLVED]  (Read 3664 times)

0 Members and 1 Guest are viewing this topic.

Offline DELTA67Topic starter

  • Regular Contributor
  • *
  • Posts: 57
  • Country: fr
Re: I2C on STM32F4
« Reply #25 on: August 21, 2023, 10:23:30 pm »
Ok, I'll retry again.
 

Offline DELTA67Topic starter

  • Regular Contributor
  • *
  • Posts: 57
  • Country: fr
Re: I2C on STM32F4
« Reply #26 on: August 22, 2023, 08:52:05 am »
I simulated that example with a 24c02 (Addr = 0xA0) and it partially worked.
How did you built it? It seems that it's based on the standard peripheral driver. The keil uvision 5 uses the CUBEMX HAL.
can you attach your project and all the files used in simulation please?
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: I2C on STM32F4
« Reply #27 on: August 22, 2023, 01:47:45 pm »
The link I posted earlier only uses CMSIS headers, no peripheral library of any kind, should work just fine.
Have you tried?
I'm using CubeIDE.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline DELTA67Topic starter

  • Regular Contributor
  • *
  • Posts: 57
  • Country: fr
Re: I2C on STM32F4
« Reply #28 on: August 22, 2023, 04:35:29 pm »
I've found a working example for STM32F4 here:
https://blog.embeddedexpert.io/?p=613
I've made some modifications in order to get it working for the BLack PILL (F411):

* In main.c:
- I commented the clock config in order to use default 16MHz HSI clock
Code: [Select]
//SysClockConfig();
* In i2c.c:
- I enabled the pull-ups for PB8 and PB9
Code: [Select]
GPIOB->PUPDR|= 1<<16 | 1<<18;
- I modified the values of CR2, CCR and TRISE registers:
Code: [Select]
I2C1->CR2|= 16; // APB1 = 16MHz
I2C1->CCR|= 80;
I2C1->TRISE= 11;
Et voilĂ  !!

But when testing in PROTEUS it does not work as before. No start condition and no address transmission.
Later, I'll try with a lower frequency for APB1 (8MHz) and lower freq for SCL (50kHz) and see.
Thanks for ALL for your help
« Last Edit: August 22, 2023, 04:42:15 pm by DELTA67 »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: I2C on STM32F4
« Reply #29 on: August 22, 2023, 04:41:47 pm »
The model is definitely bugged.
As in the datasheet, I2C SR2 MSL bit goes to 0 (Slave mode) when a stop is detected.
I have some code using that flag, it worked perfectly in real hardware.
MSL never goes back to 0 in Proteus.

Just use LL. Does exactly what you would do by hand. No overhead, no bloat. Just skip rewriting the wheel.
This is my function:
Code: [Select]
ErrorStatus I2C1_MasterTransmit(uint8_t DevAddress, uint8_t *pData, uint16_t Size)
{
  ErrorStatus status = SUCCESS;
  LL_I2C_GenerateStartCondition(I2C1);
  while(LL_I2C_IsActiveFlag_SB(I2C1) != 1);

  LL_I2C_TransmitData8(I2C1, DevAddress);
  while(!LL_I2C_IsActiveFlag_ADDR(I2C1) || !LL_I2C_IsActiveFlag_TXE(I2C1)){
    if(LL_I2C_IsActiveFlag_AF(I2C1))
      break;
  }
  LL_I2C_ClearFlag_ADDR(I2C1);
  if(LL_I2C_IsActiveFlag_AF(I2C1)){
    status = ERROR;
    LL_I2C_ClearFlag_AF(I2C1);
  }
  else{
    while(Size--)
    {
      LL_I2C_TransmitData8(I2C1, *pData++);
      while(!LL_I2C_IsActiveFlag_TXE(I2C1));
      if(LL_I2C_IsActiveFlag_AF(I2C1)){
        status = ERROR;
        break;
      }
    }
  }
  while(!LL_I2C_IsActiveFlag_BTF(I2C1));
  LL_I2C_GenerateStopCondition(I2C1);
  while(LL_I2C_IsActiveFlag_MSL(I2C1));
  return status;
}


Code: [Select]
__STATIC_INLINE void LL_I2C_GenerateStartCondition(I2C_TypeDef *I2Cx)
{
  SET_BIT(I2Cx->CR1, I2C_CR1_START);
}
Code: [Select]
__STATIC_INLINE void LL_I2C_GenerateStopCondition(I2C_TypeDef *I2Cx)
{
  SET_BIT(I2Cx->CR1, I2C_CR1_STOP);
}
Code: [Select]
__STATIC_INLINE void LL_I2C_TransmitData8(I2C_TypeDef *I2Cx, uint8_t Data)
{
  MODIFY_REG(I2Cx->DR, I2C_DR_DR, Data);
}
Code: [Select]
__STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_ADDR(I2C_TypeDef *I2Cx)
{
  return (READ_BIT(I2Cx->SR1, I2C_SR1_ADDR) == (I2C_SR1_ADDR));
}

And so on. That's LL. Inlining avoids any slow calls. Simple functions and calls, nothing convoluted.
« Last Edit: August 22, 2023, 04:50:25 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline DELTA67Topic starter

  • Regular Contributor
  • *
  • Posts: 57
  • Country: fr
Re: I2C on STM32F4
« Reply #30 on: August 22, 2023, 09:45:31 pm »
The model is definitely bugged.
I confirm that.
I've tested with different APB1 frequencies (16, 8 and 4MHz) and SCL frequencies 100 and 50kHz.

Thanks David alpha, wek and Others.
How to tag this thread [SOLVED] ?
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: I2C on STM32F4
« Reply #31 on: August 22, 2023, 10:03:22 pm »
Edit the title in the first message?
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline DELTA67Topic starter

  • Regular Contributor
  • *
  • Posts: 57
  • Country: fr
Re: I2C on STM32F4
« Reply #32 on: August 23, 2023, 08:31:03 am »
Ok, thanks.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf