cancel
Showing results for 
Search instead for 
Did you mean: 

Can I use two SPIs as a master and slave in one board? (Nucleo-L432KC)

LMiya
Associate

I'm testing my small project using SPI.

In that project, the board, Nucleo-L432KC (used as a mbed board), has to receive data from a SPI master as a SPI slave, and send processed data to another device as a SPI master.

Then, the board has to work as a SPI slave and a SPI master at the same time.

Nucleo-L432KC has 2 SPI (SPI1 and SPI3) and so I tried to configure both of them, but SCLK output as a master didn't work.

Is it possible to use 2 SPI as a master and a slave?

Configuration

 SPI1 as a slave

 using MOSI = A6, MISO = A5, SCLK = A4, SSEL = D3

 format:(8,3), frequency:6750000

 SPI3 as a master

 using MOSI = D11, MISO = D12, SCLK = D13, SSEL = D9

 format:(8,3), frequency:6750000

 If they are not configured at the same time, it seems to work well.

a part of my code:

  SPI_3_SSEL = 1;
  spi_3.format(SPI_3_BITS, SPI_3_MODE);
  spi_3.frequency(SPI_3_FREQ);
  SPI3->CR1 &= ~0x80;                 //MSBFirst
 
  
 
  spi_1.format(SPI_1_BITS, SPI_1_MODE);
  spi_1.frequency(SPI_1_FREQ);
  SPI1->CR1 |= 0x80;                 //LSBFirst
  SPI1->CR2 |= 0x40;                 //Interrupt
  NVIC_SetVector(SPI1_IRQn, (uint32_t) &intrrptSPI1); //Callback function
  NVIC_SetPriority(SPI1_IRQn, 1);           //Priority
  NVIC_EnableIRQ(SPI1_IRQn);             //Enable interrupt

2 REPLIES 2

> If they are not configured at the same time, it seems to work well.

Read out the content of SPI and relevant GPIO registers - both when configured separately and working, and when configured together and not working - and compare them.

JW

LMiya
Associate

Thank you for your advice!

I checked SPI registers and they were following.

only SPI1

SPI1 CR1:00db

SPI1 CR2:1740

SPI1 SR:00c2 or 0082 or 0042 or 0002

SPI3 CR1:0000

SPI3 CR2:0000

SPI3 SR:0000

only SPI3

SPI1 CR1:035f

SPI1 CR2:1700

SPI1 SR:00c2 or 0082 or 0042 or 0002

SPI3 CR1:0000

SPI3 CR2:0000

SPI3 SR:0000

both SPI1 and SPI3

SPI1 CR1:00db

SPI1 CR2:1740

SPI1 SR:00c2 or 0082 or 0042 or 0002

SPI3 CR1:0000

SPI3 CR2:0000

SPI3 SR:0000

​(The value of SPI1-SR changed at each trial.)

It implys that SPI3 is not used??

I thought the number of SPI is determined by PIN number for MOSI, MISO, SCLK, but that's wrong?

In my code, I define SPIs as following.

const int SPI_3_BITS = 8;
const int SPI_3_MODE = 3;
const int SPI_3_FREQ = 6750000;
const PinName SPI_3_MOSI = D11;
const PinName SPI_3_MISO = D12;
const PinName SPI_3_SCK  = D13;
DigitalOut SPI_3_SSEL(D9);
SPI spi_3(SPI_3_MOSI, SPI_3_MISO, SPI_3_SCK);
 
const int SPI_1_BITS = 8;
const int SPI_1_MODE = 3;
const int SPI_1_FREQ = 6750000;
const PinName SPI_1_MOSI = A6;
const PinName SPI_1_MISO = A5;
const PinName SPI_1_SCK  = A4;
const PinName SPI_1_SSEL = D3;
SPISlave spi_1(SPI_1_MOSI, SPI_1_MISO, SPI_1_SCK, SPI_1_SSEL);

Thank you in advance