I2C and SPI at the same time?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-10-25 1:34 PM
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.
Solved! Go to Solution.
- Labels:
-
I2C
-
SPI
-
STM32F3 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-10-25 6:29 PM
Are you using both SPI and I2C on same pins?
CubeMX doesn't support dynamic HW reconfiguration. It has to be done by coding.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-10-25 6:29 PM
Are you using both SPI and I2C on same pins?
CubeMX doesn't support dynamic HW reconfiguration. It has to be done by coding.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-10-26 12:01 AM
No, I2C and SPI are assigned to different pins, but there seems to be something wrong with initialization.
Thank you for replying.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-10-26 2:08 AM
Read out and check the related GPIO and I2C registers content, compare between the working and non-working state.
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-03 1:15 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-03 7:08 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-03 7:42 AM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-04-15 7:30 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-06-27 2:04 AM
i have the same problem with stm32f103c8t, but in my case the SPI can't work, but the I2C work correctly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-06-27 2:08 AM
@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:
A complex system designed from scratch never works and cannot be patched up to make it work.
