cancel
Showing results for 
Search instead for 
Did you mean: 

Using HAL SPI on H3LIS200DL

Rogers.Gary
Senior II

Hello:

Previously using CMSIS, I was able to read or read/write this accelerometer device. Using HAL, I am having some problems.

To start, I am trying to read the ID register. I am writing in the address and a dummy byte. The address for the ID register (who am I) is 0xF. The read value of the register should return 32. According to the data sheet (see attached clip for the "read" page), the opcode should be written with the register address, and for a read, the MSB should be 1. On an 8 bit address, this should be 0x80

uint8_t address = 0xF | 0x80; //0x8F

assert_CS_low();

HAL_SPI_TransmitReceive(&hspi3, &address, &SPIdata,2, HAL_MAX_DELAY); 

assert_CS_high();

printf("return data = %i\n", SPIdata);

It returns the "ore'd" address value of 0x8F. Changing the number of bytes to 1 makes no difference.

Ditto for configuration register 1 at address 0x20. I have also tried clocking in an additional dummy byte but it does not make a difference. The clock appears to be sending the 2x 8 bit bursts on transmit.

I know the SPI setup is OK, I also have an FRAM spi device and that works with no problem. In any case, I tried changing the prescalar, etc, with no success.

Any suggestions? The returned address thing is quite bizarre, never seen this before.

1 ACCEPTED SOLUTION

Accepted Solutions
Rogers.Gary
Senior II

I see a mistake in the line:

HAL_SPI_TransmitReceive(&hspi3, &address, &SPIdata,2, HAL_MAX_DELAY);

That should have "1" in it as there is only 1 byte transferred. However, I can put 1,2 or 5 (if I send as many) and it makes no difference. It continues to return the address I send it.

As mentioned, the SPI setup is pretty much identical to the one I am using on FRAM, with adjustments tried on prescaler values. And compared to a previous project using an STM32F4.

Anyone used HAL on a similar device? Or see something wrong? Or suggest something I could try?

View solution in original post

2 REPLIES 2
Rogers.Gary
Senior II

I see a mistake in the line:

HAL_SPI_TransmitReceive(&hspi3, &address, &SPIdata,2, HAL_MAX_DELAY);

That should have "1" in it as there is only 1 byte transferred. However, I can put 1,2 or 5 (if I send as many) and it makes no difference. It continues to return the address I send it.

As mentioned, the SPI setup is pretty much identical to the one I am using on FRAM, with adjustments tried on prescaler values. And compared to a previous project using an STM32F4.

Anyone used HAL on a similar device? Or see something wrong? Or suggest something I could try?

Rogers.Gary
Senior II

Working!

Added this after first TransmitReceive, I added:

HAL_SPI_Receive(&hspi3, &SPIdata, 1, HAL_MAX_DELAY);     

And magically some data appeared.

Thanks to all the thousands of responses, unfortunately, mine was picked as the right one.