Nucleo 64 F401-RE spi problem

Post here first, or if you can't find a relevant section!
Post Reply
Plexi47
Posts: 4
Joined: Fri Feb 04, 2022 3:04 pm

Nucleo 64 F401-RE spi problem

Post by Plexi47 »

Hi all,
As I wrote in the presentation topic I'm building a film scanner that utilizes 3 stepper motors.
On top I need also to control 2 leds for which I use a DAC (MCP4822) that is controlled via SPI.
Because the stepper shield (Arduino CNC shield) use the same pins of SPI-1 (D11 MOSI D13 SCLK)
I switched to SPI-3 declaring in the program before SPI.begin
SPI.setMOSI(PC12);
SPI.setMISO(PC11);
SPI.setSCLK(PC10);
and SPI works , but when I insert the CNC shield , program hangs up.
If I disconnect the shield pins D13 and D11it works normally.
That means,I believe that SPI-1 is still active .
How can I do to to overcome this problem ?
Any idea ?
Thanks a lot
Mauro
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Nucleo 64 F401-RE spi problem

Post by fpiSTM »

Could you share your sketch?
Plexi47
Posts: 4
Joined: Fri Feb 04, 2022 3:04 pm

Re: Nucleo 64 F401-RE spi problem

Post by Plexi47 »

Code: Select all

#include <Wire.h>
#include <MCP48xx.h>
MCP4822 dac(PC9);

int Pulse = LOW;
int I2C_msg; // Stores command I2C from Raspberry PI

//various IO pins
#define UVled         A2      // UV intensity
#define OnBoardLed    13      // arduino led
#define Switch        A5      // Enable switch control

 

//------------ Stepper motors control ----------------
#define StepFeed   2       // Step      feed motor   
#define EnFeed    13       // enable    feed motor
#define DirFeed    5       // direction feed motor

#define StepCapst  3       // Step      capstan motor
#define EnCapst    8       // enable    capstan motor
#define DirCapst   6       // direction capstan motor

#define StepWind   4       // Step       winding motor 
#define EnWind    12       // enable     winding motor
#define DirWind    7       // direction  winding motor

int voltPowerLed = 1300;
int voltUVLed    = 1300;






void setup() {

    // DAC initializing
    SPI.setMOSI(PC12);
    SPI.setMISO(PC11);
    SPI.setSCLK(PC10);
    dac.init();
    dac.turnOnChannelA();
    dac.turnOnChannelB();
    dac.setGainA(MCP4822::High);
    dac.setGainB(MCP4822::High);
    dac.setVoltageA(voltPowerLed);
    dac.setVoltageB(voltUVLed);
    dac.updateDAC();

    
  Serial.begin(9600);
  Wire.begin(16);  // join I2C bus with address #16
//  Wire.onReceive(receiveEvent); // register event
//  Wire.onRequest(sendexp);

 // set pinMode Stepper motors
  pinMode(StepFeed, OUTPUT);
  pinMode(DirFeed, OUTPUT);
  pinMode(EnFeed, OUTPUT);
  pinMode(StepCapst, OUTPUT);
  pinMode(DirCapst, OUTPUT);
  pinMode(EnCapst, OUTPUT);
  pinMode(StepWind, OUTPUT);
  pinMode(DirWind, OUTPUT);
  pinMode(EnWind, OUTPUT);
  
 

  // enable position feed_motor
  digitalWrite(EnFeed, HIGH); //OFF

 // set direction on stepper motors
  digitalWrite(DirFeed, LOW);
  digitalWrite(DirCapst, LOW);
  
 
  
  // set step on stepper motors
  digitalWrite(StepFeed, LOW);
  digitalWrite(StepCapst, LOW);
  digitalWrite(StepWind, LOW);
}







this is the first part of the sketch, with only this I have the problem,looks like the library is starting the SPI-1 and then after my declaration also SPI-3
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Nucleo 64 F401-RE spi problem

Post by fpiSTM »

Thanks.

So If I well understand the MCP using the default SPI instance which uses by default the D11/12/13. For Nucleo F401 it is: PA7, PA6, PA5 (SPI1 peripheral)
And for the CNC shield you want use the PC12, PC11 and PC10 (SPI3 peripheral).

In your sketch you do this:

Code: Select all

    SPI.setMOSI(PC12);
    SPI.setMISO(PC11);
    SPI.setSCLK(PC10);
In fact you change the default pins of the default SPI instance.
In that case SPI instance no more use PA7, PA6, PA5 but PC12, PC11 and PC10.
As the MCP48xx library uses the default SPI instance it finally uses the SPI3 peripheral.
https://github.com/SteveGdvs/MCP48xx/bl ... 48xx.h#L75

You can declare a new one like this:
// MOSI MISO SCLK
SPIClass SPI_3(PC12, PC11, PC10);

Pay attention that often library uses the default SPI instance with no option to change it.
A solution is to use the same SPI but use a different CS.

For ref:
https://github.com/stm32duino/wiki/wiki/API#spi
Plexi47
Posts: 4
Joined: Fri Feb 04, 2022 3:04 pm

Re: Nucleo 64 F401-RE spi problem

Post by Plexi47 »

It works!
Problem was in the library that uses default SPI.
Modify the library by ,
As per you suggestion , open a new class SPI_3 and changed all
commands related.Now is OK ,no default SPI opened and SPI_3
working.
Thanks a lot
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Nucleo 64 F401-RE spi problem

Post by fpiSTM »

Welcome ;)
Post Reply

Return to “General discussion”