cbindgen abi_sys

This commit is contained in:
2025-10-05 17:14:38 -06:00
parent f2a5801284
commit af238b2847
17 changed files with 590 additions and 305 deletions

View File

@@ -4,11 +4,15 @@ version = "0.1.0"
edition = "2024"
[features]
default = []
defmt = ["dep:defmt"]
[dependencies]
embedded-graphics = "0.8.1"
strum = { version = "0.27.2", default-features = false, features = ["derive"] }
defmt = { version = "0.3", optional = true }
shared = { path = "../shared" }
bitflags = "2.9.4"
embedded-graphics = "0.8.1"
embedded-sdmmc = { version = "0.9.0", default-features = false }
defmt = { version = "0.3", optional = true }
[build-dependencies]
cbindgen = "0.24.0"

View File

@@ -1,27 +1,11 @@
#![no_std]
extern crate alloc;
#[allow(unused)]
use embedded_graphics::{
Pixel,
geometry::Point,
pixelcolor::{Rgb565, RgbColor},
};
use embedded_graphics::{Pixel, pixelcolor::Rgb565};
use embedded_sdmmc::DirEntry;
use shared::keyboard::KeyEventC;
pub use shared::keyboard::{KeyCode, KeyEvent, KeyState, Modifiers};
use strum::{EnumCount, EnumIter};
pub type EntryFn = fn();
#[unsafe(no_mangle)]
#[unsafe(link_section = ".syscall_table")]
pub static mut CALL_ABI_TABLE: [usize; CallAbiTable::COUNT] = [0; CallAbiTable::COUNT];
#[repr(usize)]
#[derive(Clone, Copy, EnumIter, EnumCount)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum CallAbiTable {
PrintString = 0,
SleepMs = 1,
@@ -33,20 +17,25 @@ pub enum CallAbiTable {
ReadFile = 7,
FileLen = 8,
}
pub type EntryFn = fn();
#[unsafe(no_mangle)]
#[unsafe(link_section = ".syscall_table")]
pub static mut CALL_ABI_TABLE: [usize; CallAbiTable::COUNT] = [0; CallAbiTable::COUNT];
pub type PrintAbi = extern "C" fn(ptr: *const u8, len: usize);
#[allow(unused)]
pub fn print(msg: &str) {
#[unsafe(no_mangle)]
pub extern "C" fn print(ptr: *const u8, len: usize) {
let f: PrintAbi =
unsafe { core::mem::transmute(CALL_ABI_TABLE[CallAbiTable::PrintString as usize]) };
f(msg.as_ptr(), msg.len());
f(ptr, len);
}
pub type SleepAbi = extern "C" fn(ms: u64);
#[allow(unused)]
pub fn sleep(ms: u64) {
#[unsafe(no_mangle)]
pub extern "C" fn sleep(ms: u64) {
let f: SleepAbi =
unsafe { core::mem::transmute(CALL_ABI_TABLE[CallAbiTable::SleepMs as usize]) };
f(ms);
@@ -54,8 +43,8 @@ pub fn sleep(ms: u64) {
pub type LockDisplay = extern "C" fn(lock: bool);
#[allow(unused)]
pub fn lock_display(lock: bool) {
#[unsafe(no_mangle)]
pub extern "C" fn lock_display(lock: bool) {
let f: LockDisplay =
unsafe { core::mem::transmute(CALL_ABI_TABLE[CallAbiTable::LockDisplay as usize]) };
f(lock);
@@ -63,20 +52,240 @@ pub fn lock_display(lock: bool) {
pub type DrawIterAbi = extern "C" fn(ptr: *const Pixel<Rgb565>, len: usize);
#[allow(unused)]
pub fn draw_iter(pixels: &[Pixel<Rgb565>]) {
#[unsafe(no_mangle)]
pub extern "C" fn draw_iter(ptr: *const Pixel<Rgb565>, len: usize) {
let f: DrawIterAbi =
unsafe { core::mem::transmute(CALL_ABI_TABLE[CallAbiTable::DrawIter as usize]) };
f(pixels.as_ptr(), pixels.len());
f(ptr, len);
}
pub type GetKeyAbi = extern "C" fn() -> KeyEventC;
pub mod keyboard {
use crate::{CALL_ABI_TABLE, CallAbiTable};
#[allow(unused)]
pub fn get_key() -> KeyEventC {
let f: GetKeyAbi =
unsafe { core::mem::transmute(CALL_ABI_TABLE[CallAbiTable::GetKey as usize]) };
f()
bitflags::bitflags! {
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
#[repr(C)]
pub struct Modifiers: u8 {
const NONE = 0;
const CTRL = 1;
const ALT = 2;
const LSHIFT = 4;
const RSHIFT = 8;
const SYM = 16;
}
}
#[repr(C)]
pub struct KeyEventC {
pub key: u8,
pub state: KeyState,
pub mods: Modifiers,
}
impl Into<KeyEvent> for KeyEventC {
fn into(self) -> KeyEvent {
KeyEvent {
key: self.key.into(),
state: self.state,
mods: self.mods,
}
}
}
#[derive(Debug)]
pub struct KeyEvent {
pub key: KeyCode,
pub state: KeyState,
pub mods: Modifiers,
}
impl Into<KeyEventC> for KeyEvent {
fn into(self) -> KeyEventC {
KeyEventC {
key: self.key.into(),
state: self.state,
mods: self.mods,
}
}
}
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub enum KeyState {
Idle = 0,
Pressed = 1,
Hold = 2,
Released = 3,
}
impl From<u8> for KeyState {
fn from(value: u8) -> Self {
match value {
1 => KeyState::Pressed,
2 => KeyState::Hold,
3 => KeyState::Released,
0 | _ => KeyState::Idle,
}
}
}
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum KeyCode {
JoyUp = 0x01,
JoyDown = 0x02,
JoyLeft = 0x03,
JoyRight = 0x04,
JoyCenter = 0x05,
BtnLeft1 = 0x06,
BtnRight1 = 0x07,
BtnLeft2 = 0x11,
BtnRight2 = 0x12,
Backspace = 0x08,
Tab = 0x09,
Enter = 0x0A,
ModAlt = 0xA1,
ModShiftLeft = 0xA2,
ModShiftRight = 0xA3,
ModSym = 0xA4,
ModCtrl = 0xA5,
Esc = 0xB1,
Left = 0xB4,
Up = 0xB5,
Down = 0xB6,
Right = 0xB7,
Break = 0xD0,
Insert = 0xD1,
Home = 0xD2,
Del = 0xD4,
End = 0xD5,
PageUp = 0xD6,
PageDown = 0xD7,
CapsLock = 0xC1,
F1 = 0x81,
F2 = 0x82,
F3 = 0x83,
F4 = 0x84,
F5 = 0x85,
F6 = 0x86,
F7 = 0x87,
F8 = 0x88,
F9 = 0x89,
F10 = 0x90,
Char(char),
Unknown(u8),
}
impl Into<u8> for KeyCode {
fn into(self) -> u8 {
match self {
KeyCode::JoyUp => 0x01,
KeyCode::JoyDown => 0x02,
KeyCode::JoyLeft => 0x03,
KeyCode::JoyRight => 0x04,
KeyCode::JoyCenter => 0x05,
KeyCode::BtnLeft1 => 0x06,
KeyCode::BtnRight1 => 0x07,
KeyCode::BtnLeft2 => 0x11,
KeyCode::BtnRight2 => 0x12,
KeyCode::Backspace => 0x08,
KeyCode::Tab => 0x09,
KeyCode::Enter => 0x0A,
KeyCode::ModAlt => 0xA1,
KeyCode::ModShiftLeft => 0xA2,
KeyCode::ModShiftRight => 0xA3,
KeyCode::ModSym => 0xA4,
KeyCode::ModCtrl => 0xA5,
KeyCode::Esc => 0xB1,
KeyCode::Left => 0xB4,
KeyCode::Up => 0xB5,
KeyCode::Down => 0xB6,
KeyCode::Right => 0xB7,
KeyCode::Break => 0xD0,
KeyCode::Insert => 0xD1,
KeyCode::Home => 0xD2,
KeyCode::Del => 0xD4,
KeyCode::End => 0xD5,
KeyCode::PageUp => 0xD6,
KeyCode::PageDown => 0xD7,
KeyCode::CapsLock => 0xC1,
KeyCode::F1 => 0x81,
KeyCode::F2 => 0x82,
KeyCode::F3 => 0x83,
KeyCode::F4 => 0x84,
KeyCode::F5 => 0x85,
KeyCode::F6 => 0x86,
KeyCode::F7 => 0x87,
KeyCode::F8 => 0x88,
KeyCode::F9 => 0x89,
KeyCode::F10 => 0x90,
KeyCode::Char(char) => char as u8,
KeyCode::Unknown(i) => i,
}
}
}
impl From<u8> for KeyCode {
fn from(value: u8) -> Self {
match value {
0x01 => Self::JoyUp,
0x02 => Self::JoyDown,
0x03 => Self::JoyLeft,
0x04 => Self::JoyRight,
0x05 => Self::JoyCenter,
0x06 => Self::BtnLeft1,
0x07 => Self::BtnRight1,
0x08 => Self::Backspace,
0x09 => Self::Tab,
0x0A => Self::Enter,
0x11 => Self::BtnLeft2,
0x12 => Self::BtnRight2,
0xA1 => Self::ModAlt,
0xA2 => Self::ModShiftLeft,
0xA3 => Self::ModShiftRight,
0xA4 => Self::ModSym,
0xA5 => Self::ModCtrl,
0xB1 => Self::Esc,
0xB4 => Self::Left,
0xB5 => Self::Up,
0xB6 => Self::Down,
0xB7 => Self::Right,
0xC1 => Self::CapsLock,
0xD0 => Self::Break,
0xD1 => Self::Insert,
0xD2 => Self::Home,
0xD4 => Self::Del,
0xD5 => Self::End,
0xD6 => Self::PageUp,
0xD7 => Self::PageDown,
0x81 => Self::F1,
0x82 => Self::F2,
0x83 => Self::F3,
0x84 => Self::F4,
0x85 => Self::F5,
0x86 => Self::F6,
0x87 => Self::F7,
0x88 => Self::F8,
0x89 => Self::F9,
0x90 => Self::F10,
_ => match char::from_u32(value as u32) {
Some(c) => Self::Char(c),
None => Self::Unknown(value),
},
}
}
}
pub type GetKeyAbi = extern "C" fn() -> KeyEventC;
#[unsafe(no_mangle)]
pub extern "C" fn get_key() -> KeyEventC {
let f: GetKeyAbi =
unsafe { core::mem::transmute(CALL_ABI_TABLE[CallAbiTable::GetKey as usize]) };
f()
}
}
#[repr(C)]
@@ -88,8 +297,8 @@ pub enum RngRequest {
pub type GenRand = extern "C" fn(req: &mut RngRequest);
#[allow(unused)]
pub fn gen_rand(req: &mut RngRequest) {
#[unsafe(no_mangle)]
pub extern "C" fn gen_rand(req: &mut RngRequest) {
unsafe {
let ptr = CALL_ABI_TABLE[CallAbiTable::GenRand as usize];
let f: GenRand = core::mem::transmute(ptr);
@@ -104,12 +313,17 @@ pub type ListDir = extern "C" fn(
file_len: usize,
) -> usize;
#[allow(unused)]
pub fn list_dir(path: &str, files: &mut [Option<DirEntry>]) -> usize {
#[unsafe(no_mangle)]
pub extern "C" fn list_dir(
str: *const u8,
len: usize,
files: *mut Option<DirEntry>,
file_len: usize,
) -> usize {
unsafe {
let ptr = CALL_ABI_TABLE[CallAbiTable::ListDir as usize];
let f: ListDir = core::mem::transmute(ptr);
f(path.as_ptr(), path.len(), files.as_mut_ptr(), files.len())
f(str, len, files, file_len)
}
}
@@ -121,28 +335,28 @@ pub type ReadFile = extern "C" fn(
buf_len: usize,
) -> usize;
#[allow(unused)]
pub fn read_file(file: &str, read_from: usize, buf: &mut [u8]) -> usize {
#[unsafe(no_mangle)]
pub extern "C" fn read_file(
str: *const u8,
len: usize,
read_from: usize,
buf: *mut u8,
buf_len: usize,
) -> usize {
unsafe {
let ptr = CALL_ABI_TABLE[CallAbiTable::ReadFile as usize];
let f: ReadFile = core::mem::transmute(ptr);
f(
file.as_ptr(),
file.len(),
read_from,
buf.as_mut_ptr(),
buf.len(),
)
f(str, len, read_from, buf, buf_len)
}
}
pub type FileLen = extern "C" fn(str: *const u8, len: usize) -> usize;
#[allow(unused)]
pub fn file_len(file: &str) -> usize {
#[unsafe(no_mangle)]
pub extern "C" fn file_len(str: *const u8, len: usize) -> usize {
unsafe {
let ptr = CALL_ABI_TABLE[CallAbiTable::FileLen as usize];
let f: FileLen = core::mem::transmute(ptr);
f(file.as_ptr(), file.len())
f(str, len)
}
}