WEBVTT

00:00.000 --> 00:02.280
I'm talking about embedded system design.

00:02.280 --> 00:04.680
So this is really rather tutorial, right?

00:04.680 --> 00:07.140
This isn't like a new security invention.

00:07.140 --> 00:10.200
This is just basically how to build an embedded system.

00:10.200 --> 00:15.560
And this is useful information if you somehow wanted to attack a system , an embedded system, or defend a system.

00:15.600 --> 00:23.060
It's a good idea to know how to develop them and , you know, how the development is different than just traditional software.

00:23.440 --> 00:29.980
I'm Professor from University of California, Irvine, and I teach this stuff sort of regularly.

00:30.000 --> 00:32.720
And this is going to be very compressed, right?

00:32.720 --> 00:35.240
I mean, it's only, you know, 50 minutes or something.

00:35.480 --> 00:39.020
So it's not like I could summarize the whole thing, but I'm going to touch on everything.

00:39.020 --> 00:40.980
And then you can also download the slides if you want to.

00:40.980 --> 00:43.920
I have like a QR code at the end if you wanted to.

00:43.920 --> 00:47.580
And you could pursue some of these topics more, you know, later if you wanted to.

00:48.220 --> 00:50.080
Okay, so what's an embedded system?

00:50.360 --> 00:54.960
An embedded system is basically a computer that doesn't look like a computer, okay?

00:54.960 --> 00:59.500
So like, yeah, a phone, let's say, or the projector, right?

00:59.500 --> 01:01.020
The projector is a good example.

01:01.020 --> 01:05.500
All you see, you know, there's computation going on inside it, but the interface isn't the same, right?

01:05.500 --> 01:07.600
I don't have to write code directly on it.

01:07.600 --> 01:12.420
So to work with it, I press some buttons on it or click some buttons on here, you know?

01:12.440 --> 01:15.760
So as an example, typical embedded device, like a car, right?

01:15.760 --> 01:19.820
You got a car, old car on the left, and a new car.

01:19.820 --> 01:24.660
Now, the interface is basically the same, like the driving interface is the same, right?

01:24.660 --> 01:30.300
But the new car has like 60 plus processes in it doing all kinds of internal control.

01:30.360 --> 01:34.720
So there's a lot of computation going on, but you as a user, you don't have to deal with that directly.

01:34.720 --> 01:37.800
So you have to code for each one of these microcontrollers, right?

01:37.800 --> 01:42.960
They're all just doing stuff in the background, making things better, but the interface to you is nice and simple.

01:43.080 --> 01:46.060
And so that's basically what an embedded device is.

01:47.340 --> 01:49.860
Related buzzwords, I just like to define these.

01:49.980 --> 01:53.640
You maybe have heard of cyber-physical systems, or you definitely heard of Internet of Things.

01:53.640 --> 01:55.880
These are all basically the same thing.

01:55.940 --> 01:58.420
Cyber-physical is sort of an older term.

01:58.420 --> 02:02.820
It focuses on industrial systems most of the time, right?

02:03.260 --> 02:07.560
With big sensors and actuators, like, I don't know, a plant or something like that.

02:08.040 --> 02:11.040
And Internet of Things has that word internet in it.

02:11.040 --> 02:14.060
So it focuses on the networking abilities of it.

02:14.060 --> 02:19.660
Now, in truth, nowadays most embedded systems are networked anyway, so there's not really much distinction.

02:19.660 --> 02:21.380
IoT is really an embedded system.

02:21.380 --> 02:28.120
They're all... the only difference between, like, just embedded system, if it has networking, which most of them do, then you'd call it IoT.

02:28.820 --> 02:31.100
So they're all similar concepts.

02:31.400 --> 02:35.640
So main idea is sort of at a high level, the design...

02:35.640 --> 02:40.400
one difference in designing that versus designing regular software is being efficient, right?

02:40.400 --> 02:51.680
And I find that when I teach this stuff, you know, so we have undergrads, right, who have taken, like, Python and stuff like this, and efficiency is not what they learn, right?

02:51.680 --> 02:52.520
Like, when you turn...

02:52.520 --> 02:55.120
when they turn in an assignment, it's like, okay, it worked, right?

02:55.120 --> 02:56.820
I tested it out a little bit.

02:56.840 --> 02:59.040
Barely... they don't really know how to test, but I tested it out.

02:59.040 --> 02:59.860
It worked.

02:59.880 --> 03:02.120
But, you know, did it work fast?

03:02.120 --> 03:04.420
You know, was it... how much memory did it use?

03:04.420 --> 03:08.200
And in a language like Python, you have no idea about those type of things, right?

03:08.200 --> 03:10.080
Like, the memory, you have no idea, right?

03:10.680 --> 03:16.480
So... but with embedded systems, you have to be efficient, because people... these manufacturers are cheap, cheap, cheap.

03:16.480 --> 03:18.840
Like, they don't want to spend anything on these devices.

03:18.840 --> 03:28.780
Like, they don't want to, like... like, for instance, you would never take a device like that and say, you know what, I'm just going to put, I don't know, an i9... an Intel i9 microprocessor in there.

03:28.780 --> 03:29.520
That would be stupid.

03:29.520 --> 03:31.300
It costs way too much, right?

03:31.300 --> 03:32.600
They would never do that.

03:32.600 --> 03:35.120
They would get the cheapest possible microcontroller.

03:35.160 --> 03:36.380
They'd be like, what's the cheapest thing?

03:36.380 --> 03:38.800
You know, I could save 10 cents if I got this cheaper one.

03:38.800 --> 03:39.860
Let's go for that.

03:39.860 --> 03:40.380
You know what I mean?

03:40.380 --> 03:41.700
It's always efficiency.

03:41.700 --> 03:43.280
So, efficiency is really important.

03:43.340 --> 03:44.440
And there's lots of constraints.

03:44.440 --> 03:48.020
Design costs, performance, you know, power.

03:48.020 --> 03:49.040
Power is a big constraint.

03:49.040 --> 03:50.240
How much power is it using?

03:50.260 --> 03:59.500
And so, all these things you have to really think of when you're doing embedded systems design, where in software design, pure software design, you're not trained to do that so much.

03:59.500 --> 04:03.100
You know, you don't have to... you're not thinking, oh, what is the memory that this thing is requiring?

04:03.100 --> 04:05.860
Well, with embedded systems, you really are, like, all the time.

04:07.260 --> 04:07.800
Okay.

04:08.000 --> 04:09.180
Hardware and software.

04:09.180 --> 04:13.100
So, embedded systems have hardware and they have software components, right?

04:13.140 --> 04:16.220
So, that's actually the badge from last year.

04:16.580 --> 04:18.460
You know, it's a little video game thing.

04:18.480 --> 04:19.740
And it's hardware and software.

04:19.740 --> 04:21.560
So, there's like a microcontroller in there.

04:21.560 --> 04:22.560
There's a little screen.

04:22.560 --> 04:23.520
There's some speakers.

04:23.520 --> 04:24.040
There's a bunch...

04:24.040 --> 04:25.500
I don't know what other devices are in there.

04:25.500 --> 04:27.240
Lights, LEDs, that type of thing.

04:27.260 --> 04:31.540
And they have to be controlled by some software running on the microcontroller, right?

04:31.540 --> 04:35.340
So, you have to consider hardware as well as software and design them together.

04:35.660 --> 04:37.860
So, this is not what you do in normal software, right?

04:37.860 --> 04:44.580
Normal software, like, if you're writing some Python code, you're not thinking, okay, exactly what type of monitor are they going to be running this on, right?

04:44.600 --> 04:46.060
Generally not, right?

04:46.100 --> 04:48.700
You're not... like, what is the speaker they're going to use, right?

04:48.700 --> 04:53.220
But when they designed this, they knew exactly, like, okay, this is the speaker that we're going to use.

04:53.220 --> 04:54.520
This is the cheap monitor.

04:54.520 --> 04:56.620
And that changed the way they wrote the code.

04:56.620 --> 05:01.020
So, you have to think about the hardware side of it when you're doing the design, too.

05:03.240 --> 05:04.040
Okay.

05:04.180 --> 05:05.080
Outline of the talk.

05:05.080 --> 05:09.660
I might not be able to finish all these slides just because, you know, there's only so much time.

05:09.800 --> 05:11.360
But three things.

05:11.360 --> 05:14.380
So, first, I'll talk about working with the microcontroller.

05:14.620 --> 05:17.620
Then connecting the microcontroller to external devices.

05:17.620 --> 05:19.860
So, work with the microcontroller's coding, right?

05:19.960 --> 05:21.040
Software, pretty much.

05:21.040 --> 05:24.780
Then connecting it to hardware devices, other things you want to talk to.

05:24.780 --> 05:26.180
And then debugging tools.

05:26.180 --> 05:29.420
Because debugging is different in embedded systems, too.

05:29.960 --> 05:30.180
Okay.

05:30.180 --> 05:33.100
So, microcontrollers are just these little integrated circuits.

05:33.380 --> 05:34.440
And you've seen them.

05:34.460 --> 05:38.780
Generally microcontrollers, I mean, people have definitions of these things.

05:38.780 --> 05:42.080
But generally, they're just, like, cheaper microprocessors, okay?

05:42.600 --> 05:46.440
And you can control everything directly, right?

