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
  Using Signal on PinePhone in mid-2023? dante404 47 15,554 05-03-2024, 02:19 AM
Last Post: dragonhospital
  Slarm64 on PinePhone [Unofficial Slackware ARM - 64 bit] acid andy 38 26,298 04-23-2024, 10:29 AM
Last Post: donchurch
  PinePhone app development WhiteHexagon 15 4,593 04-23-2024, 05:19 AM
Last Post: Jonnyc
Wink PINEPHONE not booting Touchwood 2 653 02-23-2024, 07:27 AM
Last Post: Touchwood
  Slack on PinePhone Adam Seline 5 5,661 12-20-2023, 07:20 AM
Last Post: nickolas
  Struggle to install LibreOffice on the PinePhone Peter Gamma 48 25,865 11-24-2023, 07:02 AM
Last Post: Peter Gamma
  Which word processor to choose for the Pinephone? Peter Gamma 8 2,368 11-23-2023, 01:06 AM
Last Post: Peter Gamma
  openSUSE for Pinephone Alefnode 75 104,552 11-17-2023, 08:37 AM
Last Post: Uturn
  Genode- Sculpt OS for the PinePhone (non Linux) Surehand53 1 1,152 11-04-2023, 07:23 PM
Last Post: tllim
  Abiword as a office mobile word processor for the Pinephone? Peter Gamma 11 2,727 10-24-2023, 08:57 AM
Last Post: Peter Gamma

Forum Jump:


Users browsing this thread: 1 Guest(s)