Skip to main content
Explorer II
August 26, 2024
Question

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

  • August 26, 2024
  • 3 replies
  • 2251 views

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

    This topic has been closed for replies.

    3 replies

    Graduate II
    August 29, 2024

    These portions of code are used for depuration purpose. If your code gets stucked here is because something is not working properly, thats why when you delete them the result isn`t the expected one. I suggest you to try and find the problem maybe checking where these flags change to this state that makes the program get stucked.

    Explorer II
    August 29, 2024

    Thank you for your reply, @RPC 

    I would like to review this code once again.

    Graduate II
    August 30, 2024

    Is your spi CS is done automatic or manual? Make sure to enable the chipSelect for the device. Check the sPI speed,  data width etc. you can try with HAL calls to ensure that the chip is responding, before going into the LL calls. This is just a suggestion to rule out any hardware issues. Another point is that after you apply reset to the chip, you need to wait for some time till the chip is ready to take any commands..

    Graduate II
    August 30, 2024

    hal_status = HAL_SPI_TransmitReceive(&hspi2, tx_data, rx_data, 1, SPI_TIMEOUT).. Check for the hal_status, if it is ok, the result  will be there in your rx_data.. both are pointers

    Explorer II
    August 30, 2024

    Thank you for your response,@Techn 

     

    Can I use the HAL library while using the LL library?

     

    best regards.

    Graduate II
    August 30, 2024

    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/

    Explorer II
    September 1, 2024

    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.