cancel
Showing results for 
Search instead for 
Did you mean: 

LIS2D12

fru999
Associate III

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 

fru999_0-1719563774185.png

fru999_1-1719563790647.png

But my Register always equal to 0. ret = HAL_OK.

I don't understand why :)

Can you help me

 

27 REPLIES 27
fru999
Associate III

and this is the definition of the function in STM32 CUBE :

HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, const uint8_t *pTxData, uint8_t *pRxData,

uint16_t Size, uint32_t Timeout)

fru999
Associate III

and the problem is the same if my datasize is 8 or 16 

fru999
Associate III

when i use the function transmitreceive i set the size data to 2 bytes

Again: did you set data size to 16b ?

here:

AScha3_0-1721318217862.png

+ your data in software also 16b , but: command/send is only in upper 8b , resonse/receive is only in lower 8b of the 16b words.

If you feel a post has answered your question, please click "Accept as Solution".
fru999
Associate III

Yes

The problem remains the same

this what i get when i analyse my signals :

fru999_0-1721380512644.png

when the thing I am supposed to send is {10001111, 0}

 

 not So the first clock tick is not supposed to be here because the count of clock rise is 17 when it supposed to be 16

+ for LIS   - maybe need this spi setting: ( high+2edge )

AScha3_0-1721383608666.png

 

+

always just 

HAL_SPI_TransmitReceive(..)

1 x 16b word, for write set the upper/lower 8b (discard the receive);

for read, as i wrote,  data in software also 16b , but: command/send is only in upper 8b , resonse/receive is only in lower 8b of the 16b word received.

If you feel a post has answered your question, please click "Accept as Solution".

this is my configuration already ;)