2021-08-11 06:56 PM
I have been trying to test and get the gyroscope to communicate via SPI for a bit however it just seems to *refuse* to respond. As per the User manual for the board UM1670, PC1 is CS, PF7 is the serial clock, PF8 is MISO, and PF9 is MOSI all for SPI5. I suspect something is wrong with my setup of pins but I cant find what, so far I have:
Enabled the SPI5 clock as well as Port C and Port F clocks.
Configured PF7-9 as AF5, no pull.
Configured PC1 as general purpose output with no pull, and then set it to high in ODR (Since CS is high by default). Then I initiate SPI5 and begin by trying to send 2 bytes of data, 0x20 and then 0xf to the Gyro's CTRL1 register. After that as a test I keep trying to get info from the WHO AM I register just to test communication.
The only thing I get on the SPI bus is the value '255', which I assume is just all 1s. I used a logic analyzer to see what was happening and saw no activity on the MISO line.
I can't seem to understand if theres something I am missing with initializing communication with the gyroscope, or am I missing something with initializing the SPI. Here are the relevant code sections:
Relevant initialization code:
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_SPI5_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct; //SPI Pins
GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI5;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
GPIO_InitTypeDef GPIO_InitStruct1; //Chip Select Pin
GPIO_InitStruct1.Pin = GPIO_PIN_1;
GPIO_InitStruct1.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct1.Pull = GPIO_NOPULL;
GPIO_InitStruct1.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct1);
GPIOC->ODR |= (1 << 1);
mySPI5.Instance = SPI5;
mySPI5.Init.Mode = SPI_MODE_MASTER;
mySPI5.Init.Direction = SPI_DIRECTION_2LINES;
mySPI5.Init.DataSize = SPI_DATASIZE_8BIT;
mySPI5.Init.CLKPolarity = 2;
mySPI5.Init.CLKPhase = SPI_PHASE_1EDGE;
mySPI5.Init.NSS = SPI_NSS_SOFT;
mySPI5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
mySPI5.Init.FirstBit = SPI_FIRSTBIT_MSB;
mySPI5.Init.TIMode = SPI_TIMODE_DISABLE;
mySPI5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
mySPI5.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&mySPI5) != HAL_OK)
{
asm("bkpt 255");
}
Call order in main
HAL_Init();
SysTick_Config(SystemCoreClock / 1000);
SystemClock_Config();
SPI5_Init();
UART1_Init();
GYRO_Init();
static void GYRO_Init(void) {
GPIOC->ODR &= ~(1 << 1);
HAL_SPI_Transmit(&mySPI5, test, 2, 10);
while (mySPI5.State == HAL_SPI_STATE_BUSY_TX) ;
GPIOC->ODR |= (1 << 1);
}
infinite while loop regarding pinging the who am I register (address 0x0f)
while (1) {
GPIOC->ODR &= ~(1 << 1);
HAL_SPI_TransmitReceive(&mySPI5, 0x0f, buffer_rx , 1, 1);
while (mySPI5.State == HAL_SPI_STATE_BUSY_TX);
GPIOC->ODR |= (1 << 1);
}
2021-08-11 08:44 PM
something else I just noticed while looking at the logic analyzer, is other than the fact that MISO (D4) does not even move (likely the reason why I only get 0xFF returned back to the master) the clock movement (D2) seems a bit not only erratic when it goes down, but duty cycle seems very very slow, though I am using the default STCUBEMX SPI initiation settings.
2021-08-12 06:56 AM
Hi, instead of starting a code from scratch (with CubeMX), you can refer to the MEMS_Example you can find in the STSW-STM32138: STM32F429 discovery firmware package (UM1662).
https://www.st.com/en/embedded-software/stsw-stm32138.html
You can start from this and modify the code for the i3g4250d.
https://www.st.com/resource/en/datasheet/i3g4250d.pdf
Tom