cancel
Showing results for 
Search instead for 
Did you mean: 

SPI communication between f030R8 (slave) and ESP32 (master)

Cmuel.1
Associate

Hello

I am trying to setup SPI communication between an ESP32 as Master (programmed with arduino ide) and an f030R8 programmed with the Cube IDE.

The stm is reading analog values from its adc pin in the main loop and should transmit them to the master on request. it should basically act like an LSM6DSM where the master sends what register it wants to read and the slave should reply the data that was caprured in the main loop.

Right now i am reading analog data from one pin in the main loop and store it in a variable.

i have setup an interrupt on the slave sellect line to handle the spi communication.

here is my esp code

#include<SPI.h>       //Library for SPI
 
#define SPI_SCK 18 //Serial Clock(Master Output)
#define SPI_MOSI 23 //Master Output,Slave Input
#define SPI_MISO 19 //
#define SPI_CS 5 //Chip Select,Slave Transmit Enable(active low,Master 
#define SS 5
char reciveData;
 
char retchr;
String command;
 
void setup (void) {
   Serial.begin(115200); //set baud rate to 115200 for usart
   pinMode(SPI_CS, OUTPUT);
 
   pinMode(SPI_SCK, OUTPUT);
   pinMode(SPI_MOSI, OUTPUT);
   pinMode(SPI_MISO, INPUT);
 
   digitalWrite(SS, HIGH); // disable Slave Select
   SPI.begin (SPI_SCK,SPI_MISO,SPI_MOSI);
   SPI.setBitOrder(MSBFIRST);
   SPI.setDataMode(SPI_MODE0);
   SPI.setClockDivider(SPI_CLOCK_DIV8); //divide the clock by 8=2 MHz 
}
 
void loop (void) {
   char c;
   
 
    if (Serial.available() > 0) {
    // read the incoming byte:
 
    command = Serial.readStringUntil('\n');
    if (command.equals("1")) {
    
      Serial.println("I received: 1");
      digitalWrite(SS, LOW); // enable Slave Select
      Serial.println("Sending 1: ");
      reciveData=SPI.transfer(1);
      //Serial.print(c);
     // Serial.print(reciveData); // MOSI MISO Link gebt echo retour
      
      while (reciveData != '\r')
      {
      reciveData=SPI.transfer(0x00);
      Serial.print(reciveData);
      delay(7);
      }
 
   digitalWrite(SS, HIGH); // disable Slave Select
   //delay(5000);
   
    }
 
 
 
 
    else  {
      digitalWrite(SS, LOW); // enable Slave Select
     
      reciveData=SPI.transfer(0);
      //Serial.print(c);
      Serial.println(reciveData); // MOSI MISO Link gebt echo retour
 
   digitalWrite(SS, HIGH); // disable Slave Select
 //  delay(5000);
   
    }
    
  }
 
}

and here is my stm code

char msg3[] = "abcdef\n\r";
int main(void)
{
 
	char msg2[10];
 
  HAL_Init();
 
 
  SystemClock_Config();
 
  MX_GPIO_Init();
  MX_SPI1_Init();
  MX_USART2_UART_Init();
  MX_ADC_Init();
  /* USER CODE BEGIN 2 */
 
  for (;;)
  	{
	  int blalslsl = 0;
	  HAL_ADC_Start(&hadc);
	  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
	  raw = HAL_ADC_GetValue(&hadc);
	  sprintf(msg2, "%hu\r\n", raw);
	  blalslsl = g_ADCValue;
	  g_ADCValue = raw;
	//  msg = "Running Main Loop!\n\r";
	  HAL_UART_Transmit(&huart2, (uint8_t*)msg2, strlen(msg2), HAL_MAX_DELAY);
  	  HAL_Delay(1000);
 
  	}
 
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
 
  /* USER CODE END 3 */
}
 
/* USER CODE BEGIN 4 */
 
 
void HAL_GPIO_EXTI_Callback ( uint16_t GPIO_Pin )
{
	if (GPIO_Pin == ss_Pin_Pin)
	{
 
		    	 HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)&msg3, (uint8_t*)&input, strlen(msg3), 1000);
 
		         //output = input;
		    	 if (input == 1) {
		    		// *msg3 = "ghjk\n\r";
		    		// sprintf(msg3, "%hu\r\n", raw);
 
		    	 }
 
		    	 if (input == 2) {
		    	    output = 5;
		    	 }
 
		    	 if (input == 0X0F) {
		    	    output = 0x69;
		    	 }
 
	}
 
	else
	{
		__NOP ();
	}
}
/* USER CODE END 4 */

right now i am just listening for incomming spi communication in the interrupt and answering with my test string msg3 = abcdef

this basically works. altho it is always sending two random characters before the actual abcde.

the idea is to look for the recived data and depending on what is send from the Master return the correct data via the if cases.

anny help on that would be greatly appriciated.

is this even the right way to do that? or is there an easier way to do this?

thanks in advance for anny help.

0 REPLIES 0