cancel
Showing results for 
Search instead for 
Did you mean: 

SPI communication between 2 STM32s

jki79ros81
Associate II
Posted on February 04, 2009 at 10:32

SPI communication between 2 STM32s

1 REPLY 1
jki79ros81
Associate II
Posted on May 17, 2011 at 13:01

Hi, everyone!

I am working with SPI communication between 2 stm32s .

( A - STM32F103RB as master , B - STM32F103RD as slave ).

The A send a command to the B , the B process the command then send response to the A.

The maximum command processing time is 5 second.

The A send a ''capture command'' and can send ''cancel command'' for it.

There is my code.

///////////////////////////////////////////////////

//. Master

.....................

u8 Capture_Command[24] = ''0x55,0xAA,..........'';

u8 Response[24];

//. send capture command

for(i = 0; i < 24; i++){

while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

SPI_I2S_SendData(SPI1, Capture_Command[i]);

}

//. receive response

while (1){

while (SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET);

SPI_I2S_SendData(SPI1, 0xFF);

while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

w_nData = SPI_I2S_ReceiveData(SPI1);

if( w_nData != 0x00){

Response[i] = w_nData;

for( i = 1; i< 24; i++){

while (SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET);

SPI_I2S_SendData(SPI1, 0xFF);

while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

w_nData = SPI_I2S_ReceiveData(SPI1);

Response[i] = w_nData;

}

break;

}

}

///////////////////////////////////////////////////

//. Slave

int main (void)

{

................................

while(1) {

//. Process Command

if(g_blReadCommand == TRUE){

// CommandProcess();

SendResponse();

g_blReadCommand = FALSE;

}

}

void SPI2_IRQHandler(void)

{

u8 w_nData = 0;

u16 w_nLen = 0;

if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == RESET) {

return;

}

w_nData = SPI_I2S_ReceiveData(SPI2);

if( g_nPacketLen == 0 ){

if(w_nData == 0x55 ){

PACKET_DATA[0] = w_nData;

g_nPacketLen = 1;

}

else{

}

return;

}

if( g_nPacketLen == 1 ){

if(w_nData == 0xAA ){

PACKET_DATA[1] = w_nData;

g_nPacketLen = 2;

}

else{

g_nPacketLen = 0;

}

return;

}

PACKET_DATA[g_nPacketLen++] = w_nData;

if( PACKET_DATA[0] == 0x55 && PACKET_DATA[1] == 0xAA){

if( g_nPacketLen == 24){

g_blReadCommand = TRUE;

g_nPacketLen = 0;

}

}

else{

g_nPacketLen = 0;

}

}

void SendResponse()

{

u8 Test_Res[25] = {0xAA, 0x55 , 0x50 , 0x01 , 0x04 , 0x00 , 0x00 , 0x00 , 0x03 , 0x05 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00, 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x5C , 0x01 };

u16 i;

for( i = 0; i < 24; i++){

while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

SPI_I2S_SendData(SPI2, Test_Res[i]);

}

}

/////////////////////////////////////////////////////////

I used fullduplex mode.

When the master(A) send command data ,the slave receive it correctly.

But when slave send response, master not receive it.

What is wrong?

Please help me.

Thank for you help and advice.

[ This message was edited by: jki79ros81 on 04-02-2009 15:05 ]

[ This message was edited by: jki79ros81 on 04-02-2009 15:07 ]