Reputation: 21
The MCU I used is STM32F042K6T6. I have configured the SPI in bidirectional mode in order to read the sensor MLX90316. Now the read function is blocked in waiting the the RXNE signal. If I have understood correctly, once I have put the SPI into rx mode, the SCLK is automatically generated and the receiving procedure is started. But the RXNE is never set and nothing in RxFIFO. It seems that the receiving part of the SPI is not working at all. Even I put the MCU in full duplex mode, when I initial the data transmitting by writing the SPI1->DR, the data is transmitted successfully but the problem still remains.
The same code is tested on a STM32F405RGT6 and it functions correctly(if I don't care about the data because the GPIO is not configured). I totally have no idea about this strange problem...
Thank you in advance.:)
The Initial function
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* SPI configuration -------------------------------------------------------*/
SPI_I2S_DeInit(SPI1);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; // Initially Tx
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // Clock steady high
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // Data write on rising (second) edge
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
SPI_Cmd(SPI1, ENABLE);
The tramsmission function is:
void spi_send_8bit(uint8_t Data){
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //wait buffer empty
SPI_SendData8(SPI1, Data);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); //wait finish sending}
uint8_t spi_recv_8bit(void){
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) ; // wait data receive
return SPI_ReceiveData8(SPI1);}
static void Read(void){
MLX90316_1_Set(); //Pull down the CS line
SPI_BiDirectionalLineConfig(SPI1, SPI_Direction_Tx);
spi_send_8bit(0xAA);
spi_send_8bit(0xFF);
while (SPI_GetReceptionFIFOStatus(SPI1) != SPI_ReceptionFIFOStatus_Empty)
SPI_ReceiveData8(SPI1);
SPI_Cmd(SPI1, DISABLE);
SPI_BiDirectionalLineConfig(SPI1, SPI_Direction_Rx);
SPI_Cmd(SPI1, ENABLE);
Data[0] = spi_recv_8bit();
Data[1] = spi_recv_8bit();
Data[2] = spi_recv_8bit();
Data[3] = spi_recv_8bit();
SPI_BiDirectionalLineConfig(SPI1, SPI_Direction_Tx);
spi_send_8bit(0xFF);
spi_send_8bit(0xFF);
spi_send_8bit(0xFF);
spi_send_8bit(0xFF);
MLX90316_1_Cl();}
Upvotes: 2
Views: 8986
Reputation: 1
I have the same problem related, the clock out is never sent, even after the instruction SPI2->CR1 &= ~SPI_CR1_BIDIOE;
My solution: Before the above instruction, put:
SPI2->CR1 |= SPI_CR1_RXONLY;
SPI2->CR1 &= ~SPI_CR1_RXONLY;
This outputs the clock and sets the bit SPI_FLAG_RXNE
.
I already use the internal 48MHz clock.
Upvotes: 0
Reputation: 19
I had the same problem, I solved it by setting up FRXTH bit in SPI1->CR2 register.
Upvotes: 2
Reputation: 11
While this may be an "old" post now I found it so it may be of use to others, I also have a problem with RXNE on both an STM32F030 and 042.
I found single stepping (IAR IDE) that the CPU hangs at the RXNE poll loop:
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
I tried:
while (SPI_GetReceptionFIFOStatus(SYS_SPI) == SPI_ReceptionFIFOStatus_Empty);
Same result, the receiver doesn't let us know it's completed.
I put a breakpoint on the line following these, which logically would not have been reached- it is!!! Is this a SPI peripheral or debug issue perhaps. RXNE's descriotion leaves a lot to be desired, it almost reads as it it's "FIFO ALMOST FULL"
Upvotes: 1
Reputation: 7691
This function won't work:
uint8_t spi_recv_8bit(void){ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) ; // wait datareceive return SPI_ReceiveData8(SPI1);}
You have to write a dummy value to DR
register to trigger the CLK
pulses in SPI master mode. You could use your spi_send_8bit()
function to do that.
Upvotes: 1