05:46.440 --> 05:50.460
Meaning all the hardware on the chip, the programmer can control.

05:50.460 --> 05:52.120
And I'll go through what I mean by that.

05:52.120 --> 05:59.580
Normally, you can't control a lot of things on regular microcontrollers, microprocessors, you can't control, okay?

05:59.580 --> 06:02.020
Like, say you want to print something on the screen, right?

06:02.020 --> 06:03.700
You call print, right?

06:04.080 --> 06:06.860
But print asks the operating system to do the control.

06:07.080 --> 06:08.920
Basically, it calls a system call.

06:08.920 --> 06:14.440
The system calls that, checks it out, calls the driver, and the driver puts it on the screen, you know?

06:14.440 --> 06:20.020
So, with microcontrollers, the programmer is doing that directly themselves, okay?

06:21.540 --> 06:24.100
Yeah, so, how do you program one of these things?

06:24.100 --> 06:25.580
So, you have to program them.

06:25.780 --> 06:32.980
So, you write your code on some development environment, like a laptop, and eventually you've got to put it onto the microcontroller.

06:32.980 --> 06:44.100
So, either you do it with some programmer like that one, like you have this programmer device connected by USB to your development, and then you put the chip in there and connect it to that.

06:44.100 --> 06:46.980
This is a really old-fashioned one, but same idea, right?

06:46.980 --> 06:48.960
And you can do it that way.

06:49.920 --> 06:53.840
And in fact, that's how it's really done in products, right?

06:53.920 --> 06:58.640
Because the other way is like this, which is much more common if you're going to do it yourself.

06:58.800 --> 07:00.220
It's built into the board.

07:00.220 --> 07:03.420
So, say you buy a board like this, or an Arduino, or whatever.

07:03.420 --> 07:05.800
This is a board we use in class, right?

07:05.840 --> 07:09.360
And this board, it has the programming hardware built into it, okay?

07:09.360 --> 07:19.200
So, all you need to do is connect your laptop, whatever, by USB to the board, and the board will do... actually, with this one, this is the main chip that runs the code.

07:19.200 --> 07:20.580
This one deals with the programming.

07:20.580 --> 07:24.040
It takes the USB, transfers it into this guy.

07:24.040 --> 07:27.860
It has a bootloader running on it, and it programs it, okay?

07:27.880 --> 07:34.340
So, this is like what do-it-yourselfers use, but this hardware here is excess.

07:34.720 --> 07:38.100
Like, you don't really need that once you deploy the device, right?

07:38.100 --> 07:42.280
So , in real devices, they do it this way, and then all they're deploying is this.

07:42.280 --> 07:45.280
This is useful, though, in practice for regular people.

07:46.580 --> 07:47.140
Okay.

07:47.140 --> 07:48.420
So, ARM.

07:48.420 --> 07:54.240
So, ARM is the brand of microcontroller that is the most common, okay?

07:54.240 --> 07:59.700
So, you know, Intel and AMD, Intel-class machines dominate microprocessors, right?

07:59.700 --> 08:05.440
Well, in microcontrollers, Intel has, like, no... they don't make anything that anybody uses, really, right?

08:05.640 --> 08:07.700
Instead, ARM makes them.

08:07.700 --> 08:12.860
Now, ARM, what they do is they lease the design.

08:12.860 --> 08:14.460
So, they don't make silicon, right?

08:14.460 --> 08:15.520
They don't make the chip.

08:15.520 --> 08:18.600
They lease the designs to other companies who make the chips.

08:18.600 --> 08:21.160
So, maybe they lease it to TI, TI makes chips.

08:21.200 --> 08:24.460
Maybe they lease it to STMicroelectronics, they would make the chips, right?

08:24.460 --> 08:28.760
But ARM is the type of microprocessor, microcontroller that's in there, okay?

08:28.760 --> 08:34.040
It's a dominant one, and there are different classes of them that I don't need to go over, that I don't feel like going over right now.

08:34.040 --> 08:36.120
But there are, for instance, Macs, right?

08:36.120 --> 08:39.740
Those M1, M4s, and all those, those are all ARM, right?

08:40.340 --> 08:43.220
Basically, everything in everybody's phone is going to be an ARM.

08:43.220 --> 08:46.780
Not everybody's, there's some that aren't, but most of them are going to be ARMs.

08:46.780 --> 08:50.180
Like, most of these devices use ARM microcontrollers, you know?

08:51.300 --> 09:00.540
And that's only relevant because the machine language is different, but you don't really have to worry about that unless you're coding in assembly, which you probably are not.

09:01.280 --> 09:01.820
Okay.

09:01.820 --> 09:04.160
Now, microcontrollers have peripherals.

09:04.160 --> 09:08.900
What I mean by this is, okay, this, I know you can't read the details of this.

09:08.900 --> 09:12.280
I took this out of a data sheet for a microcontroller I used to use for class.

09:12.420 --> 09:15.760
Now, up here, I'll just tell you, up here is the CPU, okay?

09:15.760 --> 09:18.720
This is the main processor, the ARM microcontroller, right?

09:18.720 --> 09:21.120
And that thing is what runs your code.

09:21.120 --> 09:23.460
So, it executes instructions, right?

09:23.800 --> 09:30.120
The rest of it is all, like, other stuff that connects to it and does useful things, okay?

09:30.120 --> 09:31.620
So, for instance, there's memories.

09:31.620 --> 09:32.660
I can't even read them.

09:32.760 --> 09:33.980
Memories and stuff like this.

09:34.020 --> 09:40.920
But what you see, this diagram, just so you know, this was one long column in the data sheet, but I couldn't fit it onto the slide that way, okay?

09:41.260 --> 09:46.880
This, but the main thing is this CPU is connected by these two long buses, these buses, right?

09:46.940 --> 09:50.480
Connected by these buses to all these peripherals, what are called peripherals.

09:50.480 --> 09:54.360
Now, these peripherals are what do interesting stuff on microcontrollers.

09:54.360 --> 09:55.680
They do all kinds of things.

09:55.680 --> 09:57.880
Like, for instance, general purpose I.O., right?

09:57.880 --> 09:58.560
The pins.

09:58.560 --> 10:00.900
If you want to control the pins, that's a peripheral.

10:00.900 --> 10:04.020
Or if you want to communicate by UART, that's another peripheral.

10:04.020 --> 10:07.840
Or if you want to do analog to digital conversion, that's another peripheral, right?

10:07.840 --> 10:11.080
There are all these blocks are peripherals, okay?

10:11.080 --> 10:16.880
So, when you're coding, you know, some instructions, some things you do are the same, right?

10:16.880 --> 10:17.800
C is C.

10:17.800 --> 10:19.420
I'm assuming C right now, right?

10:19.420 --> 10:20.380
C is C.

10:20.380 --> 10:26.220
But the big difference with microcontroller programming is that you have to control these peripherals specifically.

10:26.220 --> 10:30.120
You have to tell them exactly what to do step by step, okay?

10:30.180 --> 10:37.600
And so, there's a bunch of peripherals that you've got to learn about for any particular microcontroller and learn what it can do and how to control it, okay?

10:38.720 --> 10:39.820
Peripheral control.

10:39.820 --> 10:41.160
So, common peripherals, okay?

10:41.160 --> 10:50.640
Memory, GPIO, general purpose I.O., timers, you've got communication, all these different protocols that you can use, analog to digital converter, there's a whole bunch of peripherals, okay?

10:50.920 --> 10:53.380
So, embedded software directly controls peripherals, all right?

10:53.380 --> 10:54.280
I said that.

10:55.160 --> 10:55.780
Yeah.

10:55.820 --> 10:56.680
Yeah, okay.

10:56.780 --> 11:05.120
Now, another thing to think about when you're doing embedded system design is this distinction between whether you're using an operating system or not, okay?

11:06.080 --> 11:11.240
So, you can make some devices operate with a real operating system, like a full-on operating system.

11:11.240 --> 11:19.620
Like, I was seeing a talk, I don't know, yesterday or something like that, and somebody was talking about, you know, he was talking about attacking embedded systems.

11:19.620 --> 11:22.560
Like, I've seen many talks like this where they talk about attacking embedded systems.

11:22.560 --> 11:26.140
Maybe they use a router as an example, how to dump the firmware, whatever.

11:26.480 --> 11:32.520
And these devices, a lot of these devices have just Linux running on them, like a small Linux, right?

11:32.520 --> 11:35.440
And that's just running a full-on operating system, okay?

11:35.940 --> 11:37.300
So, you can do that.

11:37.300 --> 11:43.260
Now, the downside of that is that, you know, it's easy because it's running Linux, but there's overhead, right?

11:43.260 --> 11:45.860
You're running an operating system on top of the microcontroller.

11:45.980 --> 11:55.180
So, in the operating system, it's a burden on the microcontroller, meaning, you know, it requires a lot more memory and a lot more flash and all kinds of junk, right?

11:55.180 --> 11:59.040
So, if you want to be really cheap, you might not want to use a full-on operating system.

11:59.360 --> 12:02.380
So, then sometimes people use bare metal.

12:02.380 --> 12:03.260
It's the other way.

12:03.320 --> 12:09.840
Bare metal is where you don't have any operating system of any kind and you just write your code and you do everything yourself, okay?

