mirror of
https://github.com/lupyuen/pinephone-nuttx.git
synced 2025-01-13 05:08:32 +08:00
Adding Long Packet
This commit is contained in:
parent
5e33e3d476
commit
e2f6aa6dd7
1 changed files with 34 additions and 28 deletions
62
display.zig
62
display.zig
|
@ -90,43 +90,49 @@ fn compose_long_packet(
|
|||
) []u8 {
|
||||
_ = buf;
|
||||
debug("compose_long_packet: channel={}, cmd={x}, len={}", .{ channel, cmd, len });
|
||||
// Packet Header (4 bytes):
|
||||
// TODO:
|
||||
// - Data Identifier (DI) (1 byte):
|
||||
// Virtual Channel Identifier (Bits 6 to 7)
|
||||
// Data Type (Bits 0 to 5)
|
||||
// (Virtual Channel should be 0, I think)
|
||||
const dc = 0;
|
||||
// Data Identifier (DI) (1 byte):
|
||||
// Virtual Channel Identifier (Bits 6 to 7)
|
||||
// Data Type (Bits 0 to 5)
|
||||
// (Virtual Channel should be 0, I think)
|
||||
assert(cmd < (1 << 6));
|
||||
const vc: u8 = 0;
|
||||
const dt: u8 = cmd;
|
||||
const di: u8 = (vc << 6) | dt;
|
||||
|
||||
// - Word Count (WC) (2 bytes):
|
||||
// Number of bytes in the Packet Payload
|
||||
const wc = @intCast(u16, len);
|
||||
const wcl = @intCast(u8, wc & 0xff);
|
||||
const wch = @intCast(u8, wc >> 8);
|
||||
// Word Count (WC) (2 bytes):
|
||||
// Number of bytes in the Packet Payload
|
||||
const wc: u16 = @intCast(u16, len);
|
||||
const wcl: u8 = @intCast(u8, wc & 0xff);
|
||||
const wch: u8 = @intCast(u8, wc >> 8);
|
||||
|
||||
// TODO:
|
||||
// - Error Correction Code (ECC) (1 byte):
|
||||
// Allow single-bit errors to be corrected and 2-bit errors to be detected in the Packet Header
|
||||
// See "12.3.6.12: Error Correction Code", Page 208:
|
||||
// https://github.com/sipeed/sipeed2022_autumn_competition/blob/main/assets/BL808_RM_en.pdf)
|
||||
const ecc = 0;
|
||||
const header = [4]u8 { dc, wcl, wch, ecc };
|
||||
// TODO: Error Correction Code (ECC) (1 byte):
|
||||
// Allow single-bit errors to be corrected and 2-bit errors to be detected in the Packet Header
|
||||
// See "12.3.6.12: Error Correction Code", Page 208:
|
||||
// https://github.com/sipeed/sipeed2022_autumn_competition/blob/main/assets/BL808_RM_en.pdf)
|
||||
const ecc: u8 = 0;
|
||||
|
||||
// TODO: Checksum (CS) (2 bytes):
|
||||
// 16-bit Cyclic Redundancy Check (CRC)
|
||||
// See "12.3.6.13: Packet Footer", Page 210:
|
||||
// https://github.com/sipeed/sipeed2022_autumn_competition/blob/main/assets/BL808_RM_en.pdf)
|
||||
const cs: u16 = 0;
|
||||
const csl: u8 = @intCast(u8, cs & 0xff);
|
||||
const csh: u8 = @intCast(u8, cs >> 8);
|
||||
|
||||
// Packet Header (4 bytes): Data Identifier, Word Count, Error Correction COde
|
||||
const header = [4]u8 { di, wcl, wch, ecc };
|
||||
_ = header;
|
||||
|
||||
// Packet Payload:
|
||||
// - Data (0 to 65,541 bytes):
|
||||
// Number of data bytes should match the Word Count (WC)
|
||||
// Data (0 to 65,541 bytes):
|
||||
// Number of data bytes should match the Word Count (WC)
|
||||
assert(len <= 65_541);
|
||||
|
||||
// TODO: Packet Footer:
|
||||
// - Checksum (CS) (2 bytes):
|
||||
// 16-bit Cyclic Redundancy Check (CRC)
|
||||
// See "12.3.6.13: Packet Footer", Page 210:
|
||||
// https://github.com/sipeed/sipeed2022_autumn_competition/blob/main/assets/BL808_RM_en.pdf)
|
||||
const footer = [2]u8 { 0, 0 };
|
||||
// Packet Footer: Checksum
|
||||
const footer = [2]u8 { csl, csh };
|
||||
_ = footer;
|
||||
|
||||
// TODO: Concatenate the header, payload and footer
|
||||
// TODO: Packet = Packet Header + Payload + Packet Footer
|
||||
var pkt = std.mem.zeroes([1024]u8);
|
||||
assert(pkt.len >= header.len + len + footer.len); // Increase pkt size
|
||||
std.mem.copy(u8, pkt[0..header.len], &header);
|
||||
|
|
Loading…
Reference in a new issue