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-29 05:10 AM
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.
2024-08-29 05:41 AM
Thank you for your reply, @RPC
I would like to review this code once again.
2024-08-30 01:02 AM - edited 2024-08-30 01:06 AM
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..
2024-08-30 01:42 AM
Thank you for your response, @Techn
spi CS is done manually.
Is it possible to tell us how to enable chip select for the device?
best regards.
2024-08-30 02:22 AM
Did you keep your SPI_polarity HIGH, SPI phase is 2nd edge. Then configure the GPIO connected to the CS pin of the device to GPIO output and make it low while reading . Please share the ioc file
2024-08-30 02:40 AM
I think the setup is done.
Thank you in advance for your cooperation.
2024-08-30 02:58 AM
select GPIO output level as high since the default for CS is high, the chip is not selected normally, selected only when you do the read
2024-08-30 03:05 AM
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
2024-08-30 04:24 AM
Thank you for your response,@Techn
Can I use the HAL library while using the LL library?
best regards.