AnsweredAssumed Answered

stm32f103c8t6 spi communication with Arduino

Question asked by abo_mosalm.ahmed on Apr 15, 2017
Latest reply on Apr 16, 2017 by abo_mosalm.ahmed

Hello 

 

I devolved a code for stm32 as a master and for arduino as a slave , to send a byte through SPI.

But it did not work. I want to know what is the wrong in my code.

 

STM32 Master Code :

 

static void SPI_Config(void);

int main()
{
SPI_Config();


for (int z=0 ; z<50 ; z++){
SPI_I2S_SendData(SPI1,'A');
}
return 0;
}

 

static void SPI_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
// Configure SPI1_CLKa and SPI1_MOSI
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// SPI1 no remap (NSS/PA4, SCK/PA5, MISO/PA6, MOSI/PA7)
GPIO_PinRemapConfig(GPIO_Remap_SPI1, DISABLE);
// Spi init
/* SPI configuration *****************************************/
SPI_InitStructure.SPI_Direction =
SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler =
SPI_BaudRatePrescaler_64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 0;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}

 

 

Arduino Slave code:

 

#include <SPI.h>
char buff [50];
volatile byte indx;
volatile boolean process;

void setup (void) {
Serial.begin (115200);
pinMode(MISO, OUTPUT); // have to send on master in so it set as output
SPCR |= _BV(SPE); // turn on SPI in slave mode
indx = 0; // buffer empty
process = false;
SPI.attachInterrupt(); // turn on interrupt
}
ISR (SPI_STC_vect) // SPI interrupt routine
{
byte c = SPDR; // read byte from SPI Data Register
if (indx < sizeof buff) {
buff [indx++] = c; // save data in the next index in the array buff
if (c == '\r') //check for the end of the word
process = true;
}
}

void loop (void) {
if (process) {
process = false; //reset the process
Serial.println (buff); //print the array on serial monitor
indx= 0; //reset button to zero
}
}

Outcomes