pinephone-nuttx/display.zig

194 lines
5.8 KiB
Zig
Raw Normal View History

2022-10-05 15:36:22 +08:00
//***************************************************************************
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. The
// ASF licenses this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
//***************************************************************************
2022-10-05 16:21:33 +08:00
//! PinePhone MIPI DSI Driver for Apache NuttX RTOS.
//! This MIPI DSI Interface is compatible with Zephyr MIPI DSI:
//! https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/drivers/mipi_dsi.h
/// Import the Zig Standard Library
2022-10-05 15:36:22 +08:00
const std = @import("std");
2022-10-05 16:21:33 +08:00
/// Import the LoRaWAN Library from C
const c = @cImport({
// NuttX Defines
@cDefine("__NuttX__", "");
@cDefine("NDEBUG", "");
// NuttX Header Files
@cInclude("arch/types.h");
@cInclude("../../nuttx/include/limits.h");
@cInclude("nuttx/config.h");
@cInclude("inttypes.h");
@cInclude("unistd.h");
@cInclude("stdlib.h");
@cInclude("stdio.h");
});
2022-10-05 15:36:22 +08:00
/// MIPI DSI Processor-to-Peripheral transaction types
const MIPI_DSI_GENERIC_LONG_WRITE = 0x29;
/// Write to MIPI DSI
2022-10-05 16:47:38 +08:00
pub export fn nuttx_mipi_dsi_dcs_write(
2022-10-05 15:36:22 +08:00
dev: [*c]const mipi_dsi_device, // MIPI DSI Host Device
channel: u8, // Virtual Channel ID
2022-10-05 16:21:33 +08:00
cmd: u8, // DCS Command
buf: [*c]u8, // Transmit Buffer
len: usize // Length of Buffer
) isize { // On Success: Return number of written bytes. On Error: Return negative error code
2022-10-05 16:39:41 +08:00
_ = dev; _ = buf;
debug("mipi_dsi_dcs_write: channel={}, cmd={x}, len={}", .{ channel, cmd, len });
2022-10-05 16:47:38 +08:00
std.debug.panic("nuttx_mipi_dsi_dcs_write not implemented", .{});
2022-10-05 15:36:22 +08:00
return 0;
}
/// MIPI DSI Device
2022-10-05 16:21:33 +08:00
pub const mipi_dsi_device = extern struct {
/// Number of Data Lanes
data_lanes: u8,
2022-10-05 15:36:22 +08:00
/// Display Timings
2022-10-05 16:21:33 +08:00
timings: mipi_dsi_timings,
/// Pixel Format
pixfmt: u32,
/// Mode Flags
mode_flags: u32,
2022-10-05 15:36:22 +08:00
};
/// MIPI DSI Read / Write Message
2022-10-05 16:21:33 +08:00
pub const mipi_dsi_msg = extern struct {
/// Payload Data Type
type: u8,
/// Flags controlling message transmission
flags: u16,
/// Command (only for DCS)
cmd: u8,
/// Transmit Buffer Length
tx_len: usize,
/// Transmit Buffer
tx_buf: [*c]const u8,
/// Receive Buffer Length
rx_len: usize,
/// Receive Buffer
rx_buf: [*c]u8,
2022-10-05 15:36:22 +08:00
};
/// MIPI DSI Display Timings
2022-10-05 16:21:33 +08:00
pub const mipi_dsi_timings = extern struct {
/// Horizontal active video
hactive: u32,
/// Horizontal front porch
hfp: u32,
/// Horizontal back porch
hbp: u32,
/// Horizontal sync length
hsync: u32,
/// Vertical active video
vactive: u32,
/// Vertical front porch
vfp: u32,
/// Vertical back porch
vbp: u32,
/// Vertical sync length
vsync: u32,
2022-10-05 15:36:22 +08:00
};
pub export fn null_main(_argc: c_int, _argv: [*]const [*]const u8) c_int {
_ = _argc;
_ = _argv;
test_zig();
return 0;
}
pub export fn test_zig() void {
_ = printf("HELLO ZIG ON PINEPHONE!\n");
2022-10-05 16:47:38 +08:00
_ = nuttx_mipi_dsi_dcs_write(null, 0, MIPI_DSI_GENERIC_LONG_WRITE, null, 0);
2022-10-05 15:36:22 +08:00
}
2022-10-05 16:32:45 +08:00
///////////////////////////////////////////////////////////////////////////////
// Panic Handler
/// Called by Zig when it hits a Panic. We print the Panic Message, Stack Trace and halt. See
/// https://andrewkelley.me/post/zig-stack-traces-kernel-panic-bare-bones-os.html
/// https://github.com/ziglang/zig/blob/master/lib/std/builtin.zig#L763-L847
pub fn panic(
message: []const u8,
_stack_trace: ?*std.builtin.StackTrace
) noreturn {
// Print the Panic Message
_ = _stack_trace;
_ = puts("\n!ZIG PANIC!");
_ = puts(@ptrCast([*c]const u8, message));
// Print the Stack Trace
_ = puts("Stack Trace:");
var it = std.debug.StackIterator.init(@returnAddress(), null);
while (it.next()) |return_address| {
_ = printf("%p\n", return_address);
}
// Halt
while(true) {}
}
///////////////////////////////////////////////////////////////////////////////
// Logging
/// Called by Zig for `std.log.debug`, `std.log.info`, `std.log.err`, ...
/// https://gist.github.com/leecannon/d6f5d7e5af5881c466161270347ce84d
pub fn log(
comptime _message_level: std.log.Level,
comptime _scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
_ = _message_level;
_ = _scope;
// Format the message
var buf: [100]u8 = undefined; // Limit to 100 chars
var slice = std.fmt.bufPrint(&buf, format, args)
catch { _ = puts("*** log error: buf too small"); return; };
// Terminate the formatted message with a null
var buf2: [buf.len + 1 : 0]u8 = undefined;
std.mem.copy(
u8,
buf2[0..slice.len],
slice[0..slice.len]
);
buf2[slice.len] = 0;
// Print the formatted message
_ = puts(&buf2);
}
///////////////////////////////////////////////////////////////////////////////
// Imported Functions and Variables
/// For safety, we import these functions ourselves to enforce Null-Terminated Strings.
/// We changed `[*c]const u8` to `[*:0]const u8`
extern fn printf(format: [*:0]const u8, ...) c_int;
extern fn puts(str: [*:0]const u8) c_int;
/// LoRaWAN Event Queue
extern var event_queue: c.struct_ble_npl_eventq;
/// Aliases for Zig Standard Library
const assert = std.debug.assert;
const debug = std.log.debug;