-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib_basics.zig
1010 lines (868 loc) · 31.4 KB
/
lib_basics.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pub const lib = struct {
pub const Bss = struct {
pub fn prepare() void {
@memset(@ptrCast([*]u8, &__bss_start), 0, @ptrToInt(&__bss_end) - @ptrToInt(&__bss_start));
}
};
pub const Adc = struct {
pub const busy_registers = mmio(0x40007400, extern struct {
busy: u32,
});
pub const events = mmio(0x40007100, extern struct {
end: u32,
});
pub const registers = mmio(0x40007500, extern struct {
enable: u32,
config: u32,
result: u32,
});
pub const registers_config_masks = struct {
pub const extrefsel = 0x30000;
pub const inpsel = 0x001c;
pub const psel = 0xff00;
pub const refsel = 0x0060;
pub const resolution = 0x00003;
};
pub const tasks = mmio(0x40007000, extern struct {
start: u32,
stop: u32,
});
};
pub const ClockManagement = struct {
pub fn prepareHf() void {
crystal_registers.frequency_selector = 0xff;
tasks.start_hf_clock = 1;
while (events.hf_clock_started == 0) {}
}
pub const events = mmio(0x40000100, extern struct {
hf_clock_started: u32,
lf_clock_started: u32,
});
pub const crystal_registers = mmio(0x40000550, extern struct {
frequency_selector: u32,
});
pub const tasks = mmio(0x40000000, extern struct {
start_hf_clock: u32,
stop_hf_clock: u32,
start_lf_clock: u32,
stop_lf_clock: u32,
});
};
pub const Exceptions = struct {
var already_panicking: bool = undefined;
var panic_handler: ?fn (message: []const u8, trace: ?*builtin.StackTrace) noreturn = undefined;
pub fn handle(exception_number: u32) noreturn {
panicf("exception number {} ... now idle in arm exception handler", .{exception_number});
}
pub fn prepare() void {
already_panicking = false;
panic_handler = null;
}
pub fn setPanicHandler(new_panic_handler: ?fn (message: []const u8, trace: ?*builtin.StackTrace) noreturn) void {
panic_handler = new_panic_handler;
}
};
pub const Ficr = struct {
pub fn deviceId() u64 {
return @as(u64, contents[0x64 / 4]) << 32 | contents[0x60 / 4];
}
pub fn dump() void {
for (contents) |word, i| {
log("{x:2} {x:8}", .{ i * 4, word });
}
}
pub fn isQemu() bool {
return deviceId() == 0x1234567800000003;
}
pub const contents = @intToPtr(*[64]u32, 0x10000000);
pub const radio = @intToPtr(*extern struct {
device_address_type: u32,
device_address0: u32,
device_address1: u32,
}, 0x100000a0);
};
pub const Gpio = struct {
pub const config = mmio(0x50000700, [32]u32);
pub const config_masks = struct {
pub const input = 0x0;
pub const output = 0x1;
};
pub const led_anode_number_and_cathode_number_indexed_by_y_then_x = [5][5][2]u32{
.{ .{ 1, 1 }, .{ 2, 4 }, .{ 1, 2 }, .{ 2, 5 }, .{ 1, 3 } },
.{ .{ 3, 4 }, .{ 3, 5 }, .{ 3, 6 }, .{ 3, 7 }, .{ 3, 8 } },
.{ .{ 2, 2 }, .{ 1, 9 }, .{ 2, 3 }, .{ 3, 9 }, .{ 2, 1 } },
.{ .{ 1, 8 }, .{ 1, 7 }, .{ 1, 6 }, .{ 1, 5 }, .{ 1, 4 } },
.{ .{ 3, 3 }, .{ 2, 7 }, .{ 3, 1 }, .{ 2, 6 }, .{ 3, 2 } },
};
pub const registers = mmio(0x50000504, extern struct {
out: u32,
out_set: u32,
out_clear: u32,
in: u32,
direction: u32,
direction_set: u32,
direction_clear: u32,
});
pub const registers_masks = struct {
pub const button_a_active_low: u32 = 1 << 17;
pub const button_b_active_low: u32 = 1 << 26;
pub const i2c_scl: u32 = 1 << 0;
pub const i2c_sda: u32 = 1 << 30;
pub const nine_led_cathodes_active_low: u32 = 0x1ff << 4;
pub const ring0: u32 = 1 << 3;
pub const ring1: u32 = 1 << 2;
pub const ring2: u32 = 1 << 1;
pub const three_led_anodes: u32 = 0x7 << 13;
pub const uart_rx = 1 << 25;
pub const uart_tx = 1 << 24;
};
};
pub const Gpiote = struct {
pub const config = mmio(0x40006510, [4]u32);
pub const config_masks = struct {
pub const disable = 0x0;
};
pub const tasks = struct {
pub const out = mmio(0x40006000, [4]u32);
};
};
pub fn I2cInstance(instance_address: u32) type {
return struct {
pub fn prepare() void {
registers.enable = 0;
Gpio.registers.direction_set = Gpio.registers_masks.i2c_scl | Gpio.registers_masks.i2c_sda;
registers.pselscl = @ctz(u32, Gpio.registers_masks.i2c_scl);
registers.pselsda = @ctz(u32, Gpio.registers_masks.i2c_sda);
registers.frequency = frequencies.K400;
registers.enable = 5;
}
pub fn probe(device_address: u32) !void {
device_addresses.device_address = device_address;
tasks.startrx = 1;
defer tasks.stop = 1;
try wait(&events.byte_break);
}
pub fn readBlocking(device_address: u32, data: []u8, first: u32, last: u32) !void {
device_addresses.device_address = device_address;
tasks.starttx = 1;
registers.txd = first;
try wait(&events.txdsent);
tasks.startrx = 1;
var i = first;
while (i <= last) : (i += 1) {
if (i == last) {
tasks.stop = 1;
}
try wait(&events.rxready);
data[i] = @truncate(u8, registers.rxd);
}
}
pub fn readBlockingPanic(device_address: u32, data: []u8, first: u32, last: u32) void {
if (readBlocking(device_address, data, first, last)) |_| {} else |err| {
panicf("i2c device 0x{x} read {} errorsrc 0x{x}", .{ device_address, err, I2c0.errorsrc_registers.errorsrc });
}
}
fn wait(event: *volatile u32) !void {
const start = Timer0.capture();
while (true) {
if (event.* != 0) {
event.* = 0;
return;
}
if (errorsrc_registers.errorsrc != 0) {
return error.I2cErrorSourceRegister;
}
if (Timer0.capture() -% start > 500 * 1000) {
return error.I2cTimeExpired;
}
}
}
pub fn writeBlocking(device_address: u32, data: []u8, first: u32, last: u32) !void {
device_addresses.device_address = device_address;
tasks.starttx = 1;
registers.txd = first;
try wait(&events.txdsent);
var i = first;
while (i <= last) : (i += 1) {
registers.txd = data[i];
try wait(&events.txdsent);
}
tasks.stop = 1;
}
pub fn writeBlockingPanic(device_address: u32, data: []u8, first: u32, last: u32) void {
if (writeBlocking(device_address, data, first, last)) |_| {} else |err| {
panicf("i2c device 0x{x} write {} errorsrc 0x{x}", .{ device_address, err, I2c0.errorsrc_registers.errorsrc });
}
}
pub const device_addresses = mmio(instance_address + 0x588, extern struct {
device_address: u32,
});
pub const events = mmio(instance_address + 0x104, extern struct {
stopped: u32,
rxready: u32,
unused1: [4]u32,
txdsent: u32,
unused2: [1]u32,
error_event: u32,
unused3: [4]u32,
byte_break: u32,
});
pub const frequencies = struct {
pub const K100 = 0x01980000;
pub const K250 = 0x04000000;
pub const K400 = 0x06680000;
};
pub const errorsrc_registers = mmio(instance_address + 0x4c4, extern struct {
errorsrc: u32,
});
pub const errorsrc_masks = struct {
pub const overrun = 1 << 0;
pub const address_nack = 1 << 1;
pub const data_nack = 1 << 2;
};
pub const registers = mmio(instance_address + 0x500, extern struct {
enable: u32,
unused1: [1]u32,
pselscl: u32,
pselsda: u32,
unused2: [2]u32,
rxd: u32,
txd: u32,
unused3: [1]u32,
frequency: u32,
});
pub const short_cuts = mmio(instance_address + 0x200, extern struct {
shorts: u32,
});
pub const tasks = mmio(instance_address + 0x000, extern struct {
startrx: u32,
unused1: [1]u32,
starttx: u32,
unused2: [2]u32,
stop: u32,
unused3: [1]u32,
suspend_task: u32,
resume_task: u32,
});
};
}
pub const LedMatrix = struct {
pub var max_elapsed: u32 = undefined;
pub var image: u32 = undefined;
var scan_lines: [3]u32 = undefined;
var scan_lines_index: u32 = undefined;
var scan_timer: TimeKeeper = undefined;
pub fn prepare() void {
image = 0;
max_elapsed = 0;
Gpio.registers.direction_set = Gpio.registers_masks.three_led_anodes | Gpio.registers_masks.nine_led_cathodes_active_low;
for (scan_lines) |*scan_line| {
scan_line.* = 0;
}
scan_lines_index = 0;
putChar('Z');
scan_timer.prepare(3 * 1000);
}
pub fn putChar(byte: u8) void {
putImage(getImage(byte));
}
pub fn putImage(new_image: u32) void {
image = new_image;
var mask: u32 = 0x1;
var y: i32 = 4;
while (y >= 0) : (y -= 1) {
var x: i32 = 4;
while (x >= 0) : (x -= 1) {
putPixel(@intCast(u32, x), @intCast(u32, y), if (image & mask != 0) @as(u32, 1) else 0);
mask <<= 1;
}
}
}
fn putPixel(x: u32, y: u32, v: u32) void {
const anode_number_and_cathode_number = Gpio.led_anode_number_and_cathode_number_indexed_by_y_then_x[y][x];
const selected_scan_line_index = anode_number_and_cathode_number[0] - 1;
const col_mask = @as(u32, 0x10) << @truncate(u5, anode_number_and_cathode_number[1] - 1);
scan_lines[selected_scan_line_index] = scan_lines[selected_scan_line_index] & ~col_mask | v * col_mask;
}
pub fn update() void {
if (scan_timer.isFinished()) {
const elapsed = scan_timer.elapsed();
if (elapsed > max_elapsed) {
max_elapsed = elapsed;
}
scan_timer.reset();
const keep = Gpio.registers.out & ~(Gpio.registers_masks.three_led_anodes | Gpio.registers_masks.nine_led_cathodes_active_low);
const row_pins = @as(u32, 0x2000) << @truncate(u5, scan_lines_index);
const col_pins = ~scan_lines[scan_lines_index] & Gpio.registers_masks.nine_led_cathodes_active_low;
Gpio.registers.out = keep | row_pins | col_pins;
scan_lines_index = (scan_lines_index + 1) % scan_lines.len;
}
}
pub fn getImage(byte: u8) u32 {
return switch (byte) {
' ' => 0b0000000000000000000000000,
'0' => 0b1111110001100011000111111,
'1' => 0b0010001100001000010001110,
'2' => 0b1111100001111111000011111,
'3' => 0b1111100001001110000111111,
'4' => 0b1000110001111110000100001,
'5' => 0b1111110000111110000111111,
'6' => 0b1111110000111111000111111,
'7' => 0b1111100001000100010001000,
'8' => 0b1111110001111111000111111,
'9' => 0b1111110001111110000100001,
'A' => 0b0111010001111111000110001,
'B' => 0b1111010001111111000111110,
'Z' => 0b1111100010001000100011111,
else => 0b0000000000001000000000000,
};
}
};
pub const Power = struct {
pub const registers = mmio(0x40000400, extern struct {
reset_reason: u32,
});
};
pub const Ppi = struct {
pub fn setChannelEventAndTask(channel: u32, event: *volatile u32, task: *volatile u32) void {
channels[channel].event_end_point = @ptrToInt(event);
channels[channel].task_end_point = @ptrToInt(task);
}
pub const registers = mmio(0x4001f500, extern struct {
channel_enable: u32,
channel_enable_set: u32,
channel_enable_clear: u32,
});
const channels = mmio(0x4001f510, [16]struct {
event_end_point: u32,
task_end_point: u32,
});
};
pub const Radio = struct {
pub const events = mmio(0x40001100, extern struct {
ready: u32,
address_completed: u32,
payload_completed: u32,
packet_completed: u32,
disabled: u32,
});
pub const registers = mmio(0x40001504, struct {
packet_ptr: u32,
frequency: u32,
tx_power: u32,
mode: u32,
pcnf0: u32,
pcnf1: u32,
base0: u32,
base1: u32,
prefix0: u32,
prefix1: u32,
tx_address: u32,
rx_addresses: u32,
crc_config: u32,
crc_poly: u32,
crc_init: u32,
unused0x540: u32,
unused0x544: u32,
unused0x548: u32,
unused0x54c: u32,
state: u32,
datawhiteiv: u32,
});
pub const rx_registers = mmio(0x40001400, extern struct {
crc_status: u32,
unused0x404: u32,
unused0x408: u32,
rx_crc: u32,
});
pub const short_cuts = mmio(0x40001200, extern struct {
shorts: u32,
});
pub const tasks = mmio(0x40001000, extern struct {
tx_enable: u32,
rx_enable: u32,
start: u32,
stop: u32,
disable: u32,
});
};
pub const Rng = struct {
pub fn prepare() void {
registers.config = 0x1;
tasks.start = 1;
}
pub const events = mmio(0x4000d100, extern struct {
value_ready: u32,
});
pub const registers = mmio(0x4000d504, extern struct {
config: u32,
value: u32,
});
pub const tasks = mmio(0x4000d000, extern struct {
start: u32,
stop: u32,
});
};
pub const SystemControlBlock = struct {
pub fn requestSystemReset() void {
registers.aircr = 0x05fa0004;
}
pub const registers = mmio(0xe000ed00, extern struct {
cpuid: u32,
icsr: u32,
unused1: u32,
aircr: u32,
scr: u32,
ccr: u32,
unused2: u32,
shpr2: u32,
shpr3: u32,
});
};
pub const Temperature = struct {
pub const events = mmio(0x4000c100, extern struct {
data_ready: u32,
});
pub const registers = mmio(0x4000c508, extern struct {
temperature: u32,
});
pub const tasks = mmio(0x4000c000, extern struct {
start: u32,
stop: u32,
});
};
pub const Terminal = struct {
pub fn attribute(n: u32) void {
pair(n, 0, "m");
}
pub fn clearScreen() void {
pair(2, 0, "J");
}
pub fn hideCursor() void {
Uart.writeText(csi ++ "?25l");
}
pub fn line(comptime fmt: []const u8, args: var) void {
format(fmt, args);
pair(0, 0, "K");
Uart.writeText("\n");
}
pub fn move(row: u32, column: u32) void {
pair(row, column, "H");
}
pub fn pair(a: u32, b: u32, letter: []const u8) void {
if (a <= 1 and b <= 1) {
format("{}{}", .{ csi, letter });
} else if (b <= 1) {
format("{}{}{}", .{ csi, a, letter });
} else if (a <= 1) {
format("{};{}{}", .{ csi, b, letter });
} else {
format("{}{};{}{}", .{ csi, a, b, letter });
}
}
pub fn reportCursorPosition() void {
Uart.writeText(csi ++ "6n");
}
pub fn restoreCursor() void {
pair(0, 0, "u");
}
pub fn saveCursor() void {
pair(0, 0, "s");
}
pub fn setScrollingRegion(top: u32, bottom: u32) void {
pair(top, bottom, "r");
}
pub fn showCursor() void {
Uart.writeText(csi ++ "?25h");
}
const csi = "\x1b[";
};
pub const TimeKeeper = struct {
duration: u32,
start_time: u32,
fn capture(self: *TimeKeeper) u32 {
Timer0.capture_tasks[0] = 1;
return Timer0.capture_compare_registers[0];
}
fn elapsed(self: *TimeKeeper) u32 {
return self.capture() -% self.start_time;
}
fn prepare(self: *TimeKeeper, duration: u32) void {
self.duration = duration;
self.reset();
}
fn isFinished(self: *TimeKeeper) bool {
return self.elapsed() >= self.duration;
}
fn reset(self: *TimeKeeper) void {
self.start_time = self.capture();
}
fn wait(self: *TimeKeeper) void {
while (!self.isFinished()) {}
self.reset();
}
pub fn delay(duration: u32) void {
var time_keeper: TimeKeeper = undefined;
time_keeper.prepare(duration);
time_keeper.wait();
}
};
pub fn TimerInstance(instance_address: u32) type {
return struct {
pub fn capture() u32 {
capture_tasks[0] = 1;
return capture_compare_registers[0];
}
pub fn prepare() void {
registers.mode = 0x0;
registers.bit_mode = if (instance_address == 0x40008000) @as(u32, 0x3) else 0x0;
registers.prescaler = if (instance_address == 0x40008000) @as(u32, 4) else 9;
tasks.start = 1;
const now = capture();
var i: u32 = 0;
while (capture() == now) : (i += 1) {
if (i == 1000) {
panicf("timer {} is not responding", .{instance_address});
}
}
// panicf("timer {} responded {} now {} capture {}", .{instance_address, i, now, capture()});
}
pub const capture_compare_registers = mmio(instance_address + 0x540, [4]u32);
pub const capture_tasks = mmio(instance_address + 0x040, [4]u32);
pub const events = struct {
pub const compare = mmio(instance_address + 0x140, [4]u32);
};
pub const registers = mmio(instance_address + 0x504, extern struct {
mode: u32,
bit_mode: u32,
unused0x50c: u32,
prescaler: u32,
});
pub const short_cuts = mmio(instance_address + 0x200, extern struct {
shorts: u32,
});
pub const tasks = mmio(instance_address + 0x000, extern struct {
start: u32,
stop: u32,
count: u32,
clear: u32,
});
};
}
pub const Uart = struct {
var stream: std.io.OutStream(Uart, stream_error, writeTextError) = undefined;
var tx_busy: bool = undefined;
var tx_queue: [3]u8 = undefined;
var tx_queue_read: usize = undefined;
var tx_queue_write: usize = undefined;
var updater: ?fn () void = undefined;
pub fn drainTxQueue() void {
while (tx_queue_read != tx_queue_write) {
loadTxd();
}
}
pub fn prepare() void {
updater = null;
Gpio.registers.direction_set = Gpio.registers_masks.uart_tx;
registers.pin_select_rxd = @ctz(u32, Gpio.registers_masks.uart_rx);
registers.pin_select_txd = @ctz(u32, Gpio.registers_masks.uart_tx);
registers.enable = 0x04;
tasks.start_rx = 1;
tasks.start_tx = 1;
tx_busy = false;
tx_queue_read = 0;
tx_queue_write = 0;
}
pub fn isReadByteReady() bool {
return events.rx_ready == 1;
}
pub fn format(comptime fmt: []const u8, args: var) void {
std.fmt.format(stream, fmt, args) catch |_| {};
}
pub fn loadTxd() void {
if (tx_queue_read != tx_queue_write and (!tx_busy or events.tx_ready == 1)) {
events.tx_ready = 0;
registers.txd = tx_queue[tx_queue_read];
tx_queue_read = (tx_queue_read + 1) % tx_queue.len;
tx_busy = true;
if (updater) |an_updater| {
an_updater();
}
}
}
pub fn log(comptime fmt: []const u8, args: var) void {
format(fmt ++ "\n", args);
}
pub fn readByte() u8 {
events.rx_ready = 0;
return @truncate(u8, registers.rxd);
}
pub fn setUpdater(new_updater: fn () void) void {
updater = new_updater;
}
pub fn update() void {
loadTxd();
}
pub fn writeByteBlocking(byte: u8) void {
const next = (tx_queue_write + 1) % tx_queue.len;
while (next == tx_queue_read) {
loadTxd();
}
tx_queue[tx_queue_write] = byte;
tx_queue_write = next;
loadTxd();
}
pub fn writeText(buffer: []const u8) void {
for (buffer) |c| {
switch (c) {
'\n' => {
writeByteBlocking('\r');
writeByteBlocking('\n');
},
else => writeByteBlocking(c),
}
}
}
pub fn writeTextError(context: Uart, buffer: []const u8) stream_error!u32 {
writeText(buffer);
return buffer.len;
}
const stream_error = error{UartError};
const events = mmio(0x40002108, extern struct {
rx_ready: u32,
unused0x10c: u32,
unused0x110: u32,
unused0x114: u32,
unused0x118: u32,
tx_ready: u32,
unused0x120: u32,
error_detected: u32,
});
const error_registers = mmio(0x40002480, extern struct {
error_source: u32,
});
const registers = mmio(0x40002500, extern struct {
enable: u32,
unused0x504: u32,
pin_select_rts: u32,
pin_select_txd: u32,
pin_select_cts: u32,
pin_select_rxd: u32,
rxd: u32,
txd: u32,
unused0x520: u32,
baud_rate: u32,
});
const tasks = mmio(0x40002000, extern struct {
start_rx: u32,
stop_rx: u32,
start_tx: u32,
stop_tx: u32,
});
};
pub const Uicr = struct {
pub fn dump() void {
for (contents) |word, i| {
log("{x:2} {x:8}", .{ i * 4, word });
}
}
pub const contents = @intToPtr(*[64]u32, 0x10001000);
};
pub const Wdt = struct {
pub const reload_request_registers = mmio(0x40010600, extern struct {
reload_request: [8]u32,
});
pub const registers = mmio(0x40010504, extern struct {
counter_reset_value: u32,
reload_rewuest_enable: u32,
});
pub const tasks = mmio(0x40010000, extern struct {
start: u32,
});
};
pub fn hangf(comptime fmt: []const u8, args: var) noreturn {
log(fmt, args);
Uart.drainTxQueue();
while (true) {}
}
pub fn mmio(address: u32, comptime mmio_type: type) *volatile mmio_type {
return @intToPtr(*volatile mmio_type, address);
}
pub fn panic(message: []const u8, trace: ?*builtin.StackTrace) noreturn {
if (Exceptions.panic_handler) |handler| {
handler(message, trace);
} else {
panicf("panic(): {}", .{message});
}
}
pub fn panicf(comptime fmt: []const u8, args: var) noreturn {
@setCold(true);
if (Exceptions.already_panicking) {
hangf("\npanicked during panic", .{});
}
Exceptions.already_panicking = true;
log("\npanicf(): " ++ fmt, args);
var it = std.debug.StackIterator.init(null, null);
while (it.next()) |stacked_address| {
dumpReturnAddress(stacked_address - 1);
}
hangf("panic completed", .{});
}
fn dumpReturnAddress(return_address: usize) void {
var symbol_index: usize = 0;
var line: []const u8 = "";
var i: u32 = 0;
while (i < symbols.len) {
var j: u32 = i;
while (symbols[j] != '\n') {
j += 1;
}
const next_line = symbols[i..j];
const symbol_address = std.fmt.parseUnsigned(usize, next_line[0..8], 16) catch 0;
if (symbol_address >= return_address) {
break;
}
line = next_line;
i = j + 1;
}
if (line.len >= 3) {
log("{x:5} in {}", .{ return_address, line[3..] });
} else {
log("{x:5}", .{return_address});
}
}
fn typicalVectorTable(comptime mission_id: u32) []const u8 {
var buf: [1]u8 = undefined;
const mission_id_string = std.fmt.bufPrint(&buf, "{}", .{mission_id}) catch |_| panicf("", .{});
for (mission_id_string) |*space| {
if (space.* == ' ') {
space.* = '0';
} else {
break;
}
}
return ".section .text.start.mission" ++ mission_id_string ++ "\n" ++
".globl mission" ++ mission_id_string ++ "_vector_table\n" ++
".balign 0x80\n" ++
"mission" ++ mission_id_string ++ "_vector_table:\n" ++
" .long 0x20004000 // sp top of 16KB\n" ++
" .long mission" ++ mission_id_string ++ "_main\n" ++
\\ .long lib_exceptionNumber02
\\ .long lib_exceptionNumber03
\\ .long lib_exceptionNumber04
\\ .long lib_exceptionNumber05
\\ .long lib_exceptionNumber06
\\ .long lib_exceptionNumber07
\\ .long lib_exceptionNumber08
\\ .long lib_exceptionNumber09
\\ .long lib_exceptionNumber10
\\ .long lib_exceptionNumber11
\\ .long lib_exceptionNumber12
\\ .long lib_exceptionNumber13
\\ .long lib_exceptionNumber14
\\ .long lib_exceptionNumber15
;
}
export fn lib_exceptionNumber01() noreturn {
Exceptions.handle(01);
}
export fn lib_exceptionNumber02() noreturn {
Exceptions.handle(02);
}
export fn lib_exceptionNumber03() noreturn {
Exceptions.handle(03);
}
export fn lib_exceptionNumber04() noreturn {
Exceptions.handle(04);
}
export fn lib_exceptionNumber05() noreturn {
Exceptions.handle(05);
}
export fn lib_exceptionNumber06() noreturn {
Exceptions.handle(06);
}
export fn lib_exceptionNumber07() noreturn {
Exceptions.handle(07);
}
export fn lib_exceptionNumber08() noreturn {
Exceptions.handle(08);
}
export fn lib_exceptionNumber09() noreturn {
Exceptions.handle(09);
}
export fn lib_exceptionNumber10() noreturn {
Exceptions.handle(10);
}
export fn lib_exceptionNumber11() noreturn {
Exceptions.handle(11);
}
export fn lib_exceptionNumber12() noreturn {
Exceptions.handle(12);
}
export fn lib_exceptionNumber13() noreturn {
Exceptions.handle(13);
}
export fn lib_exceptionNumber14() noreturn {
Exceptions.handle(14);
}
export fn lib_exceptionNumber15() noreturn {
Exceptions.handle(15);
}
const builtin = std.builtin;
const format = Uart.format;
const std = @import("std");
const symbols = @embedFile("symbols.txt");
extern var __bss_start: u8;
extern var __bss_end: u8;
extern var __debug_info_start: u8;
extern var __debug_info_end: u8;
extern var __debug_abbrev_start: u8;
extern var __debug_abbrev_end: u8;
extern var __debug_str_start: u8;
extern var __debug_str_end: u8;
extern var __debug_line_start: u8;
extern var __debug_line_end: u8;
extern var __debug_ranges_start: u8;
extern var __debug_ranges_end: u8;
pub const log = Uart.log;
pub const ram_u32 = @intToPtr(*volatile [4096]u32, 0x20000000);
pub const I2c0 = I2cInstance(0x40003000);
pub const I2c1 = I2cInstance(0x40004000);
pub const Timer0 = TimerInstance(0x40008000);
pub const Timer1 = TimerInstance(0x40009000);
pub const Timer2 = TimerInstance(0x4000a000);
};
pub const typical = struct {
pub const Adc = lib.Adc;
pub const assert = std.debug.assert;
pub const Bss = lib.Bss;
pub const builtin = std.builtin;
pub const ClockManagement = lib.ClockManagement;
pub const Exceptions = lib.Exceptions;
pub const Ficr = lib.Ficr;
pub const format = Uart.format;
pub const Gpio = lib.Gpio;
pub const Gpiote = lib.Gpiote;
pub const I2c0 = lib.I2c0;
pub const lib_basics = lib;
pub const log = Uart.log;
pub const math = std.math;
pub const mem = std.mem;
pub const LedMatrix = lib.LedMatrix;
pub const panic = lib.panic;
pub const panicf = lib.panicf;
pub const Power = lib.Power;
pub const Ppi = lib.Ppi;
pub const std = @import("std");
pub const SystemControlBlock = lib.SystemControlBlock;
pub const Temperature = lib.Temperature;