12:09.880 --> 12:11.960
You control everything in detail.

12:11.960 --> 12:18.080
See, if you have a full-on operating system, you have drivers, you have all kinds of stuff that make your life programming easier, right?

12:18.080 --> 12:20.300
But if you're doing bare metal, then you don't, right?

12:20.300 --> 12:24.840
You have to really understand all the details of how to control the peripherals.

12:25.020 --> 12:27.940
And then there's this in-between, which is real-time operating systems.

12:27.940 --> 12:31.280
Real-time operating systems are basically stripped back operating systems.

12:31.800 --> 12:38.100
They don't have like a nice GUI or any of that stuff, but maybe they support multithreads, you know, threading.

12:38.100 --> 12:41.720
They support that and they also support scheduling algorithms.

12:41.820 --> 12:45.780
So, basically they're made to satisfy real-time deadlines, right?

12:45.780 --> 12:52.620
So, if you have some tasks that need to be performed, like, you know, I got to sample this thing 8,000 times a second, this audio signal, right?

12:52.620 --> 12:55.280
And you got to meet that deadline no matter what.

12:55.280 --> 13:01.940
A real-time operating system is the way to go because it'll make sure that it interrupts everything around that deadline, you know what I mean?

13:01.940 --> 13:04.940
So, they have scheduling algorithms that they use to support that.

13:05.600 --> 13:06.500
Okay.

13:06.500 --> 13:12.480
So, embedded systems are said to have a, if you're going to program one, you have what's called a toolchain, usually.

13:12.480 --> 13:16.720
Toolchain is basically an IDE and they're all sort of the same, right?

13:16.720 --> 13:19.480
I mean, that's just generic IDE looking thing, right?

13:19.480 --> 13:27.060
This particular one is ST microelectronics IDE, STM cube IDE, but they're all basically the same thing, okay?

13:27.060 --> 13:27.920
And you've seen them.

13:27.920 --> 13:32.340
Here's the code and here's all the files in the project and all that stuff, right?

13:32.460 --> 13:40.420
So, the one difference in an embedded system IDE is that you have a programmer that's attached to it.

13:40.420 --> 13:46.680
So, one thing you have to do is, like, once you compile the code, you actually cross-compile the code.

13:46.680 --> 13:52.520
So, compiling a code, normally, if I compile code on this machine, it'll be compiled to execute on this machine, right?

13:52.520 --> 13:53.720
On this microprocessor.

13:53.720 --> 13:58.080
But with an embedded system, you compile it on this machine to run on a different processor, right?

13:58.080 --> 13:59.720
That's called a cross-compiler.

13:59.720 --> 14:08.740
So, this thing uses a cross -compiler and the last step is to take the cross-compiler executable and program it into the flash of the actual device.

14:08.840 --> 14:10.520
And that's what a programmer does.

14:10.700 --> 14:12.700
So, it's some programmer software.

14:12.700 --> 14:19.560
So , usually this thing has, like, this extra button in there, it's like program, and it just programs it onto the embedded device after you've compiled.

14:20.160 --> 14:20.480
Okay.

14:20.480 --> 14:23.100
So, IDEs, though, they don't look that much different.

14:23.660 --> 14:30.060
Now, one, another sort of painful difference in embedded programming is datasheets.

14:30.060 --> 14:33.700
Here's a datasheet, or the reference manual, whatever they call it, okay?

14:33.720 --> 14:36.420
These datasheets are massive, okay?

14:36.420 --> 14:40.360
They describe everything that a component is supposed to do.

14:40.360 --> 14:44.620
Now, you can get datasheets on all kinds of integrated circuits, all kinds of components, right?

14:44.620 --> 14:46.620
But right now, I'm talking about the microcontroller.

14:46.620 --> 14:51.020
So, like, this is the microcontroller I'm going to use in class in the fall, actually.

14:51.060 --> 14:53.220
I'm just starting to learn it and use it in the fall.

14:53.220 --> 14:56.160
I was like, oh, let me use it in this talk so I can start getting comfortable with it.

14:56.200 --> 14:59.840
Its datasheet is 1,757 pages long, okay?

14:59.840 --> 15:02.780
That is a painful datasheet, right?

15:02.780 --> 15:09.180
And it describes every peripheral on there, how you use every peripheral, every little detail, okay?

15:09.240 --> 15:13.600
And you have to be familiar with a chunk of it, right?

15:13.600 --> 15:16.820
And learn how to search really well, okay?

15:16.820 --> 15:22.080
Like, you can't memorize all that, but you got to search, you got to remember to look at that datasheet, okay?

15:22.080 --> 15:26.600
And then sometimes, in the very worst case, the datasheet is incomplete, right?

15:26.600 --> 15:34.600
It doesn't say what happens in a certain case, which is real hard to find, because you're like, well, I couldn't find it, but there are 1,700 pages, right?

15:34.600 --> 15:36.300
Maybe I missed it, right?

15:36.300 --> 15:37.800
Or it's wrong.

15:37.800 --> 15:38.720
That happens too.

15:38.720 --> 15:42.920
The datasheet will say something incorrect, and that's very painful, okay?

15:43.060 --> 15:50.140
And, you know, I use a lot of Google, because usually, whenever you find that, somebody else hopefully has run into the same issue.

15:50.520 --> 15:54.220
So, anyway, you have to get used to reading these datasheets, okay?

15:54.220 --> 15:55.400
Just in general.

15:56.220 --> 15:57.520
So, importance of datasheets.

15:57.520 --> 16:00.620
So, it is very easy to miss important information.

16:00.620 --> 16:02.680
So, as a broad example, right?

16:02.680 --> 16:08.880
Say I'm talking about my 1,700 page datasheet, and I want to do... it leads to bugs, which can be exploited.

16:08.880 --> 16:10.880
This is where a lot of bugs can happen, right?

16:10.980 --> 16:14.120
So, say I want to do UART communication, right?

16:14.120 --> 16:17.460
I want to communicate with another device by UART, which is protocol, right?

16:17.780 --> 16:20.800
So, there's a section in the datasheet on UART, right?

16:20.800 --> 16:21.500
Just on that.

16:21.500 --> 16:23.620
So, you're like, okay, let me read that whole section, right?

16:23.660 --> 16:26.540
Okay, based on that section, my code works.

16:26.540 --> 16:29.100
And you test it out, and you're like, okay, it works, right?

16:29.140 --> 16:35.760
But then you didn't notice that in this other section, 500 pages later, they were talking about reset, okay?

16:35.760 --> 16:39.160
And in reset condition, maybe UART works differently, right?

16:39.160 --> 16:44.680
There's like a special case that you missed, because it was far off in the datasheet, right?

16:44.680 --> 16:46.100
Very painful, okay ?

16:46.100 --> 16:47.300
This happens, right?

16:47.300 --> 16:57.100
And as a side note, this is like the first thing, like, if you really wanted to make malware for an embedded system, this is what you would do, like exploit the drivers that are written on there, right?

16:57.100 --> 17:00.220
Because it's very easy to put a bug into these things.

17:00.220 --> 17:08.860
There's this embedded system capture the flag, embedded capture the flag that MITRE runs, and we have a team that competes in that every year, and the CMU team usually wins.

17:08.860 --> 17:10.160
They didn't win this year.

17:10.240 --> 17:23.800
But this is how they broke our system and a lot of other systems, was by basically, they didn't say this, but I'm pretty sure they fuzzed the drivers for I2C, which is a communication protocol.

17:23.800 --> 17:28.520
They fuzzed the I2C drivers until they found such a vulnerability, and then they exploited it.

17:28.960 --> 17:32.360
So, anyway, data cheats are painful and important to understand.

17:33.440 --> 17:34.820
Peripheral control registers.

17:34.820 --> 17:35.200
Okay.

17:35.200 --> 17:40.040
So, when you're writing code, you have to control these peripherals and tell them what to do.

17:40.120 --> 17:43.160
So, peripherals are controlled through registers.

17:43.160 --> 17:47.900
Now, a register in a chip is just a storage element, you know?

17:47.900 --> 17:49.300
Like a 32-bit register.

17:49.300 --> 17:51.240
Typically, these are 32-bit devices.

17:51.240 --> 17:52.620
They've got a 32-bit register, right?

17:52.620 --> 17:54.800
It just stores 32 zeros and ones.

17:54.800 --> 18:02.800
And all of these peripherals, every peripheral has associated with it some registers that are used to control it and to observe it, right?

18:02.940 --> 18:15.760
And the whole goal of your code, and if you want to talk to a particular register and make it do something, say you want to do analog to digital conversion, you have to read all about its registers, write the right bits into the right registers, and then it'll do it.

18:15.980 --> 18:16.220
Okay?

18:16.220 --> 18:17.940
That's how you do control.

18:18.540 --> 18:24.160
These registers are like the interface to all the peripherals on your microcontroller, okay?

18:24.160 --> 18:31.620
So, a lot of this, like, my students, they sort of... a lot of them get sick of this, because you're, like, looking at all these tedious details.

18:31.620 --> 18:37.480
Oh, I see it's bit five of this register makes this start, and bit six puts it in this mode and all this.

18:37.480 --> 18:40.860
That's why the data sheet's, like, 1,700 pages long, okay?

