Skip to content

Commit 43837a7

Browse files
committed
[hdsc] HC32L110 crc16
1 parent 1bce9e8 commit 43837a7

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

examples/hdsc/hc32l110/build.zig

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub fn build(b: *std.Build) void {
1414

1515
const examples: []const Example = &.{
1616
.{ .name = "blinky", .file = "src/blinky.zig" },
17+
.{ .name = "crc16", .file = "src/crc16.zig" },
1718
};
1819

1920
for (examples) |example| {

examples/hdsc/hc32l110/src/crc16.zig

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const std = @import("std");
2+
const microzig = @import("microzig");
3+
const hal = microzig.hal;
4+
const time = hal.time;
5+
const crc16 = hal.crc16;
6+
7+
pub fn main() !void {
8+
hal.clock.gate.enable(.Crc);
9+
10+
const value = crc16.calculate(u8, &.{ 0xDE, 0xAD, 0xBE, 0xEF });
11+
if (value != 0xE5CB) @panic("invalid value");
12+
13+
if (!crc16.check(u8, &.{ 0xDE, 0xAD, 0xBE, 0xEF }, 0xE5CB))
14+
@panic("invalid value");
15+
16+
while (true) {}
17+
}

port/hdsc/hc32l110/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ HAL and register definitions for the HC32L110 family of microcontrollers
66
- [ ] adc
77
- [ ] adt
88
- [-] clocks
9-
- [ ] crc
9+
- [x] crc
1010
- [ ] flash
1111
- [x] gpio
1212
- [ ] i2c

port/hdsc/hc32l110/src/hal/crc16.zig

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const std = @import("std");
2+
const microzig = @import("microzig");
3+
const chip = microzig.chip;
4+
5+
const CRC = chip.peripherals.CRC;
6+
7+
pub fn calculate(T: type, data: []const T) u16 {
8+
switch (T) {
9+
u8, u16, u32 => {},
10+
else => @compileError("unsupported type"),
11+
}
12+
13+
CRC.RESULT.write(.{ .RESULT = 0xffff, .FLAG = 0 });
14+
15+
const ptr: *volatile T = @ptrCast(&CRC.DATA);
16+
for (data) |d| {
17+
ptr.* = d;
18+
}
19+
20+
return CRC.RESULT.read().RESULT;
21+
}
22+
23+
pub fn check(T: type, data: []const T, value: u16) bool {
24+
CRC.RESULT.write(.{ .RESULT = value, .FLAG = 0 });
25+
26+
const ptr: *volatile T = @ptrCast(&CRC.DATA);
27+
for (data) |d| {
28+
ptr.* = d;
29+
}
30+
31+
return CRC.RESULT.read.FLAG;
32+
}

0 commit comments

Comments
 (0)