YouTube2Text

EECS 373 - Fall 2026 - Lecture 6: “Interrupts” — Transcript

by Interactive Sensing and Computing Lab · 9,273 words · 1,556 segments · language en · Watch on YouTube

Full transcript

  1. 0:02All right, we can begin. Let's see here.
  2. 0:09All right, sorry for technical delays.
  3. 0:11We're just getting slides downloaded.
  4. 0:15So, today we're going to be talking
  5. 0:16about interrupts.
  6. 0:17Um we'll go actually go through the uh
  7. 0:20the data sheet as well. But, homework is
  8. 0:23due today. There's office hours after
  9. 0:25class. And then uh next week or next on
  10. 0:29Wednesday we'll talk through logistics
  11. 0:31of the exam. We'll talk about that a
  12. 0:32little bit today, but uh I'll also go
  13. 0:34through the data sheet. So, before we
  14. 0:36get started, are there any questions?
  15. 0:39Yeah.
  16. 0:40>> How many lectures uh are there that are
  17. 0:42going to be covered on the exam?
  18. 0:43>> How many lectures?
  19. 0:44>> Yes. I figured what lectures is the
  20. 0:46content coming from?
  21. 0:48>> Um we'll talk So, we're not going to be
  22. 0:50able to get into deep into um
  23. 0:52interrupts, and you won't have a lab on
  24. 0:54interrupts. So, I would say the
  25. 0:55nitty-gritty of getting into interrupts
  26. 0:57is probably not going to be on the exam,
  27. 0:58but maybe a conceptual question would
  28. 1:01probably be really reasonable after
  29. 1:02today's lecture.
  30. 1:05And we'll do Wednesday we'll do a full
  31. 1:06review of what's going to be on the
  32. 1:08exam.
  33. 1:09Give you guys a chance to ask questions.
  34. 1:11What other questions you have?
  35. 1:13Yeah.
  36. 1:14>> Do we need labs?
  37. 1:16>> Yes, you have lab this week. Go to lab.
  38. 1:19So, we're not going to make the labs due
  39. 1:21this week. It's a little bit longer lab,
  40. 1:23but it also takes your pressure off,
  41. 1:24right? So, you should go to lab. They're
  42. 1:26going to be staffed. They're going to be
  43. 1:27giving you the presentations on how to
  44. 1:29do the lab, but because we have an exam
  45. 1:31this week, we're not making labs due
  46. 1:34this week. So, it kind of takes some of
  47. 1:35the pressure off you. So, you do have an
  48. 1:36extra week. So, if you wanted to skip
  49. 1:38lab, that's fine, I suppose, but then
  50. 1:41nobody's going to be there to
  51. 1:42necessarily tell you how to do the lab
  52. 1:44next week. If that makes sense. They'll
  53. 1:46still be staffed, but they're not going
  54. 1:47to be giving the presentation.
  55. 1:50What other questions you have?
  56. 1:55Okay.
  57. 1:59So, let's see.
  58. 2:01So, let's off with the component of the
  59. 2:02day.
  60. 2:03How many people have used servos before?
  61. 2:07Oh, most of you. Is that a question?
  62. 2:10No, that was no question. Okay, just
  63. 2:12telling me you're excited. All right.
  64. 2:14How servos servos work servos are just a
  65. 2:17a DC motor, but they have one special
  66. 2:19trick. The special trick
  67. 2:23is that Oh, I don't want that. The
  68. 2:25special trick is that they have a little
  69. 2:27piece of feedback in there. There's a
  70. 2:28little piece of hardware
  71. 2:30in there, which is basically a
  72. 2:31comparator.
  73. 2:32And that comparator is working to look
  74. 2:35at the pulse train coming in and go to a
  75. 2:38particular location. So, most more
  76. 2:40normally a motor just rotates. You put
  77. 2:42DC power and goes clockwise or
  78. 2:44counterclockwise. This one has a special
  79. 2:46feature of it will go to a particular
  80. 2:47location. How is that defined? Well,
  81. 2:50there's these three uh pins or wires
  82. 2:52that are put there. So, power and
  83. 2:54ground, that makes sense, and also
  84. 2:55signal. And then signal has a little
  85. 2:57pulse train.
  86. 2:58So, the width of that pulse encodes the
  87. 3:02angle that it should be at at some
  88. 3:04mapping that will be defined in the data
  89. 3:05sheet. And so, you have some that will
  90. 3:07go plus or minus 90°, and you'll have
  91. 3:09others that will go plus or minus 180,
  92. 3:12and some of that will also spin if you
  93. 3:14go all the way to the extent. So,
  94. 3:16there's different types of servos, and
  95. 3:19they come in different weight categories
  96. 3:20and torque categories.
  97. 3:22But, that allows you to do things like
  98. 3:23this. So, here person has an IMU and the
  99. 3:26controller, and then the servo is trying
  100. 3:28to mimic
  101. 3:29those uh
  102. 3:31um
  103. 3:32motions.
  104. 3:33What questions you have?
  105. 3:38Okay.
  106. 3:41All right. Actually, we got ahead, so
  107. 3:44we're not going to do a quick review of
  108. 3:45memory map IO, but let's go through the
  109. 3:47data sheet just to save that we did it.
  110. 3:51Um
  111. 3:52Okay, so on the assignment page
  112. 3:56is
  113. 3:57uh the data sheet.
  114. 3:59And so, the reason we give you I should
  115. 4:01have explained this earlier. I think
  116. 4:03we'll have to write up a formal syllabus
  117. 4:04for this class cuz we get lots of edge
  118. 4:06condition questions. But, the idea of a
  119. 4:08data sheet is that uh we want to ask
  120. 4:12more interesting questions of you about
  121. 4:14how embedded systems can be put
  122. 4:15together.
  123. 4:16But, it's not really fair. And so, what
  124. 4:19we used to do is give you like, here's a
  125. 4:21data sheet. In 473, they'll give you
  126. 4:22like, here's a real data sheet.
  127. 4:24The challenge is that that puts a lot of
  128. 4:26pressure on you in a 2-hour exam to read
  129. 4:29like 11-page data sheet and come up with
  130. 4:31an answer, right? That's And that's also
  131. 4:33not how engineering works. I've never
  132. 4:34been in a situation where my boss is
  133. 4:36like, you know, read this data sheet and
  134. 4:38program this thing in 2 hours or you're
  135. 4:40fired. Like, that that never happens.
  136. 4:42So, we're going to give you the data
  137. 4:44sheet beforehand and it outlines, for
  138. 4:46instance, this mythical processor that
  139. 4:48we've made and you've you've seen it
  140. 4:49from your other ones. Uh so, you get
  141. 4:51this mythical processor. Uh formatting
  142. 4:54already got screwed up. Cool.
  143. 4:57Uh that's unscrewed up. Uh it has This
  144. 5:00is kind of like what you would normally
  145. 5:01see in a data sheet. Has architecture,
  146. 5:02has pin assignments. And then it has
  147. 5:04some registers.
  148. 5:06And some of those registers have
  149. 5:07particular functions and mappings. And
  150. 5:09so, it's your job to be able to review
  151. 5:11all this information beforehand and
  152. 5:13figure out like how to use the thing,
  153. 5:16basically.
  154. 5:17And then we also give a data sheet for
  155. 5:19this mythical microphone.
  156. 5:21And a data sheet for this LCD screen,
  157. 5:23which is different than last
  158. 5:25uh F25's
  159. 5:28uh LCD screen cuz it has a different
  160. 5:29interface.
  161. 5:31And so, you should be go through these
  162. 5:33data sheets and read through and make
  163. 5:34sure you understand what's in there. And
  164. 5:36if there's you have questions, you can
  165. 5:38raise them, put them in Piazza, come to
  166. 5:39office hours. And sometimes students
  167. 5:41will find errors. They'll be like, "Hey,
  168. 5:42look, your addressing scheme is crazy.
  169. 5:44Right? Is this supposed, you know, okay,
  170. 5:45we'll we'll fix those errors." And uh if
  171. 5:47there are any issues, we'll update them
  172. 5:50on Piazza so everybody uh is aware of
  173. 5:52those. But, you could also, as a study
  174. 5:55uh method, you could probably figure out
  175. 5:57what the exam problem is.
  176. 6:00Right? Like, you could just write it
  177. 6:01down.
  178. 6:02And just guess what the exam problem is.
  179. 6:04We're not too creative. So, you could
  180. 6:06say, well, like, okay.
  181. 6:08Let's see. You're probably going to have
  182. 6:10a microcontroller.
  183. 6:12It probably has a GPIO interface.
  184. 6:15It has some weird audio interface.
  185. 6:19>> [snorts]
  186. 6:19>> There's a microphone.
  187. 6:22And there's an LCD screen.
  188. 6:24So, pretty much, you could probably
  189. 6:25figure out what's on the exam.
  190. 6:30What do you think?
  191. 6:32What questions you have?
  192. 6:38Yeah.
  193. 6:39>> Do we get a data sheet in
  194. 6:41>> No.
  195. 6:43I mean, we're going to give We're going
  196. 6:44to give you a copy of this data sheet on
  197. 6:46the exam. And then, if you look at the
  198. 6:48very bottom of the data sheet, they have
  199. 6:51here are like some There's an appendix,
  200. 6:54right? So, here's what a timing diagram
  201. 6:55looks like for an APB. Here are some
  202. 6:57symbols that you could use. So, you
  203. 6:58don't have to remember that. Here a
  204. 7:00whole bunch of instructions.
  205. 7:02Like, your ability to remember
  206. 7:03instructions is probably not that
  207. 7:05important as an engineer. Your ability
  208. 7:06to to know how to use them when you look
  209. 7:08at a reference sheet is really
  210. 7:09important.
  211. 7:17So, does this feel overwhelming or does
  212. 7:19this seem like, okay, I just got to do
  213. 7:21some more studying?
  214. 7:24All right. Over Well, okay. I saw some
  215. 7:26people going like that. You will see
  216. 7:28we'll go through There's two practice
  217. 7:30exams on the on the website. The
  218. 7:33assignment page one is I didn't make
  219. 7:35that. It was from last year's winter and
  220. 7:36one I made
  221. 7:38uh last year
  222. 7:40uh fall. But, I'll I'll answer questions
  223. 7:43on all of them. And you can kind of see
  224. 7:45what What is the challenge that you're
  225. 7:47up against? Um I would say that the
  226. 7:49trick is really how to kind of like lab
  227. 7:52is how do you architect these things?
  228. 7:54And specifically, also remembering that
  229. 7:56this data sheet like
  230. 7:59you could imagine that if you read the
  231. 8:02the data sheet here, this tells you what
  232. 8:04the architecture of a peripheral is.
  233. 8:07Just by reading it. you could guess. You
  234. 8:09can look through the datasheet and
  235. 8:10imagine what that peripheral is made of,
  236. 8:13>> [clears throat]
  237. 8:13>> how it functions on the inside. This
  238. 8:14defines how it's going to operate. It's
  239. 8:17an important thing to remember.
  240. 8:19All right. Well, we'll do a a more
  241. 8:21detailed review
  242. 8:22uh tomorrow.
  243. 8:25But for now, we're going to start uh
  244. 8:29Oops. Slides got a little out of
  245. 8:30control.
  246. 8:31We'll start doing uh interrupts.
  247. 8:36All right.
  248. 8:37So,
  249. 8:38up until now, you've been living in this
  250. 8:40a world where you're you're basically
  251. 8:42like in a while one loop and everything
  252. 8:43is happening um synchronously. Um
  253. 8:48but there's a problem where the external
  254. 8:50world is not a synchronous event. It
  255. 8:51doesn't happen
  256. 8:53uh as on a function call. It doesn't
  257. 8:55happen on a clock schedule. So, um
  258. 8:57you have events like people pressing
  259. 8:59buttons or sensors going off and you
  260. 9:01need some methodology to allow that that
  261. 9:05information to go and interrupt your
  262. 9:07system process so you can respond. So,
  263. 9:09this is really about being responsive.
  264. 9:11And it's being responsive to things that
  265. 9:12are internal and external to your
  266. 9:14microcontroller.
  267. 9:16Um
  268. 9:17So, applications make system calls.
  269. 9:19Software
  270. 9:20uh does instructions, but what about
  271. 9:23these external events? How do you know?
  272. 9:25So, probably to term, there's two
  273. 9:27versions.
  274. 9:29Uh there is two ways to figure out an
  275. 9:30external event works. So, one would be
  276. 9:32you could just pull and that's probably
  277. 9:34mostly what you've done so far. You can
  278. 9:35just go check the GPIO pin, measure a
  279. 9:38thing. Yes.
  280. 9:41>> Your mic is not working.
  281. 9:42>> My mic is not working.
  282. 9:44Thank you for telling me. Let's see.
  283. 9:46This is
  284. 9:50How about now?
  285. 9:52Okay.
  286. 9:53Thank you.
  287. 9:55Um so, there's basically two ways to
  288. 9:58check for uh external events. One would
  289. 10:00be polling. And so, that is where you
  290. 10:02could go and you could just like do a
  291. 10:05load, right? And you could just load the
  292. 10:08value of some GPIO uh
  293. 10:10register, like the input for a GPIO pin
  294. 10:13and to see if that
  295. 10:18and then an interrupt lets the processor
  296. 10:22get notified through some other method
  297. 10:24that this polling event has occurred.
  298. 10:26>> [clears throat]
  299. 10:27>> So,
  300. 10:28I don't know who made these slides, but
  301. 10:31so the the example of polling is trying
  302. 10:33like texting with notifications turned
  303. 10:36off. Basically, the only way you get to
  304. 10:38know if anything happens is your
  305. 10:39response from the other person is if
  306. 10:41you're actually looking at your screen.
  307. 10:42So, that just takes your attention away.
  308. 10:44Nothing is going to alert you that this
  309. 10:46event has occurred.
  310. 10:48So, like the conceptual problem with the
  311. 10:50polling is from the
  312. 10:52minimal case if you have a very small
  313. 10:54program, it's not that big a deal. So,
  314. 10:56here we're going to go and look
  315. 10:58at a push button
  316. 10:59here and then you're going to go turn
  317. 11:01that LED on or off if that button
  318. 11:04[snorts] has been pressed.
  319. 11:06And so, if the software is just looping
  320. 11:08through this thing continuously,
  321. 11:09[clears throat]
  322. 11:11and it's very fast. It only takes three
  323. 11:12cycles to respond to. However, it
  324. 11:15doesn't do anything else and that's the
  325. 11:16fundamental process problem. So, your
  326. 11:18processor utilization is like zero
  327. 11:20because you can't do any other tasks.
  328. 11:22And it gets worse
  329. 11:24because if you start adding other tasks
  330. 11:26like let's spend 100 cycles doing
  331. 11:27something else,
  332. 11:29now you're less responsive to the LED
  333. 11:33events. And now 100 cycles might not be
  334. 11:36a big issue, but as you keep going with
  335. 11:38more and more tasks on your
  336. 11:39microcontroller, you'll find that you
  337. 11:40become far less responsive to the
  338. 11:42external world or external events
  339. 11:45without
  340. 11:47while doing polling. So, we need another
  341. 11:49mechanism to to take care of this.
  342. 11:52So, the external method is interrupts.
  343. 11:55And so, you know, textbook definition
  344. 11:58informs the program that some event has
  345. 12:00taken and breaks flow execution. So,
  346. 12:02let's take a look at what that looks
  347. 12:03like.
  348. 12:04So,
  349. 12:06this idea of
  350. 12:07an interrupt is an interrupt service
  351. 12:09routine. So, it begins with a
  352. 12:13um
  353. 12:14uh some piece of hardware triggering off
  354. 12:17an event that will cause an interrupt
  355. 12:19service routine. And that will pull up a
  356. 12:21piece of code that does a particular
  357. 12:23task.
  358. 12:25Uh then you would enter in that service
  359. 12:27routine and then you would resume your
  360. 12:29regularly scheduled program. And as
  361. 12:31we'll see, the
  362. 12:33I don't know if it's the goal, but the
  363. 12:34function is that the program that was
  364. 12:36originally running is transparent. It's
  365. 12:38not even aware that this uh interrupt
  366. 12:40occurred.
  367. 12:42So, uh interrupts are just nothing more
  368. 12:45than uh glorified procedure calls.
  369. 12:48Um
  370. 12:48they should generally be transparent to
  371. 12:50the program running, like I just said.
  372. 12:53All right. So, some examples. So, table
  373. 12:55misses, illegal divide by zeros,
  374. 12:58um supervisor calls. These would be
  375. 13:00examples of like this system faults or
  376. 13:02instruction faults.
  377. 13:04Um when we think about So, we use names
  378. 13:07like system exceptions. Uh typically
  379. 13:09think about interrupts. Uh
  380. 13:11>> [clears throat]
  381. 13:11>> this class is usually something from the
  382. 13:13outside world causing an interrupt or
  383. 13:15like a peripheral causing an interrupt.
  384. 13:17We'll talk about timers. For timers will
  385. 13:19go off and alert the core to some sort
  386. 13:22of event. But, they do have a a number
  387. 13:23of different um
  388. 13:25names that you'll see throughout the
  389. 13:27literature.
  390. 13:29Um [snorts] so, yeah, some examples.
  391. 13:31Power failures, push buttons. All of
  392. 13:33these things can cause uh an interrupt
  393. 13:36to [clears throat] occur. One thing
  394. 13:37that's maybe not obvious is when you
  395. 13:39power on reset effectively. So, when you
  396. 13:42first boot up your thing, it goes into a
  397. 13:45um
  398. 13:46an interrupt uh that will uh load your
  399. 13:49device uh state and then return
  400. 13:52uh you and put you into main.
  401. 13:55Okay.
  402. 13:58Oh, and again, the word external here,
  403. 14:00we're going to use the word external
  404. 14:01interrupts. External is reference to the
  405. 14:04core, not to the chip necessarily. So,
  406. 14:07an external interrupt could be uh a
  407. 14:09peripheral.
  408. 14:12All right. So, let's go fast. We'll fill
  409. 14:13in the details later. So,
  410. 14:15uh how does interrupt work? A hardware
  411. 14:17or sometimes firmware, I mean there's a
  412. 14:19special construction you could use, but
  413. 14:21generally hardware generates an
  414. 14:22interrupt signal. It determines which
  415. 14:26interrupt fired. So, just because code
  416. 14:28got stopped doesn't mean you know what
  417. 14:30caused that code uh to get stopped and
  418. 14:33what you should your response should be.
  419. 14:35So, you have to determine what code
  420. 14:37should be executed in response to that
  421. 14:39interrupt firing.
  422. 14:40Uh
  423. 14:40>> [clears throat]
  424. 14:41>> so, the main program is halted. You
  425. 14:43change your processor mode. You go into
  426. 14:45this thing called interrupt service
  427. 14:46routine.
  428. 14:47Uh
  429. 14:48you do your task and then return the
  430. 14:49process to its original state so that
  431. 14:51you can continue your program. So, a lot
  432. 14:53of what we're going to be doing is
  433. 14:55trying to hold the state of the
  434. 14:56processor so you don't and you store it
  435. 14:58somewhere so you're not going to get
  436. 14:59lost. Basically, the same as when you do
  437. 15:02a branch
  438. 15:04uh or when you do a subroutine call, you
  439. 15:06have to keep track of your link
  440. 15:07register.
  441. 15:08All right. So, the question for you
  442. 15:11is how do we get interrupt from
  443. 15:13peripherals to the core? So, why don't
  444. 15:14you take, you know, 60 seconds,
  445. 15:16brainstorm with your neighbor. How do
  446. 15:18you actually [clears throat] get these
  447. 15:19signals
  448. 15:20uh from a
  449. 15:22peripheral like a timer to the core?
  450. 15:39>> [laughter]
  451. 16:18>> All All what do we think? How do we
  452. 16:19actually get the signals?
  453. 16:20To the core.
  454. 16:22Okay.
  455. 16:31You have any ideas? Anybody?
  456. 16:33What's the method?
  457. 16:34Yeah.
  458. 16:35>> Through the bus.
  459. 16:36>> You could do it through the bus.
  460. 16:38Now, remember there's a there's a
  461. 16:39problem with that.
  462. 16:41That the peripherals
  463. 16:43cannot cause any bus transactions. Only
  464. 16:47the requestor, or the bridge in this
  465. 16:49case, can denote that this thing should
  466. 16:52talk. So, there's no way for the
  467. 16:53peripheral to say, "Hey, I need to have
  468. 16:55an important message. I need to do
  469. 16:56something."
  470. 16:59What's another way we can do it?
  471. 17:03You have to you have to invent
  472. 17:04something. This is a trick question.
  473. 17:06Yeah.
  474. 17:09You have I am hard of hearing.
  475. 17:12>> [clears throat]
  476. 17:15>> I'll just write that just That's
  477. 17:17perfect. Maybe you looked ahead on the
  478. 17:19slides. That's fantastic. There you go.
  479. 17:21That's exactly what you did.
  480. 17:23Oh, this is the old uh
  481. 17:25I know this is the old block diagram on
  482. 17:28your particular device on the GPIOs in
  483. 17:30the advanced peripheral block, but
  484. 17:32regardless, you're really going to add
  485. 17:33this extra wire. In fact, for every
  486. 17:36interrupt, there is going to be an extra
  487. 17:39wire that goes from wherever it's
  488. 17:40located, so usually GPIOs or uh UARTs or
  489. 17:44wherever it is, there are going to be
  490. 17:45these extra lines that go back to the
  491. 17:48core. And it's just a single bit. It's
  492. 17:50not like a bus. It's just saying, "Hey,
  493. 17:52this interrupt occurred, and now it's
  494. 17:55time to do some task."
  495. 17:58So, if you go look at some of the
  496. 18:00you know, ARM literature, here's what
  497. 18:02essentially what it looks like. It's a
  498. 18:03brief block diagram of everything. So,
  499. 18:05you have your processor core, then you
  500. 18:07have your NVE. So, the uh this is nested
  501. 18:10vector interrupt uh controller. And this
  502. 18:13is the the kind of second You can call
  503. 18:16it processor, but the combinational
  504. 18:17logic here
  505. 18:18>> [clears throat]
  506. 18:18>> is going to interpret the IRQs that are
  507. 18:20interrupt uh requests from the
  508. 18:23peripherals and then change the
  509. 18:25execution mode of the core. So, here you
  510. 18:28have a peripheral just like we saw
  511. 18:30there's a wire coming in. This is
  512. 18:31different than the bus system. It pulls
  513. 18:33high and then the NVIC's job is to
  514. 18:35figure out, "Hey, what am I supposed to
  515. 18:38do with that particular line going
  516. 18:40high?" Cuz that is just one bit. And
  517. 18:42then it will change the execution of the
  518. 18:45core.
  519. 18:46Yeah.
  520. 18:55>> [clears throat and snorts]
  521. 18:59[sighs]
  522. 19:00>> I Essentially, yes. Now, you have to
  523. 19:03read through the data sheet. It's not a
  524. 19:05guarantee that every GPIO pin will be
  525. 19:08interruptible in the sense that it has
  526. 19:10that capability. Although, I think for
  527. 19:12the processor you're using that is true.
  528. 19:15But, you would look through and say,
  529. 19:16"Okay, does this have that
  530. 19:17functionality?" And if the data sheet
  531. 19:19says it does, then probably means
  532. 19:20there's a wire that goes back.
  533. 19:28What do you mean by outside peripheral?
  534. 19:35Yeah.
  535. 19:37Ah, okay. So, you have to have
  536. 19:39specialized logic. So, uh when you say
  537. 19:41plug in, you probably mean like on a
  538. 19:43PCB, right? Okay. So, that PCB you're
  539. 19:46going to have to route the signal into
  540. 19:48your device. Like it has to be wired in.
  541. 19:50And let's just say it's digital, let's
  542. 19:51make it easy, and it's a GPIO pin. And
  543. 19:53let's just make it even easier say that
  544. 19:55GPIO that temperature sensor goes high
  545. 19:57when it's over a threshold. And you have
  546. 19:59to configure your GPIO pin to say,
  547. 20:01"Okay, on this event, then trigger this
  548. 20:04IRQ." And how do you do that? Oh, well,
  549. 20:07just like we do everything. You go read
  550. 20:08through the data sheet and you're like,
  551. 20:09"Okay, this register defines that I'm
  552. 20:12going to turn on an interrupt."
  553. 20:14And then I'm going to be positive edge
  554. 20:16or negative edge.
  555. 20:18It's a good question.
  556. 20:19Other questions you got?
  557. 20:22Yeah.
  558. 20:23>> Um so, you said it's the interrupt
  559. 20:25>> procedure for the hardware.
  560. 20:27Um from the way you described it, it
  561. 20:29sounds to me like the processor is
  562. 20:30executing the instructions and then it
  563. 20:31clears the mask and then it works again.
  564. 20:33I don't see how it would prevent it from
  565. 20:35working anymore.
  566. 20:37>> Uh so there are there's a manual way to
  567. 20:40trigger an interrupt in firmware with a
  568. 20:42special instruction.
  569. 20:43>> [cough]
  570. 20:43>> It's [clears throat] not used often,
  571. 20:45like I don't know why you would do it,
  572. 20:46but it's good for testing. So that's why
  573. 20:48sometimes we use it in the lab. So you
  574. 20:49can like
  575. 20:50force this interrupt to occur. But
  576. 20:53generally, you software to configure the
  577. 20:55interrupt so that the hardware will
  578. 20:57automatically make that
  579. 20:59decision for you.
  580. 21:00>> [clears throat and cough]
  581. 21:01>> Does that make sense?
  582. 21:05All right.
  583. 21:10All right, so let's see.
  584. 21:12Yeah.
  585. 21:14And so let's go through a quick example
  586. 21:16here. We're going to be in
  587. 21:17main,
  588. 21:19right? You're We're going around. You're
  589. 21:20going through executing, independent of
  590. 21:22anything you're doing, some interrupt
  591. 21:25has been fired. And so the NVIC's job
  592. 21:28is to go through and look in what's
  593. 21:30called the interrupt vector table.
  594. 21:32>> [cough and clears throat]
  595. 21:33>> And so one of those lines goes high, and
  596. 21:35then it has to say, "Okay,
  597. 21:37that line is associated with a
  598. 21:39particular
  599. 21:40location in the table, and then it says,
  600. 21:43'Okay, that means I have to go run
  601. 21:46a piece of code.' Well, what code is
  602. 21:47that? Well, that code lives at this
  603. 21:49address.
  604. 21:51And so now it breaks the processor's
  605. 21:53normal execution. It basically does a
  606. 21:56fancy branch, and there's a special
  607. 21:57extra conditions there, but it does a
  608. 21:59fancy branch over to this section of
  609. 22:01code. You run that code, and then when
  610. 22:04you're done, you reset the state of the
  611. 22:06processor, and you go back, and then
  612. 22:09the processor continues. So what you
  613. 22:11have to do is make sure that you don't
  614. 22:13interrupt or corrupt the processor by
  615. 22:16executing this code. So this is a lot
  616. 22:18like how we had to be ABI compliant when
  617. 22:21we were dealing with
  618. 22:23function calls. We have to make sure
  619. 22:24that we're not clobbering all the
  620. 22:26registers in the in the main program
  621. 22:29when we're executing this interrupt
  622. 22:30routine.
  623. 22:35Uh so, here we talk about the vector
  624. 22:37table is where that information stored.
  625. 22:39So,
  626. 22:40uh remember from our diagram, we have uh
  627. 22:42the space here. Uh in the code space,
  628. 22:45the very bottom, the very smallest entry
  629. 22:48here
  630. 22:49uh well, first that is set to your
  631. 22:51Sorry, the the very zero zero defines
  632. 22:54your stack pointer. Then the vector
  633. 22:56table is on top of that. And that is by
  634. 22:58ARM's um
  635. 23:00instructions in the sense uh that that's
  636. 23:02where the vector table has to live. But
  637. 23:03it is just a location in memory. That's
  638. 23:05all it is. And it's just a uh uh
  639. 23:08um a set of addresses of where functions
  640. 23:10are going to be.
  641. 23:12Um and then often times, this usually
  642. 23:14comes up in lab, uh
  643. 23:16you actually store your code up here in
  644. 23:18flash and then on boot it is uh put into
  645. 23:21SRAM.
  646. 23:24So, here's a more detailed uh diagram of
  647. 23:27what the vector table could look like.
  648. 23:29Your processor might be slightly
  649. 23:31different, but the the concepts are all
  650. 23:32the same.
  651. 23:34So, here um
  652. 23:36the vector table is just a functional
  653. 23:37like an address book. Uh interrupt
  654. 23:39service uh routine is called when um
  655. 23:43uh
  656. 23:44Yeah, I just basically said this, but
  657. 23:45interrupt service routine call is called
  658. 23:47and then you have to look up in here.
  659. 23:49So, uh
  660. 23:50>> [clears throat]
  661. 23:50>> the stack Oops.
  662. 23:55And so, the the PC is always initialized
  663. 23:57to
  664. 23:58uh instruction or address four, not
  665. 24:00address zero. Address zero is uh stack
  666. 24:04pointer. And then this is the interrupt
  667. 24:06routine that happens on startup, which
  668. 24:07is the reset handler. So, when you brown
  669. 24:10out, or sorry, when power is
  670. 24:11disconnected, uh when it comes back on,
  671. 24:14it has to know what to do. It just
  672. 24:15doesn't just go and start executing
  673. 24:19uh the first instruction in memory or
  674. 24:20main. It does a special task, which is
  675. 24:23the boot sequence. And the reason they
  676. 24:24just they
  677. 24:26they changed that from just like
  678. 24:27whatever code you put in it. The boot up
  679. 24:29sequence uh often times is very
  680. 24:32processor
  681. 24:33specific. Things happen to happen in a
  682. 24:35certain order. All this is obfuscated
  683. 24:38from you in your IDE cuz this is for
  684. 24:40you. Now, you certainly could go and
  685. 24:42change that boot up sequence and I had a
  686. 24:43project where you were harvesting
  687. 24:46RF energy and [clears throat] the power
  688. 24:49source had high source resistance, which
  689. 24:51means it was easy if you pulled a lot of
  690. 24:53current you would brown yourself out.
  691. 24:55You'd get in this weird state where you
  692. 24:56had almost enough power and voltage to
  693. 24:58do something, but then you sip a little
  694. 25:00of that power and then you would brown
  695. 25:02out. And so, what we had to do is
  696. 25:04actually tell it don't copy any of that
  697. 25:06information over
  698. 25:08when you go from flash into SRAM or even
  699. 25:12build any of that stuff. The immediate
  700. 25:13instruction was go to sleep for end
  701. 25:16period of time so we can
  702. 25:18save more power. So, not that that's
  703. 25:20really important for your labs, but just
  704. 25:22remember that there is this extra piece
  705. 25:24of code that's going to run before you
  706. 25:26jump into main and you could edit it if
  707. 25:28you ever had to.
  708. 25:32Okay.
  709. 25:34Uh so, say we're going to look up some
  710. 25:35IRQs. So,
  711. 25:37a request comes in. Uh it says STM32,
  712. 25:40but it's exactly the same for your
  713. 25:43devices as well.
  714. 25:45Um
  715. 25:46So, what happens is an interrupt routine
  716. 25:49service call comes in and it has some
  717. 25:51index to it. So, all those lines we saw
  718. 25:54go from zero to something.
  719. 25:56Right? And
  720. 25:58the bottom set of
  721. 26:01interrupts or system
  722. 26:04exceptions are all defined by
  723. 26:07ARM. So, ARM says these ones have to
  724. 26:10happen and they're far more important
  725. 26:12than any of the other ones, any of the
  726. 26:13vendor specific ones. You can imagine
  727. 26:15you don't want a vendor specific
  728. 26:17interrupt like a watchdog timer or
  729. 26:19something being more important than the
  730. 26:21reset handler. That has to happen for
  731. 26:23your
  732. 26:24chip to function. So, ARM says that
  733. 26:26these 16 have to exist even though
  734. 26:29many of them are reserved, they're just
  735. 26:31saying these must exist. And so these
  736. 26:34are system exceptions and what else we
  737. 26:35have here? I can't really see, but
  738. 26:39you know, hard faults, memory management
  739. 26:41faults, bus faults, all of those are
  740. 26:43about the inner workings of the
  741. 26:46core, not necessarily about the
  742. 26:48peripherals.
  743. 26:50>> [snorts]
  744. 26:53>> Uh
  745. 26:54and so the inner So these numbers here
  746. 26:57have to be remapped. It's more I'm
  747. 26:59pointing this out is because ARM says
  748. 27:01that these are negative 1 through -14.
  749. 27:04Um
  750. 27:06and that's just to denote that those are
  751. 27:07system exceptions rather than the normal
  752. 27:09exceptions. So when you're looking
  753. 27:09through your documentation, you might be
  754. 27:11like, "How am I supposed to do a
  755. 27:13calculation on negative numbers? You
  756. 27:15know, these should just be whole ints."
  757. 27:17Well, that's just because there's an
  758. 27:18offset applied and you have to go and
  759. 27:20add it. Um 16 to your IRQ number to know
  760. 27:24where it belongs.
  761. 27:28All right, so now, you know, so again,
  762. 27:31what's happening here is that your NVIC
  763. 27:33is getting the number on some index. It
  764. 27:35says, "Okay, the the lower or the maybe
  765. 27:37the 12th
  766. 27:38indexed wire goes off. I need to add 16
  767. 27:41to that or subtract 16 and then that
  768. 27:43tells me where in this
  769. 27:46table I need to go and find which
  770. 27:49interrupt just got fired." And I call
  771. 27:51that
  772. 27:52put the PC to that a memory location and
  773. 27:54go forward.
  774. 27:57So the program status register
  775. 28:00holds a lot of information in this. We
  776. 28:02previously just talked about the
  777. 28:04basically the ALU flags that come out
  778. 28:06the end.
  779. 28:07There's other things in here as well.
  780. 28:09TI's This basically if then take
  781. 28:12bits and some other process generation
  782. 28:15flags. But then here the bottom eight
  783. 28:18are the exception number and this tells
  784. 28:20processor what is the exception that is
  785. 28:21running at this moment.
  786. 28:25Uh yep.
  787. 28:28All right.
  788. 28:30So if you got a external IRQ, so this
  789. 28:32would be like an external interrupt is
  790. 28:34going off a GPIO pin. You'd go, you'd
  791. 28:38figure out do the offset here to figure
  792. 28:39out what memory address you need to go
  793. 28:42to. So, here you're getting the seventh
  794. 28:44law. Let's see this sorry, this is the
  795. 28:4664th. And then you come up here and you
  796. 28:48would look and say, "Okay, this this
  797. 28:49memory address needs to go and then I
  798. 28:51need to go to this location." So, all
  799. 28:54the NVIC is really a fancy look up table
  800. 28:56mechanism. [clears throat]
  801. 28:57We'll see later that you can set up
  802. 28:59priorities on the interrupts.
  803. 29:01There's another example.
  804. 29:04Okay.
  805. 29:06That's the the general idea here that is
  806. 29:08the the NVIC is that it's really
  807. 29:11combinatorial logic. It's not running
  808. 29:13arbitrary code, but you'll see that it
  809. 29:14has to make decisions about which
  810. 29:16interrupts get fired when.
  811. 29:19Uh so, let's go through um another
  812. 29:21example here.
  813. 29:25All right. So, here
  814. 29:26a GPIO pin is been configured to go
  815. 29:30high, so pin
  816. 29:32you know, A3. It gets raised. That means
  817. 29:35that the GPIO module itself is going to
  818. 29:37raise a line and that is the ninth line,
  819. 29:40let's say. The NVIC needs to go through
  820. 29:43and decide, "Okay, calculate the offset,
  821. 29:45go through here and look in the NVIC
  822. 29:47table for the correct memory
  823. 29:51location." Then it gets that address and
  824. 29:53puts that into the core.
  825. 29:55Now the core is going to be executing
  826. 29:57this new piece of code
  827. 29:59um
  828. 30:00uh and uh
  829. 30:03we have to make sure that the rest of
  830. 30:04the processor isn't corrupted.
  831. 30:07All right. So, question.
  832. 30:12Okay. So, what are the what are the
  833. 30:14things if we know the IRS must be
  834. 30:16ABI compliant,
  835. 30:18what state information do you think that
  836. 30:21we have to store or save in the core?
  837. 30:24Let me take 30 sec or a minute, talk
  838. 30:27to yourselves and try and identify all
  839. 30:28the pieces of information that would
  840. 30:30have to be saved in order to do this
  841. 30:33function.
  842. 30:43>> [clears throat]
  843. 30:46[groaning]
  844. 32:01>> All right, so these interrupt services
  845. 32:03need to be ABI compliant.
  846. 32:05So, what what signals would we have to
  847. 32:07save?
  848. 32:17That's a good idea. I don't So, we could
  849. 32:19just store that
  850. 32:22from just a data point of view, we could
  851. 32:24just store just the first four if our
  852. 32:27function was aware that that was
  853. 32:30supposed to do. So, we could still keep
  854. 32:31that little
  855. 32:33carrier for some ABI compliance, but
  856. 32:35there are other registers that we
  857. 32:37normally wouldn't consider for ABI
  858. 32:38compliance. And you were talking about
  859. 32:39some of them. What were the other ones?
  860. 32:42Yep.
  861. 32:45PC link register, right? Because what's
  862. 32:47going to happen is you're going to do
  863. 32:48some completely other piece of code.
  864. 32:51And if you go and come back, there is no
  865. 32:56there's not the built-in kind of
  866. 32:59main does not know where that was and
  867. 33:01doesn't All right, you don't want to
  868. 33:02interrupt your main function. So we need
  869. 33:04PC
  870. 33:05link register. What else do we need?
  871. 33:09Yeah.
  872. 33:23>> [snorts]
  873. 33:27>> Well,
  874. 33:30Ah.
  875. 33:33That's a great question. Okay, so hold
  876. 33:35on. You get two unboxed things. It's not
  877. 33:37a requirement that we save every
  878. 33:39register if we know our
  879. 33:41function isn't going to clobber that. So
  880. 33:43we can tell the function you only you
  881. 33:45can have the first four scratch space.
  882. 33:47Let's just do that.
  883. 33:48But the function will go and manipulate
  884. 33:51things like your
  885. 33:53program counter, right? And so if you
  886. 33:56don't save your program counter when you
  887. 33:57go back to your main, you get lost. So
  888. 33:59we know we have to save that.
  889. 34:01Um and we'll talk about the details of
  890. 34:03who does what in a second.
  891. 34:05Uh we talked about the program counter
  892. 34:07and link register. What else do we need
  893. 34:08to save?
  894. 34:09Yeah.
  895. 34:11Yeah, we need a new stack pointer as
  896. 34:12well.
  897. 34:13Anything else we can think of?
  898. 34:16Running out of registers.
  899. 34:19Yeah, that's great. So the program
  900. 34:21status register cuz remember if you just
  901. 34:23did some instruction
  902. 34:25and you were counting on So you did some
  903. 34:26add as let's say and that updated it
  904. 34:29the status bit and then the next thing
  905. 34:31was like a comparison. Well, if your
  906. 34:34interrupt service routine goes and
  907. 34:35messes that up, then you're lost, right?
  908. 34:37So you need to save that piece of
  909. 34:39information as well. Yes?
  910. 34:48It replaces the polling issue. So, the
  911. 34:50problem with polling is that you make a
  912. 34:53trade-off. You poll, either can be very
  913. 34:55responsive,
  914. 34:57but you can't get any work done cuz all
  915. 34:59you're doing is spending all your time
  916. 35:01looking to see if someone pushed the
  917. 35:02button. But, if you have most, you know,
  918. 35:04processors or program going to have a
  919. 35:07couple of hundred, maybe a thousand
  920. 35:08lines of code or more, right? Well, then
  921. 35:10you have to just go through all that
  922. 35:12code before you can go look at that
  923. 35:13thing. So, you become unresponsive.
  924. 35:15So, an interrupt allows you to do this
  925. 35:17asynchronous thing. You just run your
  926. 35:19code, you calculate pi, you do whatever
  927. 35:20you're doing, and then when somebody
  928. 35:22presses the button, it's interrupts you.
  929. 35:26It's a good question.
  930. 35:28Yeah.
  931. 35:32Sorry, what?
  932. 35:35Yes.
  933. 35:40We just We just dump it. We assume we're
  934. 35:42just going to start losing our pipeline.
  935. 35:44Uh that's just built into the penalty
  936. 35:47that you'll see as uh going in and out
  937. 35:49of service routines.
  938. 35:51It's a good pipeline hit.
  939. 35:52>> [clears throat]
  940. 35:54>> All right, another question. Craig?
  941. 36:01>> [snorts]
  942. 36:09>> Uh yes, and the hardware is going to
  943. 36:11take care of that for you. You don't
  944. 36:13have to keep track of that.
  945. 36:18I think I have microphones. I'm going to
  946. 36:19have to give you guys microphones or
  947. 36:20something
  948. 36:21to uh
  949. 36:22get get hard of hearing.
  950. 36:24All right, what other questions we have?
  951. 36:27Guys are thinking ahead. It's great.
  952. 36:29>> [clears throat]
  953. 36:30>> All right, so let's go through a little
  954. 36:31bit more about how the stacking
  955. 36:33unstacking process happens. So, this is
  956. 36:35done in hardware.
  957. 36:37So, here are your your at your user
  958. 36:39program, you've written this program,
  959. 36:41and then some event occurs. So,
  960. 36:43uh this is time and then an interrupt
  961. 36:45signal happens, okay? And then what we
  962. 36:48do is we call it stacking. This is a
  963. 36:50hardware driven event or hardware
  964. 36:52initiated event where
  965. 36:56you need to go through and you take
  966. 36:58your active point and stack pointer and
  967. 37:00you go and you push all this information
  968. 37:02onto the stack.
  969. 37:04So, you're going to put your program
  970. 37:05status register, your PC, your link
  971. 37:07register. Wait.
  972. 37:09Oh, wait. We don't have to do stack
  973. 37:10pointer because stack pointer will as we
  974. 37:12leave will get returned back to its
  975. 37:13previous location. So, I was wrong about
  976. 37:15stack pointer.
  977. 37:16Um
  978. 37:17R12 R12 is because it's the inner
  979. 37:20procedural
  980. 37:21um
  981. 37:23uh register scratch space, so it's
  982. 37:24another scratch space that is available.
  983. 37:27So, we have to save that and then you
  984. 37:28have R0 through R
  985. 37:30uh three.
  986. 37:32So, then you go enter your handler.
  987. 37:36You at some point you're going to exit,
  988. 37:38right? This normal exit of your
  989. 37:39interrupt.
  990. 37:40>> [snorts]
  991. 37:40>> And then hardware is going to unstack
  992. 37:42this.
  993. 37:43Um or at least an automated process. You
  994. 37:45don't have to write the code. The and
  995. 37:47the
  996. 37:48uh action of branching
  997. 37:50uh will un
  998. 37:52uh stack everything.
  999. 37:54And then you will return the uh program
  1000. 37:57to its original state because all those
  1001. 37:59things will go back where they were. And
  1002. 38:01the part that I forgot is that the stack
  1003. 38:03pointer will also
  1004. 38:04uh go back to its original location
  1005. 38:06pointing at the original location. So,
  1006. 38:08that is uh preserved as well.
  1007. 38:13And so, this part uh is done
  1008. 38:15automatically. You don't actually have
  1009. 38:16to push or pop anything. That's done for
  1010. 38:19you.
  1011. 38:20And as you'll find out, this is actually
  1012. 38:22done in an interrupt service routine
  1013. 38:24itself. It's kind of this extra thing
  1014. 38:27that happens before and after uh your
  1015. 38:30code execution.
  1016. 38:33Yeah.
  1017. 38:33>> So, what's PSP
  1018. 38:36>> PSP. Oh, program stack.
  1019. 38:39Um
  1020. 38:39>> No, no. PC and PS.
  1021. 38:41>> Oh,
  1022. 38:42this is this is state of your uh
  1023. 38:44processor. It's the
  1024. 38:46I'm I'm
  1025. 38:46blanking on the the phrase. It is
  1026. 38:49Help me out somebody. Yeah, the process
  1027. 38:50stack pointer. No. No.
  1028. 38:55I don't know. What's it called? Help me
  1029. 38:57out. Help the old guy out.
  1030. 39:00>> [clears throat]
  1031. 39:00>> No, it's not program counter.
  1032. 39:02>> [snorts]
  1033. 39:02>> PSR
  1034. 39:03>> Yeah.
  1035. 39:04Program status register, thank you. So,
  1036. 39:06it's the basically the state of your ALU
  1037. 39:08flags and all those other things, right?
  1038. 39:10So, like that has to exist. It's
  1039. 39:12actually uh 32-bits and it holds some
  1040. 39:15other uh states that we don't talk
  1041. 39:17about. Uh your IT flags, your DE flags,
  1042. 39:20and then uh which interrupt you're in.
  1043. 39:22And we'll talk about That's important
  1044. 39:23cuz later on we're going to be doing
  1045. 39:24nested interrupts when an interrupt
  1046. 39:26interrupts an interrupt.
  1047. 39:27>> So, is that the
  1048. 39:29PSR or the
  1049. 39:32>> Oh, I see.
  1050. 39:34>> There's different versions of how to
  1051. 39:36write it. I will uh make sure the slides
  1052. 39:38are
  1053. 39:39>> So, it's the PSR. [snorts]
  1054. 39:45>> Sorry, what did you say
  1055. 39:46R0 was?
  1056. 39:48>> Ah, same reason that we uh had to
  1057. 39:51we the
  1058. 39:52calling function needs some scratch
  1059. 39:54space.
  1060. 39:55Right? And so, we're going to the
  1061. 39:58hardware's going to
  1062. 39:59preserve that those uh instruction those
  1063. 40:02registers because if main could not do
  1064. 40:04it. Main normally if you call a function
  1065. 40:06it's main's job to pass variables there,
  1066. 40:09and if you they want to keep it they're
  1067. 40:11going to save that put it on stack. But
  1068. 40:13main never called this. Something else
  1069. 40:14called it. This interrupt called it. So,
  1070. 40:16the interrupt itself is going to
  1071. 40:18uh or the the action of that is going to
  1072. 40:20save those registers so they don't get
  1073. 40:22corrupted by the interrupt service
  1074. 40:23routine.
  1075. 40:26No.
  1076. 40:26But if the just like with a normal ABI
  1077. 40:29compliant function, uh if the the
  1078. 40:32called function goes and
  1079. 40:34manipulates those registers, right? It's
  1080. 40:36responsible and it's responsibility is
  1081. 40:38to put them back. So, it's they're
  1082. 40:40caller safe.
  1083. 40:43Does that make sense?
  1084. 40:46What?
  1085. 40:47>> Wait, what is the ISR doing?
  1086. 40:49>> The ISR
  1087. 40:51So, there's two things. There's The ISR
  1088. 40:53is a function, but how you get to that
  1089. 40:55function is done through hardware.
  1090. 40:56>> Oh, okay. Okay.
  1091. 40:57>> Right? So, the hardware is doing this
  1092. 40:59task for you.
  1093. 41:01But, because the main engine know they
  1094. 41:04was about to get called, the main can't
  1095. 41:05go and save R0 through R3 for itself.
  1096. 41:09So, something else has to do that task.
  1097. 41:17What other questions you have? Clearly,
  1098. 41:18I Yeah.
  1099. 41:19>> I'm looking at the documentation for the
  1100. 41:22PSP.
  1101. 41:22>> Yeah.
  1102. 41:23>> It says like it's used for like when the
  1103. 41:26architecture is running in privileged
  1104. 41:27mode.
  1105. 41:29So, it's like a stack pointer for
  1106. 41:30>> [cough and clears throat]
  1107. 41:30>> processes.
  1108. 41:31>> That's true.
  1109. 41:32>> Running in privileged mode.
  1110. 41:33>> Uh
  1111. 41:35I'll have to go look at the look at
  1112. 41:37exactly what's on what's being slide,
  1113. 41:39but
  1114. 41:40uh you're right.
  1115. 41:41The PSP is uh for different process
  1116. 41:43mode. We don't really cover that in this
  1117. 41:45class though.
  1118. 41:48I mean, other than I mean, just to point
  1119. 41:50out to students that these other There's
  1120. 41:53shadow registers that you'll see in your
  1121. 41:54documentation for your program uh your
  1122. 41:57PSP, but uh that is not
  1123. 42:00I think maybe the other slide has an
  1124. 42:02error then. So, I'll go look that up and
  1125. 42:03report back next week.
  1126. 42:06Okay.
  1127. 42:09Uh what else?
  1128. 42:12Yep.
  1129. 42:13Uh there's different uh your program
  1130. 42:14stack pointer and your main stack
  1131. 42:15register have slightly different uh
  1132. 42:18connotations. Uh let me see if I can
  1133. 42:20find that slide. Well, that'll come up
  1134. 42:21in a second, but basically, they're
  1135. 42:23holding your state of your mode. Uh you
  1136. 42:25don't really doesn't really matter if
  1137. 42:27you're in for this class if you're in
  1138. 42:28handler mode or thread mode.
  1139. 42:30Um
  1140. 42:31we just I'm just putting this here for
  1141. 42:33for your reference.
  1142. 42:34Okay, great. And we're going to do that.
  1143. 42:36Um
  1144. 42:37okay.
  1145. 42:39So, depending on what mode you are in,
  1146. 42:42your return [snorts]
  1147. 42:43address is one of these uh different if
  1148. 42:46you're
  1149. 42:47floating point versus non-floating point
  1150. 42:49versus handler mode versus thread mode,
  1151. 42:51you have a different link register
  1152. 42:53address. This is when you're leaving
  1153. 42:55your ISR, where should you go?
  1154. 42:58So, in this class, we're going to go
  1155. 43:00Almost all the examples are going to go
  1156. 43:02to We're going to be in thread mode.
  1157. 43:03We're going to go back to this uh
  1158. 43:05address. And so, you notice that that's
  1159. 43:07really high up in the memory. So,
  1160. 43:09basically, that is the key to tell the
  1161. 43:12hardware, "Okay, now you're supposed to
  1162. 43:14unstack." The hardware is not keeping
  1163. 43:16track inherently if you are in an ISR or
  1164. 43:20not. Um but, the link register says,
  1165. 43:24"Okay, if you go back to that location,
  1166. 43:27if you put that into your PC, then you
  1167. 43:29are supposed to do the unstacking event,
  1168. 43:32right? Then you go and return the state
  1169. 43:34of the processor, and go back." That's
  1170. 43:36the That's the trick of where you're
  1171. 43:38supposed to essentially branch back
  1172. 43:40from.
  1173. 43:41So, let's go through an example here.
  1174. 43:44So, say we're stacking,
  1175. 43:46or sorry, we're uh we're in a normal
  1176. 43:48function.
  1177. 43:50Then, all of a sudden,
  1178. 43:51uh say your SysTick timer goes off. And
  1179. 43:54now, the program counter says that you
  1180. 43:56need to jump to a different um location.
  1181. 44:02So, here, we're going to assume that
  1182. 44:03we're in this mode.
  1183. 44:05And then, we're going to go and stack.
  1184. 44:07So, here, we're going to go and take So,
  1185. 44:10there I've corrected it.
  1186. 44:12Uh here it is the program status
  1187. 44:14register. That was a mistake on the
  1188. 44:15other slide. Uh so, here, we have to
  1189. 44:17save the program status register, uh the
  1190. 44:20PC, the link register, R12, and R0 R3.
  1191. 44:24Those get pushed up on the stack, so
  1192. 44:26that uh now the function that runs can
  1193. 44:29use those bottom uh four registers
  1194. 44:31however it wants. If it wanted to use
  1195. 44:33R4, then it would have to push that to
  1196. 44:35the stack. And the function would
  1197. 44:37Um and then, we start doing your
  1198. 44:39execution of your interrupt.
  1199. 44:43Uh when we're
  1200. 44:44uh done, uh we're going to You notice
  1201. 44:46here that the link register is that
  1202. 44:48specific value. And that's to know to
  1203. 44:50what should happen when it returns
  1204. 44:53uh back. So, here, we're going to go
  1205. 44:55through, we're going to do some tasks.
  1206. 44:57Oh, here we're going to use our uh we're
  1207. 44:59going to use our full uh oh Oh, I'm
  1208. 45:02showing the problem. Yeah, so here's R5
  1209. 45:04gets four gets updated. We're going to
  1210. 45:06branch back, and we're going to branch
  1211. 45:08back to that special address which
  1212. 45:09causes unstacking. And here is the
  1213. 45:11showing the error function. This is not
  1214. 45:14what's supposed to happen.
  1215. 45:15Didn't show on my shows on my screen.
  1216. 45:19How can you not see
  1217. 45:20How is it possible you see different
  1218. 45:21things than I do?
  1219. 45:30Oh, you missed all those good things I
  1220. 45:32was telling you then.
  1221. 45:33Uh
  1222. 45:41Okay.
  1223. 45:49Okay.
  1224. 45:51So, this is the screen's not updating.
  1225. 45:54>> [clears throat]
  1226. 46:40All right, we're back to a wired
  1227. 46:42scenario. All right, no more airplay.
  1228. 46:45Where were we? Okay. So, here we've
  1229. 46:47entered our instruction, our ISR
  1230. 46:50hardware did the stacking. Don't know
  1231. 46:52which guy it's last in the C. The
  1232. 46:55program stack
  1233. 46:57uh program encounter, or sorry, the
  1234. 46:58stack pointer gets updated that
  1235. 47:00location.
  1236. 47:01Uh, the link register gets updated with
  1237. 47:03that special,
  1238. 47:05uh, return address, which signals that
  1239. 47:07unstacking should occur. And then your
  1240. 47:09program, your PC points to the address
  1241. 47:13of this new,
  1242. 47:14uh, of the interrupt service routine.
  1243. 47:16And it's a routine because this is what,
  1244. 47:19uh, is going to run. And so, an
  1245. 47:20interrupt request causes the NVIC to
  1246. 47:24look up the location of the interrupt
  1247. 47:26service routine, and then the NVIC will
  1248. 47:28cause all of this to happen. It will
  1249. 47:30initiate all this. It will enter your
  1250. 47:32interrupt service routine, which we are
  1251. 47:34just about to do, and then we'll do this
  1252. 47:35task. And we'll see that R4 is going to,
  1253. 47:38uh, identify the error that's going to
  1254. 47:40occur.
  1255. 47:43Any questions on the setup so far?
  1256. 47:48Yeah.
  1257. 47:50>> Uh,
  1258. 47:51what are the two modes?
  1259. 47:52I don't know.
  1260. 47:53>> Sorry.
  1261. 47:53>> What are the two modes again?
  1262. 47:56>> It's less important.
  1263. 47:57It doesn't really matter for how we're
  1264. 47:59using this class. It's more like, uh,
  1265. 48:01smart students at this university who,
  1266. 48:04uh, know more than I do. I was looking
  1267. 48:06at the documentation more recently. Um,
  1268. 48:08it'll trip you up. So, you can look
  1269. 48:09through the slides and be like, "Okay,
  1270. 48:10why is there this freaking normal and
  1271. 48:12this normal mode? And what is the
  1272. 48:13difference?" And it's not really that
  1273. 48:15important.
  1274. 48:15>> [snorts]
  1275. 48:16>> Um, it really is The reason it comes up
  1276. 48:18is because you have to know which mode
  1277. 48:19you are in to know what your, uh, link
  1278. 48:22register return path is.
  1279. 48:24It's a good question. Wait, I saw some
  1280. 48:26other hands.
  1281. 48:31Yeah. You have to be very loud though.
  1282. 48:34>> How come you didn't see the
  1283. 48:36R0 and R1?
  1284. 48:38>> Why How come we didn't see that?
  1285. 48:40Uh, arbitrary reasons. It's completely
  1286. 48:42arbitrary reasons. But, it's just like
  1287. 48:45with the ABI from times, uh, thing. So,
  1288. 48:47the
  1289. 48:48ABI decided
  1290. 48:50that R0 through R3 are just ones that
  1291. 48:52are going to be saved for, um,
  1292. 48:54ABI call and return for this. So, this
  1293. 48:56is just trying to mirror what the ABI
  1294. 48:58normally does.
  1295. 49:00Does that make sense?
  1296. 49:04So, you could save everything,
  1297. 49:06but then that would be wasteful. You
  1298. 49:08have to actually function You have to do
  1299. 49:09extra slots and storage for R
  1300. 49:13uh the other the other things, right? R4
  1301. 49:15through R11. So, we're just not going to
  1302. 49:17This is from a hardware standpoint here.
  1303. 49:18I'm just going to show that the error
  1304. 49:20that occurs We're just showing you some
  1305. 49:21of this stuff. They're not loading up
  1306. 49:23the hardware.
  1307. 49:25>> Is it guaranteed that the rest of the
  1308. 49:27registers uh R4 through R11 say are
  1309. 49:31going to be preserved in the interrupt
  1310. 49:33call or is it just like
  1311. 49:34>> The programmer of that interrupt call is
  1312. 49:37responsible for saving those registers.
  1313. 49:39Just as a uh pro as the function
  1314. 49:42programmer or sorry, person who writes
  1315. 49:44the function would be responsible for
  1316. 49:45saving those. So, the same rules apply
  1317. 49:48when you're writing interrupts as you
  1318. 49:49would in main functions.
  1319. 50:00Ah, that's a good question.
  1320. 50:03So, normally what happens, right? You
  1321. 50:04put that link register and then it tells
  1322. 50:06you how to get back.
  1323. 50:07But, we don't just want to get back to
  1324. 50:10the where we were in the program.
  1325. 50:12That's not enough because
  1326. 50:15that would mean that say that program
  1327. 50:17status register too could be corrupted.
  1328. 50:19It could be changed. So, we need some
  1329. 50:21way to say, "Hey,
  1330. 50:23take the program status register that's
  1331. 50:25on the stack and put it back."
  1332. 50:27I need some way to say, "Hey, R0 through
  1333. 50:29R3 need to go back and R12 needs to go
  1334. 50:32back." And so, rather than burdening you
  1335. 50:35and making you as the person who has to
  1336. 50:36write the interrupt service routine do
  1337. 50:38that every single time, right? you don't
  1338. 50:40want to have to write that every time.
  1339. 50:43What happens is this piece of that If
  1340. 50:45you branch back to that particular
  1341. 50:48location, it causes the hardware to
  1342. 50:50unstack. So, now the [clears throat]
  1343. 50:52hardware's going to do all that stuff
  1344. 50:54for you. And really, it's not
  1345. 50:56hardware or is it? It's just another
  1346. 50:58little
  1347. 50:59uh piece of code that says, "Do this
  1348. 51:01task." But, it's automatically done for
  1349. 51:03you. That much.
  1350. 51:22Which slide, Emma?
  1351. 51:29Sorry, I really can't hear anything.
  1352. 51:43This slide?
  1353. 51:44No.
  1354. 51:4639. Thank you. Thank you.
  1355. 51:58Yeah.
  1356. 52:06But, she's showing that this is the
  1357. 52:07normal factor of the heater.
  1358. 52:09The red is the stack pointer area, so
  1359. 52:10it's loaded down.
  1360. 52:11And, this is showing you all the steps.
  1361. 52:13There's nothing new here. It's nothing.
  1362. 52:19Correct.
  1363. 52:20Because it's it's just an automated
  1364. 52:21process. When I say hardware is doing
  1365. 52:23it, it means that it's a set it's a set
  1366. 52:26function. It always stacks the same
  1367. 52:28things. It always unstacks the same
  1368. 52:31things. It always does it in the same
  1369. 52:32order. That just makes it so that you as
  1370. 52:35a programmer
  1371. 52:37of the interrupt service routine don't
  1372. 52:38have to unstack anything, and there's
  1373. 52:41nothing that has to happen when you
  1374. 52:42start. You don't have to program
  1375. 52:43anything when you
  1376. 52:45get an interrupt. It's done for you.
  1377. 53:01All right. So, now we're going to go
  1378. 53:03through, and in this example, we are you
  1379. 53:06know the the you have a misbehaving
  1380. 53:09interrupt service routine. So, sure it
  1381. 53:12can use R3, no problem. R3 has already
  1382. 53:15been preserved on the stack. So, it is
  1383. 53:19scratch space
  1384. 53:20for the interrupt service routine. But,
  1385. 53:23R4 is not and that should have been. If
  1386. 53:25you needed to use that space, it should
  1387. 53:26have been put up on the stack. So, do
  1388. 53:28nothing in here. Typically, you want to
  1389. 53:30keep your IRQs short. You don't want to
  1390. 53:32have a thousand lines in your IRQ just
  1391. 53:35because you are blocking the progress of
  1392. 53:38main. So, typically a strategy would be
  1393. 53:42if there's a small task [clears throat]
  1394. 53:43to do like take data from one location
  1395. 53:45and put it in somewhere else, do that.
  1396. 53:47Or maybe you're just going to set a flag
  1397. 53:49that says, "Hey,
  1398. 53:51this event occurred. Next time I go and
  1399. 53:53through main, go and process that flag."
  1400. 53:56So, normally you don't want to do a lot
  1401. 53:57here, but certainly there's enough time
  1402. 53:59to go and
  1403. 54:00push something to the stack if you
  1404. 54:02needed the extra space card board. But,
  1405. 54:04this example did not do that.
  1406. 54:09Okay?
  1407. 54:10And so now we're going to branch. Where
  1408. 54:12are we going to branch to? We're going
  1409. 54:13to branch to special location.
  1410. 54:15That special location is going to cause
  1411. 54:17the unstacking event to occur. So, now
  1412. 54:19[snorts] we're going to unstack all
  1413. 54:21those things. Essentially, we're going
  1414. 54:23to place them. Oops.
  1415. 54:26Come back.
  1416. 54:28We're going to take all those things and
  1417. 54:29put them back onto your registers here
  1418. 54:32including the program status register
  1419. 54:34going to be updated to this value.
  1420. 54:36So, here we go. All those things get
  1421. 54:38updated and because
  1422. 54:41it's just a normal pop or yeah, pop
  1423. 54:44operation, your stack pointer goes back
  1424. 54:46to where it was. So, stack pointer is
  1425. 54:48not apparently needed to be saved on the
  1426. 54:50stack. It just will go back to where it
  1427. 54:52was. And then the problem here is
  1428. 54:54because this is a misbehaving function,
  1429. 54:56now R4 has been updated and it should
  1430. 54:58not have been updated.
  1431. 55:03What questions do we have?
  1432. 55:06Yeah.
  1433. 55:06>> Okay, so the stacking process is
  1434. 55:09always saved
  1435. 55:10>> people are better than
  1436. 55:11hacking auto violence and they are
  1437. 55:13better than hacking.
  1438. 55:14>> [snorts]
  1439. 55:14>> Right. And you don't
  1440. 55:15If you don't
  1441. 55:16>> [clears throat]
  1442. 55:16>> I just like um
  1443. 55:18If you're running a a normal function,
  1444. 55:21you are uh not allowed to manipulate our
  1445. 55:24core for a while.
  1446. 55:26And so,
  1447. 55:27uh without
  1448. 55:28uh returning back how it's supposed to
  1449. 55:29be, just as the same thing in our
  1450. 55:32service return to the client.
  1451. 55:33>> I see. So, this type of challenge is to
  1452. 55:35save my core.
  1453. 55:37>> Yep.
  1454. 55:37>> All right. Yes.
  1455. 55:48>> Okay. So,
  1456. 55:52I actually went through that way faster
  1457. 55:54than normal. Okay. So, uh so, recap. So,
  1458. 55:57uh we talked a lot about polling versus
  1459. 55:59uh interrupts. So, remember polling is
  1460. 56:02has the time trade-off penalty. Where
  1461. 56:06how much work you can do do in main, uh
  1462. 56:09the more work you do there, the less uh
  1463. 56:11reactive you will come to external
  1464. 56:13events.
  1465. 56:14Uh interrupt [clears throat] is just a
  1466. 56:15method for allowing those signals to get
  1467. 56:17into the core.
  1468. 56:19Uh your peripherals
  1469. 56:20will be configured to uh initiate
  1470. 56:24interrupt uh service requests. And that
  1471. 56:27is not a lot of data. It's not like it
  1472. 56:29tells you tells the Nvic what happened.
  1473. 56:32It's just a line, literally a wire that
  1474. 56:34goes high. It gets interpreted by the
  1475. 56:36Nvic, and the Nvic has to make a
  1476. 56:37decision about what to do. And so, uh
  1477. 56:41I think on your device you have about 96
  1478. 56:44different interrupts that could occur.
  1479. 56:46And so, that means there's 96 wires.
  1480. 56:49Your Nvic could handle up to 256. It's
  1481. 56:51just a lot of overhead. And so, there's
  1482. 56:52a trade-off your the the vendor has to
  1483. 56:55make saying the arm could do 256.
  1484. 56:58However, that's a lot of overhead, so
  1485. 57:00we'll just do the the more than minimum
  1486. 57:02necessary for the job. And so, there's
  1487. 57:05about 96 different interrupts
  1488. 57:07uh that are available on your device.
  1489. 57:10Um so when that interrupt gets called
  1490. 57:13or that line gets raised, it's the
  1491. 57:15NVIC's job to go look up, okay, so the
  1492. 57:18nth
  1493. 57:19uh interrupt line went high, it needs to
  1494. 57:21go do the subtraction off of 16 and
  1495. 57:23figure out where in the lookup table the
  1496. 57:27address is for the service routine.
  1497. 57:30And the and the
  1498. 57:32uh
  1499. 57:33uh lookup table is just the bottom bits
  1500. 57:36of memory is starting at location two in
  1501. 57:38memory and then moving up. So it's not
  1502. 57:41like a special table, it's just memory.
  1503. 57:44Uh yeah, there we go. Um so then then
  1504. 57:47when that occurs, you have stacking so
  1505. 57:48that the um
  1506. 57:51uh
  1507. 57:51the points of the table points to where
  1508. 57:54the program counter needs to go for the
  1509. 57:57inner service routine and that's
  1510. 57:59somewhere in memory.
  1511. 58:01And then hardware takes over. Hardware
  1512. 58:03does the stacking.
  1513. 58:05Um it uh will save the state of the
  1514. 58:07register. It will then go and allow uh
  1515. 58:10the inner service request to go. Uh
  1516. 58:13you'll process that and then you'll
  1517. 58:14branch back that special address. That
  1518. 58:16special linkback address simply states,
  1519. 58:19do unstacking for me. So if you did, you
  1520. 58:22could do bad things. You could do a bad
  1521. 58:23thing like just um manually branch back
  1522. 58:27to that address from like a normal
  1523. 58:29function call or just in main and your
  1524. 58:32processor will go wild, right? You'll
  1525. 58:33take whatever is in the stack and you'll
  1526. 58:35dump it into your processor and see what
  1527. 58:36happens. Shouldn't do that, but it's no
  1528. 58:39more magical than just um
  1529. 58:42uh branching to a particular location.
  1530. 58:46All right.
  1531. 58:47Uh and then when you on yeah, when you
  1532. 58:49unstack the processor just goes back to
  1533. 58:50its normal operation. And so next week
  1534. 58:53we'll talk about So tomorrow or
  1535. 58:55Wednesday's class will be exam review
  1536. 58:57and then next week we'll talk about the
  1537. 58:59details of the
  1538. 59:00>> [clears throat]
  1539. 59:00>> uh nested vector interrupt controller.
  1540. 59:02And really what we're going to be
  1541. 59:03talking about is what happens we have
  1542. 59:06multiple interrupts occurring cuz and in
  1543. 59:09real embedded systems you'll have, you
  1544. 59:11know, 20 interrupts going off or
  1545. 59:13configured. And sometimes interrupts
  1546. 59:15will interrupt interrupts. And you have
  1547. 59:18to figure out which one is the important
  1548. 59:19interrupt, which one is will keep the
  1549. 59:20plane from crashing, and which one is
  1550. 59:22just trying to figure out what the
  1551. 59:23volume button has been pressed up and
  1552. 59:24down.
  1553. 59:26All right.
  1554. 59:28Yeah. And so that will end class early
  1555. 59:31and we'll have start office hours early
  1556. 59:33as well.

About this transcript

This page contains the full transcript of EECS 373 - Fall 2026 - Lecture 6: “Interrupts” by Interactive Sensing and Computing Lab , generated from the public captions YouTube serves with the video. The transcript has 9,273 words across 1,556 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.