18:40.900 --> 18:43.780
So, that's how you control them, through these registers.

18:44.060 --> 18:49.860
So, and if you... anything you want to do, drive a value onto a pin, or do A to D conversion, UART, or whatever.

18:50.480 --> 18:54.160
Now, so, as an example, right?

18:54.160 --> 19:01.080
I pulled out, on the right, I know you can't see it, but on the right, it's basically a part of a data sheet.

19:01.080 --> 19:06.320
It's the schematics inside a data sheet for a particular... for this STMicroelectronics chip, okay?

19:06.320 --> 19:09.860
And this schematic is showing me one pin in the GPIO.

19:09.860 --> 19:12.640
So, GPIO stands for General Purpose Input Output.

19:12.640 --> 19:14.200
That means the pins of the chip.

19:14.200 --> 19:17.240
If you drive to ones and zeros to turn lights on, or whatever, right?

19:17.240 --> 19:18.540
And you can read them, too.

19:18.540 --> 19:21.780
Maybe some of the device is driving a 0-1 output, you want to read it.

19:21.840 --> 19:24.640
So, this is, like, the hardware behind a pin.

19:24.640 --> 19:27.660
So, over here is the pin, in tiny letters, says IO pin, right?

19:27.660 --> 19:28.500
This is the pin.

19:28.520 --> 19:31.100
And then, this is all the logic associated with the pin.

19:31.100 --> 19:36.380
And then, over here, to the left, over here, is, like, the microcontroller, okay?

19:36.640 --> 19:41.720
So, and the only thing I want to highlight here is that they show some of these registers.

19:41.720 --> 19:43.540
I put them... I highlighted them in red.

19:43.540 --> 19:46.000
And they name them in the data sheet.

19:46.100 --> 19:49.800
GPIO... oh, and I'm talking about GPIO port A.

19:49.800 --> 19:51.040
There are many ports on these things.

19:51.040 --> 19:52.140
So, I'm talking about port A.

19:52.400 --> 19:56.620
IDR, GPIO-A-BSS-RR, and GPIO-A-ODR.

19:56.720 --> 19:59.160
So, like, IDR is the input data register.

19:59.220 --> 20:08.080
So, what that means is, if you want to read, if that, say, some other device is driving a 1 onto that pin, and you want to read what it's driving, you read the IDR register.

20:08.080 --> 20:15.080
You read the appropriate bit of the IDR, bit 0 of the IDR register will tell you what's being driven onto port A, pin 1.

20:15.160 --> 20:16.560
Pin 0, rather, you know?

20:16.560 --> 20:20.480
So, you read this register inside your code, right?

20:20.480 --> 20:22.480
And then this register is to set output bits.

20:22.480 --> 20:24.400
Say I want to set a particular output bit high.

20:24.400 --> 20:26.680
I set the appropriate bit in this register.

20:26.680 --> 20:29.260
And say I want to write all the output bits to something.

20:29.260 --> 20:33.720
I go to this ODR, output data register, and write all the pin outputs.

20:34.280 --> 20:39.160
So, the idea here is just that every peripheral has a set of registers.

20:39.160 --> 20:42.520
And you have to learn what all of them do, what they're all for.

20:44.400 --> 20:45.200
Okay.

20:45.940 --> 20:48.300
Now, registers are memory mapped.

20:48.540 --> 20:53.740
So, what that means is that in order to write to a register, you write to a memory address.

20:53.780 --> 21:01.780
So, every one of these registers is mapped to an address in memory, which you will find, again, in the datasheet or reference manual, whatever it is, right?

21:01.780 --> 21:08.060
In the datasheet, it will be like, GPIOA, output data register, is that address whatever, okay?

21:08.060 --> 21:13.220
And then all you do to write to that register is you write to that address, okay?

21:13.220 --> 21:15.620
And these addresses are in the datasheet.

21:15.760 --> 21:24.540
So, you have to... and now, usually, like, in my class, I have students read the datasheet in hard, you know, hash to find it all at the top of their code, right?

21:24.560 --> 21:31.260
They usually, like, with these devices, they usually have, like, a header file that has them all defined for you.

21:31.280 --> 21:34.560
You know, most IDEs have that.

21:34.580 --> 21:42.180
So, you can just include, hash include, whatever that header file is, and then you can just write to... then these definitions are already made.

21:42.180 --> 21:44.840
Otherwise, you have to hash to find them explicitly in your code.

21:46.180 --> 21:48.500
By the way, if there's any questions, go ahead and raise your hand.

21:48.600 --> 21:52.580
I'm perfectly fine answering questions right in the middle, if that's fine with me.

21:52.580 --> 21:53.680
And I know I'm going fast.

21:53.680 --> 21:56.960
I always do that anyway, but anyway.

21:56.960 --> 21:58.160
All right.

21:58.560 --> 22:01.020
So, blinking an LED.

22:01.120 --> 22:05.380
So, this is the one I'm going to use in class, STMicroelectronicsBoard.

22:05.480 --> 22:09.000
And just... this is the main microcontroller.

22:09.000 --> 22:13.500
This is used for loading from... this is a USB connect interface.

22:13.840 --> 22:15.900
And say I want to blink an LED.

22:16.440 --> 22:18.140
Now, on this board...

22:18.140 --> 22:22.420
oh, I should mention here, there's a difference between the microcontroller and the board, okay?

22:22.420 --> 22:25.980
The microcontroller is this chip right here, okay?

22:25.980 --> 22:28.140
It has this data sheet and all that.

22:28.140 --> 22:31.700
The board is this whole thing, which has the microcontroller on it.

22:31.700 --> 22:35.140
And the reason I bring that up is because you need both to do this.

22:35.140 --> 22:43.080
So, for instance, in order to blink an LED, basically there's going to be an LED that's wired to a particular pin of my microcontroller.

22:43.080 --> 22:47.440
And I'm going to have to know what pin is that LED connected to, right?

22:47.440 --> 22:51.540
So, now this board, like all boards, they have an LED built in, right?

22:51.540 --> 22:52.480
This has multiple.

22:52.480 --> 22:55.200
But that little red circle there, I circled the LED.

22:55.200 --> 22:58.120
You can't see it, but it's an LED built into the board.

22:58.140 --> 23:03.960
And I have to look at the board's data sheet, which is usually much easier, to say what pin is that connected to.

23:03.960 --> 23:09.060
So, looking at the board's data sheet, this LED is connected to port D, pin 14, okay?

23:09.060 --> 23:10.180
So, I know that.

23:10.360 --> 23:17.820
So, now my goal in writing this blinking LED code is to turn on... okay, well, let me skip that first line.

23:18.660 --> 23:24.440
I'm going to basically see the while loop, drive a 1 onto the pin, delay, drive a 0 onto the pin, delay.

23:24.440 --> 23:26.000
That's the basics, right?

23:26.000 --> 23:28.480
And that's a blinking LED.

23:28.740 --> 23:31.140
Now, before that, there's a couple of things I have to do.

23:31.220 --> 23:36.120
One thing is this first one, turn on the clock to the GPIO port, or D.

23:36.300 --> 23:43.780
So, what they do a lot with these peripherals to save power is they turn off the clock to these peripherals.

23:43.780 --> 23:44.800
So, there's a clock.

23:44.800 --> 23:48.560
A clock is a square wave that determines the rate at which events happen on the chip.

23:48.560 --> 23:51.060
If the clock is going, it is consuming power.

23:51.260 --> 23:55.040
So, they just shut off the clock to peripherals that are not in use by default.

23:55.260 --> 23:59.820
And so, you and your code have to turn on the clock to the peripheral before you can use it.

23:59.920 --> 24:01.040
So, you turn on its clock.

24:01.040 --> 24:02.060
You're going to have to do that.

24:02.100 --> 24:03.220
Again, there's some register.

24:03.220 --> 24:04.840
You set a bit, it turns on its clock.

24:05.240 --> 24:07.680
Then, you've got to set the port to an output pin.

24:07.680 --> 24:10.640
So, these pins, they can be inputs or outputs, okay?

24:10.640 --> 24:15.700
So, you're going to set this pin to an output so that you can drive outputs to it, okay?

24:15.700 --> 24:18.240
So, you have to do those two things first in your code.

24:18.260 --> 24:21.120
Then, you get into a while loop where you just do that, okay?

24:21.620 --> 24:27.940
So, just to show a little more detail, and we're going to do this by adjusting values of registers.

24:28.560 --> 24:29.440
So, okay.

24:29.440 --> 24:35.700
Now, I'm just showing you a few of the registers just to intimidate you, to make you see this data sheet is paying, okay?

24:35.700 --> 24:37.260
I don't expect you to understand all this.

24:37.260 --> 24:40.420
But, when you look in the data sheet, it has a description of every register.

24:40.420 --> 24:44.300
And I just went and zoomed into a piece of that, okay?

24:44.300 --> 24:45.600
So, there's this register.

24:45.860 --> 24:47.140
RCC, whatever.

24:47.220 --> 24:49.480
AHB is a bus name and all this, okay?

24:49.480 --> 24:52.700
This controls the clocks for the GPIO pins, okay?

24:52.700 --> 24:55.620
So, I've got to turn the clock on for a particular port.

24:55.620 --> 24:58.920
So, notice this register has got 0 through 15, right?

