cancel
Showing results for 
Search instead for 
Did you mean: 

What should I do when the process stops in the middle of a program?

baby_chicken
Associate III

Hello everyone,

I am writing a program to read the gyro of the ICM42688P, but in the middle of the program, the process stops because it cannot be directed.

Here is the full text of the code↓

 

 

ICM42688P::ICM42688P(SPI_TypeDef *spi2, GPIO_TypeDef *cs_x, uint32_t cs_pin): spi2(spi2), cs_x(cs_x), cs_pin(cs_pin){}


void ICM42688P::setup(void){
	
    LL_SPI_Enable(spi2);

}

void ICM42688P::SPI_TransmitReceive(uint8_t *tx_data, uint8_t *rx_data, uint8_t length) {
    uint8_t count = length;

    //CS Low
    LL_GPIO_ResetOutputPin(cs_x,cs_pin);

    //data clear
    if (LL_SPI_IsActiveFlag_RXNE(spi2) == SET) LL_SPI_ReceiveData8(spi2);

    //SPI、enable
    if (LL_SPI_IsEnabled(spi2) == RESET) LL_SPI_Enable(spi2);

    //sending receiving
    while (count > 0) {
        LL_SPI_TransmitData8(spi2, *tx_data++);  //transmission
        while (LL_SPI_IsActiveFlag_TXE(spi2) == RESET);  //waiting transmission complete
        while (LL_SPI_IsActiveFlag_RXNE(spi2) == RESET);  //waiting reception complete
        *rx_data++ = LL_SPI_ReceiveData8(spi2);  //storage
        count--;
    }

    //CS High
    LL_GPIO_SetOutputPin(cs_x,cs_pin);
}

void ICM42688P::whoami() {
    uint8_t tx_data[2];
    uint8_t rx_data[2];

    //who_am_i  read
    tx_data[0] = (0x75 | 0x80);  //read
    tx_data[1] = 0x00;  //dummy

    SPI_TransmitReceive(tx_data, rx_data, 2);

    printf("who am i = %x\r\n", rx_data[1]);
}

 

 

 

The process stops in the following part↓

 

 

 

while (LL_SPI_IsActiveFlag_TXE(spi2) == RESET); 

while (LL_SPI_IsActiveFlag_RXNE(spi2) == RESET);

 

 

 

However, if this part is deleted, the process will not stop but will not return the value expected by whoami.

Is there any good solution?
Thank you in advance.

 

microcontroller stm32f407vgt6, cubeIDE

13 REPLIES 13
Techn
Senior III

First and foremost is to identify if you have a hardware issue or not. Then the communication sequence with the sensor. Once you cross this barrier, you can use any API. 

This will help you more

https://controllerstech.com/how-to-use-spi-with-stm32/

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

Sorry for the late reply.


I did some research on this program and found that if the FRXTH bit is not set, the RXNE bit is not cleared and the process stops without getting out of the while(LL_SPI_IsActiveFlag_RXNE(SPIx) == RESET ) process.

 

Even if I know the cause of the stoppage, I don't know how to solve it.

 

Best regards.

Which GPIO is used for 

cs_x,cs_pin)

the the above code please share the definition for CS_X and CS_PIN.

In addition, for your while loop where you wait for TXE  or RXNE, add some timeout and after timerout, check what error is reported y reading the SPI status registers SPI_SR and take corrective action.

Did you see FRXTH bit  is applicable for this processor, it may be for another processor?. As I told earlier, you have to try with HAL_SPI API to avoid these kinds of complication of timeout and setting various bits. you can go through the HAL code to understand how the program flow happening for HAL_SPI_.......................

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

GPIOE, GPIO_PIN_8) in main.cpp.

 

Best regards.