03-13-2023 07:57 AM
Hello forum can you tell me how to intagrate API for stm32 spi please Thank You
03-13-2023 02:36 PM
03-14-2023 11:16 AM
this is my function
void spiWriteRegister16(uint8_t reg, uint16_t value, GPIO_TypeDef *port, uint16_t cs) {
uint8_t data[3];
data[0] = reg;
data[1] = (uint8_t)(value >> 8); // transmit MSB first
data[2] = (uint8_t)(value & 0xFF); // transmit LSB second
HAL_GPIO_WritePin(port, cs, GPIO_PIN_RESET); // make chip select low to enable transmission
HAL_SPI_Transmit(&hspi1, data, 3, 10); // write data to register
HAL_GPIO_WritePin(port, cs, GPIO_PIN_SET); // make chip select high at the end of transmission
}
uint16_t spiReadRegister16(uint8_t reg, GPIO_TypeDef *port, uint16_t cs) {
uint8_t address = reg | 0x80; // read operation with auto-increment
uint8_t data[4] = {0};
HAL_GPIO_WritePin(port, cs, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi1, &address, 1, 1); // send address
HAL_SPI_Receive(&hspi1, data, 4, 1); // read the data from device
HAL_GPIO_WritePin(port, cs, GPIO_PIN_SET);
return ((uint16_t)data[2] << 😎 | data[3];
}
void readAllAccel() {
uint8_t address = 0x03;
uint8_t txBuf[1] = {address | 0x80}; // read operation, MSB is 1
uint8_t rxBuf[20] = {0}; // initialize to 0
HAL_GPIO_WritePin(GPIOA, SPI_CS_Pin, GPIO_PIN_RESET); // make chip select low to enable transmission
HAL_SPI_TransmitReceive(&hspi1, txBuf, rxBuf, 1, 10); // send address and receive data
HAL_SPI_TransmitReceive(&hspi1, &txBuf[1], &rxBuf[1], 19, 10); // receive the rest of the data
HAL_GPIO_WritePin(GPIOA, SPI_CS_Pin, GPIO_PIN_SET); // make chip select high at the end of transmission
//Offset = 2 because the 2 first bytes are dummy (useless)
int offset = 2;
x = ((rxBuf[offset + 1] << 8 | (uint16_t)rxBuf[offset + 0])); //0x03
y = ((rxBuf[offset + 3] << 8 | (uint16_t)rxBuf[offset + 2])); //0x04
z = ((rxBuf[offset + 5] << 8 | (uint16_t)rxBuf[offset + 4])); //0x05
gyr_x = (rxBuf[offset + 7] << 8 | (uint16_t)rxBuf[offset + 6]); //0x06
gyr_y = (rxBuf[offset + 9] << 8 | (uint16_t)rxBuf[offset + 8]); //0x07
gyr_z = (rxBuf[offset + 11] << 8 | (uint16_t)rxBuf[offset + 10]); //0x08
temperature = (rxBuf[offset + 13] << 8 | (uint16_t)rxBuf[offset + 12]); //0x09
}
void softReset(){
spiWriteRegister16(CMD, 0xDEAF,GPIOA,SPI_CS_Pin);
HAL_Delay(50);
}
#define ACC_CONF 0x20 //Page 91
#define GYR_CONF 0x21 //Page 93
#define CMD 0x7E //Page 65
#define GRAVITY_EARTH (9.80665f)
uint16_t temperature = 0;
int16_t x, y, z;
int16_t gyr_x, gyr_y, gyr_z;
init device
softReset();
spiWriteRegister16(ACC_CONF,0x4027,GPIOA,SPI_CS_Pin);
spiWriteRegister16(GYR_CONF,0x760B,GPIOA,SPI_CS_Pin);
please see if this is correct Thanks
03-15-2023 06:55 AM
Hi Noro,
You SPI write, read function only supports data access that is 2 bytes long.
BMI323 sensor API https://github.com/boschsensortec/BMI323-Sensor-API has implemented the function how to access sensor.
You only need the appropriate read, write, delay_us interface, then you can access the sensor normally.
Previous attached example code has demostrated how to use SPI to access BMI323 on STM32 for your reference.
03-15-2023 08:20 AM
I connected as in your code sent, I don’t see anything after the ID