24:58.920 --> 25:00.060
Up there in the top.

25:00.320 --> 25:01.940
That's half the bits in the register.

25:01.940 --> 25:03.620
All the registers are 32 bits, right?

25:03.620 --> 25:05.520
So, I'm just looking at the first half.

25:05.560 --> 25:09.540
And if you zoom way in on this, it says GPIOD EN for enable.

25:09.860 --> 25:16.920
And then, in the data sheet, it will say, oh, pin 3 on this register enables the clock to GPIO pin 4D, okay?

25:16.920 --> 25:18.960
So, you've got to set that bit to a 1.

25:19.280 --> 25:22.040
Then, GPIOD mode register.

25:22.220 --> 25:29.700
This is... it'll set the mode... if I want to set it to an output or an input or what kind of output I want it to be, you set it here.

25:29.700 --> 25:32.980
So, the particular pin I'm interested in is pin 14, right?

25:32.980 --> 25:34.220
That's these two pins.

25:34.280 --> 25:35.540
Like, these two are 15.

25:35.540 --> 25:36.200
These two pins.

25:36.200 --> 25:38.960
And I have to set them to, like, 0, 1, if I recall.

25:38.980 --> 25:40.640
And that puts it into output mode.

25:41.080 --> 25:43.100
And then, over here, this is the output data register.

25:43.100 --> 25:52.800
If I want to set the value on the output to be... I want to set the bit high and set it low, I've got to go to bit 14, because it's pin 14 on the port, and set that to a 1 or a 0.

25:52.800 --> 25:55.020
It'll set the pin to a 1 or a 0, right?

25:55.040 --> 26:03.860
So, all these... so, I've got to look at not just the register, but the individual bit of this register, put a particular value in there, and it does what I want, okay?

26:04.680 --> 26:05.360
Okay.

26:05.360 --> 26:06.000
Question?

26:06.620 --> 26:07.300
Okay.

26:07.520 --> 26:09.540
So, here's my code for that.

26:09.540 --> 26:13.180
And I don't really expect you to follow all of it, but let me just say the structure of it.

26:14.420 --> 26:20.180
So, one thing is... so, there's a number of registers that I have to control, right?

26:20.280 --> 26:24.720
So, first thing I'm going to do, or somewhere in the code, I have to assign the addresses, right?

26:24.720 --> 26:26.820
Maybe I did that with hash defines at the top.

26:26.820 --> 26:27.960
In this code, I did it here.

26:27.960 --> 26:32.840
So, I said, this register equals, and I set it to this address, okay?

26:32.900 --> 26:36.980
And then... so, I set it to... I set these registers to their addresses, and then I set them to values.

26:36.980 --> 26:42.260
Star that equals a 1 shifted 3 over, because I want to set bit 3 high, and so on.

26:42.280 --> 26:43.800
So, I got... I do that.

26:43.800 --> 26:46.660
So, these are the initializations I was talking about, making an output register.

26:46.660 --> 26:48.500
I initialize the output to 0.

26:48.700 --> 26:50.200
Then, here's my while loop.

26:50.200 --> 26:53.040
I just say, set it to 0, set the pin to 0.

26:53.040 --> 26:55.440
Then, I have a loop, a dummy loop.

26:55.440 --> 27:00.460
Then, I set the pin to a 1 at bit shifted to bin 14, and then I have a dummy loop, and that's it.

27:01.260 --> 27:02.600
So, that's the code.

27:02.600 --> 27:03.180
Question?

27:08.970 --> 27:09.850
No?

27:10.450 --> 27:13.910
So, I'll talk to you about... I'm curious.

27:14.050 --> 27:17.230
So, you know, I've, of course, been thinking about that too, right?

27:17.230 --> 27:21.450
Because, you know... and I'm mixed...

27:21.450 --> 27:23.330
I've mixed feelings on that, right?

27:23.810 --> 27:32.910
Because... so, first of all, I can see how it'd be very useful, but I can also see how it causes people to not bother learning the details, which is exactly how security faults happen, right?

27:32.910 --> 27:39.670
You're trusting the AI model, and if it messed up, then you mess up, and you don't even know why, right?

27:39.670 --> 27:40.590
So, I don't know.

27:40.590 --> 27:42.510
But still, I'm curious, okay?

27:42.510 --> 27:47.890
I'm curious, and I might use it, but you always have... it's just not safe, security-wise.

27:50.370 --> 27:51.250
Yeah.

27:53.090 --> 27:54.650
Yeah, I think so.

27:54.650 --> 27:55.630
I mean, I agree.

27:55.750 --> 27:58.430
I wonder if that'll change one day, but who knows?

27:59.390 --> 28:01.310
But, yeah, I believe it.

28:01.450 --> 28:02.390
So, okay.

28:02.390 --> 28:03.910
So, this is a Blinky.

28:03.910 --> 28:09.190
This is sort of the most low-level type of Blinky you could write, where you directly control these registers and their values.

28:09.350 --> 28:12.050
Now, that can be tedious, okay?

28:12.050 --> 28:19.110
So, sometimes, a lot of times, when people make these devices, if they're doing bare metal without an operating system, they will use libraries.

28:19.170 --> 28:23.590
Libraries, where they make things... you know, you don't have to worry about each individual register.

28:23.590 --> 28:25.970
You just call a library function, and it does stuff for you.

28:25.970 --> 28:30.670
So, most of the time, when people make these things, they often use these libraries, right?

28:30.670 --> 28:33.210
So, there are vendor-specific libraries.

28:33.250 --> 28:37.750
Like, I'm using this for STM microelectronics.

28:37.750 --> 28:39.370
They have their HAL library, right?

28:39.970 --> 28:41.090
STM32HAL.

28:41.250 --> 28:43.970
And different vendors have their own libraries.

28:43.970 --> 28:48.590
Plus, there are vendor-independent libraries, like CMSYS is a popular one.

28:48.970 --> 28:52.250
It's across ARM, platforms, right?

28:52.310 --> 28:55.390
And then, Arduino, Arduino itself.

28:55.510 --> 28:57.390
So, Arduino, there's a particular...

28:57.390 --> 29:05.490
there's Arduino devices you can buy, but they're also... you can use the Arduino... Arduino development board has been ported to other devices, right?

29:05.490 --> 29:06.730
So, you can use Arduino.

29:06.730 --> 29:10.170
Like, I know... they use it on... there's another class in my school.

29:10.170 --> 29:12.590
They use it on an ESP32, right?

29:12.590 --> 29:15.050
So, they... but they use Arduino libraries on that, right?

29:15.050 --> 29:16.810
Which is another set of libraries.

29:16.810 --> 29:18.850
So, it's sort of a high-level library.

29:18.850 --> 29:19.990
It makes things easier.

29:20.130 --> 29:22.070
But the... yeah, I think it's on the next slide.

29:22.070 --> 29:26.250
Yeah, the downside of these libraries is that they hide details, okay?

29:26.410 --> 29:29.010
So, with any library, okay?

29:29.010 --> 29:35.930
And so, anyway, here's an example of Blinky using the HAL STM32... oh, and they're also slow.

29:35.930 --> 29:36.770
They're also slow.

29:36.770 --> 29:44.170
Because they often do... like, I was looking at the HAL library functions, and they do a lot of stuff that I don't need, okay?

29:44.170 --> 29:48.170
If I just want to make a Blinky, it is much quicker to do it my way.

29:48.170 --> 29:50.030
Now, who cares with Blinky, right?

29:50.050 --> 29:51.830
But my way is a lot more efficient.

29:51.830 --> 29:54.890
I mean, when I say my way, going straight to the registry is a lot more efficient.

29:54.930 --> 29:55.870
Like this, okay?

29:55.870 --> 30:00.830
This basically, you know, they have a toggle pin, and they have a HAL delay, thousand, whatever.

30:01.610 --> 30:04.250
But, like, things like... where is it?

30:04.250 --> 30:04.890
HAL init.

30:04.890 --> 30:07.110
HAL init initializes all the peripherals.

30:07.110 --> 30:08.730
I don't need that, okay?

30:08.930 --> 30:12.730
So, they don't have, like, a HAL init just for the GPIO, right?

30:12.730 --> 30:13.630
Not that I know of.

30:13.630 --> 30:16.450
But you initialize all these peripherals, you don't need it.

30:16.790 --> 30:20.650
And I don't even... oh, this guy's for GPIO init, but you got to do the HAL init, too.

30:20.650 --> 30:22.470
I don't quite understand the relationship there.

30:22.490 --> 30:28.410
But looking at the code, it's just a lot... when I look at the code of them, it's a lot more stuff than I did, okay?

30:28.510 --> 30:29.930
So, anyway, that's HAL.

30:29.930 --> 30:32.930
And then with Arduino, here's how you might write it.

30:32.930 --> 30:36.330
You set up in a loop, and digital write is their function.

30:37.490 --> 30:43.530
Now, you know, tradeoffs with the libraries, when you use libraries, the advantage is it makes everything easier.

30:43.590 --> 30:48.510
The disadvantage is that the libraries themselves might have exploitable bugs, okay?

30:48.510 --> 30:51.070
That's one security difficulty.

30:51.070 --> 30:53.550
But also, they're slow.

30:53.550 --> 30:55.070
They're generally slower, right?

