I made this post because i found the example code provided in SDK for I2C is not meant for Minized , if it is not realized then it would be frustrating in figuring it out.

I am not going to show how to modify that given(Buggy) piece of code but i change it to make it work (yeah its Working) , i found different alternatives and  I would like to show the Best one which is Easy to Understand and Implement/Change for our Purpose.

Before Going into It,we need to understand few things such as How I2C works and how to use the lis2ds12  sensor from STM used on Minized Board.

Simply putting on:In I2C the PS which is used as master first Sends the 7 bit address on the I2C bus and then the Sensor with that Address Responds to master depending on Read/Write Request sent as the 8th Bit.

In LIS2DS12LIS2DS12 case, we first find the Address of it as it can be 0x1D or 0x1E  based on the A0 pin voltage on LIS2DS12LIS2DS12 ,  by testing WHO_AM_I reg in LIS2DS12LIS2DS12 .

We Read a Register in LIS2DS12LIS2DS12 as follows:

1.)We send the Address (0x1D by Default for LIS2DS12LIS2DS12) from PS I2C master with Write Command for the Next Cycle

2.)We send the Register Address (for Eg:Who_Am_I = 0x0F ) from PS as the Data for the Write cycle and also tell that we will talk to it in Nex Cycle

3.)We send the Address (0x1D ) from PS I2C master with a Read Command for the Next Cycle

4.)Now the Device will send the Data i.e., the Register Value(in this case Value Present in Who_Am_I register )

We compare the Value read with the Rquired value in Who_Am_I register (Its value is fixed at 43h),If it is not equal then the Address should be(0x1E) using that Address we test the Who_Am_I register for its validity.

 

In this way we can read any register or Access Any Register in LIS2DS12LIS2DS12 sensor.

 

In terms of code,I uploaded the two Implemtations of I2C for LIS2DS12LIS2DS12 in my Github repo:

1.)I2Ctest.c is a single source file which Outputs the Temperature and Accerometer values by Intializing the required Register similarly as stated above

https://github.com/uElectron/minizedSDK/blob/master/using_I2C/I2Ctest.c

2.)I found the Hardware Abstraction Layer (HAL)Drivers from ST Microelectronics ,where we can use the HAL functions provided but it requires to Write the Platform Dependent Code for using these Drivers

https://github.com/uElectron/minizedSDK/tree/master/using_I2C/stm_based_i2c

Note here : lis2ds12_reg.c & .h files are the Driver files and I impemented the Platform related Initializations in main.c file itself

 

I should state that when working with LIS2DS12LIS2DS12 using I2C , i found LIS2DS12LIS2DS12 Datasheet from STM very useful , you can also see all the required references in one place:

https://github.com/uElectron/minizedSDK/tree/master/supportDocuments/sensorsDatasheets

 

or get it from ST micro website:

https://www.st.com/en/mems-and-sensors/lis2ds12.html

 

 

I started working on my Project,I post the Project Details in my Next Blog post