 Hello and welcome to the STM32L4 MOOC online training. My name is Andre Barata and I will guide you through the H2C hands-on session. I2C is a multi-master, multi-slave serial communication protocol. Each I2C bus has two signals, the clock signal or SCL and the data signal or STA. The protocol supports only half-to-plex communication, which means communication flows both ways, yet one direction at a time. I2C is only intended for short distance communication on the PCB. The embedded I2C interface handles communication between the microcontroller and the serial I2C bus. The peripheral controls all I2C features, such as specific sequencing, protocol, arbitration and timing. In this session, we will focus on how to properly set up the I2C in STM32QMX and how to use it to communicate with an external digital 1A converter CS43L22. This digital 1A converter is already included on the board, so no external hardware is needed to complete our tasks. The main task on this session is to read the ID and revision number from the external digital 1A converter. As a final remark, the MCU will be the master device and the digital 2A converter will be the slave device on this I2C communication. In this slide, we have the schematic for the digital 2A converter, including our board. In this session, the used signals are the serial data and clock lines connected to the I2C peripheral on PB6 and PB7 respectively. It is important that we know that A0 pin is grounded, since the device address of the DAC is modified depending on its state. Reset pin which enables I2C peripheral in external DAC must be set high before any kind of communication. Reset is directly connected to the MCUPE3 pin. At the physical layer, both serial and clock lines are an open drain design, which means external pull-up resistors are needed. Hopefully, these resistors are already assembled on L4 Discovery board. As you recall from the previous slide, reset pin must be set to high state to enable the communication in the external DAC. The MCU is initially in master transmit mode. Starts by sending a start bit followed by 7-bit address of the slave he wishes to communicate to and finally a single bit representing whether it wishes to write or to read from the slave. For write, LSP must be 0, for read, LSP must be 1. The device address of the DAC is modified depending on the state of A0 pin. As you recall from the previous slide, in our L4 Discovery board, the A0 pin is grounded, so 0. While performing a read operation, master will send the slave's address plus his intentions, in this case, to read the content from the map byte. Through the analysis of the register table of the DAC, if we send the value 0x01, we should expect to receive the ID and the revision of the slave. After all clarifications have been done, let's change to STM32Q by mix to start generating our code. With the STM32Q by mix open, we'll start a new project. We'll type once more our part number, STM32L476VG. We'll start the project and here we'll need to initialize the H2C1. And as you recall, we need to set PA3 as GPIO output, as this is the line which is directly connected to the reset line. So we can decide when or not to start the H2C communication. In the clock configuration, we are going to leave it as standard. And in the configuration, our only concern is to make sure that no pull-up and no pull-down resistor is defined as we already have it externally. After the configuration is done, we just need to save our project and generate our code. Please don't forget to select one second appropriate IDE generate code. Now we just need to press open project so we can start typing our code. We will start by typing the defined constants we want to include in our program. The first one is the memory address of the slave, where should be stored the chip ID and the revision. We will also define the mask address for writing and for reading. Then we will initialize two buffers, one for receiving and the other for transmitting data. At the beginning of the user code to blanket, we will start by enabling the slave device. As you recall, the PA3 is directly connected to the enabled line on the DAC. This pin should be set high to start the communication. We start by defining the data block we want to read. We start our communication by using HAL I2C master transmit. This function can be completed by pressing control space. The I2C handle is defined on the private variable session. The rest of the parameters can be found in the function declaration. The rest of the parameters to be filled are the device address, the pointer to the transmitting buffer, the number of transfers which in this case will be one byte and the timeout which prevents the program to stack. Before receiving the response from the slave, we will use the HAL A2C master receive. It also will have four parameters, the pointer to the handler of the device, the device address which in this case is for the reading operation, the pointer to the buffer for the receiving upcoming data, the numbers of transfers which in this case will be one and 100 milliseconds of timeout once again. After the communication is complete, we just need to disable the slave address by driving the PE3 low. We will insert a nopeline so we can stop our program after the communication. This will ease our debugging process. Now we will proceed to build our program and start debugging our project. We will double-click in our nopeline to insert our breaking point. To monitor our receiving buffer, we need to add it to watch. So we go to window, show view expressions and now we are going to edit our Erics data. Now we will launch the program and it should stop on the breaking point that we set. As we can see, some data was received in the Erics data buffer. If we double-click on it, we will realize that the first five bits are the chip ID and the three last ones are the revision. I think we are finished for this session. Thank you for watching.