-
Notifications
You must be signed in to change notification settings - Fork 2
/
mission0_mission_selector.zig
182 lines (159 loc) · 5.25 KB
/
mission0_mission_selector.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
export fn mission0_main() noreturn {
Bss.prepare();
Exceptions.prepare();
Mission.prepare();
Uart.prepare();
Timer0.prepare();
Timer1.prepare();
Timer2.prepare();
LedMatrix.prepare();
CycleActivity.prepare();
KeyboardActivity.prepare();
StatusActivity.prepare();
Mission.register(&mission1_vector_table, "turn on all leds without libraries", "mission1_turn_on_all_leds_without_libraries.zig");
Mission.register(&mission2_vector_table, "model railroad motor pwm controlled by buttons", "mission2_model_railroad_pwm.zig");
Mission.register(&mission3_vector_table, "sensors - temperature, orientation", "mission3_sensors.zig");
log("available missions:", .{});
for (Mission.missions) |*m, i| {
log("{}. {}", .{ i + 1, m.title });
}
while (true) {
CycleActivity.update();
KeyboardActivity.update();
StatusActivity.update();
}
}
const CycleActivity = struct {
var cycle_counter: u32 = undefined;
var cycle_time: u32 = undefined;
var last_cycle_start: ?u32 = undefined;
var last_second_ticks: u32 = undefined;
var max_cycle_time: u32 = undefined;
var up_time_seconds: u32 = undefined;
fn prepare() void {
cycle_counter = 0;
cycle_time = 0;
last_cycle_start = null;
last_second_ticks = 0;
max_cycle_time = 0;
up_time_seconds = 0;
}
fn update() void {
LedMatrix.update();
cycle_counter += 1;
const new_cycle_start = Timer0.capture();
if (new_cycle_start -% last_second_ticks >= 1000 * 1000) {
up_time_seconds += 1;
last_second_ticks = new_cycle_start;
}
if (last_cycle_start) |start| {
cycle_time = new_cycle_start -% start;
max_cycle_time = math.max(cycle_time, max_cycle_time);
}
last_cycle_start = new_cycle_start;
}
};
const KeyboardActivity = struct {
var column: u32 = undefined;
fn prepare() void {
column = 1;
}
fn update() void {
if (!Uart.isReadByteReady()) {
return;
}
const byte = Uart.readByte();
switch (byte) {
3 => {
SystemControlBlock.requestSystemReset();
},
12 => {
StatusActivity.redraw();
},
27 => {
Uart.writeByteBlocking('$');
column += 1;
},
'1', '2', '3', '4', '5', '6', '7', '8', '9' => {
Mission.missions[byte - '1'].activate();
},
'\r' => {
Uart.writeText("\n");
column = 1;
},
else => {
Uart.writeByteBlocking(byte);
column += 1;
},
}
}
};
const StatusActivity = struct {
var prev_now: u32 = undefined;
fn prepare() void {
prev_now = CycleActivity.up_time_seconds;
redraw();
}
fn redraw() void {
Terminal.clearScreen();
Terminal.setScrollingRegion(5, 99);
Terminal.move(5 - 1, 1);
log("keyboard input will be echoed below:", .{});
}
fn update() void {
Uart.loadTxd();
const now = CycleActivity.up_time_seconds;
if (now >= prev_now + 1) {
Terminal.hideCursor();
Terminal.move(1, 1);
Terminal.line("reset {x} up {:3}s cycle {}us max {}us", .{ Power.registers.reset_reason, CycleActivity.up_time_seconds, CycleActivity.cycle_time, CycleActivity.max_cycle_time });
Terminal.line("gpio.in {x:8}", .{Gpio.registers.in & ~@as(u32, 0x0300fff0)});
Terminal.line("", .{});
Terminal.showCursor();
restoreInputLine();
prev_now = now;
}
}
};
fn restoreInputLine() void {
Terminal.move(99, KeyboardActivity.column);
}
const Mission = struct {
title: []const u8,
panic: fn ([]const u8, ?*builtin.StackTrace) noreturn,
vector_table_address: *allowzero u32,
var missions: []Mission = undefined;
var missions_buf: [5]Mission = undefined;
fn activate(self: *Mission) void {
const reset_sp = @intToPtr(*allowzero u32, @ptrToInt(self.vector_table_address) + 0).*;
const reset_pc = @intToPtr(*allowzero u32, @ptrToInt(self.vector_table_address) + 4).*;
asm volatile (
\\ mov sp,%[reset_sp]
\\ bx %[reset_pc]
:
: [reset_pc] "{r0}" (reset_pc),
[reset_sp] "{r1}" (reset_sp)
);
}
fn prepare() void {
missions = missions_buf[0..0];
}
fn register(vector_table_address: *allowzero u32, comptime title: []const u8, comptime source_file: []const u8) void {
missions = missions_buf[0 .. missions.len + 1];
var m = &missions[missions.len - 1];
const import = @import(source_file);
m.title = title;
m.panic = import.panic;
m.vector_table_address = vector_table_address;
}
};
comptime {
const mission_id = 0;
asm (typicalVectorTable(mission_id));
}
const release_tag = "0.4";
const status_display_lines = 6 + 5;
extern var mission1_vector_table: u32;
extern var mission2_vector_table: u32;
extern var mission3_vector_table: u32;
usingnamespace @import("lib_basics.zig").typical;