 Hello and welcome to the STM32L4 MOOC online training. My name is Andre Barata and this is the dedicated session for the CAN peripheral. CAN stands for Controller Area Network and this protocol was introduced in the 80s. It was developed for automotive field applications. CAN was designed to be able to connect a network of electronic devices inside of a car. At the time there was no robust solution capable of providing fast communication on a noisy environment. The company who developed this protocol was Bosch. It was so successful that it is still being used and based on this popularity it was even adopt to other fields such as the industrial field where it is a communication standard right now. During the CAN session we will focus on implementing a very basic CAN connectivity. This means that we will implement capabilities of receiving and transmitting CAN messages. The implementation of the CAN protocol in the L4 Discovery board contains everything except the CAN transceiver. The transceiver converts the signal to the correct line voltage levels and the data from serial to differential. CAN can neither be clocked by the high-speed external or the embedded multi-speed internal. For a low-cost implementation we can use the MSI and if improved accuracy is needed it's possible to trim the clock source with the LSE just like in the USB implementation. As we don't have a real CAN transceiver in our board we won't be able to implement a real CAN connectivity. For the purpose of this exercise it is still possible to configure the CAN controller in a short circuit configuration. We have three test modes starting from the left side of the slides. The first is the silent mode where we short-circuit TX with RX line yet it's still possible to receive messages from the external world. The second is the loopback mode where TX and RX lines are short-circuit again yet you can still transmit to the outside world but not receive. The third is the combination of both previous modes but you can't either receive or transmit in this mode. On this session we will learn how to configure and generate code for the CAN peripheral in the STM32QBmx and we will also learn how to properly handle the HAL CAN functions. The goal of this hands-on will be to create a program that is able to send and receive CAN messages. Let's now jump to the STM32QBmx to start generating our code. As a first step we will click on new project then in the new window we will type our part number STM32476VG we will double click on the desired part and then new project will start. When the new project starts we are going to the peripheral tree and we are going to the CAN1 peripheral and we are going to select master mode. As a consequence two pins will light up in green PA11 and PA12. Next step will be activate LSE from the RCC tab, LSE will be used to trim the clock source for a more accurate communication process. We will proceed to the clock configuration and we will switch from the default configuration which is 4 MHz on MSI to 48 MHz which is the requirement frequency to use with CAN communication. As a final step before generating our code we will go to the configuration tab and we will configure the CAN connectivity. The process of obtaining such input values is out of the scope of this training but we will provide a link with detailed information on how to calculate them. So on prescalar we will type 12 timequantainbitsegment1 13 times timequantainsegment2 is 2 times then the operation mode will be loopback. On NVIC settings we will enable CANRX0 interrupt. To finalize we will open RCC tab just to make sure the MSI auto calibration is enabled. With all steps concluded we can save our project and generate our code selecting system workbench for STM32 as our IDE. After the code is generated by STM32QBMX and our project is imported in system workbench for STM32 we will open our main.c file stored inside the source folder. We will go to the user code begin private variables section and we will create a CAN filter configuration structure called sfilterconfig. The purpose of this structure is to configure the filter features of CAN which means the blocking or preventing the reception of unwanted CAN messages. The next step is to create two additional structures one for the transmitted CAN message called TXMessage and other for the received CAN messages called RXMessage. Then on the user code begin to section we will fill the sfilter structure with the desired configuration. By default the filtering on CAN implementation is disabled so no message will be received until we enable it. By default CAN peripheral is configured in a way that no message can be received. So we will specify the filter number with zero. On filter mode we will select mask mode as it prevents from receiving messages which are not masked as specified. The filter scaled will be defined as 32 bits. We set our numeric values to zero as this means that we do not want to block any incoming message. No numeric parameters as the filter mode and filter scale do not have any impact on in such case. Then we set filter activation to enable as we want to apply this configuration. To finalize and to submit the new settings we will call the hlcanconfig filter. The first parameter is the handler for the CAN one. The second parameter will be the pointer to the structure for the CAN filtering. At this point CAN filter is fully configured and we can move to start defining the other structures we initialized before. Now we will create a relationship between the created structures and the CAN peripheral structure fields where the message to be sent and received are stored. And we can do so by typing the following. Then we proceed to specify the structure of the message to be transmitted. The TX message fields are the STDID or standard identifier is 0ax123. RTR field is the type of message that CAN will transmit. There are few types of CAN message and the most popular is the CAN message with data which we will use in this hands on. Next item it will be the IDE. The IDE means identifier type. DLC defines the data length for the message. We will set to 8 bytes which is the maximum supported by the CAN protocol. Then we will proceed to fill the data array from the index 0 to 7. In the end we will call the hlcanreceiveIT. The reason why we call this function here is just to enable the interrupt from the data reception of the CAN interface. In the infinite loop we will periodically send the CAN message with hltransmit function where the handler and the timeout period needs to be specified. Let's now go to the stml for xx underscore it dot c and on the CAN1RxR key handler which is responsible for the data reception and as a first step we will use the function hlunlockCan1. Then we will call hlcanreceiveIT and this function is responsible for the reception of the CAN message inside the handler. We can now build our application and enter debug mode. On the watch expressions tab we will add rxmessage and txmessage structures just to be able to see what's inside of the structures. We will execute the program instruction by instruction and if everything goes accordingly we should see the message tx being configured and the message transmitted. On the message rxstructure we expect to receive the very same message. The program is working fine and achieving what we expected. Thank you for your attention.