Skip to main content
Visitor II
October 25, 2019
Solved

I2C and SPI at the same time?

  • October 25, 2019
  • 7 replies
  • 4835 views

Hi. I'm not a English speaker. Sorry for my poor English.

My code including EITHER I2C or SPI works successfully on STM32F303K8T6 Nucleo Board. However, with my code including BOTH I2C and SPI , Only SPI functions work correctly ,but I2Cs don't.

Using a ocsiloscope, It was found that voltage levels of I2C pins don't fluctuate at all when SPI initialization code exists. Is it impossible to use I2C and SPI function at the same time?

#include "main.h"
#include "dac.h"
#include "dma.h"
#include "i2c.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
 
int main(void){
 MX_GPIO_Init();
 MX_DMA_Init();
 MX_I2C1_Init();
 MX_USART2_UART_Init();
 MX_DAC1_Init();
 MX_DAC2_Init();
 MX_TIM6_Init();
 MX_SPI1_Init();
 MX_I2C1_Init();
 
 uint8_t aTxBuffer = 0x3F; //some command to send
 HAL_I2C_Master_Transmit(&hi2c1, DEVICE_ADDRESS, (uint8_t*)aTxBuffer, 1, 1000);
}

The Initialization code was generated by CubeIED Device Configuration Tool.

    This topic has been closed for replies.
    Best answer by S.Ma

    Are you using both SPI and I2C on same pins?

    CubeMX doesn't support dynamic HW reconfiguration. It has to be done by coding.

    7 replies

    S.MaAnswer
    Visitor II
    October 26, 2019

    Are you using both SPI and I2C on same pins?

    CubeMX doesn't support dynamic HW reconfiguration. It has to be done by coding.

    Mmats.503Author
    Visitor II
    October 26, 2019

    No, I2C and SPI are assigned to different pins, but there seems to be something wrong with initialization.

    Thank you for replying.

    Super User
    October 26, 2019

    Read out and check the related GPIO and I2C registers content, compare between the working and non-working state.

    JW

    Visitor II
    March 3, 2022

    Absolutely the same issue with the same MCU (and demo board)

    Any recommendations?

    As soon as I enable the SPI, the I2c cease to work (different kind off error flags appear - arbitration error / bus busy, and stuff)

    Cs.A.

    0693W00000KbEvjQAF.png

    Super User
    March 3, 2022

    Probably not your issue, but 2 things:

    • You call MC_I2C1_Init() twice - shouldn't hurt but definitely not needed
    • Your HAL_I2C_Master_Transmit() isn't going to send what you THINK/WANT it to send. You assign 0x3F to a byte variable and then pass that variable as a POINTER to the data to send. So it will try to send whatever byte is at address 0x0000003f, not the value 0x3f. You need a '&' before the variable name.

    You say the code doesn't work when the SPI init code "exists". Does "exist" mean "present in the code but not called" or does it mean "present in the code and called"? In other words, does that EXACT code send data on the I2C port if you comment out the MX_SPI1_Init() line?

    You don't check the HAL_I2C_Master_Transmit() return value, Does it return an error? Use you debugger and step into HAL_I2C_Master_Transmit() and see what is happening.

    Visitor II
    March 3, 2022

    nonono... the OP is 2 years ago.

    I use a totally different code, but the same kind of MCU, and have the same issue.

    When:

    HAL_SPI_Transmit();

    is getting called, (even once) the i2c "busy" flag turns '1'. (and if I reinit I2c, the i2c hall function dies in the middle of the function - and arbitration fault)

    -if this line is commented out - i2c works ok. (and all i2c communication is ok before the first SPI call)

    Visitor II
    April 16, 2024

    I had same issue using nucleo f303 board. I2C got stuck (error flag) with SPI init. 

    I'm not sure what is issue, but I found workaround to use I2C and SPI by DeInit SPI before using I2C. (SPI Init should done to use SPI again..).

    Do HAL_SPI_MspDeInit(&hspi1); before I2C write/read command.

    Do HAL_SPI_MspInit(&hspi1); before SPI write/read command. 

    Depend on your applicaiton, this might be cause slow response.. but for my case, this is not problem. 

    Maybe it can be more simple command if you look at those funciton.

    Thanks.

    yamadanalog

     

     

    Explorer II
    June 27, 2024

    i have the same problem with stm32f103c8t, but in my case the SPI can't work, but the I2C work correctly 

     

    Super User
    June 27, 2024

    @aouali wrote:

    i have the same problem ...  but in my case the SPI can't work, but the I2C work correctly 


    So it's not the same problem - it's a different problem, on a different chip!

    Start a new thread, and fully describe your situation:

    https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

     

    Graduate II
    June 27, 2024

    Four different people complaining about a similar issue, this looks like more than a random newbie misconfig. At the very least something needs to be communicated better in the docs or through the CubeMX GUI.

     

    @STOne-32 , perhaps you have some insight to offer for this thread?