Skip to main content
MNapi
Senior II
August 23, 2025
Solved

any idea how to connect magnetometer LIS2MDLTR made by ST ?

  • August 23, 2025
  • 2 replies
  • 294 views

here is the code I am trying 

uint8_t whoami_reg[1];
whoami_reg[0] = 0x4F | 0x80; // read command

uint8_t whoami_val[1];
whoami_val[0] = 0;

 

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // CS low

HAL_SPI_Transmit(&hspi1, whoami_reg, 1, HAL_MAX_DELAY);

HAL_SPI_Receive(&hspi1, whoami_val, 1, HAL_MAX_DELAY);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); // CS high

 

whoami_val[0] should be = 0x40

I tried all the possible settings CPOL (HIGH and LOW) , CPHA ( 1 edge and 2 edge) , slower baud and nothing, I read some posts other people had problem too with SPI but apparently made it working only with I2C

I even tried to solder another  LIS2MDLTR and another, nothing!

 

Untitled.png

Untitled1.png

 

Best answer by AScha.3

I think your SPI setting is wrong, its half-duplex-master spi :

AScha3_0-1755966533621.png

AScha3_1-1755967143355.png

+ dont drive it at max speed for beginning...try 2 Mbit or so;

2 replies

TDK
Super User
August 23, 2025

> whoami_val[0] should be = 0x40

And what do you get?

 

Send a dummy HAL_SPI_Transmit command before pulling CS low.

"If you feel a post has answered your question, please click ""Accept as Solution""."
AScha.3
AScha.3Best answer
Super User
August 23, 2025

I think your SPI setting is wrong, its half-duplex-master spi :

AScha3_0-1755966533621.png

AScha3_1-1755967143355.png

+ dont drive it at max speed for beginning...try 2 Mbit or so;

"If you feel a post has answered your question, please click ""Accept as Solution""."
MNapi
MNapiAuthor
Senior II
August 23, 2025

thanks that was the problem and it works now

tested with 10 Mbit/s 

I switch to 

CPOL = LOW and CPHA = 1 EDGE

when it powers up it works with 3 SPI after that it can be switched to  4-wire (full-duplex).

 

it reads data now