Skip to content

Commit cad2da6

Browse files
zelenskiCS107E BOT
authored andcommitted
Interrupts1 lecture
commit 2ae5f12d27c6715406f88f1c67cfaace06c142ba Author: Julie Zelenski <[email protected]> Date: Fri Nov 8 10:05:47 2024 -0800 Interrupts1 lecture
1 parent b2d9cd1 commit cad2da6

File tree

11 files changed

+252
-1
lines changed

11 files changed

+252
-1
lines changed

_data/unreleased.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ permalink,title,released
77
"/lectures/FloatingPoint_Sound/","Floating Point and Sound",false
88
"/lectures/Guest/","Guest",false
99
"/lectures/InfoSession/","Course Info Session",false
10-
"/lectures/Interrupts1/","Interrupts",false
1110
"/lectures/Interrupts2/","Interrupts, cont'd",false
1211
"/lectures/OOP/","Object-oriented programming",false
1312
"/lectures/Project/","Project",false

lectures/Interrupts1/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
released: true
3+
permalink: /lectures/Interrupts1/
4+
title: Interrupts
5+
readings: |
6+
In this first lecture on interrupts, we'll focus on the low-level mechanics and what needs to happen at the assembly level to enable and process an interrupt.
7+
+ [RiscV ISA Manual Vol II](/readings/riscv-privileged-20190608-1.pdf)
8+
---
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
NAME = main
2+
3+
ARCH = -march=rv64im -mabi=lp64
4+
ASFLAGS = $(ARCH)
5+
CFLAGS = $(ARCH) -g -Og -I$$CS107E/include -Wall -ffreestanding -fno-omit-frame-pointer
6+
LDFLAGS = -nostdlib -L$$CS107E/lib -T memmap.ld
7+
LDLIBS = -lmango -lmango_gcc
8+
9+
all : $(NAME).bin
10+
11+
%.bin: %.elf
12+
riscv64-unknown-elf-objcopy $< -O binary $@
13+
14+
%.elf: %.o
15+
riscv64-unknown-elf-gcc $(LDFLAGS) $^ $(LDLIBS) -o $@
16+
17+
%.o: %.c
18+
riscv64-unknown-elf-gcc $(CFLAGS) -c $< -o $@
19+
20+
%.o: %.s
21+
riscv64-unknown-elf-as $(ASFLAGS) $< -o $@
22+
23+
run: $(NAME).bin
24+
mango-run $<
25+
26+
clean:
27+
rm -f *.o *.bin *.elf *.list *~
28+
29+
.PHONY: all clean run
30+
.PRECIOUS: %.elf %.o
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "printf.h"
2+
3+
// Cheezy way to echo console to uart
4+
static int both_printf(const char *format, ...)
5+
{
6+
char buf[1000];
7+
8+
va_list args;
9+
va_start(args, format);
10+
vsnprintf(buf, sizeof(buf), format, args);
11+
va_end(args);
12+
console_printf(buf);
13+
return printf(buf);
14+
}
15+
16+
#define console_printf both_printf
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "console.h"
2+
#include "echo.h"
3+
#include "gpio.h"
4+
#include "gpio_extra.h"
5+
#include "uart.h"
6+
7+
static int gCount = 0;
8+
9+
static void update_screen(void) {
10+
console_clear();
11+
console_printf("\nCount of button presses\n");
12+
for (int r = 0; r < 80; r++) {
13+
console_printf("%4d ", gCount);
14+
}
15+
console_printf("\n");
16+
}
17+
18+
static void wait_for_press(gpio_id_t button) {
19+
while (gpio_read(button) != 1) ; // loop until becomes high
20+
while (gpio_read(button) == 1) ; // loop while stays high
21+
gCount++;
22+
}
23+
24+
void main(void) {
25+
gpio_id_t button = GPIO_PB4;
26+
27+
uart_init();
28+
gpio_set_input(button);
29+
gpio_set_pullup(button);
30+
console_init(24, 50, GL_AMBER, 0xff222222);
31+
32+
while (1) {
33+
console_printf("\nWaiting for button click...\n");
34+
wait_for_press(button);
35+
update_screen();
36+
}
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
NAME = main
2+
3+
ARCH = -march=rv64im -mabi=lp64
4+
ASFLAGS = $(ARCH)
5+
CFLAGS = $(ARCH) -g -Og -I$$CS107E/include -Wall -ffreestanding -fno-omit-frame-pointer
6+
LDFLAGS = -nostdlib -L$$CS107E/lib -T memmap.ld
7+
LDLIBS = -lmango -lmango_gcc
8+
9+
all : $(NAME).bin
10+
11+
%.bin: %.elf
12+
riscv64-unknown-elf-objcopy $< -O binary $@
13+
14+
%.elf: %.o
15+
riscv64-unknown-elf-gcc $(LDFLAGS) $^ $(LDLIBS) -o $@
16+
17+
%.o: %.c
18+
riscv64-unknown-elf-gcc $(CFLAGS) -c $< -o $@
19+
20+
%.o: %.s
21+
riscv64-unknown-elf-as $(ASFLAGS) $< -o $@
22+
23+
run: $(NAME).bin
24+
mango-run $<
25+
26+
clean:
27+
rm -f *.o *.bin *.elf *.list *~
28+
29+
.PHONY: all clean run
30+
.PRECIOUS: %.elf %.o
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "printf.h"
2+
3+
// Cheezy way to echo console to uart
4+
static int both_printf(const char *format, ...)
5+
{
6+
char buf[1000];
7+
8+
va_list args;
9+
va_start(args, format);
10+
vsnprintf(buf, sizeof(buf), format, args);
11+
va_end(args);
12+
console_printf(buf);
13+
return printf(buf);
14+
}
15+
16+
#define console_printf both_printf
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "backtrace.h"
2+
#include "console.h"
3+
#include "echo.h"
4+
#include "gpio.h"
5+
#include "gpio_extra.h"
6+
#include "gpio_interrupt.h"
7+
#include "uart.h"
8+
9+
static volatile int gCount = 0;
10+
11+
static void update_screen(void) {
12+
console_clear();
13+
console_printf("\nCount of button presses\n");
14+
for (int r = 0; r < 80; r++) {
15+
console_printf("%4d ", gCount);
16+
}
17+
console_printf("\n");
18+
}
19+
20+
static void button_pressed(void *aux_data) {
21+
gCount++;
22+
gpio_id_t button = *(gpio_id_t *)aux_data;
23+
gpio_interrupt_clear(button);
24+
}
25+
26+
void main(void) {
27+
gpio_id_t button = GPIO_PB4;
28+
uart_init();
29+
gpio_set_input(button);
30+
gpio_set_pullup(button);
31+
32+
interrupts_init();
33+
gpio_interrupt_init();
34+
gpio_interrupt_config(button, GPIO_INTERRUPT_NEGATIVE_EDGE, false);
35+
gpio_interrupt_register_handler(button, button_pressed, &button);
36+
gpio_interrupt_enable(button);
37+
interrupts_global_enable();
38+
39+
console_init(24, 50, GL_AMBER, 0xff222222);
40+
console_printf("\nWaiting for button click...\n");
41+
42+
int last_count = gCount;
43+
while (1) {
44+
if (last_count != gCount) {
45+
update_screen();
46+
last_count = gCount;
47+
console_printf("\nWaiting for button click...\n");
48+
}
49+
}
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
NAME = console_main
2+
3+
ARCH = -march=rv64im -mabi=lp64
4+
ASFLAGS = $(ARCH)
5+
CFLAGS = $(ARCH) -g -Og -I$$CS107E/include -Wall -ffreestanding -fno-omit-frame-pointer
6+
LDFLAGS = -nostdlib -L$$CS107E/lib -T memmap.ld
7+
LDLIBS = -lmango -lmango_gcc
8+
9+
all : $(NAME).bin
10+
11+
%.bin: %.elf
12+
riscv64-unknown-elf-objcopy $< -O binary $@
13+
14+
%.elf: %.o
15+
riscv64-unknown-elf-gcc $(LDFLAGS) $^ $(LDLIBS) -o $@
16+
17+
%.o: %.c
18+
riscv64-unknown-elf-gcc $(CFLAGS) -c $< -o $@
19+
20+
%.o: %.s
21+
riscv64-unknown-elf-as $(ASFLAGS) $< -o $@
22+
23+
run: $(NAME).bin
24+
mango-run $<
25+
26+
clean:
27+
rm -f *.o *.bin *.elf *.list *~
28+
29+
.PHONY: all clean run
30+
.PRECIOUS: %.elf %.o
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "console.h"
2+
#include "gpio.h"
3+
#include "interrupts.h"
4+
#include "keyboard.h"
5+
#include "shell.h"
6+
#include "uart.h"
7+
8+
extern void shell_set_extensions_enabled(bool); // extra features of ref library
9+
extern void console_enable_blink_cursor(void);
10+
11+
/* This program tests the keyboard using the console.
12+
* Console reads from the keyboard and displays on screen.
13+
* If the keyboard is not using interrupts, it drops events
14+
* because the screen refresh is so time consuming.
15+
*/
16+
17+
#define USE_INTERRUPTS 0
18+
19+
void main(void) {
20+
gpio_init();
21+
uart_init();
22+
keyboard_init(KEYBOARD_CLOCK, KEYBOARD_DATA);
23+
console_init(30, 80, GL_AMBER, GL_BLACK);
24+
shell_set_extensions_enabled(true);
25+
shell_init(keyboard_read_next, console_printf);
26+
27+
#if USE_INTERRUPTS
28+
interrupts_init();
29+
keyboard_use_interrupts();
30+
console_enable_blink_cursor();
31+
interrupts_global_enable();
32+
#endif
33+
34+
shell_run();
35+
}

0 commit comments

Comments
 (0)