cancel
Showing results for 
Search instead for 
Did you mean: 

LIS2DH12 how to use standard ST driver functions:Sensor_IO_Read/Write

melt brink
Associate III
Posted on July 04, 2017 at 12:03

Goodday. Im trying out the ST standard component driver for LIS2DH12 (LIS2DH12_ACC_driver.c) I have added it to my project but  compiler kicks out a undefined symbol error. Which is what I would expect as I have not defined any of the below.

extern uint8_t Sensor_IO_Read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead);

extern uint8_t Sensor_IO_Write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite);

Im trying to figure out what is required of me to define and where? I can see its an I2C R/W function with certain unsigned int's that needs to be used in that order to read and write, but I have no idea how to declare these in main.c or how to call these in conjunction with the standard I2C.c in  stdPeriph_driver library.

these two functions are nowhere else in the driver so do I need to write my own?

any explanation or direction would be greatly appreciated.

#accelerometer-lis2dh12-configure
12 REPLIES 12
Miroslav BATEK
ST Employee
Posted on July 04, 2017 at 15:55

Yes, you have to write you own these two function according to your MCU and bus which you will use (SPI or I2C). These function are than called from the sensor driver to perform reading or writing operation. Basically you should fill these two function by the reading / writing function for I2C or SPI (from stdPeriph library).

melt brink
Associate III
Posted on July 04, 2017 at 17:02

Thanks Miroslav. I am still confused my C is entry level at best. Surely if these two functions are in the driver file from ST (

LIS2DH12_ACC_driver.c) they should be defined and declared there or in the .h file. And I should just be able to call it. How would I know what to define them to. What exactly do I define?

melt brink
Associate III
Posted on July 04, 2017 at 19:30

Thanks David. I am using STM32L152. I2C is configured and working. In the two functions I understand the purpose of the last 3 uint, but what is *handle? according to other driver samples it would appear to be  the name/type of sensor.

Thanks for the advice. let me try bring the I2C r/w functions with this driver r/w and see how it goes.

Legacy member
Not applicable
Posted on July 04, 2017 at 18:53

Hello,

Have a look at a similar question and answer here :

https://community.st.com/0D50X00009XkXf3SAF

.

You basically need to do 4 steps on your target MCU (what kind of HW do you use, by the way?):

1) I2C initialization: start your host microcontroller and an I2C in a host mode

2) Find out, how can you send and read a single register (in a specific device address and at a specific register address ).

3) Read multiple bytes: that is just about writing a cycle to send more bytes the previous step. Make it a bit more general by implementing a function with these parameters:

uint8_t Sensor_IO_Read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead);

4) Write multiple bytes: the same as for 3) , but for reading a byte. Again, make it general by implementing a function with these parameters:

uint8_t Sensor_IO_Write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite);

Write these functions in a separate C file (so its HW independent for your future use), create a header file for this C file and include the header file to the file from where you have posted the 'extern' declarations.

(Or, if you don't care that much about portability or it seems too complicated for you, (and you don't mind a not so perfect code) delete the 'extern' keywords and define the functions in the same .c source file as the rest of the code is).

If you need some help with I2C generally, I would suggest searching on Youtube for 'I2C tutorial' (additionaly maybe + your platform), there is quite a few videos that will help you get the idea, why the read adn write function need all the attributes.

David

Legacy member
Not applicable
Posted on July 04, 2017 at 19:37

Hello,

It is a pointer to a handle, that is a c structure used in the HAL concept.

Have a look at this description of STM32L1 HAL drivers, chapter 22: 

http://www.st.com/content/ccc/resource/technical/document/user_manual/97/4d/5f/9a/ed/e4/4e/66/DM00132099.pdf/files/DM00132099.pdf/jcr:content/translations/en.DM00132099.pdf

 

If you are new to this concept, I would personally recommend an STM online course (for free!): 

http://www.st.com/content/st_com/en/about/events/events.html/stm32cube-basics-online-course-with-hands-on-exercises-a.html

  

If covers CubeMX configurator and HAL concept, contains useful examples on real HW etc.

David

melt brink
Associate III
Posted on July 05, 2017 at 09:15

Thanks David, I am familiar with the existence of  HAL but have not had the chance to use it and that is probably why I am confused. I am using stdperiph_driver. Would this driver  still work? If so, I suppose I need to code some interface between the lines above and lines below. Would it not be easier just to leave the driver and set up the LIS2DH12 registers directly from my I2C functions below, I still need to to convert these to a function as a buffer.

`void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)`

`uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)`

Legacy member
Not applicable
Posted on July 17, 2017 at 19:00

Hello, sorry for a late answer, I had a holiday.

I am not familiar with these drivers, but there is always an option to write your own driver functions. I personally sometimes write them, because the code is much shorter and easier to read.

On the other hand, ST provides the drivers with maximum compatibility in mind, it might be more difficult to learn how to use them, but then it's easier to migrate across MCU families, set the sensors with a different configuration etc.

melt brink
Associate III
Posted on July 19, 2017 at 08:36

I would like to post my solution but this editor does not except the code <> so it comes up as a paragraph. Any ideas?

melt brink
Associate III
Posted on July 19, 2017 at 10:29

Lots of research later and looking at samples from others I have attached what is working for me on the STM32L read and write to accel LIS2DH12 using std perph lib functions. Reads and writes only 1 byte. combines the LSB and MSB of the X,Y,Z . LIS2DH12 is configured with no interrupts, no Fifo.

________________

Attachments :

lis2dh12_stm32l152.c.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyZ6&d=%2Fa%2F0X0000000b9X%2F4ywFlPauyNsWQwAGDX2zFzIPzsUtkgcbCeCVeVtNIJ4&asPdf=false