30:55.070 --> 30:59.250
So, it depends on how efficient you really need to be, but they're generally slower.

30:59.250 --> 31:03.970
Most of the time, people use these libraries, though, in all honesty, because they do make things more convenient.

31:05.190 --> 31:05.870
Okay.

31:06.070 --> 31:06.630
Okay.

31:06.630 --> 31:08.110
Next bunch of the talk.

31:08.550 --> 31:12.010
Connecting the microcontroller to external devices, okay?

31:12.470 --> 31:15.750
So, you want to... so, it's not all about just programming the microcontroller.

31:15.750 --> 31:17.350
You got to connect to other things.

31:17.990 --> 31:20.450
So, analog interfacing, start with that.

31:20.450 --> 31:26.190
So, there are a lot of these little analog devices that you might want to control, or you might want to observe.

31:26.190 --> 31:28.630
So, like an LED, you might want to turn it off and on.

31:28.630 --> 31:30.770
A motor, you might want to drive it, right?

31:30.770 --> 31:33.330
And that's if it's an actuator, right?

31:33.510 --> 31:35.110
You're driving it to do something.

31:35.110 --> 31:42.530
Then there's also sensors that you want to read, like a push button, or a potentiometer, it's a little knob, you might want to read these.

31:42.550 --> 31:43.970
This is just a few of them, right?

31:43.970 --> 31:46.010
Now, these are all simple ones, okay?

31:46.030 --> 31:49.130
Simple analog actuators and sensors.

31:49.330 --> 31:57.070
So, with those, you have to do a little bit of analog electronics, a little bit of analog electronics.

31:57.070 --> 31:59.890
Now, analog electronics are painful, okay?

31:59.890 --> 32:04.610
Analog electronics are hard in general, but this much you can do.

32:04.610 --> 32:07.390
So, like with me, I don't do analog electronics.

32:07.390 --> 32:08.230
I can do digital.

32:08.230 --> 32:14.710
Analog is going too far, but that's like a basic chunk of it that you need to know, and you just reuse, okay?

32:14.710 --> 32:20.790
In practice, embedded designers are not generally analog electronics professionals, usually.

32:21.250 --> 32:23.170
So, Ohm's law.

32:23.170 --> 32:25.710
Okay, this is really, really, like, simple.

32:25.990 --> 32:29.490
So, don't be intimidated, you may remember this from physics or something.

32:29.490 --> 32:30.590
V equals IR.

32:30.590 --> 32:33.470
So, voltage is like the electric pressure, right?

32:33.470 --> 32:36.150
I is the current, and R is resistance.

32:36.150 --> 32:38.230
So, think of this like water going through pipes.

32:38.230 --> 32:39.670
It's exactly the same thing.

32:39.690 --> 32:43.150
The pump is like the battery, the voltage source, right?

32:43.170 --> 32:45.450
So, it supplies pressure, generates pressure.

32:45.530 --> 32:48.090
So, in these pipes, the pipes are the wires.

32:48.150 --> 32:49.590
Current is current, right?

32:49.590 --> 32:51.850
The flow of electrons per unit time.

32:51.890 --> 32:55.390
And then resistance is anything that blocks the current.

32:55.390 --> 33:04.870
So, you know, junk, put some rocks in the pipe, or there are low resistance materials or something like that, or narrow wires.

33:05.110 --> 33:09.070
A wide wire has a lot less resistance than a narrow wire, and so on.

33:09.070 --> 33:11.030
So, anyway, V equals IR.

33:11.110 --> 33:14.890
This, you have to be a little familiar with it to use some of these devices.

33:15.230 --> 33:17.170
So, diodes and LEDs.

33:17.170 --> 33:20.210
So, I'm just going to talk about LEDs, because they're like a simple device, right?

33:20.470 --> 33:23.610
LED stands for light emitting diode.

33:23.610 --> 33:26.370
So, a diode symbol is just like this, right?

33:26.370 --> 33:29.770
A diode, it has one, two ends, an anode and cathode.

33:29.770 --> 33:31.090
They are not symmetrical, okay?

33:31.090 --> 33:35.270
And current flows from the anode to the cathode, flows straight through, okay?

33:35.330 --> 33:37.610
That's all it does, and current can't go backwards.

33:37.670 --> 33:38.770
Like, that's what a diode is.

33:38.770 --> 33:40.570
It's like a valve, can't go backwards.

33:40.690 --> 33:50.330
You have to apply a certain amount of voltage, so the anode side has to have higher voltage than the cathode side, by about 1.9 volts or something like that, and then it flows, right?

33:50.330 --> 33:51.830
Below that, it doesn't flow.

33:52.470 --> 33:59.350
And then with an LED, a light emitting diode, this is what a regular diode looks like, a light emitting one, when current flows, it lights up, okay?

33:59.350 --> 34:00.390
So, that's a diode.

34:00.390 --> 34:10.970
All you have to do to make a diode light up is apply current, make this like zero volts, and make this three volts, or some 3.3, whatever you want, and then current will flow to light a light up, okay?

34:11.290 --> 34:14.010
So, how do you do that?

34:14.010 --> 34:16.010
So, say I want to do that, right?

34:16.010 --> 34:20.890
I want to make my, I have an Arduino or whatever board, and I want to make a blinky example, right?

34:20.890 --> 34:23.390
So, I want to be able to light up an LED.

34:23.690 --> 34:31.290
So, I've got to take that LED, connect its anode, one side of to an output pin, any output pin, pin 13 in this case.

34:31.290 --> 34:33.910
The other side goes to ground, which is zero volts.

34:34.730 --> 34:38.910
And when I set this pin 13 high, then it's going to light up.

34:38.910 --> 34:41.490
When I set the pin low, it's not going to light up, okay?

34:41.510 --> 34:45.070
Now, note that I put this little resistor in the way, okay?

34:45.110 --> 34:48.650
Now, the reason I did that is so that I didn't blow up my diode, okay?

34:48.650 --> 34:52.170
Because these diodes, this is the one little analog electronics you have to know.

34:52.170 --> 34:55.510
These diodes are limited in the current that they can deal with.

34:55.510 --> 35:02.670
So, if you look in their data sheet, maybe 10 million, like I think 20 milliamps is like the maximum that they can take, right?

35:02.670 --> 35:09.670
So, if I'm using a 3.3 volt device thing, like my board is 3.3 volts, right?

35:09.710 --> 35:12.190
If I'm using that, and I say, well, what's the current going through it?

35:12.190 --> 35:16.810
If I use about 330 ohms of resistance, then I get like 10 milliamps.

35:16.810 --> 35:18.270
That's safe, right?

35:18.270 --> 35:21.590
So, I need this resistor of 330, okay?

35:21.590 --> 35:23.270
That's like all you need to know, okay?

35:23.270 --> 35:26.750
You don't really need to know much more analog electronics than that.

35:27.010 --> 35:28.950
But yeah, I need a little circuit like that.

35:29.290 --> 35:33.450
Now, then once you get it, once you decide, okay, this is how I have to wire it together, right?

35:33.450 --> 35:34.450
Like this.

35:34.610 --> 35:36.530
Then you got to wire it together, right?

35:36.530 --> 35:37.370
Physically wire it.

35:37.370 --> 35:42.870
So, notice there's an Arduino or whatever board, and then there are two components, a resistor and a diode.

35:43.090 --> 35:48.530
So, I got to put them together, and usually do-it-yourselfers are going to put it together on a breadboard.

35:48.530 --> 35:50.970
Now, a breadboard is basically, it looks like this.

35:50.970 --> 35:52.090
It's got holes.

35:52.090 --> 35:53.830
These holes, stick wires in them, right?

35:53.830 --> 35:55.550
And it just sort of grips the wire.

35:55.770 --> 35:57.550
But you can pull it up, push it and pull it out.

35:57.550 --> 35:58.830
You can redo it.

35:59.130 --> 36:03.230
And so, these rows of five are all connected.

36:03.350 --> 36:05.410
So, all these pins are connected.

36:05.410 --> 36:06.570
All these pins are connected.

36:06.570 --> 36:09.030
But this row is not connected to this row, okay?

36:09.030 --> 36:12.390
So, the use of this is, say I want to connect a resistor to a diode.

36:12.390 --> 36:20.050
I put one pin of the resistor in one hole in the row, put one pin of the diode in another hole in the same row, they are now connected, okay?

36:20.050 --> 36:21.790
So, that's how you make connections.

36:21.810 --> 36:28.650
So, you can take this picture and connect them on a breadboard like this, okay?

36:28.810 --> 36:29.610
That's the idea.

36:29.610 --> 36:32.030
And in fact, here's a picture of that.

36:32.150 --> 36:34.150
So, I got, you can't read all the details.

36:34.150 --> 36:36.570
This is actually an Arduino over here.

36:36.670 --> 36:38.390
So, this is pin 13.

36:38.390 --> 36:41.230
It's connected to this row where there's a resistor.

36:41.290 --> 36:46.250
The other pin of the resistor goes into this row, which is connected to the diode.

36:46.250 --> 36:50.630
The other pin of the diode goes into this column, which is connected to ground, okay?

36:50.790 --> 36:59.010
So, you have to, that's another thing you have to kind of get used to, seeing a schematic, like a drawing of the components, and then wiring it on a breadboard, right?

