cancel
Showing results for 
Search instead for 
Did you mean: 

SPI mode change

YK.1
Associate II

Hi all,

I'm working on Full-duplex SPI with Cube-MX and HAL.

I'm going to dynamically set the SPI mode(Master/ Slave). So, init the SPI according to the input pin (input pin=1 for master and 0 for slave). The master MCU Transmit and the slave receive correctly. But, the slave Tr. and also master RX doesn't work. But, when I change the slave to the code which is only initiated as a slave, the master Rx and slave Tr. are correct, too.

The problem is here, I need to have a same code for master and slave and set the mode to depend on the input pin(as mentioned before).

7 REPLIES 7
TDK
Guru

Is there a question here? "X doesn't work" is hardly enough info.

Master and slave do different things and will need to run different code.

If you feel a post has answered your question, please click "Accept as Solution".
YK.1
Associate II

Thanks for your response.

I know what you mean. But, I need to select the spi mode depend on external condition(GPIO read) so, may I have a chance to merge the master and slave code in a unique code and init the spi mode to change according to the external condition at the first init.(Not during the the operation and dynamically)?

As TDK wrote, elaborate on "does not work". What are the observed symptoms and how do they differ from the expected behaviour?

Also give us more info on your setup. What hardware, which STM32? What is the counterpart in the other end of the bus?

JW

YK.1
Associate II

Thank you. Let me explain again in detail.

I need to have a full-duplex SPI and I'm using Cube-Mx and HAL on STM32F103RET6 MCU.

Here are two STM32f103 which are connected together for SPI transmission.

1)The 1st scenario:

   a)Set one of MCUs as a master->Init as a master (master code)

   b)Set the second one as a slave->Init as a slave (slave code)

   It results in correct full-duplex SPI. 

2)The 2nd scenario (Which I need to have):

The mode of MCU(Master/Slave) will be determined from the external command on GPIO Pin.

Assuming: for first MCU PA0=1 then this MCU should be a master and for the second one, PA0 will be 0. So, it should play a slave role.  

I've combined master and slave code(which are in the 1st scenario) as following:

(SPI init.)

if(HAL_GPIO_ReadPin(PowerLoop_GPIO_Port,PowerLoop_Pin)==1)

{

 masterFlag=1;

}

elseif(HAL_GPIO_ReadPin(PowerLoop_GPIO_Port,PowerLoop_Pin)==0)

{

slaveFlag=1;

}

  if (masterFlag==1)

  {

   MX_SPI1_Master_Init();    

  }

 if ( slaveFlag==1)

  {    

  MX_GPIO_Slave_Init();

  }

The problem is here. by using of combined code the Slave transmit. dosen't work even though.

It seems to be the same as the first scenario by using flags at the beginning and before initiation of SPI but it doesn't.  

    

TDK
Guru

There is no problem doing what you're doing. You just need to ensure the peripherals are initialized correctly in either case. HAL_SPI_Init() takes parameters which indicate slave or master. GPIO settings are the same for both, except for the CS pin.

  if (masterFlag==1)
  {
   MX_SPI1_Master_Init();    
  }
 if ( slaveFlag==1)
  {    
  MX_GPIO_Slave_Init();
  }

So if it's a master, you initialize the SPI peripheral and if it's a slave, you initialize GPIO pins but no peripheral? Why would you expect that to work?

If you feel a post has answered your question, please click "Accept as Solution".
YK.1
Associate II

Surry. This mistake happened during copy-paste action.

Both of GPIO&SPI are initiate individually for master or slave. In other word,for master gpio and spi initiate as a master and for the slave gpio and spi initiate a slave.

I just change MX_GPIO_Init() and MX_SPI_Init() which are exactly same as what are in the master or slave code(first scenario). So, is there any header or library or something like that should be considered?

TDK
Guru

Be aware that MAX_SPIx_Init calls HAL_SPI_MspInit, which must do the correct thing based on if you want master or slave.

Hard to debug code when it has typos introduced from copy/paste. Good luck.

If you feel a post has answered your question, please click "Accept as Solution".