cancel
Showing results for 
Search instead for 
Did you mean: 

I2C and SPI at the same time?

Mmats.503
Associate

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.

1 ACCEPTED SOLUTION

Accepted Solutions
S.Ma
Principal

Are you using both SPI and I2C on same pins?

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

View solution in original post

7 REPLIES 7
S.Ma
Principal

Are you using both SPI and I2C on same pins?

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

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

Thank you for replying.

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

JW

ACrna.1
Associate

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

Bob S
Principal

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.

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)

__yamadanalog__
Associate

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