2025-03-30 11:47 AM
Basic function is to calculate Heading 25 times per second.
Using C++, STMCubeMX, and STM32L476IDE.
Chip is the STM32L476RET. Is this chip fast enpough?
Need heading 25 times per second for main loop processing. Using timer 3.
HAL is working, but is much to slow.
Do not know how much faster LL would be.
Code below for the Mag.
void Get_Mag (void); // Gets Mag, MagF, MagFG
// 1. Reads Mag x,y,z
// 2. Converts to 16 bits,
// 3. Converts to floating Point,
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,GPIO_PIN_SET); // Sets Mag CS On
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4,GPIO_PIN_RESET); // Sets XL CS Off
TX[0] = (OUTX_L_REG_M | 0x80);
Mag_Data[0] = 0;
HAL_SPI_TransmitReceive(&hspi1,&TX[0],&Mag_Data[0],7,100);
TX[0] = (OUTX_H_REG_M | 0x80);
Mag_Data[1] = 0;
HAL_SPI_TransmitReceive(&hspi1,&TX[0],&Mag_Data[1],4,100);
TX[0] = (OUTY_L_REG_M | 0x80);
Mag_Data[2] = 0;
HAL_SPI_TransmitReceive(&hspi1,&TX[0],&Mag_Data[2],2,100);
TX[0] = (OUTY_H_REG_M | 0x80);
Mag_Data[3] = 0;
HAL_SPI_TransmitReceive(&hspi1,&TX[0],&Mag_Data[3],2,100);
TX[0] = (OUTZ_L_REG_M | 0x80);
Mag_Data[4] = 0;
HAL_SPI_TransmitReceive(&hspi1,&TX[0],&Mag_Data[4],2,100);
TX[0] = (OUTZ_H_REG_M | 0x80);
Mag_Data[5] = 0;
HAL_SPI_TransmitReceive(&hspi1,&TX[0],&Mag_Data[5],2,100);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5,GPIO_PIN_RESET); // Sets Mag CS Off
// I know the six reads could be done with one read.
// *********** Convert to 16 bits *************************************
Magx = (Mag_Data[1] << 8) + Mag_Data[0]; // Combine high and low
Magy = (Mag_Data[3] << 8) + Mag_Data[2];
Magz = (Mag_Data[5] << 8) + Mag_Data[4];
MagxF = Magx; // Convert to floating point
MagyF = Magy;
MagzF = Magz;
MagxFG = Magx * 0.061; // Convert to mg in floating point
MagyFG = Magy * 0.061;
MagzFG = Magz * 0.061;
} // End of Get Mag
void Get_XL (void); // Gets Ax, kk, AxFG, AxAxFG is similar to Get_Mag
2025-04-01 3:21 AM - edited 2025-04-01 3:26 AM
Please see How to insert source code.
What do you mean by SPI five-wire ?
@JBonn wrote:Need heading 25 times per second
Should be entirely doable. How many bytes per second is that?
@JBonn wrote:HAL is working, but is much to slow.
Do not know how much faster LL would be.
So start by finding what, exactly, is making your HAL-based code so slow.
Why do you read just 1 byte at a time?
Why not do a single 6-byte transaction?
PS:
Did you look at the software ST provides:
https://www.st.com/en/mems-and-sensors/lsm303agr.html#tools-software
The datasheet shows a four-wire interface:
https://www.st.com/resource/en/datasheet/lsm303agr.pdf#page=16