2023-05-04 01:24 AM - edited 2023-11-20 06:29 AM
I'm getting introduced to the STM32 environment and I have been trying to use SPI protocol to send messages between these two boards. My setup is as follows:
And in the CubeIDE both boards have this setup, with the difference that the master board has the selection of Full duplex master in SPI and the slave Full duplex slave. The clocks are configured the same way in both boards and I believe I'm not leaving anything.
The master code is the following:
#include "main.h"
#include <stdio.h>
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart2;
uint8_t data_rx[1];
uint8_t data_tx[1];
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI1_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_SPI1_Init();
while (1)
{
data_tx[0] = 3;
HAL_SPI_Transmit(&hspi1, data_tx, sizeof(data_tx), 5000);
}
}
The slave code is the following:
#include "main.h"
#include <stdio.h>
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart2;
uint8_t data_rx[1];
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI1_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_SPI1_Init();
while (1)
{
HAL_SPI_Receive(&hspi1, data_rx, sizeof(data_rx),5000);
}
}
I'm trying to make it as simple as possible but with these codes and the setup I have detailed before I'm not getting anything at data_rx. I don't know what is wrong as there is not much to see.
Any recommendations to use SPI protocol in this context will be appreciated, thank you.
2023-05-04 03:41 AM
Observe signals using oscilloscope/LA.
2023-05-04 04:25 AM
...and please check MOSI/MISO:
Currently you have connected output with output and input with input.
So you would have to cross both.
[edit]my assumption was wrong, the wiring is correct[/edit]
Regards
/Peter
2023-05-04 05:16 AM
> Currently you have connected output with output and input with input.
I see MOSI from master connected to MOSI of slave, and MISO from slave connected to MISO of master, so it's input to output and output to input as it's supposed to be.
JW
2023-05-04 05:21 AM
Oops, you are right, Jan, I was confused with another communication.
2023-05-04 08:24 AM
How do you have the NSS configured for master and slave (show your MX_SPI1_Init code)? Also make sure that the slave it running (and waiting) before you start the master. Off hand I am not sure how the SPI hardware behaves if it were to start receiving in the middle of a packet. You should add a delay in the master's loop to make it easier to debug (so there is a gap between packets). And, as always, check the return values from the HAL calls - probably not an issue here but a good habit.
2023-05-04 07:30 PM
do you not think that you need to make the CS(chip select) pin from high to low for communication?
first, make the cs pin from high to low
and then try the same, I guess it will solve your issue if I am not wrong then.
2023-05-08 04:13 AM
I don't know why I didn't think about this. This fixed the communication problem, as the master is now sending messages to the slave and the slave recieves them correctly. The issue I have now is to send a message from the slave to the master.