I2C (Inter-Integrated Circuit), pronounced I-squared-C, is a synchronous, multi-master, multi-slave, packet switched, single-ended, serial communication bus invented in 1982 by Philips Semiconductor (now NXP Semiconductors). It is widely used for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication.
The DA14531 has one I2C peripheral which supports the following features:
The DA14531 SDK provides an easy to use driver which abstracts over all the required register operations.
The project with the code for this tutorial is available on Github at https://github.com/vicara-hq/da14531-tutorials
Download the project and copy it. The project has to be placed inside the Dialog SDK6 folder. Navigate to <SDK6_ROOT>/projects/target_apps/template and paste it in this folder. The project is a modified version of the empty_peripheral_template project provided by Dialog. But to keep this tutorial series as open source as possible, all the following steps will use the SmartSnippets Studio.
We will use a DA145xx Pro motherboard with a DA14531 Tiny module daughterboard with a STEVAL-MKI160V1. The STEVAL-MKI160V1 is an evaluation kit for the LSM6DS3, which is a 6-axis IMU. Other than this, we will also need a few jumpers, a breadboard and 2 4.7 kOhm resistors. If you cannot find 4.7 kOhm resistors, you can also try resistors between 1 kOhm to 10 kOhm but this might affect the working of the circuit.
Connect these components according to the below circuit diagram
Note: VCC in the circuit refers to 3V
The aim of this tutorial is to use I2C to read the WHO_AM_I register and reset the IMU.
The WHO_AM_I register on the LSM6DS3 provides a fixed device ID. This register should be read first to determine if the circuit and the IMU is working as expected.
First, we need to configure the pins to be used for I2C. I would suggest that you use the same pins as mentioned in the below code as other pins are connected to other components like buttons, LEDs on the daughterboard.
The below structure defines the configuration of the I2C peripheral. We set the peripheral to Master mode and the speed as Fast i.e. 400 kHz. We also se the addressing mode to 7 bits and the address of the LSM6DS3, which is 0x6A.
Note: The address of the LSM6DS3 changes to 0x6B when the pin SD0/SA0 is connected to VCC instead of GND.
We also need to call the I2C peripheral init function.
The structure variable definition and the calling of the init function is done in the periph_init() function in the user_periph_setup.c file.
In the user_empty_peripheral_template.c file, we create the required functions to read and write data using I2C.
We also need to write the functions to read the WHO_AM_I register and to reset the IMU.
As an example, I have called these functions in the custom app_on_init callback. For more details about the custom callback, take a look at my tutorial on GPIOs, buttons and LEDs.
Build the project and start the debugger with the DA14531 connected to your computer. If there are any issues with the working of the circuit, the execution of the program will stop. You can also place a breakpoint in the read_who_am_i_reg() function and step through it to view the value read from the LSM6DS3.