Use 2 SPI buses for DAC and Shift Regsiter

Post here first, or if you can't find a relevant section!
Post Reply
justinjools
Posts: 41
Joined: Thu May 02, 2024 11:10 am

Use 2 SPI buses for DAC and Shift Regsiter

Post by justinjools »

Hi, I want to use two SPI buses, one for DAC and the other for a shift register with STM32F411

Do I do it like this:

// SPI1
SPI.setMOSI(PB15); // MOSI2
SPI.setMISO(PB14); // MISO2
SPI.setSCLK(PB13); // SCK2-4

// SPI2
SPI.setMOSI(PB5); // MOS1-3
SPI.setMISO(PB4); // MISO1-3
SPI.setSCLK(PB3); // SCK1-3
SPI.begin();
GonzoG
Posts: 491
Joined: Wed Jan 15, 2020 11:30 am
Answers: 36
Location: Prudnik, Poland

Re: Use 2 SPI buses for DAC and Shift Regsiter

Post by GonzoG »

If you want them to be 2 different interfaces you need to name them differently.
But remember if you use libraries, some of them have hardcoded only one SPI interface and it's name is SPI, so no matter what you do, they will use same interface.
Composite
Posts: 12
Joined: Fri Feb 16, 2024 11:09 pm

Re: Use 2 SPI buses for DAC and Shift Regsiter

Post by Composite »

#include "SPI.h"
#define CS_PIN1 PA15

SPIClass SPI_i2;
#define CS_PIN2 PB12

Code: Select all

// ADC
  Serial.print(F("\n\tSPI-2..."));
  delay(100);      
  SPI_i2.setMOSI(PB15);
  SPI_i2.setMISO(PB14);
  SPI_i2.setSCLK(PB13);
  SPI_i2.beginTransaction( CS_PIN2, SPISettings(8000000UL, MSBFIRST, SPI_MODE0));
  Serial.print(F("\tdone."));
  delay(100);      

  Serial.print(F("\n\tmcp3562..."));
  delay(100);      
  init_mcp3562();  
  attachInterrupt( 29, data_read, FALLING); // PD2  
  Serial.print(F("\tdone."));
// DAC
  Serial.print(F("\n\tSPI-3..."));
  delay(100);      
  SPI.setMOSI(PC12);
  SPI.setMISO(PC11);
  SPI.setSCLK(PC10);
  SPI.beginTransaction(CS_PIN1, SPISettings(8000000, MSBFIRST, SPI_MODE2));
  Serial.print(F("\tdone."));
  delay(100);
Pins declaration for nucleo-G474re.
justinjools
Posts: 41
Joined: Thu May 02, 2024 11:10 am

Re: Use 2 SPI buses for DAC and Shift Regsiter

Post by justinjools »

Thanks guys.
@GonzoG. I'm using Mozzi DSP audio library and I have the DAC working, fingers crossed I'm able to use 2 SPI bus with it.
@Composite. Thanks for the code, I imagined I would have to define the two SPI's differently. I will test it out today.

The main difference is the SPI.beginTransactions but what are the settings: 8000000UL, MSBFIRST, SPI_MODE0. Does this work for any peripheral?

( CS_PIN2, SPISettings( 8000000UL, MSBFIRST, SPI_MODE0) );

I think I don't need these lines which look like they are for the DAC.

init_mcp3562();
attachInterrupt( 29, data_read, FALLING); // PD2
justinjools
Posts: 41
Joined: Thu May 02, 2024 11:10 am

Re: Use 2 SPI buses for DAC and Shift Regsiter

Post by justinjools »

I just tried to test this code with only 1 SPI bus. I am getting error:

Compilation error: 'SPI' does not name a type; did you mean 'SPI1'?

9 | SPI.beginTransaction(CS_PIN, SPISettings(8000000, MSBFIRST, SPI_MODE2));
| ^~~
| SPI1

Code:

#include "SPI.h"
#define CS_PIN PB1

SPI.setMOSI(PA7);
SPI.setMISO(PA6);
SPI.setSCLK(PA5);
SPI.beginTransaction(CS_PIN, SPISettings(8000000, MSBFIRST, SPI_MODE2));
Composite
Posts: 12
Joined: Fri Feb 16, 2024 11:09 pm

Re: Use 2 SPI buses for DAC and Shift Regsiter

Post by Composite »

shift register 595 drivem by stm32g474re-nucleo:

Code: Select all

  Serial.print(F("\n\tSPI-3..."));
  delay(100);      
  SPI.setMOSI(PC12);
  SPI.setMISO(PC11);
  SPI.setSCLK(PC10);

  SPI.beginTransaction(CS_PIN1, SPISettings(10000000UL, MSBFIRST, SPI_MODE0));
  Serial.print(F("\tdone.")); 
more details on parameters for SPISettings:
https://www.arduino.cc/reference/en/lan ... ation/spi/
Post Reply

Return to “General discussion”