36:59.010 --> 37:01.430
Like, the relationship between it, you have to be able to do that, okay?

37:01.430 --> 37:04.710
But it's straightforward, you just practice it a little.

37:05.230 --> 37:05.890
Okay.

37:06.050 --> 37:09.570
So, that's, you know, simple electronics wiring, right?

37:09.670 --> 37:12.550
So, you do that, and then your program should work and blink the light.

37:14.250 --> 37:14.970
Okay.

37:15.490 --> 37:18.330
So, I was just talking about GPIO.

37:18.530 --> 37:22.710
GPIO is like the first thing you learn to use on your microcontroller.

37:22.870 --> 37:29.850
But there are other things on the microcontroller, all kinds of peripherals, a whole bunch of them, and you eventually learn how to use all of them, right?

37:29.890 --> 37:32.430
Can't teach them all now, but you learn how to use all of them.

37:32.430 --> 37:36.190
And each one of them has a set of registers devoted to it that describe its behavior.

37:36.950 --> 37:38.730
Another thing is synchronous protocols.

37:38.730 --> 37:49.190
But when you have like a board and you want to connect it to another hardware device, you often use, you have to use some kind of communication protocol.

37:49.250 --> 37:51.030
Synchronous protocols are common.

37:51.030 --> 37:53.290
They're faster, they share a clock.

37:53.290 --> 38:00.750
Anyway, I can't go into details of the protocols, but FPI and I2C are the top ones, you know, that people use.

38:01.070 --> 38:02.550
And give you an example.

38:02.550 --> 38:05.890
Say I got like this, this is an Arduino, actually.

38:05.890 --> 38:09.930
And I only connected it to two components that I bought off of Adafruit or whatever, right?

38:10.010 --> 38:13.410
And these two components I know are I2C compatible.

38:13.410 --> 38:18.690
So when you look at the data sheet for these guys, it'll say, oh, this speaks I2C, this speaks I2C.

38:18.730 --> 38:21.050
So then you say, okay, I'm going to use I2C.

38:21.050 --> 38:24.070
So you wire it to the I2C pins of your Arduino.

38:24.290 --> 38:27.970
They're SDA and SCL, the two wires, two pins for I2C.

38:27.970 --> 38:30.730
SDA is a data line, SCL is a clock, right?

38:30.730 --> 38:34.670
And these guys, there's like a master and slaves.

38:34.910 --> 38:42.830
If you say this is the master, controlling everything, these are the slaves, and this guy can communicate with these guys using an I2C protocol, okay?

38:44.250 --> 38:54.390
Now, I don't have time to describe I2C, but just so you know, all of these protocols, like every other peripheral, is controlled through a set of registers.

38:54.390 --> 38:59.270
So there are certain registers like I2C1, CR1, and CR2.

38:59.270 --> 39:03.550
Control register one, this should say it too.

39:03.850 --> 39:14.230
The control register is used to control different aspects of the I2C, how fast the clock is, and all kinds of stuff about I2C and all its parameters when you look in the data sheet and see what they're all for.

39:14.390 --> 39:17.610
Also, I2C DR, DR is its data register.

39:17.610 --> 39:24.630
So if I want to write a byte on I2C, I want to send a byte, I write it into the DR register, and it'll get sent, right?

39:24.630 --> 39:31.850
So my program doesn't have to, you don't have to know I2C, you just have to know, oh, if I write into this DR register, it will get sent, right?

39:31.850 --> 39:36.730
And then on the receiving end, if I read from this DR register, I will receive whatever was sent, okay?

39:36.730 --> 39:40.870
So there's hardware built in that deals with all the details of the protocol.

39:42.570 --> 39:45.090
Okay, so, okay, good, make it.

39:45.090 --> 39:48.550
So, yeah, so the last part is debugging tools.

39:48.550 --> 39:52.690
So there are some different debugging tools when you're doing embedded design, right?

39:52.690 --> 39:55.510
So here's just my, some random design, okay?

39:55.510 --> 39:59.750
And it's got board and some components, some hardware components, all wired together, okay?

39:59.770 --> 40:02.350
Now, you probably use standard debuggers, right?

40:02.350 --> 40:05.050
You set a breakpoint, you step through the code and all that.

40:05.350 --> 40:11.230
So you can do, you can do that, right, with this, but that doesn't, that control, that works on the micro control.

40:11.230 --> 40:14.730
You can set a breakpoint, step through it, but what about all the rest of the devices, right?

40:14.730 --> 40:16.490
There are other things that you want to view.

40:16.550 --> 40:21.810
So it is very common in these designs to want to see the value on a wire.

40:21.810 --> 40:24.890
You might want to say, oh, is this high or is this low, okay?

40:24.890 --> 40:27.850
Random wire, very common to need to do that.

40:27.850 --> 40:29.050
You're like, I wonder what's being sent.

40:29.050 --> 40:30.510
Is it a one or a zero, right?

40:30.550 --> 40:31.790
So how do you do that?

40:31.790 --> 40:33.410
One way is with multimeters.

40:33.410 --> 40:35.670
So the first thing, you always have to have it.

40:35.830 --> 40:38.070
You gave some out, Sydney, yesterday, right?

40:38.190 --> 40:45.290
Multimeter, it's just a little, you get like a handheld one or a bench one, and basically it tells you the voltage between two points.

40:45.470 --> 40:47.350
So you can tell if something is high or low.

40:47.350 --> 40:50.310
It can read the current, how much current is going through there if you need to know.

40:50.310 --> 40:52.550
It can read resistance between two points.

40:52.850 --> 40:54.330
It can read connectivity.

40:54.390 --> 40:56.730
So it'll like, they almost always have connectivity.

40:56.730 --> 40:59.070
So if these two pins are connected, it'll beep.

40:59.070 --> 41:01.530
So if you're like, oh, is this wire connected to this one?

41:01.530 --> 41:02.710
It'll beep, they are.

41:03.330 --> 41:05.070
And they have probes.

41:05.070 --> 41:10.150
So these two probes, like you take a black and a red probe, put them in like here and here, right?

41:10.170 --> 41:15.630
And then the other ends of these, you touch to the wires that you want to test the voltage on, for instance, okay?

41:15.690 --> 41:17.870
So multimeters are like basic tool.

41:17.990 --> 41:20.450
But the thing about multimeters is that they are very slow.

41:20.790 --> 41:28.910
Meaning, you know, if you have a chip running at 16 megahertz, it's not fast, but that's a heck of a lot faster than a multimeter can respond, right?

41:28.910 --> 41:32.450
So multimeters are only good for looking at signals that are constant.

41:32.450 --> 41:36.510
It's held high, like for half a second at least, or held low, half a second.

41:36.570 --> 41:38.850
Changing signals, you can't use a multimeter.

41:38.890 --> 41:41.190
So one thing you use is an oscilloscope.

41:41.190 --> 41:46.890
Now an oscilloscope, it basically, it'll have a screen and it just plots voltage over time, okay?

41:47.090 --> 41:52.690
Now oscilloscopes though, they're usually very few probes, two or four probes.

41:52.730 --> 41:57.090
So they can look at two signals, two wires at a time, or four wires at a time.

41:57.630 --> 42:00.610
And here's like an old one, an old analog scope.

42:00.930 --> 42:05.510
And now the analog scopes, they only work for repeating signals, okay?

42:05.510 --> 42:10.350
So the signal has to be like the same over and over, like a waveform, a sine wave or something.

42:10.350 --> 42:11.670
And it'll work for that, right?

42:11.670 --> 42:12.370
The old scopes.

42:12.370 --> 42:14.890
The new scopes, digital scopes, they're not like that.

42:14.890 --> 42:18.350
They can read, they can just sort of read it and capture it, right?

42:18.350 --> 42:20.730
Capture it at snapshot time and show it to you on the screen.

42:20.970 --> 42:23.830
But then you can say, oh, look at this time, it was high.

42:23.830 --> 42:25.510
Now at this time, it was low, right?

42:25.510 --> 42:27.390
And these things are very fast.

42:27.570 --> 42:35.150
You got to get a, they cost a lot of money, but you can get one that's faster than what you're, it's like 10 times faster than whatever you're sampling.

42:35.210 --> 42:36.970
And you can use an oscilloscope.

42:37.470 --> 42:42.770
Now oscilloscopes, one thing that they, one thing you need to do with oscilloscopes is do triggering, okay?

42:42.870 --> 42:47.670
So triggering is determining when to start recording.

42:47.670 --> 42:49.710
So these things, they have a buffer.

42:49.710 --> 42:52.270
I don't know, maybe say it has a two gigabyte buffer, okay?

42:52.310 --> 42:56.810
But for every, it's sampling, but it's got to be sampling like a lot faster than your signals.

42:56.810 --> 43:05.910
So say it's sampling 200, 200 megahertz, that's not even that fast, but say it's sampling 200 megahertz, it will fill up and every sample is recording a lot of data.

43:05.910 --> 43:07.870
It'll fill up this buffer very quickly.

43:08.010 --> 43:11.110
So you need triggering to determine when to start recording, okay?

43:11.110 --> 43:13.470
To see what you want, right?

43:13.610 --> 43:16.050
So one way to do is manual triggering.

43:16.050 --> 43:17.650
You hit a button and it goes, okay?

