cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103RB SPI latency

felipe
Associate II
Posted on February 04, 2013 at 17:30

Hi all,

I trying to use the SPI and I am using uVision Keil. The clock appear with a latency that I can take off by removing

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

But when I do that I read wrong the data. The 3 last bytes is what I have to read from ADS1 The first byte is the command for read the conversion (ADC). Here's the code:

/* SPI1 configuration ------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); /* Enable SPI1 */
SPI_I2S_SendData(SPI1, 0x01); /* Send SPI1 data (LEITURA ADS1241)*/
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0){} /* Espera conversão*/
while (TxIdx < 3){
SPI_I2S_SendData(SPI1, 0x00); /* Send SPI1 data */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI1_Buffer_Rx[TxIdx] = SPI_I2S_ReceiveData(SPI1);
TxIdx++;
}
TxIdx=0;

And here's the latency. If I use the SPI_BaudRatePrescaler with 128 the latency go away. But its too slow. 0690X000006052fQAA.png I hope somebody can help me cuz I really need this done and I have no ideia what is going on.
4 REPLIES 4
Posted on February 04, 2013 at 17:37

Perhaps if you want it to sustain a stream you need to send bytes when TXE is set, and read when RXNE is set, instead of serializing the operation so it sequences in the least efficient manner?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
felipe
Associate II
Posted on February 04, 2013 at 18:15

Thanks for the answer Clive.

What do you mean with''instead of serializing the operation so it sequences in the least efficient manner?'' I am sending a byte when RXNE is set. or not? I put the TXE nothing changes.

while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, 0x01); /* Send SPI1 data (LEITURA ADS1241)*/
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
// DelayUs(1); 
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0){} /* Espera conversão*/ 
while (TxIdx < 3){
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); 
SPI_I2S_SendData(SPI1, 0x00); /* Send SPI1 data */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI1_Buffer_Rx[TxIdx] = SPI_I2S_ReceiveData(SPI1);
TxIdx++;
}

Posted on February 04, 2013 at 18:50

Serializing, ie doing ONE thing at a time and precluding advancement to the next state until the perceived completion of the current state, ignoring the fact that there is in fact a buffer, and the operations are not mutually exclusive.

i = 0;
j = 0;
while(j < 4)
{
if ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) != RESET) && (i < 4))
SPI_I2S_SendData(SPI1, TxData[i++]);
if (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) != RESET)
RxData[j++] = SPI_I2S_ReceiveData(SPI1);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
felipe
Associate II
Posted on February 04, 2013 at 20:20

Thank you, it worked.

Sorry for the ignorance and a beginner in SPI e STM32.

Bye