Full text
Final Degree Project Bachelor’s Degree in Industrial Technology Engineering CAN FD Datalogger based on Python and Raspberry Pi REPORT Author: Albert Burgués Marsà Supervisor: Manuel Moreno Eguílaz Call: June 2020 Escola Tècnica Superior d’Enginyeria Industrial de Barcelona
Pag. 2 Report
Review This project pretends to show the development of a CAN FD datalogger in a way that one reader with basic knowledge in the field of electronics and programming in Python can understand the concepts treated in it, as they will be briefly explained before jumping into them. In first place, the basic concepts of the different types of communication between electronic devices and communication protocols that will be approached on this project such as SPI and CAN FD are explained, remarking their principal characteristics and different advantages. In second place, the hardware components used in the project are exposed, showing the specifications of each component. In the case of the CAN FD controller used in this project, the software part will also be explained, exposing how the data is stored, the different registers that are used and how they work and finally, how the data has to be sent to the controller in order to be correctly processed. Finally, the process of the datalogger development is explained step by step, starting for the modifications done to an already existing Python library in order to adapt it to this project and how some aspects of the original CAN FD driver are improved. Then, the making of the main script responsible of running all the libraries needed to set up the datalogger is described. For a more advanced datalogger, it is explained how hardware filters and masks can be implemented, improving the functionality of the CAN FD driver. Finally, some tests for the filters will be showcased in order to demonstrate their effectivity and the overall performance of the datalogger.
Pag. 4 Report Summary REVIEW ____________________________________________________ 3 SUMMARY _________________________________________________ 4 1. GLOSSARY _____________________________________________ 8 2. INTRODUCTION _________________________________________ 9 2.1. Objectives of the project..................................................................................9 2.2. Previous requirements ....................................................................................9 3. INTRODUCTION TO COMMUNICATION PROTOCOLS __________10 3.1. Parallel communication .................................................................................10 3.2. Serial communication ....................................................................................11 3.2.1. Asynchronous communication ................................................................... 12 3.2.1.1. CAN protocol ................................................................................. 12 3.2.1.2. CAN FD protocol ........................................................................... 13 3.2.2. Synchronous communication ..................................................................... 15 3.2.2.1. SPI communication ........................................................................ 15 4. HARDWARE DESCRIPTION________________________________18 4.1. Raspberry Pi .................................................................................................18 4.2. MCP2517FD CLICK .....................................................................................19 4.2.1. SPI interface ............................................................................................. 20 4.3. Assembly ......................................................................................................21 5. DEVELOPMENT OF CAN FD DATALOGGER __________________23 5.1. Initial state .....................................................................................................23 5.2. Modification of the CAN FD library ................................................................24 5.2.1. Assert / Deassert ...................................................................................... 25 5.2.2. Read / Write ............................................................................................. 26 5.3. Running CANFD library: Tests......................................................................27 5.3.1. Test 1 ...................................................................................................... 28 5.3.2. Test 2 ...................................................................................................... 29 5.3.3. Test 3 ...................................................................................................... 30 5.3.4. Test 4 ...................................................................................................... 31 5.3.5. Test 5 and 6 ............................................................................................. 31
5.4. Setting up loopback mode ............................................................................ 32 5.4.1. Creation of random parameters ................................................................. 32 5.4.2. Message transfer ...................................................................................... 33 5.4.3. Receiving the message ............................................................................. 33 5.4.4. Data logging............................................................................................. 33 5.5. Modifications and improvements on the original code.................................. 34 5.5.1. Modification of transmitMessageTasks and transmitChannelLoad: ............... 34 5.6. Filters and masks ......................................................................................... 37 5.6.1. Classes ................................................................................................... 38 5.6.2. Functions ................................................................................................. 40 5.6.2.1. filterDisable / filterEnable ................................................................. 40 5.6.2.2. filterObjectConfigure / filterMaskConfigure ........................................ 41 5.6.2.3. filterToFifoLink ............................................................................... 41 5.6.3. Script ....................................................................................................... 41 5.6.4. Errors ...................................................................................................... 42 5.6.5. Tests ....................................................................................................... 43 5.6.5.1. Test 1 ............................................................................................ 43 5.6.5.2. Test 2 ............................................................................................ 44 5.6.5.3. Test 3 ............................................................................................ 45 6. TEMPORAL SCHEDULE __________________________________ 46 7. BUDGET ______________________________________________ 47 8. ENVIRONMENTAL IMPACT _______________________________ 48 CONCLUSIONS _____________________________________________ 49 ACKNOWLEDGMENTS ______________________________________ 50 BIBLIOGRAPHY ____________________________________________ 51
CANFD datalogger based on python and Raspberry Pi Pag. 7
Pag. 8 Report 1. Glossary CAN Controller Area Network CAN FD Controller Area Network with Flexible Data-rate CLK Clock signal Cs Chip Select signal FIFO First-In-First-Out stack GPIO General Purpose Input-Output RX reception TX Transmission MISO Master In, Slave Out MOSI Master Out, Slave In SDI Slave Data In SDO Slave Data Out RAM Random Access Memory SPI Serial Peripheral Interface .py Python file extension
CANFD datalogger based on python and Raspberry Pi Pag. 9 2. Introduction 2.1. Objectives of the project The main objective of this project is to develop and assembly a CANFD datalogger to be used in the cars manufactured in the upcoming years. One Raspberry PI and Python are the core of the datalogger. A secondary objective is to apply the knowledge about software programming in Python learned during the Bachelor Degree in Industrial Engineering. Finally, the project is a great opportunity to learn fundamentals on electronics and communications protocols, especially CANFD, the new generation of communications buses in the automotive industry. 2.2. Previous requirements In order to fully understand the concepts treated on this project, it would be optimal to have previous knowledge in Python programming, as the basic concepts will be considered known. Concepts as Python classes, basic functions and libraries (time, binascii, datetime…) will not be explained. Besides of that, it is recommended to have previous experience related with electronic communication protocols such as SPI and CAN FD.
Pag. 16 Report The data to be sent has arbitrary choice of message size, content, and purpose, which makes it simple and flexible. Its interface consists of four communication buses: CLK: The clock signal generated by the master that dictates the instructions of the whole system. MOSI: Meaning Master Out Slave In, it is the bus responsible for sending the data from the master to the slave. MISO: Master In Slave Out, as it is a full duplex communicating system It permits to send data also from slave to master. CS / SS: The chip select signal or also called slave select, it is the responsible to establish communication or not with each slave, sending a 0 (enabled) or 1 (disabled). Normally for every sent message the master opens communication with the slaves and closes it after the transmission is done. Figure 8: Scheme of master device communicating with three slaves through SPI. Source: [10] Figure 9: Example of a signal sent (S) and another one received (F) simultaneously by SPI. Source: [11]
CANFD datalogger based on python and Raspberry Pi Pag. 17 For the above reasons, its simplicity, and because the selected board for this project that carries the CANFD controller offers an easy access to work with SPI interface, this method will be the one selected to build the communication between the Raspberry Pi and the CANFD controller on this project. The following scheme shows the structure of the datalogger, formed by the CAN FD board that will receive the data frames from any device, the Raspberry Pi that will read this frame to process it and finally, one SD memory card to store the data buffer in it. MISO CANFD Board Raspberry Pi (Data processing) Micro-SD Card (data storage) MOSI SS CLK CANFD data Figure 10: Scheme of the distribution between components of the CAN FD datalogger. Source: own.
Pag. 18 Report 4. Hardware description 4.1. Raspberry Pi The Raspberry Pi is a small computer built in a single electronic board running Linux. It offers plenty of options to stablish connection with other devices starting by the most essential, which are four USB ports to connect mouse and keyboard necessary to operate with it, Ethernet port for internet browsing, HDMI, and of course a micro - USB entry for power supply. Moreover, it has the GIPIO, which is an arrangement of pins capable of sending electrical pulses to communicate with peripherals most likely to be microcontrollers, through various communication protocols that has available, being one of them the SPI. Also, it has a slot to put a micro-SD card where the operating system will be stored, while at the same time all the remaining space will be used as free storage room. Figure 11: Scheme of the Raspberry Pi GPIO layout. Source: [12]
CANFD datalogger based on python and Raspberry Pi Pag. 19 4.2. MCP2517FD CLICK The MCP2517FD is an external embedded system that incorporates a CANFD controller and a high-speed transceiver in one board, adding a pin setup to send and receive data via SPI, which will be connected to the Raspberry Pi. Therefore, it makes it easy to add a CAN FD channel to any source. It includes the following features: CAN FD bus input It offers improved electromagnetic compatibility due to its included transceiver and electrostatic discharge performance. The transceiver is designed for high-speed (up to 5Mbit/s) Supports both, CAN frames in the Classical format and CAN FD format. Works with 20MHz or 40MHz of SPI clock. Needs both 3.3v and 5v supply which will be fed from the Raspberry Pi board. It carries 32 FIFO’s, that can be configured as transmit or receive channels The messages are stored in its RAM memory which is divided in registers, where each one has its own identifier or address, and can store four bytes of information in it (one word of 32 bits). Each register has its own purpose and pre-set parameters that can change its value. For example, as shown in fig. 13, the register used to define the identifier of a CANFD message, takes four parameters: The “SID”, which is the standard identifier of the message, taking 11 bits, the “EID”, which is the extended identifier, with 18 bits, the “SID11” parameter which consists of one bit that if takes the value 1 the standard ID is extended to 12 bits, and finally the last two bits are unimplemented, meaning that it’s value does not matter. Figure 12: mcp2517fd click board. Source: [13]
Pag. 20 Report Figure 13: Transmit message object register. Source: [1] (Page 64) 4.2.1. SPI interface In order to communicate with other peripherals using SPI, it is crucial to understand how the data must be sent to the controller in order to make it comprehensible, so it can know what to do next. For every time it is wanted to establish communication, a message called data buffer has to be built. As the following table shows, there are three basic commands that can be given to the controller, and with them it is possible to execute any action the controller can do. Every instruction is associated with a specific command (C), that corresponds to a binary number. This command has to be in first place of the whole data buffer, so the first thing the controller will notice is the type of task it has to execute. Then the letters A and D stand for “address” and “data”. If the instruction to be given is reset, then there is no need of specifying any address or data, so the buffer of information to be sent will only be the reset command and the controller will know what to do. In case that it is wanted to read some information from the controller, the first chunk of data will be again the instruction command, followed by the address from where it is wanted to read, completing the buffer of information to send. In exchange, when the instructions are received, the controller will return the data corresponding to the given address. Table 2: SPI instructions. Source: [1] (page 67)
CANFD datalogger based on python and Raspberry Pi Pag. 21 Finally, if the instruction is to write, the buffer to be sent will be composed by the write instruction, the address where it is wanted to write, and lastly all the data to write, always respecting the maximum of 32 bits per register. In this case, the controller will not return anything, but the content of its register will remain modified. Finally, for every buffer of information to be sent, the chip select value has to go from high to low (1 to 0 in binary) and remain like this until the message has been completely sent, then it has to change to high again. If these two actions are not done in every instruction given to the controller, it will not receive anything. Fig. 14 above shows an example of the behaviour of every SPI channel during the execution of read and write instructions. As it can be seen, the first thing to happen is the chip select pin nCS going from high to low before starting to transfer any data. Once that is done, the SDI pin can start receiving the data buffer with all the necessary information, which consists of 4 bits for the command instruction C followed by 12 bits for the address A. What happens next depends on which instruction was given. If the instruction was write, the controller will keep receiving the next bits of information through SDI channel, which are the bits of data D to write on the given address, and it will do it automatically once it has received them. If the instruction was read, the controller will extract the data from the given address and send it to any device through the SDO channel. Lastly, the nCS pin is set to high again stating that the operation has been completed. 4.3. Assembly The datalogger system is formed by the Raspberry Pi unit and the MCP external CAN FD controller, which have to be interconnected by the different SPI buses. Figure 14: SPI instruction format. Source: [1] (page67)
Pag. 22 Report Firstly, the power supply of the CAN FD board consists of one bus for the 3 V supply, plus another one for 5 V, and lastly the ground connection. Then, for the communication, the board has “SDO” and “SDI” pins, standing for slave data out and slave data in, which will be connected to the MISO and MOSI Raspberry pins, respectively, since the output data of the board will be the data income for the Raspberry Pi and vice versa. The board’s chip select pin (CS), which will do the function of enabling and disabling the slave, -in this case the CAN FD boardwill be assigned to a free pin of the Raspberry Pi, so its value can be controlled at any time. In the pin layout of the Raspberry Pi, there are some pins that have no particular use, and they can be set to 0 or 1 logical value at any time. Finally, the SCK pin of the board stating for SPI clock is connected to the CLK pin in order to synchronise the clocks of both devices. As it can be seen in fig. 15, the Raspberry Pi needs to be connected to other peripherals in order to operate with it, since the programming of this project will be done from it and the help of a PC will not be needed at any time. The Raspberry Pi needs a power supply of 5v and 3 A, it has an HDMI port which permits to connect it with a monitor, then mouse and keyboard can be added in its USB ports to manipulate it, and finally an Ethernet port adds the possibility to connect it to internet so it can connect to other devices to send and receive its files. Figure 15: Assembly of the datalogger components. Source: own. Power cable HDMI port Ethernet port Mouse + Keyboard
CANFD datalogger based on python and Raspberry Pi Pag. 23 5. Development of CAN FD datalogger 5.1. Initial state The starting point of this project was an already developed set of libraries that will be discussed later, where the most important one is called “canfdlib.py”, which was a previously created library by another student with the purpose to operate with the CAN FD controller trough a computer. This library was responsible for the configuration of the controller, and the gathering of all necessary functions to work with it. The used connection element between controller and PC in this scenario was the MPSSE (Multi-Protocol Synchronous Serial Engine), a cable that transfers information between a USB port and SPI. Therefore, as canfdlib had been designed for a different purpose, next step was to re-design it in order to make it functional for the communication with a Raspberry Pi instead of a computer. To do so, it was necessary to remove all the functions and parts of the code related with the USB connection, and re make them in order to work with the SPI protocol. Before making any modification to the library, the way it works in every part had to be well understood in order to know which elements had to be changed and how, and thus it will be explained. Canfdlib contains the CANFD_SPI class. This class defines an object that will represent the SPI communication itself, and it will be the container of all the following functions, called methods of the class. Finally, these functions can be classified in different groups or sections where each one carries out a specific task, and they will be discussed hereafter. On the first lines of the original canfdlib, all libraries needed (constants, classes, binascii,...) are called in. These libraries contain the constant parameters and basic functions that will be used repeatedly in canfdlib. Then, the __init__ method is called to create the CANFD_SPI object, setting all needed parameters to define it. Next step is to create all the variables that will be used by the following functions (transmit and receive buffers, error flags, operation mode…etc.) and setting them to default values, which are imported from the constants library. The first function called “initialize”, is used to prepare the CAN FD controller to be used by resetting it to default state and set up the configuration registers. Then, the basic functions that will be used to establish communication via SPI are defined, such as writeByte and readByte. Next group of functions is the bigger one, and takes the role of configuring the controller (setting
Pag. 24 Report up which the transmit and register FIFO registers are, setting up operation mode, initialise the RAM memory…). Finally, the last group of functions which will be discussed more consistently in 5.4.2, are the ones that will be used in the datalogger main script. It is composed by more complex communication functions, such as transmitMessageTasks, transmitChannelEventGet and TransmitChannelLoad. The task they do when working together is to check if the FIFO queue is empty before sending a message, and if empty, the message is then sent to the controller. The same operation happens when a message has to be received from the controller to any device. 5.2. Modification of the CAN FD library In this chapter the process followed to change and improve the original source will be explained. When the code was well understood, it was time to discard the useless files and select the ones that had to be reconverted, which where the following: CAN FD library “canfdlib.py” whose structure has been exposed earlier. The “constants.py” file that contains all the unvarying parameters and useful register addresses of the controller. It also contains parameters such as the basic SPI instructions (reset, read, write), CAN operation modes (which are encrypted in hexadecimal values), message ID’s and many other parameters that would take too much space if written inside of CAN FD library. The “classes.py” file defines every important register of the controller as an object with its own parameters and properties in order to make them easy to set and edit later. As explained earlier, each register consists of four bytes or 32 bits of information, and each one has a different purpose, having different parameters of different lengths. Finally, every register object offers the option to build a word automatically by putting together all the parameters of the register by their correct order, which is useful when all the parameters are defined, but it is wanted to write the whole register in the address, so it can be done with a simple “self.writeWord(registerObject.word, address)”. The “rw_test.py” file is a Python script that calls every function of the CAN FD library and puts it to work in order to check that they are all correct and functional.
CANFD datalogger based on python and Raspberry Pi Pag. 25 One of the original files was “ft.py”, a library that makes possible the communication between a USB port and SPI through the MPSSE cable, containing all basic functions for communication, classes and needed parameters. This library carried a class that represents the SPI communication as it happens in canfdlib, which is called “SPI” and contains the basic functions to communicate through it. As MPSSE cable will not be used, ft library was removed and the line that imported it in canfdlib was deleted. With the useless files discarded and the ones to be changed selected, it was time to focus on making them work for the purpose of this project. As canfdlib contains all the functions responsible to execute every action, first task was to make sure that messages could be sent to the CAN FD controller and also be read from it. To do so, the first functions that needed to be changed where: assert, deassert, read and write. All of these functions where using elements imported from ft library or they were entirely written in it, and now as this library had been disassociated from canfdlib they had to be re-written inside of it instead of being called in from outside. 5.2.1. Assert / Deassert What assert and deassert functions do is to change the value of the chip select pin. As explained before in the hardware description, before giving any instruction to the controller chip select it has to switch from logic 1 to 0 (deassert), and when finished it has to come back to 1 (assert). At first instance assert, deassert, read and write where already made as methods of the “SPI” Class CANFD_SPI Class SPI Figure 16: Initial files of the project with their principal classes. Source: own. (Deleted)
Pag. 32 Report 5.4. Setting up loopback mode Once it had been proven that all functions of the library were working correctly, it was time to use them together. The objective of this part of the project was to create a new script that simulates a realistic usage of the CAN FD datalogger interacting with a car. To recreate this situation, the intention was to set up a loop in which the Raspberry Pi creates a random CAN FD message and checks if the TX buffer is not full. If not, sends it to the controller as it was being sent from a car. When the controller has received this message the Raspberry Pi checks if there are any new messages in the RX buffer, and if affirmative, it reads the complete CAN FD message extracting the data from it and saving it in a text file. In the first lines of the loopback mode script, the main libraries canfdlib and constants are called in. Random library is also used in the process to create the CAN FD messages that will be explained hereafter, binascii is used to help visualize all the different numeric formats that will be used, and finally time and datetime libraries will allow to know the exact time the messages arrived in the Raspberry Pi, so this information will be added next to the text file. Next the SPI connection is set up by defining the “canfd” object which represents the CANFD_SPI class, and calling the initialize function to configure the controller. Then the controller operation mode is set to loopback mode and finally the loop that will contain all the steps is created. 5.4.1. Creation of random parameters The first part of the loop has the function to create the random elements of the CAN FD message that will be sent to the CAN FD controller. Firstly, the length of the data buffer that will be sent (DLC) is defined by generating a random number of bytes between 1 and 64, as this is the maximum data length that CAN FD allows. Then, for every byte in the DLC, a random value is generated and put in a list, generating the buffer of data that will be transferred, called txd. Then the identifier of the message is defined by creating a random number of 11 bits of length.
CANFD datalogger based on python and Raspberry Pi Pag. 33 5.4.2. Message transfer The second part of the loop has the goal to send the CAN FD message to the controller. The function responsible for this action is called transmitMessageTasks, and takes the previously made identifier, txd and the DLC as parameters. TransmitMessageTasks is subdivided in three steps that will be explained hereafter. Firstly, transmitMessageTasks defines the values of all the parameters that form the CAN FD message (SID, EID, DLC, FDF, IDE…). The second task this function does is to check the register that shows the state of the TX flags by calling the transmitChannelEventGet function. TX flags are responsible of reporting the status of the FIFO channel configured as a transmit one, and they are used to check if it is full or not. If TX FIFO channel is not full, this means that the message can be sent, and if it is found out that the channel is full, the transmission is aborted. The last task then is to prepare the CAN FD message and sent it. This operation is developed by transmitChannelLoad function, which creates a buffer of data that will be filled by every part of the whole CANFD message in the correct order, and finally sending it to the controller with the writeByteArray function. 5.4.3. Receiving the message Once the controller has received the CANFD message, the Raspberry Pi proceeds to read it. The function called to do this is receiveMessageTasks, which has a similar structure than transmitMessageTasks, but without the first step. RX flags are read by calling the receiveChannelEventGet function. If the RX FIFO channel is not empty, recceiveMessageGet function is called to read the message in the RX FIFO channel. If it is empty, the task is finished. 5.4.4. Data logging Finally, when the CAN FD message has been read from the CAN FD controller, the last part of the loop has the objective to create a text file and then store in it every message received from the controller, showing a history of all the data obtained. To do it, firstly the time when the message was received is saved by calling the datetime library
Pag. 34 Report and the function now. Then, the text file is created by using the open command. In the next lines, every part of information that is wanted to include is added, and the format in how it will be displayed is set up. As it can be seen on Listing 8, every message received will be written in a row, where first the time when it arrived will be shown, then the length of the message, followed by its identifier, and lastly the message will be shown in hexadecimal format. Lastly, as an extra feature, the whole loop was introduced inside of a conditional command that is able to break it at any time by pressing control + C keys on the keyboard, giving the option to manually end the program. 5.5. Modifications and improvements on the original code Once the whole library was functional and it could run in loopback mode without any problems, it was decided to optimize some details of the library with the goal to make it easier to understand and to work with. 5.5.1. Modification of transmitMessageTasks and transmitChannelLoad: The original transmitMessageTasks function, which has the responsibility to elaborate a complete CANFD message by joining all of its elements and send it to the CAN FD controller, only received the data buffer to transfer txd as a parameter. That was because the identifier of Listing 8: Data logged on text file. Source: own.
CANFD datalogger based on python and Raspberry Pi Pag. 35 the message and its DLC were intrinsic properties of the message, defined before by another function called initTxObj. The task initTxObj developed was to define all of the CAN FD elements and giving them their values. As this simple task could be included inside of transmitMessageTasks, the function initObj was deleted and included on the first lines of transmitMessageTasks, as it can be seen on the green rectangle in listing 9. As a consequence of the last modification done in the function, the identifier of the message and the DLC were added as parameters, and thus they may take any value. As DLC was now a given parameter, there was no necessity of using the expression “self.txdlc”, which was fixed to 64 in the original code. Thus, it was deleted from the code. With the addition of the new parameters, as it can be seen in the blue rectangle in listing 9, three evaluations were added to the code in order to make sure that the given values are correct. In first place, it is checked that the value of the message identifier is not bigger than 0x7FF, as a CAN FD standard identifier takes 11bits. Secondly, it is determined that the length of the data buffer is not smaller than the actual DLC, and the last test checks if the DLC takes a valid number, as the CAN FD messages have to take a particular length between 0 and 64 bytes, as shown in table 1. If any of these three tests is not passed satisfactory, the function ends the task and returns an empty list. The last modification done in this function was deleting an unnecessary part of the code, shown in the red rectangle in listing 9. As all necessary tests to prove that the parameters were correct were already taken and simplified in a short space, the part of the original function that checked that the DLC matches the length of the data buffer was no longer needed, thus removed. Listing 9: TransmitMessageTasks function. Source: own.
Pag. 36 Report Iden and DLC parameters are then added to transmitChannelLoad. As this function is inside of transmitMessageTasks, these parameters are directly transferred to transmitChannelLoad, which uses them later to build the CAN FD message. Finally, as it was found out that the property self.state is not used in this project, it was deleted from all the library.
CANFD datalogger based on python and Raspberry Pi Pag. 37 5.6. Filters and masks When the datalogger was finally functional and optimized, in order to make it a more complete device, it was wanted to incorporate hardware filters and masks. In electronic terms, a filter refers to an obstacle that only permits to pass the desired information through a communication channel, and blocks the rest of it. As filters can either work with software or hardware, the difference between them and why it was decided that was better to implement a hardware one will be explained hereafter. Software filters can be implemented by programming functions that take the parameters that are wanted to filter, evaluate them and decide if they can pass or not with a conditional command. As they work by calling software commands, these filters are easier to implement, but the disadvantage that they present is that working with software functions makes the device’s CPU actively analyse the variables that are wanted to be controlled, adding more computational load to the whole system and therefore making it slower and less efficient. On the other hand, hardware filters are more complex to program, but they work by using passive elements, which means that they do not use resources from the CPU unit, making them the most efficient option. The variable that was wanted to control or filter in this case was the identifier of the CAN FD message, as it describes which magnitude does the data quantify (voltage of the battery, temperature of a component…etc.). In a practical use of the datalogger, its user may not have interest on getting all the data that comes from a vehicle, but just gathering data from a certain part. On the other hand, a mask is a data buffer that has the same length as the filter, and it is used to determine which bits of every checked message must be compared with the filter to see if they match. If the mask bit is logic 0, the corresponding bit of the message is allowed to pass, regardless of its value. And if equals to logit 1, the corresponding bit of the message must match the filter bit. 0 1 1 0 0 0 0 0 0 0 0 1 1 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 0 0 0 1 Filter Mask Message Message can pass Figure 18: Example of the filter and mask behaviour. Source: own.
Pag. 38 Report 5.6.1. Classes In order to start working with filters and masks, it was necessary to insert the structure of all the registers that would be used. The two most essential registers for using filters on this controller are CiFLTOBJ and CiMASK registers, which represent the filter object and mask object itself, and they are defined by the following parameters: SID (11bits): This parameter takes the value of the identifier of the CAN FD message that is wanted to filter. EID (18 bits): In case of working with an extended CAN FD format, this parameter takes the value of the extended identifier that is wanted to filter. SID11 (1 bit): This parameter takes one bit and equals to logic 1 in case the standard ID has to be extended to 12 bits. EXIDE (1 bit): This bit determines if either extended or normal identifier messages are matched. As shown in table 3, the result not only depends on its value, but also depends on the value of the same bit for the mask object called MIDE. Unimplemented (1 bit): The last bit of these two registers is not used and read as a logic 0 EXIDE\MIDE 0 1 0 Standard Identifier Extended identifier 1 Extended identifier Standard Identifier Table 3: EXIDE/MIDE values. Source: own. Figure 19: CiFLTOBJ register. Source: [1] (page 13)
CANFD datalogger based on python and Raspberry Pi Pag. 39 Lastly, CiFLTCON register was added to classes. This register is also associated with every existing filter on the controller, and its function as its name suggests, is to control them by its two parameters. The first one, called Enable takes only one bit and it is the responsible to enable or disable every filter associated to it, being disabled when its value is logic 0 and vice versa. The second and last parameter, called BufferPointer, takes 5 bits and its function is to indicate in which channel the corresponding filter has to be applied. For example, in this case the filter has to be applied in the FIFO channel configured as the receive channel (RX). This register only takes one byte of information for each filter instead of taking 32 bits per filter as the rest, and the remaining two bits that were not mentioned are unimplemented. To define those registers and their parameters, a class for each one of them was created. As it can be seen in listing 10, these classes were divided in two parts. The first one is for defining the register with its parameters by using _fields_ command, and the second part provides the register object with the capacity of writing it automatically in a word of 32 bits, adding all the parameters in order to the resulting data buffer. Listing 10: Structure of a register class. Source: own. Figure 20: CiFLTCON register. Source: [1] (page 13)
Pag. 40 Report Finally, the first three addresses referred to the three different types of filter register, CiFLTOBJ0, CiMASK0 and CiFLTCON0 were added as variables to the constants file in order to work with them later. Each one of these types of registers has 32 channels as MCP2517FD CLICK allows to have 32 different filters active. Therefore, each channel was also added as a variable and linked to a number from 0 to 31. Adding this list, will allow to find the address of any filter register. For example, the CiFLTOBJ address for filter 3 would be: CiFLTOBJ0 + CAN_FILTER3. 5.6.2. Functions Once the registers are recognised by the library, it is possible to create objects with their parameters, edit them easily and write them in a word of 32 bits format if necessary. The next step is then to create the functions that would execute the operations to make the filters work. 5.6.2.1. filterDisable / filterEnable As it is mentioned on the MCP2517FD CLICK datasheet [1], filters can only be modified while they are disabled. For this reason, a function called filterDisable has been created in order to automatically disable any filter when needed. This function takes the number of the filter’s channel that is wanted to disable as a parameter, and then it creates the address of the CiFLTCON register that refers to this filter by summing the address of CiFLTCON0 plus the given number of the channel. Then, it saves the byte of the previously made address on a CiFLTCON object in order to change its enable bit from logic 1 to logic 0 and finally, overwrites the modified byte on the same address, having changed this filter from enabled to disabled. Listing 11: filterDisable function. Source: own.
CANFD datalogger based on python and Raspberry Pi Pag. 41 5.6.2.2. filterObjectConfigure / filterMaskConfigure FilterObjectConfigure and filterMaskConfigure functions are needed to setup the CiFLTOBJ and CiMASK registers. Once their parameters are defined, this function automatically writes the words of 32 bits that represents the CiFLTOBJ and CiMASK in its register, and it does it in two steps. First, the address where the register belongs is defined, and then, since the parameters of the register are already defined, the word command is called in order to put them in order inside of a data buffer of 32 bits of information, and finally this word is written in the defined address. 5.6.2.3. filterToFifoLink Lastly, filterToFifoLink has the objective to set up the CiFLTCON register, giving values to its parameters BufferPointer and Enable. To give these values, they are directly acquired as a parameter of the function. Finally, it creates the resulting buffer of data by calling the byte command, and writes the content of the register in its address defined by CiFLTCON0 + the channel of the filter, as in the other cases. 5.6.3. Script Once the classes and functions needed have already been created, they can be used together and finally set up the filters to work. The part of the canfd library where the filters would be set up is inside the initialize function, as it is the responsible of setting up the CAN FD controller before start working. In order to set up the filters and masks, first of all the filterDisable function is called, since they cannot be modified while active. Then, the filter that is wanted to apply is defined by giving value to all of its parameters, and since in this case it is wanted to filter the identifier of a nonextended CAN FD message, the only important parameter of the filter that has to be given is the SID, which indicates the message identifier that is wanted to not let pass. Once the filter is defined, filterObjectConfigure function is called in order to write it in its register, which will be the first one, as we are only using one filter. Then, the masks are defined following the same procedure but when defining its parameters, the given value for the MSID is 0x7FF in hexadecimal, or eleven 1’s in binary, as the CAN FD standard identifier has 11 bits and all of them are wanted to be checked.
Pag. 48 Report 8. Environmental impact As electronic devices are made of semiconductor materials that can be harmful for the environment if not properly recycled, it is always important to analyse its life cycle. Regarding to the Raspberry Pi, which is a small computer with remarkably high quantity of these materials, it adheres to the EU regulations in relation to the supply of electronic equipment. Over the last decade, several initiatives against e-waste that ends up in landfills on electronic manufacturers have been coming out as it can be seen in [15]. The factory which produces the Raspberry Pi units is owned by Sony, a company that extracts for itself the minerals needed for its electronic components. These materials, which are called “conflict minerals” come in their majority from countries with poor care for the environment as for equality and worker rights, and that is why Sony has its policy that applies to the components they source right down to the ores mined [16]. The fact that the Raspberry Pi foundation is associated to Sony, also made a great change in sustainability as previously all of its electronic components were imported from China, and since they affiliated with Sony their production moved to the U.K., where their company is settled, saving an important footprint cost for the transportation between these two countries. On the other hand, the fact that this project implements CAN FD instead of normal CAN makes a great difference in terms of energy consumption for the motives below. As explained in section 3.2.1.2, CAN FD can transmit data frames at a significantly higher speed than normal CAN. Needing less time to send every data frame reduces the amount of wasted energy, which at a useful life time scale, can make a remarkable difference in terms of energy consumption. Table 6: CAN vs. CAN FD transmission speed. Source: [22]
CANFD datalogger based on python and Raspberry Pi Pag. 49 Conclusions The objective of this project has been accomplished. A functional CAN FD datalogger capable of implementing hardware filters has been developed from a slave MCP2517fd click board used as a CAN FD external controller, and a master Raspberry Pi used as a storing device, connecting them through SPI communication. Also, an already existing library used to communicate a PC running Windows with the MCP2517FD click device through SPI communication has been successfully modified and improved in order to work with a Raspberry Pi. This library is capable of accessing all the different important registers of the MCP2517FD click and modify them in order to set it up and start receiving messages from any source capable of sending CAN FD messages, and send them to the Raspberry Pi where they will be saved in a text file with the time and date of reception (timestamp). For future projects as a continuation to this one, the majority of the source code can be improved by optimizing it, as it uses a notorious amount of unnecessary commands (if and for) that makes the code imperfect and could be undoubtedly more efficient. In addition to that, a WiFi module could be integrated to the datalogger in order to make it capable to communicate with other devices in order to send the data it gathers instantly, and not needing to use a micro-SD card to store the information in it. Personally, this project has given me knowledge in micro controllers, software and electronics from a point where I had no base, proving to me that if something is appealing, it can be learnt much faster than one can expect. CAN FD is a very powerful communication that has its place in the future, and I appreciate having learnt on how to use it as it may be useful for the upcoming years. On the other hand, I have learnt how the Raspberry Pi is such a good option to launch a huge variety of projects for its potential, and these are only some of the things amongst many others.
Pag. 50 Report Acknowledgments I would like to thank to my family and friends for the unconditional support they have given me these years in order to overcome all obstacles and who offered their help every time I needed it. To my supervisor Manuel Moreno Eguílaz, first of all for offering me the opportunity to participate in this project that has stimulated my interest in software and electronics, and also for the help he has given to me during all the process of my final degree project. To my friend Albert Arlà for always being there, and in this project in particular for always being willing to sit together and spend the time it takes to clear any doubts or problems I had.
CANFD datalogger based on python and Raspberry Pi Pag. 51 Bibliography [1] Microchip External CAN FD Controller with SPI Interface. Mcp2517Fd datasheet. 2018. Accessed: 2020-02-02 [2] National instruments. CAN FD Explained – A Simple Intro (2020). https://www.csselectronics.com/screen/page/can-fd-flexible-data-rate-intro/language/en Accessed: 2020-02-01 [3] CAN in Automation (CIA). CAN FD – The basic idea. https://www.can-cia.org/canknowledge/can/can-fd/. Accessed: 2020-02-05 [4] Wikipedia. CAN FD. https://en.wikipedia.org/wiki/CAN_FD Accessed: 2020-02-01 [5] ¿Cual es la diferencia entre un Puerto serie y un Puerto paralelo?. https://curiosoando.com/cual-es-la-diferencia-entre-un-puerto-serie-y-un-puerto- paralelo. Accessed: 2020-03-15 [6] Wikipedia. Parallel communication. https://en.wikipedia.org/wiki/Parallel_communication Accessed: 2020-03-15 [7] Alchitry. Asynchronous serial. https://alchitry.com/blogs/tutorials/asynchronous-serial Accessed: 2020-03-15 [8] Texas Instrumentals. What do CAN bus signals look like? https://e2e.ti.com/blogs_/b/industrial_strength/archive/2015/06/04/what-do-can-bus- signals-look-like Accessed: 2020-04-03 [9] Wikipedia commons. Sync v async. https://commons.wikimedia.org/wiki/File:Sync_V_async.jpg Accessed: 2020-03-15 [10] Wikipedia. Serial Peripheral Interface. https://es.wikipedia.org/wiki/Serial_Peripheral_Interface Accessed: 2020-02-04 [11] Electric imp. Dev center. SPI Explained. https://developer.electricimp.com/resources/spi Accessed: 2020-02-04 [12] Megaleecher. Easy to understand Raspberry Pi GPIO. https://www.megaleecher.net/raspberry_pi_gpio_pinout_helper#axzz3LtNvrK5v Accessed: 2020-02-25 [13] Mikroe. MCP2517FD click. https://www.mikroe.com/mcp2517fd-click Accessed: 2020- 03-11
Pag. 52 Report [14] Raspberry Pi. Environmental impact of the Raspberry Pi. https://www.raspberrypi.org/forums/viewtopic.php?t=63620 Accessed: 2020-06-12 [15] Wikipedia. Waste and electronic equipment directive. https://en.wikipedia.org/wiki/Waste_Electrical_and_Electronic_Equipment_Directive#U K_implementation Accessed: 2020-06-12 [16] Sony. Addressing the Issue of Conflict Minerals. https://www.sony.net/SonyInfo/csr_report/sourcing/materials/conflict_minerals.html Accessed: 2020-06-12 [17] Ebay. MCP2517FD click. https://www.ebay.es/itm/392638213290 Accessed: 2020-06- 15 [18] Amazon. Monitor. https://www.amazon.es/Philips-243V7QDSB-00-resoluci%C3%B3n- tecnolog%C3%ADa/dp/B06Y13QBYL/ref=sr_1_3?__mk_es_ES=%C3%85M%C3%85 %C5%BD%C3%95%C3%91&dchild=1&keywords=monitor&qid=1592740927&sr=8-3 Accessed: 2020-06-15 [19] Amazon. HDMI cable. https://www.amazon.es/AmazonBasics-Velocidad-Ethernet- Est%C3%A1ndar- Reciente/dp/B014I8SIJY/ref=sr_1_1_sspa?__mk_es_ES=%C3%85M%C3%85%C5% BD%C3%95%C3%91&dchild=1&keywords=hdmi+cable&qid=1592741078&sr=8-1- spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEyVVlOV1dKNDhCQTNRJm VuY3J5cHRlZElkPUEwOTUxNjg5MjRBVUo5NUEyQTNVWCZlbmNyeXB0ZWRBZElk PUEwMzM5NjYzMllZT1FWQzRCWlRKNyZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb 249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU= Accessed: 2020-06-15 [20] Amazon. HP mouse. https://www.amazon.es/HP-Wired-%C3%93ptico-1000DPI- Ambidextro/dp/B01G8NHMGW/ref=sr_1_9?__mk_es_ES=%C3%85M%C3%85%C5% BD%C3%95%C3%91&dchild=1&keywords=mouse&qid=1592741962&sr=8-9 Accessed: 2020-06-15 [21] Amazon. Keyboard. https://www.amazon.es/VicTsing-Teclado-Espa%C3%B1ol- Chiclet- Windows/dp/B07H2SLJNQ/ref=sr_1_24_sspa?__mk_es_ES=%C3%85M%C3%85%C 5%BD%C3%95%C3%91&dchild=1&keywords=keyboard&qid=1592741996&sr=8-24- spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEzT000R1NTRk9ESUI2JmV uY3J5cHRlZElkPUEwMTk2MTU5VEU0WFc1UUIzVE1LJmVuY3J5cHRlZEFkSWQ9Q
CANFD datalogger based on python and Raspberry Pi Pag. 53 TAwNjg4MDkzTzZKTFZMS0JFQUNZJndpZGdldE5hbWU9c3BfbXRmJmFjdGlvbj1jbGl ja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ== Accessed: 2020-06-15 [22] Universitatea Politehnica Timișoara. CAN with Flexible Data-Rate. CAN FD. http://www.aut.upt.ro/~pal-stefan.murvay/teaching/nes/Lecture_05_CAN-FD.pdf Accessed: 2020-06-16