hello my name is Dave Robbins and I'm giving a talk called getting naughty on the CAN bus with the car hacking village badge so welcome to Las Vegas the home of the $30 cocktail these days kind of strange the badge here I'll talk more about what that is but this is the badge and go to the next slide what is what are we gonna talk about I'm just gonna briefly talk about me and I'm gonna talk about the company that I run I'm the CEO and I'm gonna talk a bit a little bit about the badge so just give you the basics so if you don't really want a lot of detail you can just skip out after that we're gonna talk about the Pi Pico which is the CPU or the board computer board that's used in the badge we're going to talk briefly about the CAN bus which is the technology used for networking and many industrial machines including vehicles we're going to talk about how we could generate and receive CAN data and then we're going to talk about a couple methods that we can do some of them that you really wasn't easy to do before this this new part I'll show you the software that comes with the badge there's actually a serial console and then I'll talk about other software for the badge and then related to that I've been doing this kind of stuff for over 30 years it's kind of my passion computers electronics I originally was a hardware person but there's just so much software with microcontrollers you had to learn that and I did every kind of software starting out with assembly code back in the day you know C code Python I did GPU programming which is really interesting I did FPGA programming and I did a lot of other things I did a lot of other things embedded software when it's programming etc so and we do all that at our work I started Intrepid control systems in my college dorm room in Ann Arbor Michigan in 1994 and we've been just supporting the vehicle electronics industry in the Detroit area and then eventually globally and then I am a dog person but I'm too busy to have a dog so I also do I'm not part-time dog watcher so now I'll talk about Intrepid control systems we're a company we were founded almost 30 years ago we provide test tools for automotive we're around the can hacking a lot related to can but we do way more than can automotive Ethernet is getting bigger than can now for future vehicles Intrepid is a global company around 200 people offices in Asia three in China Japan Korea India and then Europe we have Germany and in college school in the UK we are a partner for the OEM so a lot of the equipment we develop this basically on their specification and we're kind of like farther our new products or stuff that will appear in app in production like three to five years out so some of our new products like Ethernet things like that in car hacking village you know 2026 that you know that technology will be working on that I'm also looking combined with the packet hacking million village it's really a lot of the same technology we've been working on cybersecurity tools for a long time foundational OEM is wanted to be able to benchmark other companies so we developed a lot of tools for benchmarking including you know just figuring out how cars work so that we've been doing that for a long time and that happens to have a use case and cybersecurity just figuring out how a system works and its flaws the aftermarket it's a similar type of industry where people want to install a new radio and to take out the factory radio and the can bus is involved so they have to figure out how to make all that work you might have heard some of our main products vehicle spy it's common in the industry value can is our low-cost can can FD and then neo I interfaces which we continue to evolve so the latest one the neo I fire 3 has 16 can FD channels and in 2 gigabit Ethernet so Intrepid is way more than can bus we do every protocol in the vehicle can bus is the most fun though because it's it's it's you know it's not too hard to learn and you can get a lot done a lot of hacking hacking done with it okay I'm gonna give a demonstration of the badge right now so I'm gonna switch to the full screen okay so what we have here is most interesting part here is this is the PI Pico right here and this is the CPU and the USB interface for the badge the PI Pico has GPIO around it and we're using some of the GPIO and we actually connect it to a can physical layer here this can physical layer is from microchip it's capable of can FD as well although we're not doing any can FD with the badge that's basically something that based on the talk you could do can FD based on what I talked about there's two buttons and then there's two switches one of the switch turns on and off the power for the battery the battery is just used to kind of flash an LED while it's not plugged in the USE but most of the badge features only run on USB so so we have some just regular LEDs that are just powered off USB and then these are actually controlled by the CPU one of the switches doesn't do anything there's been those two buttons and there's you can hear that there's a buzzer on the back the badge itself has a screen that kind of explains a lot and then there's a QR code that links to our website with has more information just a heads up some of the soap screen has errata in terms of what pins connected to what so example the buzzer says GP 19 it's actually GP 20 so if you do sit down and write some software for it Python or C++ or basic or whatever your poison is go ahead and do that what what it does with when you just plug it in is you can push the button and it'll start a countdown and then what that's going to do is it's going to launch a kill on the canvas so it's actually going to generate canned messages out of this connector what you would wire in which I'll show a wiring setup I got later so just to give you an example so there's there's if you hold one button it does one kill another button does another kill if you hold both it does a third kill and I'll explain what those kills are later but we'll just go ahead and try one now you can see this LED illuminated This is the actual CAN bus activity, so we're sending out CAN messages right now, and the idea is that we're either doing a type of denial of service or sending incorrect frames, and there's actually three different fills, which I'll show you later, but this is never going to let you talk, so I think this is generating zero frames. If you generate continuous zero frames, that's the highest priority message. It will disable the CAN bus. Now, if you go plug this into your car and things go bad, don't call me, okay, because older cars weren't necessarily tested to be attacked, so use at your own risk or rent a car. I don't even know, but the first time I plugged into an old Ford, not with this badge, but I just had the CAN bus speed wrong. And the whole car just shut down. I had to just disconnect the battery and reset the whole car, so your mileage will vary. I'm interested to see what people in the community plug this thing into. So that's the basics of the badge. I'll demonstrate the USB software later. So this right here are some screenshots of the USB console. So if you plug this in to Linux or Mac or whatever, and you open up a serial console, the TTYSC. S whatever, whatever USB, the serial port it is, this will show up, and it will allow you to set up, configure the badge. The kills with the buttons are predefined kills, and then the menus allow you to configure out exactly what you want to do. First, let's talk about the Pi Pico. What is the Raspberry Pi Pico? Traditionally, the Raspberry Pi Foundation has been building single-view board computers that are based on Linux. And then the Raspberry Pi Pico is... This is the first single-board computer that is just a microcontroller. So the microcontroller runs almost no OS or no LL. It's lower cost, lower power. It's a lot different than the Raspberry Pi. Now, you might hear something called RP2040. To clear the confusion is the Raspberry Pi Pico is the name for the single-board computer. The microprocessor or the microcontroller on the board is the RP2040. Now, with the Raspberry Pi, they didn't sell the processor independently. It's just you buy the board, and then you can't buy the chip and make your own board. But that's different with the Raspberry Pi Pico. Now, the Pico board costs between $4 and $6, and they just released the $6 version last, I think last month, and it includes Wi-Fi. So you can do Wi-Fi type projects with this that connect to an access point or act like an access point. So it's pretty cool for $6 how much you get. The board itself is programmed. And it's supported by Raspberry Pi with C and C++, and Python. So there's a... I believe it's a micro-Python... Yeah, a micro-Python port for Raspberry Pi, or for Pico, which you can write... And it genteWAYs quickly write scripts. The board, it has the Raspberry Pi community behind it. So I think that's what is most interesting. to me is that getting support and finding people other people that did things the initial release was last year and I think maybe it didn't get as much attention as it should have because it didn't have wireless so now that has wireless will be really interesting to see how this board does take off the tools are all free so like if you've been in the embedded systems area for a long time its tools are a pain to get are very expensive I mean at work we've paid you know $10,000 for a C compiler because you know for the power PC because that's that was all that was available at the time but these tools are all free GCC Visual Studio Code and then even a hardware debugger you can use a PI Pico to beat debug another PI Pico so so you can use the $4 PI Pico to debug the other one which you got to have a debugger if you're doing any serious work but the most interesting feature of this board and the only reason I learned about it is because it's available availability if any type of microprocessor chips right now is so important and right now at least right now you can you can get these in thousands of quantities for projects so like at the for the batch we we bought reels of these months and months ago so you've made we had all the parts we needed the the the badge hosts the rp 1210 microcontroller which is when it first came out i looked at it and i just dismissed it as another metoo product but since it was hard for us to source well last year all we did was redesign our project products at work for availability of components which was not not the most interesting work but we learned a lot so this year i took a look at what was available and this part is super available and it's 70 cents if you buy the full reel and you can buy it directly from raspberry pi so i took a look at it and i was pretty impressed with what it what it had first of all cortex m0 who cares but there's a new trend to have like microcontrollers that have multi-cores and it opens up some interesting applications including the one that i'm talking about today but the most i think breakthrough thing that this part has is it has eight bit bang processors called a pio and i feel like there's so many problems in my career and embedded that are solved with this it's uh it's just a total breakthrough and it's a game changer especially on such a part that's so low cost and low power which before you really only option was fpga which is very complicated very complicated tools very hungry power hungry very expensive so it's kind of it's a game changer for the industry another thing is it's very fast 125 megahertz and that's a clock rate of eight nanoseconds so you can do a lot and you know things like bit banging canvas at 500 kilohertz is well i'm not going to give it away but it could be possible there are videos online where people overclocked it to over 300 megahertz so feel free to check those out and then use that you know use that your own you know use that your own experience so if you if you overclock it and you test it and everything it works that's great so if you want to bit bang things that uh you know 300 megahertz would be like three three or four nanoseconds or you know under four nanoseconds uh go ahead and do that it has usb which is really useful for bootloading and connecting to a pc it's 12 megabit which is disappointing because the thing is is that the pio engine can move so much data it just doesn't have anywhere to go it can't go up to a pc but it's a 70 cent part so that's that it's really great at pwm channels using the pio and the pf pwm you can generate a p pwm on every pin of this device so like things like uh robotics applications or you're controlling servos or you know you have a bunch of LEDs that you want to dim and you know full color LEDs it's really awesome and it's got like the standard digital peripherals and the pin mapping is real flexible so it's really useful when you're designing boards it does not have onboard flash so it actually has to load code from a serial flash at Udo which is not so great if you're used to microcontroller and there's no code protection so like if you design something with this it will be very easy to copy and voila this part is on the DEF CON 30 badge so this part right here is RP 2040 so I'm not really sure why they chose it I'm really interested to learn but I would guess having it available is probably maybe a good reason and I'm really excited to learn more about this badge this week and of course on the back of the badge is the Raspberry Pi Pico and it's just soldered down and you can we actually place this with our machines our SMT machine so it came on a reel it's placed right on the board and our guys did a really good job where the pins if you look at the pin hole pins they are totally open so you can put a pin header through the DEF CON badge and you can use the rest of the IO so think outside the badge use it you know write some code use the other pins to do stuff and have fun all right so now I'm going to talk a little bit about CAN bus it's very popular and it's kind of a huge focus of the car hacking Village the CAN bus also used in many other particles so like a boat if you have a boat there's something called NEMA I think 2000 and that uses the CAN bus so we were talking about like all the different ways you we can make different badges like we could have boat kill these on airplanes you could have airplane kill which no one liked that uh no one liked that name because we don't you know we don't want to get picked up by an NSA at DEF CON but anyway the CAN bus is used everywhere and all these attacks would work on that what is the basics when was CAN bus created who cares I mean really who cares uh it's been around forever it's going to be around forever more too so everyone listen to this when you're on your deathbed you're going to look over it at that machine keeping you alive and it's probably running CAN bus so and it's it's just a great uh inexpensive cheap protocol it's it's very robust and of course when people say what does that mean it's like you can really configure it all around it still probably will work and uh which is a terrible way to design a system ideally you design the system correctly and then whatever happens in the field you know it still works but I've seen systems over the years where they just take that robustness and use it as a feature why did they make the CAN bus who cares who you know go find go find another video online to learn about why they created it but we're going to talk about the what and how and this is just a basic CAN network where we have two wires like we invented the colors for CAN high and CAN low but I just remembered uh you know at the beginning we always said yellow is like the sun and green is like the grass but I don't it's been so long I don't remember but each end of the network is terminated so can needs a termination because it only drives the CAN bus uh in one direction and then it relies on the network your your bits will be all messed up and we actually have a feature in our product where we generate a pulse on CAN and based on the the the fall time of the transceiver back to recessive you can automatically determine if the network is terminated what does the bus actually look like so if you connected an oscilloscope to the bus there are two signals CAN high and CAN low and CAN high whenever there's a there's a dominant bit it goes high and CAN low goes low so it's a differential bus so the receiver actually measures the difference between the two lines and the difference is real important because in a vehicle the ground is used to carry current for all the different parts of the chassis so what happens is whenever currents are flowing through the ground they'll be on IR drop which will change the voltage at the ECU and if it goes you know if you're using the the ground as a reference you can have some real interesting problems like when only certain motors go off you lose communication so having a differential Network is a huge benefit now uh how do we get to this protocol oh on the badge that I'm sorry the the the thigh on the badge this little part uh that can cost you know between 30 cents and and the last year people were selling them for eight dollars. dollars but that part for microchip it generally it takes in a digital logic signal and then it generates the actual physical layer system so with the PI Pico will be generating this digital signal and the five will generate that what's important when you configure the network now when you first look at can and they show you this this diagram it's just like you immediately have like you need like aspirin or you know ibuprofen immediately because it just gives you a headache and like who cares about time component whatever the main important thing about this is the bit time and that's the length of the actual bit and then the sample point those are the two things that care so if you got those correct the can bustle work now the sample point is when the digital logic actually looks at the bit to determine if it's one or zero okay so what happens is you can have it wrong but then on certain networks if you wire it a certain way you won't you'll get errors and there's actually attack by by Candice labs that you can do with the badge called a Janus attack which messes with this I wish I'll have more information about that later so what do we want to do with the badge we want to generate can boss frames so now I'm going to talk about how we're do that what's really awesome and I probably didn't realize this at the start when I looked at this part and having two CPU cores and an embedded system this is really useful because normally bit banging is just not possible because you have a you have a core and it's like it's got a handle USB or you know or it has to you know process some data or something like that you can kind of do some stuff with interrupts but it's a it's never really deterministic and you know you can stop everything and bit bang and that does work we did some stuff with that but if you do if you have to bit bang for too long and kind of you know things stop working so for example like USB on this on this badge it's interrupt driven so if you don't service it it will actually disconnect from USB so it's kind of annoying when you're using a debugger but having a separate CPU core allows you to do these bit banging and still have your full application running so you'll notice when I sent the cam messages with the badge it sent the cam messages and the LEDs flash and everything else still works it's pretty cool now you can have a second core but like how does it use how do they work together in with this With the RP2040, they did a really good job of allowing it to operate independently if you use it correctly. So there's, there's, essential for bit banging is you want to be able to be deterministic. That means when you write the software, it's exactly going to have that many cycles and that timing and everything. And they did, so the way you do it on this part is they have a feature called single cycle IO, which I'll explain. There's something called a bus matrix with SRAM, and then they have FIFOs between the cores so they can communicate. So I'll talk a little bit about that. And all this, all these diagrams are from the RP2040 data sheet, so you can learn more. It's really fun. Now, there's other CPUs that have dual cores out there. So NXP has them, Microchip has them. I would say, I can't tell you if they're done deterministically. Although, Microchip does have a DSPIC core, which I use to program and I love, but it's only 16 bit. They have a dual core now that runs at 10 nanoseconds. Here we go. Single cycle IO. So normally with an ARM processor, what happens is they, they have load and store instructions, meaning that's the only way memory is changed. It's through these instructions, which require access to the bus. And they all, they're always multi-cycle. So you, to do a load, it will have to go on the bus and then, and then the IO itself is memory mapped. So it will be a specific address they have to go to, to change. Now with a Cortex M0+, they have this feature called single cycle IO. So the logic in the core actually looks at what address that you're running to. And if it happens to be a specific address. It will, it can change that logic with one cycle. So the load and store instructions for those address take part eight nanoseconds. So you can actually, there's a, there's a register that will allow you to toggle a GPIO and you could just have a inline code that will, that you could generate a clock pulse every eight nanoseconds. So you can, you can bit pang things, you know, up to 65, 62 megahertz, you know, 62 and a half or half the clock rate. So it's, it's a really cool feature for a bit pang. Now, do you really need this to do CAN because like, if you look at the eight nanoseconds compared to like a standard CAN bit time at 500 kilobits, which is two microseconds, that's, you know, that's, you, do you need single cycle IO for that? I don't think so. But like, if you want to, if you want to do a faster protocol, like maybe CAN FD, then you want to have is, you know, you want to be able to save every instruction. Okay. Cool. Now, this is the other part. So the CPU just exists in the part and it needs access to RAM and it needs access to IO. So the single cycle IO takes care of, of the IO. Now the other part is you need RAM and you need RAM for your stack. You need RAM for your code because this is a processor that, you know, there's no flash or anything. And so when you go. To access the RAM, if you have to share the RAM with another core, that's a problem because depending on priorities, who gets it in cycles. So the trick is, is they created this thing called the bus matrix, which is actually kind of hard to see right here, but it's basically a logic, a logic device that, um, there's, there's certain masters, which in this case is there's three, uh, there's the core one, or core zero. Which is the first core. It gets kind of confusing. And then there's a second core, core one, and then DMA, and they all access, they go into this little box called the matrix, um, and then there's all these things that hang off of it. And the beauty of the matrix is as long as you're not accessing the same, uh, peripheral at the same time, or the, or, or the, um, I don't know what the best, let's call it peripheral. The, that there will be no weights for that, that, uh, resource. So for example, if core, uh, core zero is talking to this SRAM, and then core one is talking to another SRAM, uh, there will, there, there won't be any blocking. Uh, so that allows the core to run, uh, it allows the core to run without, without any blocking. So it gives you a deterministic clock rate for the core. Now it's important to know if you use this part, that depending on what address you ask since these SRAMs, they're striped. So, uh, if like you might take one address, but the next address will be on this SRAM. It's actually a cool thing because it allows, if, if you're running code out of the SRAM from both parts, they'll, they'll, they'll, they'll not always, uh, block each other. Um, you know, so that's interesting. But like, if you're going. If you're going for determinism, you can't use that striped feature. So in the code that I wrote uses one of the, one of the 4k SRAMs. So all the code and the stack all go in there. Okay. So this is basically a summary of what I just said. The, we use this independent SRAM, we're calling it, they call it scratch X actually in the code and then the second core. Another thing I want to mention that I might've skipped is, uh, on this first part here, there's actually a FIFO. And this allows you to communicate between the cores and that does, that's also deterministic. So, uh, core one, there's a FIFO that goes in one direction and then core zero has the opposite. So core zero can write to the FIFO and not block. And then core one can read that FIFO, you can check if something's there and use it or not. And that's how we actually trigger the CAN transmission. So I'm going to stop and I'm going to do a code demo of, of the CPU. Okay. Uh, Bitbang. So, uh, this is, this is going to be Visual Studio itself. Uh, so I'm back and I'm going to show, uh, the code for generating the, the CAN message. This is Visual Studio 22 and I have a plugin called Visual GDB, which has some tools, uh, for, uh, uh, it has some tools for using Visual Studio 20, uh, 22, actually all the previous versions, uh, with, for embedded process. That's pretty good. Uh, I'm having some issues with the debugger between the two parts. Uh, but let's go ahead and look at this. Uh, so this, this, this particular, uh, file is called, uh, CAN message builder. And this is, uh, uh, the, uh, I'll show you the first step. So basically what comes happens is. There's an init, uh, function here and it looks at what the type of kill it is. So this is the arbitrary kill. And then it will, uh, load some settings in the kill. And there is a, uh, function called create CAN message bits. So we're going to go look at that. Uh, so there's this object called, uh, CAN, which is an actual, it takes the CAN message and creates bits. It's ones and zeros based on the, the information. So we're loading in the CAN ID, uh, the different pieces, uh, of the extended frame, the data. And then there's, calls this function called CAN to bits. Uh, so what it does is it goes through here and it checks, uh, the bits and then adds them to an array. And, uh, I tried to do CAN FD, but it's, it's not supported. Uh. But if you, it just goes through the different fields of the CAN protocol and generates the bits. And if you go look at this function, uh, it's kind of interesting. It actually does the CRC and the, the bit stuffing of CAN, which I'm not going to explain in this video, but that's a key part, key parts of the CAN protocol. Uh, so that generates the actual ones and zeros. And then, uh, we come back to where we started. There's this build CAN message. And what we have here is, uh, there, there's this class called ARM. So normally you would use an assembler, which is a separate, uh, source file. Uh, but I've written a C++ class that dynamically generates the CortexM0 instructions. Now there's only like 30 instructions in the CortexM0, which is, uh, why I thought it would be hard to just dynamically generate messages. And it wasn't too bad. Uh, the, the instructions have derivatives. So I only really implemented the ones I'm using. Uh, so it'll probably grow over time. What's really cool about the CortexM0 is the instructions are binary compatible with all the CortexM. So M stands for microcontroller. So some of the more popular ones are CortexM3 or 4, uh, that have like more advanced instructions. And floating point in M7 as well. Uh, they're out there. So this code technically will work on those processors. Of course, the delays are different. Uh, because the, they're designed differently. So some of them have like dedicated RAM or they have a different number of stages and pipelines. So things take longer. Anyway, uh, what we're doing, the first thing we're doing is we're initializing the program at the address of that particular SRAM. Now the key part about a CortexM0. The CortexM part is the addresses that you call have to have a one in them. Otherwise they, you get a hard fault because one is the, the, the, the first bit says it's a thumb or arm instruction and the CortexM only supports, uh, thumb. Uh, so, so that's a weird thing I had to learn. Lots of, lots of crashing and hard to debug. One of the problems I have with this, with my setup right now is the second core is really difficult to debug. So there's the, the tools don't that I have. And I looked for, I, I haven't seen any really great solutions to debug both cores, which is kind of a bummer. Uh, so anyway, uh, we're, we're basically generating arm assembly here, move immediate logical shift here. Uh, and then there's literals. Uh, this is kind of interesting arm is a 16 bit instruction set, so it can only load, uh, small, uh, constants. So the way they do it is they actually just in the code itself, they'll have the 32 bit constant and then they'll load that from memory. Uh, so that's the assembler actually already does that, but like we do it dynamically. So back going through the, uh, code here, here's where we actually generate the code for generating CAN. So we have the CAN, we have the bit buffer, which is just ones and zeros at this point. And we either generate a set instruction or a clear instruction. Uh, so what, what the clear instruction, it's just storing, uh, data to the, the memory location where it controls the, uh, output. So there's a different address for clear versus set. Uh, and then there's a, it does a branch and link, which is basically calling a subroutine, uh, to a delay. So based on your bit time, the delay, uh, will be different. And I'll show you basically that at the end here, we actually build a delay function. Okay. And depending on, uh, what you're doing, you're generating, you're going to, you're going to build a delay for your bit period, and then you'll build a delay for consecutive messages. And that's called the inner frame period. So we'll go to this. Um, so here, uh, you pass in the number of the delay in nanoseconds, and then it generates a cycle accurate delay, um, with an assembler. Now, how did I get this? Perfect. I used an oscilloscope and I verified it. So step one, I calculate it. Step two, it didn't make sense. Step three, you go back and trying to figure out what went wrong. And eventually that the, the instructions or the, the knowledge matched the actual, uh, um, the code. So what's cool is the difference between Cortex M zero plus and the other court, all the other cortex ends, it has two, two staged pipeline. So that means that the branches take two cycles versus three. And all the other cortex sounds. So if you want to port this code, that would be definitely a consideration. Um, but the end result is, uh, the main core software calls a function called, sorry, that's the wrong source code, uh, start. And then we'll call this thing called send message. Okay. Okay. And all this does is if the FIFO is ready, it pushes a value into the FIFO. And then the second core, um, this is process core one. Um, it just, uh, basically runs the program and sends the message and it doesn't show where it's checking the FIFO. So I actually have to go to a different source file, uh, for that. So here's the actual source for the main is checking the FIFO. And then if it does, it calls that function that will trigger the send. Uh, so this is the actual initialization for the code. And, uh, this is where you start the core. So the first core starts the second core and that generates scan messages. Okay. Okay. So, uh, now you might not understand all that code, but the main purpose of the presentation is the time trying to give you clues and give you the idea in your head that it's definitely possible to bit bang digital protocols that in the past, I think we're too fast to do that. So, uh, for example, a can frame is, uh, uh, typically the 500 kilobits. That's it's the bit time is, is 200 or 2000. Uh, nanoseconds. So with an eight nanosecond clock rate, you have 250 instructions between each bit, uh, that the CPU can use to, to, to, to deal with can. So it just opens up some, some really cool opportunities, uh, uh, for, for can hacking. Now, of course, now we want to receive CAN bus frames. So how can we do that? And, uh, this gave me a chance to talk about the bit bang processor. Uh, so. So the bit bang processor is, is a revolutionary, and it's a huge part, huge, uh, uh, uh, it's a, it's a huge piece of this part. So it's such an important part. It's got its own section in the data sheet. It's got, it takes up a huge part of the dye area, but it's, it's really just super capable. And what it is is there's actually two of them. Um, and the two PIO modules each have four bit bang processors, uh, and the four bit bang processors share some resources. And then they have their own independent resources as well. Uh, and the, it's, what's kind of cool, uh, that you can, there's ways that the, the, the bit bang processors can kind of start at the same time. And, uh, since they're all cycle accurate, you can do a lot of really cool, uh, coordination between the two. Uh, but it's very complicated. It's not complicated. It's actually very simple, but it's how to use it for a critical application. You just have to sit down and look at the instructions and see how it works. Uh, uh, but I, I did this. This is just a very simple, crappy, uh, CAN receive. So what does it have? It has eight different instructions. It doesn't sound like a lot, uh, but low and lower instructions are good because it maybe gives you, allows you to do an understanding of it. Uh, it's got FIFOs, uh, to communicate with the CPU. Uh, so basically the CPU will throw data in the FIFO and, and read data out. So for example, if you were doing CAN transmission, you would put those, those bits from CAN into that FIFO. And then the, the, the, the bit bank processor would read them out and send them. Uh, for this example with the receive, uh, I sampled the bus and then I slide the bits into the FIFO for the CPU to read. Uh, there's, there's 32 instructions only, and they're shared within the PIO by all the PIOs. So the CAN receive, or I'm going to show, or the code, um, it's, it's, I don't, it's, it's, uh, it takes like say 10 instructions or something. All those state machines could run simultaneously with those instructions. Uh, so if you wanted to capture, you know, eight CAN channels at the same time, you can do that. Uh, cause it's just, it all works in parallel. Pretty, pretty amazing. Uh, what are the instructions? Uh, jump, uh, the jump, it's a conditional jump or non-conditional jump, and it can jump anywhere in those 32, uh, instructions. Uh, it can jump on IOPIN, it can jump on, there's some scratch registers or accumulators, uh, stuff like that. Uh, wait, wait for, wait is waiting for usually a pin, uh, to change state. Uh, so for CAN, we're waiting for that start of frame. Uh, so as soon as that goes low, then it signals the start of a new CAN frame. In, uh, in is, in and out are just like USB where they're, they're the direction to the host. So in means it's going to the CPU, out means it's coming from the CPU. So in is, is basically shifting data in, um, into a shift register. Uh, and then out is out, er, er, from pins into a shift register, and then out is out from a shift register, two pins. Uh, so if you're doing CAN transmission, use out, CAN reception, we use in. Uh, push, push, uh, moves data from, uh, one of the registers, either the shift register or the, or the, uh, scratch register into the FIFO, and then pull, pulls a data value out. Uh, move, move just moves data between the different parts. Uh, different registers in the part. And the IRQ, it generates interrupt for the processor, which we actually use for the CAN reception. And then set will actually set IO pins or set values of the scratch registers. Okay. So now, uh, we're going to do a demonstration of the receive code for CAN bus. Okay. So we're back to the visual studio and this is in a class called CAN RX. Um. So just like, uh, there was a class that was created for generating Cortex M zero instructions. Uh, there's also an assembler that you can write assembly code for the PIO. Uh, but I wrote a class that just generates the PIO. It's very simple. Uh, and I, I want to dynamically change them in code. And honestly, the assembler, it just is, um, it's easier to use code. The only downside is that, uh, you have to have this code in your project. So the sum, the assembler that I wrote is extra code in your project where the assembler, if you just use theirs, uh, it just, it just works just like a C compiler, which is, it's actually really cool, um, how they did that. So when you learn more about it, but we're just going to go through this code. Uh, the CAN receive and the badge is, is kind of not a very good one. It didn't have a lot of time to work on it. Uh, but I just want to demonstrate the PIO engine. So it only works at one specific, uh, bit rate, which is 500 kilobits. So the first thing you're do, you're going to do is set up the clock period of the PIO. Now it can run at eight nanoseconds. What there's advantage is if you can use a small, a closer clock rate, which I'll explain. So this actually sets it up. So you have 32 different clocks per bit. Okay. Uh, so first thing we do, we initialize the, uh, PIO engine, and then we're going to set up the FIFOs. The FIFOs are configurable. There's a lot of cool things you can do with them. Uh, but two of the features that I use in this is there's a four word. So 32 bits, four 32 bit, uh, word FIFO on in and out, but you can actually join them. So they can be all in or all out. So I actually joined those 32 bit FIFOs, which means there's, uh, 200 fit or yeah, 32 times eight bits. Uh, and that's actually smaller than a CAN frame, any CAN frame. So you can actually, uh, use the FIFO to generate, to can frame. Uh, capture an entire CAN frame, uh, which is pretty cool, uh, without any CPU intervention. So we set the pins. We're only using one pin and this is the RX pin off the CAN transceiver. Uh, then we start the, uh, the assembler. Uh, we start encoding. Uh, I'm not going to talk about wrap target, uh, but we add a label. So for jumps, you need labels. Uh, so this is just like in the assembler. The first thing we're going to do is we're going to, uh, set up a, uh, uh, inner frame timeout. So this is how we detect when. Detect when the CAN message is done is there is a certain number of bits in between the messages. Um, then we're going to go ahead and sit ahead and wait for CAN to go low. Uh, so this false means low. Uh, so this is the wait instruction. Um, now each instruction can have its own delay. So if I look in there, each instruction can have its own delay. Uh, so that means after the instruction executes, it will actually wait this amount of time to go to the next instruction. Uh, so that's where the time determinism. All of the state machine is extremely useful. So what we're doing here is we're just waiting for the sample point to be 75%. And then what that is, is since I set up 32 clocks, it's 24 clocks to the sample point. Uh, so here we go. We're just going to fall into this and then we do a jump and we do a, a jump based on the CAN RX to determine if it's high or low. Now what's weird about the jump instruction, it only will jump on a high. So you have to design your code. Uh, if you want to, you want to do something when it's low, then you got to just jump somewhere else. But it totally works. It just uses another instruction. That's all. Uh, so if it doesn't jump, the CAN is low. When CAN is low, we refresh the timeout. Uh, and we're storing this into this Y scratch register. And then we, we call the IN instruction to, to sample the CAN line and put it, and that puts it into the, the, the shift register. And then we just jump, um, always back to checking the CAN line. Now, how, how, why am I checking the CAN line immediately? I'm actually not because in this IN instruction, we're waiting 32, uh, uh, clocks. So 32 clocks is one whole bit. And then I subtract away four because there's actually, each instruction takes one clock. So basically I took away the, the, the clocks of the instruction and then resampled the bit. If CAN is high, um, which is idle bus, uh, we sample the bit and then we jump. But in this jump, we actually decrement the Y scratch register. So what this does is it, it, it will, if, if Y is zero, it will, it will not jump. If not, it jumps and then decrements Y. So it's kind of a post decrement. If Y is zero at this point, then we fall through the loop to this. And then we, we flush the FIFO. So the FIFO will automatically flush when it fills 32 bits. But since the CAN is not going to end on a 32 bit boundary, we have to flush that data in. Um, and then we, we actually generate an interrupt to the processor. Okay. And then this just goes back to, uh, the wait, which is waiting for a start of frame. So this thing is just going to run. And then anytime it sees the start of frame, it's going to sample the CAN bus until it's idle. Okay. And then it will, it will put that data in the FIFO. Now I call this a crappy CAN decoder because it's missing a key feature of CAN and that's called resynchronization. Uh, so whenever there's a, uh, uh, a dominant edge, whenever the CAN is driven by the transceiver, uh, you're supposed to check the, the time resynchronization. So like if you, if you know a lot about UART, you, UART time, the, the receiver, since it's an asynchronous protocol, it has to derive its own res, the sample clock from the bits. So CAN, what it does is that anytime there's a high to low transition, it, it does what's called a resynchronization. Uh, where it, where it compares to where it thought the edge was and then adjusts its clock. Now I didn't do any of that. Is it possible to do? Absolutely it's possible to do. It's just more work. And I didn't have time to do, do that in this, this, this, uh, example. And, and, and honestly with the PIO received, it can totally work. The only problem with receive is, uh, acknowledgement. So like the, the PIO, I don't think can keep track of which bit is which and decode. Um, but maybe, maybe handshaking with the CBU can do that. But like knowing where to set the acknowledge bit is, is definitely going to be the biggest challenge. Now, normally if you're just connecting to existing bus, you don't care about the acknowledgement or you're just spying. So definitely PIO for receive totally could work. Uh, and transmit really is very simple. You generate the bits. And then. You just compare, uh, that's the sample point. Is that the bit that I said, um, is it the same? If it's the same, then your trans your transmitter, if you're not, then you just stop transmitting. So that, I think that would be very easy to do PIO. Uh, so let's look at the other, uh, functions in this. So how does the CPU actually get access to that data? Uh, so every so often this, the CPU and its main loop will call process. And what it does is it looks at the message data. Um, and then it just, it just parses them and it changes the bits that de-stuffs them. So can bit stuffing, and then it builds a can message. Now, where does this thing get filled up? There's actually an interrupt that's called. Uh, so we have an interrupt callback here. And then that is called here. And we actually read the, read the FIFO into this temporary date data section, which is basically then eventually processed by the main core. Which is this code. Uh, so just this is just an example of it can be done. Uh, uh, and, uh, we'll go back to ending this presentation, the demo. We have our Pico scope up here. And, uh, I'm going to see some quick settings. So, first of all, channel a, I have a 10 X pro. And I want the range to be. Uh. Plus or minus 5, so we can. See the voltage and I have the probe here and I'm. Uh, going to touch the. Uh, touch the I open of the can transceivers. All right, so it looks like we got a good. Way for him there. Uh, so I'm going to do is I'm going to run 1 of the kills. I don't have another camera on the badge just because I ran out of USB ports. Uh, so we're going to do is we're going to zoom in here. And this is just generating error frames. I'm going to set up the trigger. And this little diamond is the trigger point. You can move it wherever you want. So we're just. Put it right there. So we're basically just change train the horizontal and I'll go ahead and stop that. And we have our curse. Cursors here. Now. This is the logic waveform. So outside of the transceiver. And we're going to go ahead and. Look at measure this. And you can see we're at almost exactly 16 microseconds. So a 2 microsecond bit time is. Is that's going to be 8, 8 pulses. Okay, so that's what we're generating with the. With the. That's the. Your cans and jeopardy kill. And then kill the zeros. Let's go ahead and do that. You can do it from the USB prompt, or it's just more fun to do it with the badge. All right, so go ahead and run that and I'll turn the trigger back on and. Yes. So can message. So you can see it's generating. So this is the idle bus and then this is basically can ID 0. The Pico scope has some candy coding features on it. That's really cool. But I don't have time to show that right now. And then finally, the random. Kill. Which is not not as much. That is as exciting. Hold both buttons for the random kill. Now, this 1 generates random delays between the messages as well. Right. Just random size message, random can ID. It will actually generate errors once in a while as well. Okay, so that's the predefined kills. And let's go ahead and I'll talk about the software. 1st of all, there's a version number. I think we're releasing 5 or 6. 6 has the power management features to run off the battery. So that might be the 1 you get. It shows how many message bits you're generating. And it tells you the time. So this standard can message is like 250 microseconds. And that's this particular message right here. There's 16 stuff bits. So it's kind of a waste and can has to generate these stuff bits. So depending on the data in the message, it's either going to be shorter or longer. The bit period. So this is the actual baud rate. So 200 nanoseconds. And then transmit count is how many you're going to send when the kill is issued. And that's just for the arbitrary kill. That's this one down here. And then you can actually generate an inner frame period. That's nanosecond accurate as well. So if you wanted to do like 10 milliseconds between the message response, that lets you see can messages. Can't ramp the cam data. So that will change the data in the can message or inner frame. The ramp, the inner frame period. That's the width between the message. You can ramp. You can actually ramp the bit period. So if you wanted to send, you know, 500 kilobits and then mess with the bit timing to see how an ECU responds, you can do that. And then ramp can ID will change the can ID. So these are the different settings. So let's go ahead and change the can message. So this is a can message configuration. You can change the can ID. The length of the frame, the bytes. You can add invalid CRC, invalid R0, which is a common issue that with a lot of can controllers, it wasn't defined what to do about that. You can change the inner frame separation time and the end of frame separation time. So if you want to violate that rule where you send a message earlier, you can do that. Generate an error frame. This just generates that error frame that you saw in the Jeopardy kill. And also you can have an extended can frame with 29-bit IDs, and that's what's used in trucking and industrial applications a lot, boats. Remote frame, this is a frame that no one really uses. But, you know, why not see what happens? So I'm going to go ahead and generate different can frames. So I'll hit 3 and then enter in some different bytes. You can see we changed the message. And then I'll quit. And then what I'll go ahead and do is I'm going to kill the boss here with the – okay, so we can see the scope. And I'm going to hold the scope probe on the can chip. And then I'll go ahead and – okay. And then it went ahead and generated a lot of messages. So let me slow down the scope. And I'll do the kill again. And there you go. There's your message. Now, what's cool about the 555 messages is that it's shorter. I don't know if you know it because it has a lot less stuff bits. You don't need stuff bits if every other bit is different. So let's do something else. I'll send the kill. And we'll ramp the can data. So I'll select 6. And then there's different patterns you can generate. Walking ones is pretty cool where it just shifts a 1 through the can data every frame. Let's try to do that. Oops. Did you catch that? This is where the vehicle spy tool will come in handy to see actually what it's doing. Now, just to show some of the other features I'm not going to demo, the can response refrigeration. And what this does is you can set up a can ID, and that will trigger the arbitrary kill. So you can transmit arbitrary kill. So when it gets 3, 4, 2, it will trigger the kill. There is a bug with this version where it actually kills the badge. So it's kind of like suicide feature, I guess. So the kill kills itself. But you can just power cycle the badge, and it will do it. And then there's a can monitor. The can monitor is actually using the PIO code I just showed. And it only really decodes the can ID at this point. And there are some issues with the data. But the data is correct. It's just the decoding, transiting the bit stream into the can messages. There's a bug in it. Anyway. That's pretty much the badge. And if you have any questions, I'm going to be at the DEF CON Village on Friday all day. I might be there Saturday. I'm not sure, depending on what else is going on. Okay. Just wrapping up here. There's a company called Canis Labs that had came up with the idea of using the Raspberry Pi Pico with CAN. And they made a couple of products. One is just the Raspberry Pi Pico. Connected to a transceiver. That's called CanHack. And then another one actually connects to a microchip CAN controller. So it can do CAN bus. And they have an API. But they support Python very well. The CTO of Canis Labs, Dr. Ken Tyndall, he has ported their software. And he did a number of talks about cybersecurity online that are pretty interesting and a lot of attacks. But he designed those attacks into his Pi Pico. Code. What's awesome is that his CAN hack board is basically identical. It's the Pi Pico connected to a CAN pin. So he ported his software to the badge. And it's available for download. So you can generate different attacks from a CAN or a Python REPL. So I encourage you to learn more about that port. And as well as the different attacks that badge can do. So he's got some research that he's done with it. So please check that out. It's really cool. And a real great bonus for the badge. And finally, conclusions. The RP2040. Take a look at it. It's super interesting. The dual-core CPU and the BitBang engine. The high clock rate, which you can probably overclock. You can probably do a lot of destruction to digital protocols. Everything is really digital protocol now. So like wireless, all it is is digital protocols. And they're on a carrier. And what's interesting about engineering and where cybersecurity happens is people are testing things. But they don't test it to the point where they need to. It might be just impossible. It's just hard enough to get things working these days. So you ship the product. And then maybe 10 years go by. And then you come around with something like this. And then you interact with the protocol in a way no one ever thought. And you can discover something. And then you have some interesting zero days and things like that. So I think this is a real interesting tool for any protocol. This is just an example on the CAN bus. So you can use that CPU cycle accurate 8 nanoseconds or less. And it's not cumbersome like an FPGA. So you can do a lot of cool stuff. And can you do CAN bus with RP2040? Absolutely. Two. 500 kilobit CAN bus. You have 250 instructions between bits. Now, you would have to do it the exactly opposite way as I did it. So what I did, I used the CPU to do transmission. Because the arbitrary transmission was the main feature of the badge. And then I used the PIO to do reception. Actually, the opposite will work where you use CPU to do reception. Because then you can do things like decode the CAN bus data length. Generate the acknowledgement in the proper location. Things like that. Which you really need to keep track of numbers and do some math. But the PIO absolutely can do transmission. And they're totally independent processes. So you can transmit with the PIO while you're receiving with the CPU. And you can do arbitration by just checking the bits. If you put it on the bus, it's not there, you stop. So it's totally possible. I don't know about CAN FD. There's some bit time changes and stuff like that. I would have to say, you know, I only spent like a few days with this. And there's a lot of creative ways to use the CPU and to use PIO. So I think, you know, someone with a lot of time and talent probably could do a lot with this. Anyway, and hopefully the conclusion is the CHEV badge that Intrepid has designed is pretty awesome. And maybe I would like to think it was one of the best badges. At least in terms of functionally what it can do. And it's a great value. So I hope you pick one up. We made a ton of them in our factory in Troy. So please take a look. And it would be great to meet you in person and answer any of your questions in the village. I appreciate it. Or contact me on Twitter or LinkedIn. And if you're super talented, you know, Intrepid is always hiring. So that's that. I put the plug in. But enjoy DuffCon. Enjoy Vegas. And have a good trip home.