How to use SPI2

Post here first, or if you can't find a relevant section!
mrguen
Posts: 24
Joined: Tue Mar 03, 2020 1:21 pm
Answers: 2

Re: How to use SPI2

Post by mrguen »

I a m sorry but I can't make SPI work with alternate pins to drive a TFT. It works with default pins (PA5,PA7) but not when using

Code: Select all

  
  SPI.setMOSI(PB_5);
  SPI.setSCLK(PB_3);
  SPI.begin(); 
  // even when adding this in any case 
  __HAL_RCC_I2C1_CLK_DISABLE();
  
ON STM32F401RCT6

other pins are defined as

Code: Select all

  
  #define TFT_CS PB0  // tried PA4, PB4: same
  #define TFT_RST PB9
  #define TFT_DC PB8
  
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: How to use SPI2

Post by fpiSTM »

For core Board, PB5 and PB3 used by default the SPI3 instance and as you keep the PA6 for MISO which use SPI1 then this could not work as SPI instance are not the same.
Try to uncomment PB_5 and PB_3 in the peripheralPins.c or better define your own PinMap_SPI_MOSI and PinMap_SPI_SCLK at sketch level.

Why this:

Code: Select all

  // even when adding this in any case 
  __HAL_RCC_I2C1_CLK_DISABLE();
I think I knwo where you get this but this is not related to STM32F4xx...
mrguen
Posts: 24
Joined: Tue Mar 03, 2020 1:21 pm
Answers: 2

Re: How to use SPI2

Post by mrguen »

@fpiSTM
About __HAL_RCC_I2C1_CLK_DISABLE(); I saw that there was an incompatibility on some boards with I2C1 so I tried in case it would solve the problem.

Yes, uncommenting PB_3 and PB_5 affectation to SPI1 worked.

So the question might be: why it was commented?

Thanks
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: How to use SPI2

Post by fpiSTM »

mrguen wrote: Mon Aug 17, 2020 3:36 pm @fpiSTM
About __HAL_RCC_I2C1_CLK_DISABLE(); I saw that there was an incompatibility on some boards with I2C1 so I tried in case it would solve the problem.
Yes for STM32F103Cx as I've explained in the related issue.
mrguen wrote: Mon Aug 17, 2020 3:36 pm Yes, uncommenting PB_3 and PB_5 affectation to SPI1 worked.
Fine.
mrguen wrote: Mon Aug 17, 2020 3:36 pm So the question might be: why it was commented?
Only the first pin found in the array are used so it was commented to be able to use SPI3 instance. This also allows to save some space. Anyway, I'm currently working to remove this limitations which can currently be solved using the redefinition at sketch level. See above.
mrguen wrote: Mon Aug 17, 2020 3:36 pm Also could you point me to the resource on how to define it at sketch level?
https://github.com/stm32duino/wiki/wiki ... nmap-array
rikysuhendra
Posts: 2
Joined: Sun Sep 13, 2020 4:26 am

Re: How to use SPI2

Post by rikysuhendra »

[ENGLISH]
Hello,

I want to share what I experienced in making the system
who implemented STM32F103C [STM32F103C8T6] stm32duino

in 2019,
Initially, I used the Arduino IDE v.1.5.x software
and using EThernet_STM.h
but because of the design needs that require A0-A9 as a digital input
10-bit absolute rotary sensor, then SPI must be moved from SPI-1 to SPI-2
in this condition I modify the W550.cpp to be like this
C: \ Users \ [your PC] \ AppData \ Local \ Arduino15 \ packages \ stm32duino \ hardware \ STM32F1 \ 2019.12.31 \ libraries \ Ethernet_STM \ src \ utility
-------------------------------------------------- -------------
#include <stdio.h>
#include <string.h>
#include <SPI.h> // add by riky

#include "utility / w5100.h"
#if defined (W5500_ETHERNET_SHIELD)

// W5500 controller instance
W5500Class W5100;

#define SPI_CS 10
// # define STM32_SPI_CS PA4 // edit by riky
#define STM32_SPI_CS PB12 // change by riky
// # define SPI2_NSS_PIN PB12 // add by riky
SPIClass SPI_2 (2); // add by riky

void W5500Class :: init (void)
....
..
etc
-------------------------------------------------- -------------

but after I upgraded the Arduino IDE 1.5.x to v.1.8.x
can no longer ... :(
so I have to do more, and pray to the Lord Jesus
... finally God showed me a directory and a file
and directs me to have to modify the variant.h file that's in
C: \ Users \ [yourPC] \ AppData \ Local \ Arduino15 \ packages \ STM32 \ hardware \ stm32 \ 1.8.0 \ variants \ PILL_F103XX

on the line: [the standard is SPI-1]

// SPI Definitions
#define PIN_SPI_SS PA4
#define PIN_SPI_MOSI PA7
#define PIN_SPI_MISO PA6
#define PIN_SPI_SCK PA5

change to: [SPI-2]

// SPI Definitions
#define PIN_SPI_SS PB12
#define PIN_SPI_MOSI PB15
#define PIN_SPI_MISO PB14
#define PIN_SPI_SCK PB13


and change from Ethernet_STM.h to Ethernet.h

Hallelujah ... Praise God
works ... :)

ok, hope it's useful
Lord Jesus bless you and your family so much - so much - so much
amen!

[INDONESIA]
Halo,