43:17.650 --> 43:26.950
That works fine if you're approximately know when, when the thing is you want to record, but a lot of times you don't know, like you want to record when a certain event happens, okay?

43:27.110 --> 43:30.530
With oscilloscopes, another way triggers level triggering.

43:30.530 --> 43:34.250
So they repeatedly trigger whenever the signal reaches a certain level.

43:34.250 --> 43:38.690
When it goes above X amount of voltage, it triggers and it draws it, okay?

43:38.690 --> 43:40.010
That's another way to trigger.

43:40.390 --> 43:42.550
Also, there's one shot versus continuous triggering.

43:42.550 --> 43:46.330
So one shot means record once and just put that on the screen and hold it there.

43:46.690 --> 43:51.450
Continuous means record once, put it on the screen, record again, put it on the screen, over and over and over.

43:51.510 --> 43:53.990
And so for a repeating signal, that's useful.

43:53.990 --> 43:55.850
You can see if it's changing or what.

43:57.150 --> 43:58.630
Similar to an analog scope.

43:58.790 --> 44:01.430
Now, then another thing that's really useful is a logic analyzer.

44:01.430 --> 44:08.630
These cost even more, but the downside of a logic analyzer is that they are only digital.

44:08.650 --> 44:10.170
See, an oscilloscope is an analog device.

44:10.170 --> 44:11.870
It'll measure analog voltage.

44:12.130 --> 44:14.450
An oscilloscope is either zero or one, okay?

44:14.450 --> 44:15.310
It's only going to show that.

44:15.310 --> 44:18.190
So it's less information, but they have a lot more pins.

44:18.190 --> 44:20.510
They'll have like 32 pins, 64 pins.

44:20.510 --> 44:23.490
So you can see a whole bus at a time, right?

44:23.490 --> 44:26.070
A whole big bus, which is useful, but expensive.

44:26.070 --> 44:27.730
They cost a lot of money.

44:27.730 --> 44:32.230
Or you can get the cheap one, like I do, which has like 10 pins or something, right?

44:32.230 --> 44:34.770
And it's like USB connection to my device.

44:34.770 --> 44:35.090
Okay.

44:35.090 --> 44:35.890
Question?

44:35.950 --> 44:36.410
No?

44:36.410 --> 44:36.650
Okay.

44:36.650 --> 44:36.910
Yeah.

44:37.250 --> 44:40.470
So anyway, these are useful like that.

44:40.710 --> 44:43.090
And they're actually, they're usually very smart too.

44:43.090 --> 44:44.510
They know protocol.

44:44.510 --> 44:45.850
So like they know I2C.

44:45.850 --> 44:47.470
So they'll just mark it up for you.

44:47.470 --> 44:49.350
You know, it's pretty useful actually.

44:49.750 --> 44:53.150
Here's one that I have, a USB logic analyzer.

44:53.150 --> 44:55.090
So it's connected by USB to my laptop.

44:55.090 --> 44:59.870
You download a free app and you can do stuff with it, record and stuff like that.

44:59.890 --> 45:02.510
And it's got a lot more wires that you can use to probe.

45:03.130 --> 45:05.090
And that's about it.

45:05.510 --> 45:11.770
You could download the slides if you want to download the slides and let me know if there's any questions.

45:19.830 --> 45:24.090
We do have time for about three to four questions and I'll hand you the mic.

45:24.810 --> 45:25.710
Go ahead.

45:31.410 --> 45:36.810
I work with a firmware team for whom the idea of cybersecurity is completely new.

45:37.130 --> 45:42.830
What is the best way to establish rapport with them on cybersecurity?

45:42.830 --> 45:46.610
Because they hear cybersecurity, they say, doesn't belong to me.

45:46.610 --> 45:53.990
I make devices, but you know, we're kind of notorious for, I'm an industrial control system.

45:53.990 --> 45:56.630
So that it's a new field for us.

45:56.630 --> 45:56.910
Yeah.

45:56.910 --> 46:04.570
I mean, so one thing you could do is scare them with actual documented, like a hearing these attacks that have happened to industrial control systems.

46:04.570 --> 46:06.110
I do that in my class, right?

46:06.110 --> 46:09.790
Like to entice the students and show them how important it is.

46:09.790 --> 46:11.530
Like, you know, it's easy to find, right?

46:11.530 --> 46:12.750
Especially around here.

46:12.950 --> 46:13.410
You know what I'm saying?

46:13.410 --> 46:16.550
You could get like a list of events and just show it to them.

46:16.610 --> 46:23.390
You could also make them participate in that MITRE embedded capture the flag.

46:23.510 --> 46:30.810
My undergrads, yeah, that's, so what they do in that it's like six weeks, you get to build a system that they tell you.

46:30.810 --> 46:33.970
And then six more weeks, you get to attack everybody else's system.

46:33.970 --> 46:40.690
So like these teams, every team turns it, they give you a design and then you got to build it over six weeks, make it secure.

46:40.690 --> 46:46.610
And then you give it to, then you trade it to everybody and everybody else attacks and you get points and stuff like that.

46:46.610 --> 46:57.990
And that was pretty eyeopening for my students, especially like the talk at the end, where they sort of talk about how they did it, how they attacked, like the CMU guys, saying how they attacked that I2C.

46:58.050 --> 47:01.050
Like if they hide some details, so like with the CMU guys, right?

47:01.150 --> 47:06.310
They said, yeah, we found vulnerabilities in this I2C driver and we did this and that with it, right?

47:06.310 --> 47:09.610
I'm like, I'm thinking, how did they find these vulnerabilities?

47:09.630 --> 47:12.130
And they didn't say, they had to do fuzzing.

47:12.130 --> 47:14.650
In my conclusion, they had to fuzz it, you know?

47:14.650 --> 47:17.610
Because it's not like they eyeballed it and were like, aha.

47:17.610 --> 47:19.690
I mean, I would be really shocked if they did that.

47:19.690 --> 47:22.170
But anyway, maybe that, I don't know.

47:22.810 --> 47:23.290
Yeah.

47:26.780 --> 47:29.560
So I don't work in industry, right?

47:29.560 --> 47:31.920
So I don't really have to vet anything.

47:32.120 --> 47:36.220
But I do in Bad Catch the Flag, I have them fuzz the libraries.

47:37.540 --> 47:40.280
I teach them, I do have the lecture material on fuzzing.

47:40.280 --> 47:45.620
I'm like, okay, fuzz each one of these that you're going to use because of that CMU attack, actually.

47:45.640 --> 47:46.920
But that's what I do.

47:46.920 --> 47:50.360
No, no, I just have them do it from scratch.

47:50.860 --> 47:55.720
Yeah, I just have them write their own code for fuzzing from scratch, yeah.

47:59.360 --> 48:02.440
Thank you, Professor Harris.

48:02.540 --> 48:05.780
I'm very interested in your topic.

48:05.800 --> 48:09.740
You mentioned this devices to do the diagnostics and stuff, right?

48:09.740 --> 48:18.760
But maybe this is something with security diagnostics or geo tag debugging, some stuff that should be covered?

48:18.760 --> 48:20.680
Sorry, yeah, I didn't show that.

48:20.680 --> 48:21.980
You're absolutely right.

48:22.300 --> 48:24.160
So that certainly exists.

48:24.160 --> 48:25.940
So this I didn't go into.

48:25.940 --> 48:30.660
So generally debugging, like setting breakpoints and all that is harder on an embedded system.

48:30.660 --> 48:35.780
Because when you set a breakpoint, you change the memory value, right?

48:35.780 --> 48:38.300
But these things, a lot of these embedded systems run in firmware.

48:38.300 --> 48:43.680
So you got to change the firmware to set the breakpoint, but you can't just reflash every time you want to set a breakpoint, right?

48:43.680 --> 48:46.880
So a lot of times people will use JTAG debugger.

48:46.880 --> 48:49.060
I just sort of didn't cover it as a whole.

48:49.060 --> 48:58.480
And you can get tons of information out with those, you know, you can get runtime information, like we do, we use embedded trace macro cell where you can get, you can follow all the branches.

48:58.480 --> 49:02.440
Actually, so I have a paper where we follow the branches to see if malware is running, right?

49:02.440 --> 49:05.800
Because you know, if malware is running, it'll branch to the wrong location.

49:05.800 --> 49:11.000
Some, you know, somebody did a buffer overflow and change the return address pointer, it'll jump somewhere weird, right?

49:11.000 --> 49:19.760
So with one of these debuggers in real time, you can have another processor that's ruining the branch addresses through JTAG or through whatever, SWD.

49:20.100 --> 49:21.220
So those exist.

49:21.220 --> 49:21.980
Those exist.

49:21.980 --> 49:23.360
I just didn't talk about it.

49:23.760 --> 49:24.720
Those are useful.

49:24.840 --> 49:26.120
They cost a lot of money, though.

49:26.120 --> 49:26.880
That's the problem.

49:26.880 --> 49:30.580
They cost a lot of money, like Lauterbach, thousands of dollars for one of these things.

49:30.580 --> 49:32.560
But embedded... yeah, go ahead.

49:32.840 --> 49:34.400
Yeah, that's right.

49:35.040 --> 49:37.040
I'm assuming no more questions.

49:37.720 --> 49:38.940
Okay, thanks.

