Article: NuttX RTOS for PinePhone: Feature Phone UI in LVGL, Zig and WebAssembly
#21
Hi @lupyuen   yes I can picture you teaching this topic, just by the quality and fun approach of your articles Smile  I think it could be a great idea! there is certainly plenty of stuff still to work on, it has taken me a week to get VOL+/- working, and that is just the buttons hehe.  btw here is the Zig code I ended up with using the Register approach I mentioned.  I am not keen of uppercase naming and KEYADC prefixing everywhere, but at the same time I like that the code can be easily cross referenced to the A64 User Manual.  Anyway I am sure the corporate paid influencers will spoil Zig before I make it to the next levelWink but I like the enum that doesnt impact the packed struct size.  The Zig compiler is also keeping an eye on the packed struct size matching u32, nice.

Code:
pub const CTRL_REG = packed struct(u32) {
    KEYADC_EN: bool = false,
    _1: u1 = 0,
    KEYADC_SAMPLE_RATE: u2 = 0,
    LEVELB_VOL: u2 = 0,
    KEYADC_HOLD_EN: bool = false,
    KEYADC_HOLD_KEY_EN: bool = false,
    LEVELA_B_CNT: u4 = 0,
    KEY_MODE_SELECT: Mode = Mode.NORMAL,
    _2: u2 = 0,
    CONTINUE_TIME_SELECT: u4 = 0,
    _3: u2 = 0,
    _4: u2 = 0,
    FIRST_CONVERT_DLY: u8 = 0,

    pub const Mode = enum(u2) {
        NORMAL = 0b00,
        SINGLE = 0b01,
        CONTINUE = 0b10,
    };

    pub fn asU32(self: CTRL_REG) u32 {
        return @bitCast(u32, self);
    }
    pub fn fromU32(v: u32) CTRL_REG {
        return @bitCast(CTRL_REG, v);
    }
};

pub fn enable() void {
    //enable KEYADC
    var ctrl = CTRL_REG.fromU32(KEYADC_CTRL.read());
    ctrl.KEYADC_EN = true;
    ctrl.KEY_MODE_SELECT = CTRL_REG.Mode.SINGLE;
    KEYADC_CTRL.write(ctrl.asU32());

    //enable all interrupts
    KEYADC_INTC.write(INTC_FlagsAll.asU32());
}
and the Reg32:

Code:
pub const Reg32 = struct {
    ptr: *volatile u32,

    pub fn init(addr: usize) Reg32 {
        return Reg32{ .ptr = @intToPtr(*volatile u32, addr) };
    }

    pub fn read(self: Reg32) u32 {
        return self.ptr.*;
    }

    pub fn write(self: Reg32, v: u32) void {
        self.ptr.* = v;
    }
};
  Reply


Messages In This Thread
RE: Article: NuttX RTOS for PinePhone: Feature Phone UI in LVGL, Zig and WebAssembly - by WhiteHexagon - 07-06-2023, 01:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Latest firmware for PinePhone modem! Subsentient 105 157,547 02-02-2025, 08:47 PM
Last Post: bacydentir85
  Slarm64 on PinePhone [Unofficial Slackware ARM - 64 bit] acid andy 40 36,271 12-28-2024, 12:19 AM
Last Post: mara
  How-To: Remote Control Your Phone from Desktop via VNC biketool 2 765 10-12-2024, 11:47 AM
Last Post: biketool
  Office applications for the Pinephone Peter Gamma 2 742 09-05-2024, 09:22 AM
Last Post: Peter Gamma
  Struggle to install LibreOffice on the PinePhone Peter Gamma 50 38,979 07-26-2024, 10:35 PM
Last Post: Peter Gamma
  Why does Pine64 sabotage office on the Pinephone? Peter Gamma 5 1,465 07-04-2024, 07:34 AM
Last Post: Kevin Kofler
  Which word processor to choose for the Pinephone? Peter Gamma 16 6,601 06-22-2024, 07:28 AM
Last Post: Peter Gamma
  Samba share on the Pinephone? Peter Gamma 0 757 06-16-2024, 10:26 PM
Last Post: Peter Gamma
  Possible Free Backup Carrier for PinePhone PineFone 0 687 06-13-2024, 03:45 PM
Last Post: PineFone
  Using Signal on PinePhone in mid-2023? dante404 47 25,770 05-03-2024, 02:19 AM
Last Post: dragonhospital

Forum Jump:


Users browsing this thread: 5 Guest(s)