2024-06-28 01:37 AM
I use LIS2DS12
This is my init function
void LIS2DS12_Init(void)
{
uint8_t ctrl_1 = 0x72; //0111 for 6.4kHz ODR [7:4], 00 for 2g [3:2], 1 for high frequency available[1], 0 for BDU not activated [0]
uint8_t ctrl_2; // [6] for soft_reset, [1] for disable I2C, [0] for 4 wires SPI
HAL_StatusTypeDef ret;
ret = spi_read_LIS2DS12(CTRL2_ADDR, &ctrl_2);
ctrl_2 |= 0x42; // [6] for soft_reset, [1] = 1 for disable I2C, [0] = 0 for 4 wires SPI
ret = spi_write_LIS2DS12(CTRL2_ADDR, &ctrl_2);
//reset the accelerometer
ret = spi_write_LIS2DS12(CTRL1_ADDR, &ctrl_1);
//set the accelerometer to measurement mode
spi_read_LIS2DS12(CTRL1_ADDR, &ctrl_1);
spi_read_LIS2DS12(CTRL2_ADDR, &ctrl_2);
}
with read and write :
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;
uint8_t address = regAddr | 0x80;
uint8_t sendData[2] = {address, 0};
uint8_t receiveData[2] = {0};
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
ret = HAL_SPI_TransmitReceive(&hspi2, sendData, receiveData, 2, 50);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1);
*pData = receiveData[1];
return ret;
}
The protocol for SPI transmission is
But my Register always equal to 0. ret = HAL_OK.
I don't understand why 🙂
Can you help me
2024-06-28 03:36 AM
my CS:
2024-06-28 03:39 AM
@fru999 - again:
Have you used a logic analyser or oscilloscope to see if what's actually happening in your hardware matches the protocol spec diagrams you posted ?
Among other things, you haven't told us what board(s) you're using - please see:
2024-06-28 03:57 AM - edited 2024-06-28 03:57 AM
But "like" is not "it is" .
Imagine : the receiving chip expects a sequence, as given in diagram.
But you send another, in two parts...could work, or not.
So set it to 16bit SPI data size.
2024-06-28 04:07 AM
Ahh, + set : Motorola, with NSS = hardware , to get matching /CS signal. (Then no need to set it in software.)
2024-06-28 04:27 AM
But everywhere I see, bit for the adress and it says 8 bits because each communication is 8 bit long (8bit for the adress, 8 bit for the data but the slave expects 8 bits for the adress and 8 bits for the data and it is separated). If i put 16 bit long the slave will wait for 16 bit for the adress and 16 bits for the data no ?
2024-06-28 04:28 AM
If i do that that means i will not have to toggle the pin every time ?
2024-06-28 04:37 AM
Where you "see 8bits" ?
in your pic:
I see 16bit sequence here.
>If i do that that means i will not have to toggle the pin every time ?
Right. SPI hardware doing it then.
2024-07-18 06:58 AM
Read underneath the picture, it is 8 bits the data size
But now the problem is that when I am in reading mode, after transmit receive, the receive data are equal to the transmit data...
Do you have an idea why?
2024-07-18 07:01 AM
Did you set it to 16bit frame at last ?
2024-07-18 08:05 AM
No, I have changed it and i always have hal_error and i have worked with another accelerometer wich description of SPI was the same and the data size was 8