[SOLVED] SPI STM32F407 not working

acronis
Tue Nov 07, 2017 12:10 pm
Hello.

Do the project with the MagnetoResistive sensor KMA200. (https://www.nxp.com/docs/en/data-sheet/KMA200.pdf)

board – STM32F407VET6 BLACK
Library – LeafLabs Maple (https://github.com/stevstrong/Arduino_STM32/)

SPI not working !

// http://hex.pp.ua/stm8-kma200.php
//http://forum.arduino.cc/index.php?topic=154796.0
// http://www.anagate.de/download/AnaGateSPI_KMA200-EN.pdf

//http://www.electronics.ru/files/article_pdf/3/article_3331_811.pdf

/*

angle sensor: KMA200 by NXP Semiconductors; from the datasheet:
-Pin 1 - Vdd (+5V)
-Pin 2 - Data I/O (PA6)
-Pin 3 - CLK (PA5)
-Pin 4 - GND (GND)
-Pin 5 - CS (PA4)

-SPI Info:
1-1000 kHz clock
MSB first

Using the first SPI port (SPI_1)
SS <--> PA4 <--> BOARD_SPI1_NSS_PIN
SCK <--> PA5 <--> BOARD_SPI1_SCK_PIN
MISO <--> PA6 <--> BOARD_SPI1_MISO_PIN

*/

#include <SPI.h>

#define BOARD_SPI1_NSS_PIN PA4 //Port2Pin('A', 4)

int sensor_data[3] = { 0, 0, 0 };

void setup() {

SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV128);
pinMode(BOARD_SPI1_NSS_PIN, OUTPUT);

}

void loop() {

digitalWrite(BOARD_SPI1_NSS_PIN, LOW);
sensor_data[0] = SPI.transfer(0);
sensor_data[1] = SPI.transfer(1);
sensor_data[2] = SPI.transfer(2);
digitalWrite(BOARD_SPI1_NSS_PIN, HIGH);
delay (100);
}


stevestrong
Tue Nov 07, 2017 12:32 pm
Of course is SPI working.
Only that you did not declare the right SPI instance.
Per default, SPI3 is used as SPI instance, because it is the most commonly used interface on the black F4 board.

In order to use SPI1, you have to declare your own SPI instance:
SPIClass spi(1);


acronis
Tue Nov 07, 2017 12:37 pm
OK.
now I will try.
Thank you !

acronis
Tue Nov 07, 2017 12:46 pm
Steven
whether I changed the code to SPI1 ?


#include <SPI.h>

#define BOARD_SPI1_NSS_PIN PA4 //Port2Pin('A', 4)

int sensor_data[3] = { 0, 0, 0 };

SPIClass spi(1);

void setup() {

spi.begin();
spi.setBitOrder(MSBFIRST);
spi.setDataMode(SPI_MODE0);
spi.setClockDivider(SPI_CLOCK_DIV128);
pinMode(BOARD_SPI1_NSS_PIN, OUTPUT);

}

void loop() {

digitalWrite(BOARD_SPI1_NSS_PIN, LOW);
sensor_data[0] = spi.transfer(0);
sensor_data[1] = spi.transfer(1);
sensor_data[2] = spi.transfer(2);
digitalWrite(BOARD_SPI1_NSS_PIN, HIGH);
delay (100);
}


zmemw16
Tue Nov 07, 2017 1:29 pm
ok, this is going to get confusing, it was the first time i tried to describe it.

looking at the schematic, PB3, PB4, PB5 are labelled outside the cpu frame as SPI1_CLK, SPI1_MISO, SPI1_MOSI

inside the frame and inside parenthesis in the label e.g.
BOOT1/PB2 (TIM2_CH2/SPI1_SCK)/SPI3_CLK/TRACESWO/I2S3_CK/JTDIO/PB3


acronis
Tue Nov 07, 2017 1:34 pm
Hello Stephen.

How to remap SPI1 ?
Could you tell me the code please


stevestrong
Tue Nov 07, 2017 1:44 pm
I suggest to use:
spi.beginTransaction(SPISettings(1000000)); // 1 MHz
//spi.begin();
//spi.setBitOrder(MSBFIRST);
//spi.setDataMode(SPI_MODE0);
//spi.setClockDivider(SPI_CLOCK_DIV128);

acronis
Tue Nov 07, 2017 2:20 pm
Steven, I’m confused (

What am I doing wrong ?
I need to do it on SPI1 .
Redid code


/*
Using the first SPI port (SPI_1)
SS <--> PA4 <--> BOARD_SPI1_NSS_PIN
SCK <--> PA5 <--> BOARD_SPI1_SCK_PIN
MISO <--> PA6 <--> BOARD_SPI1_MISO_PIN
*/

#include <SPI.h>

#define BOARD_SPI1_NSS_PIN PA4 //Port2Pin('A', 4)

int sensor_data[3] = { 0, 0, 0 };

SPIClass spi(1);

void setup() {

spi.beginTransaction(SPISettings(1000000)); // 1 MHz
pinMode(BOARD_SPI1_NSS_PIN, OUTPUT);

}

void loop() {

digitalWrite(BOARD_SPI1_NSS_PIN, LOW);
sensor_data[0] = spi.transfer(0);
sensor_data[1] = spi.transfer(1);
sensor_data[2] = spi.transfer(2);
digitalWrite(BOARD_SPI1_NSS_PIN, HIGH);
delay (100);
}


zmemw16
Tue Nov 07, 2017 4:49 pm
b2/b3/b4 can be either one of ‘spi1 a5-a7 remapped‘ and (more correctly to me) still labelled SPI1 OR as ‘spi3 b2-b4 native‘ and then re-named (at least to me incorrectly) as SPI1
by native i mean it NOT being subject to afio_remap.
i think you may well need a re-map/disable the debug pins call.

if i were to use cubemx to do this, as per their defined native function (left mouse drop down menu) pin functions by their initialising the pins as spi1 remapped or as spi3(native) and label them as spi1 in the pin user name(right click drop down).
the remap option to me seems the way to match the cpu pin naming to the board as per the schematic.

spi remap has been done before, site search using >afio_remap spi<
viewtopic.php?f=3&t=2672&p=35479&hilit= … spi#p35479

don’t even think about I2C1 & I2C2 :!:

it’s all a bit of a conundrum :D one way it’s board orientated, the other it’s cpu orientated.
i ran into this when i was trying to put together a cubemx file for this, hence my earlier’going to get confusing’ comment.

stephen


stevestrong
Tue Nov 07, 2017 10:22 pm
Well, I just tried this example with my current repo:
#include <SPI.h>

#define LED_PIN1 PA6 //PB9 //
#define LED_PIN2 PA7

uint16_t counter;
SPIClass spi(1);
#define SS_PIN PA4
// the setup function runs once when you press reset or power the board
void setup()
{
pinMode(LED_PIN1, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
pinMode(SS_PIN, OUTPUT);
Serial.begin(115200);
counter = 0;
spi.beginTransaction(SPISettings(1312500));
}

// the loop function runs over and over again forever
void loop() {
Serial.println(counter++);
digitalWrite(LED_PIN1, LOW);
digitalWrite(LED_PIN2, HIGH);
delay(500); // wait for a second
digitalWrite(LED_PIN1, HIGH);
digitalWrite(LED_PIN2, LOW);
delay(500); // wait for a second

digitalWrite(SS_PIN, LOW);
spi.transfer(0x55);
digitalWrite(SS_PIN, HIGH);
}


acronis
Wed Nov 08, 2017 12:15 pm
Dear friends !

Thank you all for your help in my question !
A special thank you stevestrong !
YOUR code works fine !!!

All of you correctly wrote to me. (((
I was not paying attention, the problem was in the wire connecting the CS of the microcontroller and the sensor wire was bad (Chinese quality) and there was no contact.
Changed the wire and now the management of the sensor occurs, but there is no answer because of too high speed SPI.

I need to configure the STM32F407 on the frequency of 128 MHz .
Please tell me how it right to do ?
Can it be done in Setup () ?


stevestrong
Wed Nov 08, 2017 12:35 pm
Are you really sure that 1.3125 MHz or 0.65625MHz is not good enough?

If you set the CPU to 128MHz, the USB will not work.
If you want to keep the USB serial, then 120MHz is the solution, where the SPI clock will be 0.9375MHz.
For this, you can try to use in setup:
SetupClock120MHz();


acronis
Wed Nov 08, 2017 12:41 pm
Steven , and how to make 0.65625 MHz ?

stevestrong
Wed Nov 08, 2017 1:04 pm
spi.beginTransaction(SPISettings(1000000));

acronis
Wed Nov 08, 2017 1:07 pm
Now it is clear.
And then I thought that there really is 1 000 KHz.
Thank You again !!!

Leave a Reply

Your email address will not be published. Required fields are marked *