-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmission3_sensors.zig
180 lines (164 loc) · 5.71 KB
/
mission3_sensors.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
export fn mission3_main() noreturn {
Bss.prepare();
Exceptions.prepare();
Uart.prepare();
Timer0.prepare();
Timer1.prepare();
Timer2.prepare();
LedMatrix.prepare();
ClockManagement.prepareHf();
CycleActivity.prepare();
TerminalActivity.prepare();
I2c0.prepare();
Accel.prepare();
while (true) {
CycleActivity.update();
TerminalActivity.update();
}
}
const Accel = struct {
fn prepare() void {
var data_buf: [0x32]u8 = undefined;
data_buf[orientation_configuration_register] = orientation_configuration_register_mask_enable;
I2c0.writeBlockingPanic(device_address, &data_buf, orientation_configuration_register, orientation_configuration_register);
data_buf[control_register1] = control_register1_mask_active;
I2c0.writeBlockingPanic(device_address, &data_buf, control_register1, control_register1);
}
fn update() void {
var data_buf: [32]u8 = undefined;
I2c0.readBlockingPanic(device_address, &data_buf, orientation_register, orientation_register);
const orientation = data_buf[orientation_register];
if (orientation & orientation_register_mask_changed != 0) {
format("orientation: 0x{x} ", .{orientation});
if (orientation & orientation_register_mask_forward_backward != 0) {
format("forward ", .{});
} else {
format("backward ", .{});
}
if (orientation & orientation_register_mask_z_lock_out != 0) {
log("up/down/left/right is unknown", .{});
} else {
const direction = (orientation & orientation_register_mask_direction) >> @ctz(u5, orientation_register_mask_direction);
switch (direction) {
0 => {
log("up", .{});
},
1 => {
log("down", .{});
},
2 => {
log("right", .{});
},
3 => {
log("left", .{});
},
else => {
unreachable;
},
}
}
}
}
const control_register1 = 0x2a;
const control_register1_mask_active = 0x01;
const device_address = 0x1d;
const orientation_register = 0x10;
const orientation_register_mask_changed = 0x80;
const orientation_register_mask_direction = 0x06;
const orientation_register_mask_forward_backward = 0x01;
const orientation_register_mask_z_lock_out = 0x40;
const orientation_configuration_register = 0x11;
const orientation_configuration_register_mask_enable = 0x40;
};
const CycleActivity = struct {
var cycle_counter: u32 = undefined;
var cycle_time: u32 = undefined;
var last_cycle_start: ?u32 = undefined;
var max_cycle_time: u32 = undefined;
var up_time_seconds: u32 = undefined;
var up_timer: TimeKeeper = undefined;
fn prepare() void {
cycle_counter = 0;
cycle_time = 0;
last_cycle_start = null;
max_cycle_time = 0;
up_time_seconds = 0;
up_timer.prepare(1000 * 1000);
}
fn update() void {
cycle_counter += 1;
const new_cycle_start = Timer0.capture();
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;
if (up_timer.isFinished()) {
up_timer.reset();
up_time_seconds += 1;
Accel.update();
}
}
};
const TerminalActivity = struct {
var keyboard_column: u32 = undefined;
var prev_now: u32 = undefined;
var temperature: u32 = 0;
fn prepare() void {
keyboard_column = 1;
prev_now = CycleActivity.up_time_seconds;
Temperature.tasks.start = 1;
redraw();
}
fn redraw() void {
Terminal.clearScreen();
Terminal.setScrollingRegion(5, 99);
Terminal.move(5 - 1, 1);
log("keyboard input will be echoed below:", .{});
Terminal.move(99, keyboard_column);
}
fn update() void {
if (Uart.isReadByteReady()) {
const byte = Uart.readByte();
switch (byte) {
3 => {
SystemControlBlock.requestSystemReset();
},
12 => {
redraw();
},
27 => {
Uart.writeByteBlocking('$');
keyboard_column += 1;
},
'\r' => {
Uart.writeText("\n");
keyboard_column = 1;
},
else => {
Uart.writeByteBlocking(byte);
keyboard_column += 1;
},
}
}
Uart.update();
if (Temperature.events.data_ready != 0) {
Temperature.events.data_ready = 0;
temperature = Temperature.registers.temperature;
}
const now = CycleActivity.up_time_seconds;
if (now >= prev_now + 1) {
Terminal.hideCursor();
Terminal.move(1, 1);
Terminal.line("up {:3}s cycle {}us max {}us {}.{}C", .{ CycleActivity.up_time_seconds, CycleActivity.cycle_time, CycleActivity.max_cycle_time, temperature / 4, temperature % 4 * 25 });
Terminal.showCursor();
Terminal.move(99, keyboard_column);
prev_now = now;
}
}
};
comptime {
const mission_id = 3;
asm (typicalVectorTable(mission_id));
}
usingnamespace @import("lib_basics.zig").typical;