SPI Master STM32f411

Any bugs you encounter with Flowcode should be discussed here.
Alan_37
Posts: 170
http://meble-kuchenne.info.pl
Joined: Thu Dec 03, 2020 7:23 pm
Has thanked: 51 times
Been thanked: 22 times

Re: SPI Master STM32f411

Post by Alan_37 »

Hi mnfisher,

Sure, so the SPI slave was not working on the atmega328 with the INIT SPI provided
after a bit of research, I found a way to initialize it with some C-code
I am not 100% sure the init code is as it has to be. but at least now something is working

Hope that Matrix will fix it, the following is the init code to make SPI slave work on the 328P
.

Code: Select all

// Set MISO as output
DDRB |= (1 << PB4);  // MISO
    
// MOSI, SCK, SS as input 
DDRB &= ~((1 << PB3) | (1 << PB5) | (1 << PB2));

// Enable SPI and configure as Slave
SPCR = (1 << SPE);   // Enable SPI in Slave mode, MSTR is cleared automatically for slave
So I have edited a bit my code for the Master and the slave will be including them here

What I am trying to do for Testing is ,
1: Master sends char 79
2: Slave receives 1 Char 79 --> send it back master (79) and then also send char 75
3: Master receive 2 Chars and send them out via rs232

But there is something odd the slave is not transmitting the data in the right sequence
and Master should be receiving 79 and 75 not 79 and 85
.
spi4.jpg
spi4.jpg (41.28 KiB) Viewed 844 times
Attachments
SPI TEST.fcfx
(13.36 KiB) Downloaded 31 times
AT328p.fcfx
(25.94 KiB) Downloaded 28 times

mnfisher
Valued Contributor
Posts: 1133
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 114 times
Been thanked: 596 times

Re: SPI Master STM32f411

Post by mnfisher »

I think the slave needs to be interrupt driven to stand a chance of catching the data.

So I wrote a small test program - that sets up and uses the SPI interrupt to receive data (on the Arduino).

It's hardcoded to receive 3 bytes and output them to UART - I have an Arduino Nano firing off 3 characters ("XBC") - I found I needed a small delay between bytes (I used 1ms) for things to work.

Note that the 'slave' doesn't use the SPI component at all - I just enable the SPI (and it's interrupt) using the default values (in the interrupt enable) and set the SPI pins to be input / output using input and output commands (rather than setting the registers here)

The slave should 'reply' with a value - and I'm not sure how to do that yet....
SPI_Int.fcfx
(13.75 KiB) Downloaded 27 times
And I used the Arduino IDE to send the data (my first attempt in FC - flatlined - but also in Arduino - I just persevered)

Code: Select all

#include <SPI.h>

void setup() {
  // put your setup code here, to run once:
  pinMode(9, OUTPUT);
  SPI.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(9, 0);
  SPI.transfer('X');
  delay(1);
  SPI.transfer('B');
  delay(1);
  SPI.transfer('C');
  digitalWrite(9, 1);
  delay(1000);
}
I used pin 9 as CS on the sender (pin 10 on the receiver)

Martin

Got (way) too late for more experimentation

Alan_37
Posts: 170
Joined: Thu Dec 03, 2020 7:23 pm
Has thanked: 51 times
Been thanked: 22 times

Re: SPI Master STM32f411

Post by Alan_37 »

Hi mnfisher,

Thanks for your reply and help, I will test this this tomorrow
how would you go about sending a char from Flowcode will the following work?
or I can use the Arduino code you provided in a c block ?

Code: Select all

// Load data into the SPI Data Register (SPDR) to transmit
    SPDR = 0x79;

    // Wait for transmission to complete (SPIF flag will be set when done)
    while (!(SPSR & (1<<SPIF)));

Alan_37
Posts: 170
Joined: Thu Dec 03, 2020 7:23 pm
Has thanked: 51 times
Been thanked: 22 times

Re: SPI Master STM32f411

Post by Alan_37 »

Hi, making some progress here

but I still have a problem with the sequence in which the data is transmitted
from the Slave, I am sending 75, 76 , and 77 but on the Locig analyzer I see 77, 75 , 76
slave22.jpg
slave22.jpg (54.7 KiB) Viewed 682 times
.
any idea how can I fix this please?
Attachments
SPI_Int.fcfx
(12.61 KiB) Downloaded 25 times

mnfisher
Valued Contributor
Posts: 1133
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 114 times
Been thanked: 596 times

Re: SPI Master STM32f411

Post by mnfisher »

I managed to get the slave to return data reliably - but only for one character at a time. If I attempt to do 2 bytes - the second value returned is the first value received.
I've tried setting SPDR in main and in the interrupt handler.
You are attempting to send 3 characters in the interrupt handler - which won't quite work (you are waiting for the interrupt to be 'set' again without clearing the flag) (it is also not a good idea to 'block' in an interrupt handler) - SPDR needs to be 'loaded' with a value to send before the master clocks data out to the slave (the slave cannot initiate the data transfer) - as the slave receives a data byte (and clock) from the master it replies with the data held in SPDR.
The sample here returns A..Z (as the first byte) and 'Z' as the second - which is the first byte sent by the master. All the samples I've found just show a single byte transfer (turn on a switch type).

Here check the output from the master (using putty or the Arduino IDE serial monitor)

Code: Select all

#include <SPI.h>

void setup() {
  pinMode(9, OUTPUT);
  SPI.begin();
  Serial.begin(115200);
}

void loop() {
  char c, d;
  digitalWrite(9, 0);   // Pull CS low
  c = SPI.transfer('Z');
  delay(1);						// THIS TO MAKE IT WORK !!!!
  d = SPI.transfer('X');    // d always = 'Z'
  digitalWrite(9, 1);
  Serial.print(c); Serial.println(d);
  delay(1000);
}
Stop press - just got to work with multi-byte. Adding a delay(1); between the transfers in the master fixes things - now the slave returns 'AB' 'CD' etc

Also works using delayMicroseconds(100);

Martin
Attachments
SPI_Int.fcfx
(15.11 KiB) Downloaded 23 times

mnfisher
Valued Contributor
Posts: 1133
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 114 times
Been thanked: 596 times

Re: SPI Master STM32f411

Post by mnfisher »

A little experimenting and a 20uS delay works well...

Here decoding the values returned by the slave...
LabNation_Screenshot5.png
LabNation_Screenshot5.png (135.9 KiB) Viewed 663 times

Alan_37
Posts: 170
Joined: Thu Dec 03, 2020 7:23 pm
Has thanked: 51 times
Been thanked: 22 times

Flowcode v10 Re: SPI Master STM32f411

Post by Alan_37 »

HI Mnfisher,

I just realized that this is not a problem at all cos only the last byte is always put at the beginning
so we can even use this to our advantage example the last byte you send is the number of bytes to be received
by the Master since the master is going to receive it first thing we can use it as a loop count to receive the
rest of the data .

For example, here I am sending 1,2,3,4,5,6,7,8,9,9
.
slave5.jpg
slave5.jpg (58.31 KiB) Viewed 661 times

mnfisher
Valued Contributor
Posts: 1133
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 114 times
Been thanked: 596 times

Re: SPI Master STM32f411

Post by mnfisher »

Looks to be working well!

The code is nice and simple too :-)

Martin

Post Reply