2020-11-05 03:29 AM
Hello,
I'm trying to establish SPI communication between STM32F4 discovery board with STM32F407 MCU and an Arduino uno. The arduino acts as a master and I'm using it to interface my board(which acts as slave) with the PC since I like arduino uno's serial terminal(and because the discovery board does not have virtual terminal enabled by default I have to do some hardware stuff in order to get it to work).
What I' trying to do now is send a byte from the arduino's serial terminal to the arduino via USART and then to the discovery board via SPI and toggle a led in the receive completed callback function to ensure the communication works smoothly.
However I have the following problem: The callback does not get called everytime I send something from the arduino it gets called only some times. My first thought was that maybe the arduino sends data too fast for the board to read but I don't think this is the case. My board's SPI baudrate should be APB1/2 = 21Mhz and I ensured in the arduino code that the SPI on that side is configured to the same baudrate.
I generated the STM's code with CubeMX. I'm using SPI1 as peripheral.
The settings are like this:
Frame format: Motorola
Data Size: 8 bits
First Bit: MSB First
Clock Polarity: Low
Clock Phase: 1 edge(CPOL = 0, CPHA = 0)
CRC: Disabled
NSS Signal Type: Input Hardware
I doubled checked that the same settings have been configured in arduino. Still the problem persists. The byte from the arduino to my board sent via the SPI seems to not arrive every time(because the HAL_SPI_RxCpltCallback function does not get called all the time).
Please help me understand what could cause this trouble and how can I work towards fixing it. Thank you!
I have uploaded the stm_code and the arduino code in case that is helpful.
2020-11-05 05:51 AM
> My board's SPI baudrate should be APB1/2 = 21Mhz and I ensured in the arduino code that the SPI on that side is configured to the same baudrate.
Can Arduino really send SPI at 21MHz? In general, you want the SPI slave clock speed to be at least double that of the master. It's not like UART where they need to be equal. Might be tough to debug without a logic analyzer.
2020-11-05 10:45 AM
Probably not :D. I will try reducing the speed of the arduino to around 4Mhz. For the slave I assume I do not have to do anything as long as the SPI peripheral sits on a bus which has 21Mhz frequency correct? Or will that big difference in speeds somehow be problematic? Should I use the prescaler to also get it around 8Mhz or so?
Sadly I do not have an analyzer but I may be able to go somewhere where I have access to an oscilloscope in the following days.
2020-11-19 02:05 AM
Hello again,
Sorry for the late reply I just was very busy at work and could not look into this.
I got an oscilloscope and did some testing with it. Also created a new project from scratch for my board(STM32F407G disc 1) with CubeMX and configured SPI1 as follows:
- full duplex slave
- NSS: Hardware NSS Input signal(input hardware)
- Motorola, 8 bits, MSB first, CPOL-LOW, Clock phase-1 edge
- Enabled SPI1 global interrupt under NVIC tab
And now I'm sending value 165(1010 0101) from the arduino every 1 second.
I have checked the pins with an oscilloscope and the message coming from the arduino seems to be ok. The speed the arduino is sending messages is I think 4Mhz so speed should not be an issue I think since SPI1 of the discovery board is sitting on a bus with 42Mhz speed.
So basically I just generated the code with cubeMX, I wrote no user code whatsoever other than HAL_SPI_Receive_IT(&hspi1, &_spi_data, 1); before entering the while(1) loop and put a breakpoint in the global ISR handler function: SPI1_IRQHandler from generated file stm32f4xx_it.c.
Now since the arduino is sending data properly judging from what I've seen on the scope I guess there is a problem somewhere with the configuration code from the CubeMX something that perhaps I'm forgetting to do but I can't figure out what exactly.
Now in CubeMX I have some warnings such as:
'Partly disabled conflict with SPI1: Hardware NSS Input signal'. Could that be a problem? I mention that I get these warnings on some peripherals which I have disabled in my project.
Below are the oscilloscope snapshots:
This is MOSI(yellow) data comming from arduino(master).
MISO(blue). I'm not sending anything from the code on my ST board I guess the value there is because of the way SPI works in hardware(circular buffer).
This is SCLOCK coming from the master(arduino) and SS.