2024-08-26 02:56 AM - last edited on 2024-09-13 02:32 AM by Amel NASRI
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
2024-08-30 05:00 AM
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/
2024-09-01 04:57 AM
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.
2024-09-01 10:38 PM - edited 2024-09-01 10:43 PM
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_.......................
2024-09-02 12:14 AM
GPIOE, GPIO_PIN_8) in main.cpp.
Best regards.