 Hello and welcome to the STM32L for MOOC online training. My name is André Barata and I'll guide you through the CRC hands-on session. CRC stands for Cyclic Redundancy Check. The CRC calculation is used to get the CRC code of 8, 16 or 32 bits using a polynomial generator with a configurable value and size. The CRC-based techniques can be used to verify data transmission or storage integrity. The CRC calculation can also be used to compute the software signature of an application during run time to be compared with the reference signature generated at link time and stored at a given memory location. Thanks to the fully configurability of the STM32CRC peripherals, software overhead is reduced to a minimum. Additionally, DMA can be used from back-to-back CRC calculation over a large block of data while the CPU is performing other tasks or put in sleep mode. For an optimal performance, the CRC peripheral is connected to the HP bus. Data can be read and written either by CPU or DMA. The CRC computation in GIN will hold the result of any previous CRC calculation, which then becomes the initial value of the next calculation, thus allowing a new CRC computation of data block. In this session, we will configure and generate code for the CRC peripheral on STM32QBmx. The main goal of this hands-on is to compute a CRC code and compare it to the expected value. Let's now open the STM32QBmx to start configuring our peripheral. As a first step, we will click on New Project, then we will type our part number, in this case STM32L476VG. We click on the part and we start a project. We will start by configuring the PAE8 pin to output mode. This pin will drive the green LED on the Discovery Kit. Then we will do the same with the pin PB2 and this one is connected to the red LED. Then from the peripheral tree, we are going to the CRC and we are going to activate the peripheral. There will be no need to change anything on the clock configuration, so we are going to proceed to the configuration window. On this tab, we will just check the CRC default parameters, the input data is set to bytes and there will be no data inversion. At this point, we are ready to save our project, generate our code and select System Workpence for STM32 as our IDE. After the code is generated by the STM32QBmx, let's open the main.c file which is in the source folder. As you can see, the peripheral handle structure for the CRC has been generated as well as the function to configure the system clock and to initialize GPIO and CRC. If we check the CRC init function, we can see that the peripheral is configured according to our settings in STM32QBmx. We will start coding by creating a global variable to hold CRC calculation result. Then we will create a constant variable which will be holding the expected CRC value. Finally, an array of bytes will be created. We will be using the data stored on this array to preform our CRC calculations over it. The array will contain four elements. Let's define a constant for this array size. Let's now define the array elements as 0011AA and CC in hexadecimal representation. Now the function for CRC calculation will be called. The calculation result will be stored in the CRC value variable. We can start by typing HALCRC and press control space for a list of proposals. The HALCRC calculate function seems to be the right one. If we look into the function definition, we can see that it expects three arguments. The first one is the pointer to a peripheral handle structure, the second one is a pointer to the input data buffer and the third one is the buffer length. The name of the peripheral handle structure can be copied from the top of the main.cif source file. The next parameter is the pointer to the input buffer. Since our buffer contains 8 bits inside the integers and the function expects pointers to 32 bits integers, we have to do typecasting of the pointer to adjust data types. The last parameter is the buffer length. We can use our predefined constant and pass it to the function. Once the calculation is complete, we will compare the result with the expected value and the if there is not a match, the error handler will be called. As you can see, this handler contains an infinite loop. We will toggle with the red LED in this loop to indicate a failure. For that purpose, we will use the well-known HAL GPIO toggle function. We have also to put some delay to be able to see the LED toggling. Let's put 100 milliseconds, for example. In case there is a match of both values, we will toggle with the green LED in the infinite loop on the main function. We will use the very same procedure as we just did before. We just finished the coding session. Now we will save and build the project. After building is done, let's enter the debug session. Let's run the code and you should see the green LED toggling as we had a match between values. Let's now terminate the debug session and modify the code a little bit. If you would change the expected value, compile and open the debug session, the red LED should be flashing since the CRC result doesn't match the expected value. The example is now finished. Thank you for your attention.