 Hello and welcome everyone to the STM32L for MOOC online training. My name is André Barata and I'll guide you through the SPI hands-on session. SPI stands for Serial Peripheral Interface and it provides a simple synchronous communication interface. SPI allows the microcontroller to interface with external SPI devices such as sensors, liquid crystal displays or SD cards. The interface is able to run up to 40 Mbps in master mode and 24 Mbps in slave mode. Applications benefit from the simple and direct connection to components which only require a few pins. Additionally, the interface is highly configurable to support many communication protocols over SPI. In this session we will learn how to configure the SPI peripheral in order to communicate with the gyroscope L3GD20 embedded on the board. We will configure and generate code for the necessary pins and SPI block on STM32QBmx. The main goal of this hands-on is to read the Huemi register in the gyroscope, a register which contains the version of the gyroscope. As a first step we will click on new project. On the open window we will type our part number STM32L476VG. Then we select a part and we start the project. To interface with the one board gyroscope we will go to the peripheral tree and we will enable SPI2 in full duplex master mode. We need to remap SPI2 pins on PC2, PC3 and PB10 and reassigning to pins PD3, PD4 and PD1. So by holding the control key we will drag and drop them as PDD pins are directly connected to the gyroscope on board. We also need to configure PD7 pin as output mode since it will be used as chip select to enable SPI slave. In this case the slave will be the gyroscope. That's all for the peripheral tree window. Let's now move to the clock configuration tab. No modifications are needed in the clock configuration tab so let's just skip it and go to the configuration tab. In the configuration window click on SPI2 and change the data size from 4 bits to 8 bits. The remaining parameters can be left as default. This will be all from STM32 Cubemix. Now we can save and generate our project selecting system workbench for STM32 as our IDE. Now that the code has been generated let's start by opening the main.c file from the source folder. We can see that the handle structure for SPI2 has been generated as well as the functions for system clock configuration, GPIO and SPI initialization. If we look into the SPI2 init function we can see that the handle structure for the peripheral configuration has been filled with the parameters according to our settings in STM32 Cubemix. At the end of the function the HAL SPI init function is called. This function sets the peripheral registers based on the handle structure. Looking into the GPIO init function we can see that the output pin for chip select signal has been configured. The pins to be used as SPI signals are configured. There is alternate function mode and to see its configuration we need to open nmsp.c source file which contains system level initialization. You can see the three pins for mo-c, my-so and clock signals configured to alternate function push-pull mode with SPI2 selected. Let's return to main.c and start our coding. As a first step we will create a buffer containing data to be transmitted. Then similarly a buffer to receive data. Now we will fill the first array element of the transmit buffer with the address of who am I register containing the gyroscope ID and the dedicated bit to inform the gyroscope that we are going to read from the register. Next step will be to drive the chip select to low logic level in order to enable or more precisely to select the SPI slave. If we don't know how to pass an argument we can see the comments above the function definition in the source code of the SPI peripheral driver. The chip select signal will be driven back to high logical level once the communication finishes. The high logical level means inactivity. We can copy the already used function and only change the last argument. Now we will use a function to transmit data using serial peripheral interface. We can start by typing HAL SPI and press control space for a list of proposals. Our function is called HAL SPI transmit without suffix. This means it is a blocking function which uses pooling technique. If we have a closer look on the function inside source file of SPI driver we can see that it expects 5 arguments. A pointer to the peripheral handle structure. A pointer to the transmit data buffer. A pointer to the reception data buffer. The amount of data to be transferred and the timeout in milliseconds for the pooling loop. Without timeout it may happen that the program execution will be stucked in the pooling loop. We can copy the name of the peripheral handle structure from the top of the main.c file. The next parameters are pointers to transmit data buffer and receive data buffer. As you probably know the name of the array is also pointer to its first element. The number of data transfers will be 2. In the first step we will send a read command together with the address of the who am I register. In the second step we will send a dummy byte and receive the content of the register in parallel. For the timeout let's use 100 milliseconds. We are also going to check the return value of the function and call the error handler in case of a failure. We have just concluded the coding of the program and now let's proceed to build and debug our code. Once the build has finished we are going to enter the debug session. Let's put a breakpoint on the beginning of the while loop after the communication will have been finished. If you haven't done that yet you can set optimization to none in project settings for easier debugging. Now we will add a new expression so that we can see what we have received from the gyroscope. So go to window show view expressions. At the second element of the reception buffer this means the element with index 1. Now we will run the program. As you can see the program execution stopped in the breakpoint. Also we have received the value 212. Let's change the view to exit decimal. Now as you can see the received value is d40 in exit decimal. That's the gyroscope ID value. For you to double check we suggest that you would open the datasheet from the gyroscope L3G220 and verify. The example is now concluded and we have successfully established communication with the gyroscope using SPI interface. Thank you for watching.