2024-07-22 03:11 AM
Hello,
I am using STM32H745 to setup and communicate with LIS2DS12 accelerometer via SPI 4-wires. I am trying to setup the accelerometer and I started with reading the WHO_AM_I register. It's default value is (43h) and I changed the value of the first bit to '1' so that my STM32 can read the value.
The SPI communication is setup as follows:
With the read and write fucntions set like this:
HAL_StatusTypeDef spi_write_LIS2DS12(uint8_t regAddr, uint8_t *pData) { HAL_StatusTypeDef ret; uint8_t address = regAddr & 0x7F; uint8_t sendData[2] = {address, *pData}; //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0); ret = HAL_SPI_Transmit(&hspi2, sendData, 2, 20); //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1); return ret; } HAL_StatusTypeDef spi_read_LIS2DS12(uint8_t regAddr, uint8_t *pData) { HAL_StatusTypeDef ret = 0x01; uint8_t address = regAddr | 0x80; // Set the Read/Write bit to 1 for read operation uint8_t sendData[2]= {address,0}; uint8_t receiveData[2] = {0}; //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET); HAL_StatusTypeDef status = HAL_SPI_TransmitReceive(&hspi2, sendData, receiveData, 2, 50); //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET); *pData = receiveData[1]; return ret; }
When I observe the MISO and MOSI with a logic analyzer, I can see that good register value is sent, but I have random 0s and 1s as a return of MISO. Here is a picture of how it looks:
I tried to add a reboot and/or soft reset before reading the WHO_AM_I registor, but it did not change much. Does anyone know what is the cause of this behaviour and how can it be fixed?
2024-07-22 06:17 AM
The MOSI line first byte is 0b10101110 which is a read of register 0x2E. That's not the WHO_AM_I register. The WHO_AM_I register is at 0x0F.
You should also uncomment the CS line commands. Not using them can get you out of sync with the chip.
2024-07-23 01:24 AM
Here is a better screenshot of the logic anlyzer when a query for the WHO_AM_I register is sent:
In this configuration, MISO is not reading the value of the WHO_AM_I register. Am I supposed to send another address through MOSI to get the correct return information?
CS line is commented because I setup for the hardware (STM32H745) to manage the connection. If I uncomment it, my sending information are not correct and I know that for sure I can not read the WHO_AM_I register, as in screenshot below.
2024-07-23 05:54 AM
Toggling a GPIO should work as expected. If it doesn't, perhaps the pin isn't configured, or is hooked up to something else, or isn't what you're monitoring.
What you're doing should work. Send two bytes and the second received byte has the response.