saya mau berbagi apa yang saya alami dalam membuat system
yang menerapkan STM32F103C [STM32F103C8T6] stm32duino

pada tahun 2019,
awalnya, saya mengunakan software Arduino IDE v.1.5.x
dan mengunakan EThernet_STM.h
namun karena kebutuhan design yang membutuhkan A0-A9 sebagai digital input
10-bit absolute rotary sensor, maka SPI harus dipindahkan dari SPI-1 ke SPI-2
pada kondisi ini saya memodifikasi W550.cpp menjadi seperti berikut
C:\Users\[your PC]\AppData\Local\Arduino15\packages\stm32duino\hardware\STM32F1\2019.12.31\libraries\Ethernet_STM\src\utility
---------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <SPI.h> //add by riky

#include "utility/w5100.h"
#if defined(W5500_ETHERNET_SHIELD)

// W5500 controller instance
W5500Class W5100;

#define SPI_CS 10
//#define STM32_SPI_CS PA4 //edit by riky
#define STM32_SPI_CS PB12 //change by riky
//#define SPI2_NSS_PIN PB12 //add by riky
SPIClass SPI_2(2); //add by riky

void W5500Class::init(void)
....
..
dan seterusnya
---------------------------------------------------------------

tetapi setelah saya upgrade Arduino IDE 1.5.x menjadi v.1.8.x
sudah tidak bisa lagi... :(
jadi saya harus ngoprek lagi, dan berdoa kepada Tuhan Yesus
...akhirnya Tuhan tunjukan ke saya suatu direktroy dan file
dan mengarahkan saya harus memodifikasi file variant.h yang ada di
C:\Users\[yourPC]\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\variants\PILL_F103XX

pada baris: [standard nya SPI-1]

// SPI Definitions
#define PIN_SPI_SS PA4
#define PIN_SPI_MOSI PA7
#define PIN_SPI_MISO PA6
#define PIN_SPI_SCK PA5

rubah menjadi : [SPI-2]

// SPI Definitions
#define PIN_SPI_SS PB12
#define PIN_SPI_MOSI PB15
#define PIN_SPI_MISO PB14
#define PIN_SPI_SCK PB13


dan mengganti dari Ethernet_STM.h menjadi Ethernet.h

Haleluya...Puji Tuhan
berhasil...:)

ok, semoga bermanfaat
Tuhan Yesus memberkati anda sekeluarga berlimpah-limpah-limpah
amin!
ridvanmelihh
Posts: 1
Joined: Mon Aug 02, 2021 6:40 am

Re: How to use SPI2

Post by ridvanmelihh »

Hello guys,

I am new this forum. and this is my first post. Hello Again :D

Code: Select all

#include <SPI.h>
#include <RF24-STM.h>
#include <RF24-STM_config.h>
#define CE_PIN PB11
#define MOSI2 PB15
#define MISO2 PB14
#define SCLK2 PB13
#define CSN_PIN2 PB12
#define LED_BUILTIN PC13
//SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel)
SPIClass SPI_2 = SPIClass(MOSI2, MISO2, SCLK2, CSN_PIN2);
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN2);
char data[] = "Hello World"; 
void setup()
 {
 SPI_2.begin();
 Serial.begin(9600);
 radio.begin();
 radio.openWritingPipe(pipe);
 pinMode(LED_BUILTIN, OUTPUT);
 }
void loop()
 {
  radio.write( data, sizeof(data) );
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(800);                        // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(800);                       // wait for a second
 }

In this code I can try to use a SPI2 and NRF24 module. so on my pcb I selected a SPI2 pins and writing this code. My code compilation successfully but even though I cant see the message on the receiver side. Whats the problem? I think problem is the spi2 library, Could you please help me?

Thank You!!
ABOSTM
Posts: 60
Joined: Wed Jan 08, 2020 8:40 am
Answers: 7

Re: How to use SPI2

Post by ABOSTM »

Hello @ridvanmelihh,
Welcome to his forum.
We miss a bit of information to be able to help you:
* Which board are you using? which MCU?
* Which core are you using?
Arduino_Core_STM32 : https://github.com/stm32duino/Arduino_Core_STM32
or
Roger's core : https://github.com/rogerclarkmelbourne/Arduino_STM32

* Which RF24 library ? (it seems there are several for such modules)


That said, I have the feeling that the problem is the RF24 library, which probably use default predefine SPI , and no the SPI_2 you defined.
If it is the case, you can use SPI but change pin depending on your needs (valid for Arduino_Core_STM32):
SPI.setMOSI(MOSI2);
SPI.setMISO(MISO2);
SPI.setSCLK(SCLK2);
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: How to use SPI2

Post by ag123 »

SphericalSound wrote: Sun May 17, 2020 11:53 pm Thank you so much for the info. What are the differences between Roger´s and Official ones?

What would you use?

Thank you so much :)
Roger's core has its origin in libmaple and is specific to stm32f103c{8,b} and specific boards (e.g. maple mini, 'blue pills'), steves added a libmaple core (a complete from scratch creation) for stm32f4xx works on some stm32f4xx boards. both f1 and f4 cores are in roger's repository.
STM's official core is a complete rewrite based around HAL, works on many boards, especially the nucleo boards and many more mcus (including the high end F7 and new G families & other more 'esoteric' mcu e.g. ble integrated mcu like stm32wb). thanks to fpiSTM and other contributors.
the APIs are at least a little different in places between the cores
Post Reply

Return to “General discussion”