Skip to content
Snippets Groups Projects
Commit 3be6ed1d authored by rnd's avatar rnd
Browse files

[API] Introduce card10-sys as direct dependency.

parent ecb77a01
Branches
No related tags found
No related merge requests found
Showing
with 147 additions and 90 deletions
...@@ -81,10 +81,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" ...@@ -81,10 +81,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "card10-l0dable" name = "card10-l0dable"
version = "0.1.1" version = "0.1.1"
dependencies = [ dependencies = [
"bindgen 0.51.0 (registry+https://github.com/rust-lang/crates.io-index)", "card10-sys 0.1.0",
"cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)",
"panic-abort 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"r0 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
......
[workspace] [workspace]
members = [ members = [
"card10-sys", "card10-sys",
"l0dable", "card10-l0dable",
"example", "example",
"rkanoid", "rkanoid",
] ]
......
...@@ -76,7 +76,7 @@ extension (e.g `example` must be renamed as `example.elf`). ...@@ -76,7 +76,7 @@ extension (e.g `example` must be renamed as `example.elf`).
| Crate | Description | | Crate | Description |
| ---- | --- | | ---- | --- |
| l0dable | Helper crate for building l0dables | | card10-l0dable | Helper crate for building l0dables |
| example | l0dable example | | example | l0dable example |
| rkanoid | Arkanoid clone | | rkanoid | Arkanoid clone |
...@@ -85,14 +85,14 @@ extension (e.g `example` must be renamed as `example.elf`). ...@@ -85,14 +85,14 @@ extension (e.g `example` must be renamed as `example.elf`).
### How to update the firmware bindings ### How to update the firmware bindings
1) Update the `c/` submodule to the latest firmware state. 1) Update the `card10-sys/firmware` submodule to the latest firmware state.
2) Rebuild the firmware as described above. 2) Rebuild the firmware as described above.
3) Run the following script from the project root directory 3) Run the following script from the project root directory
```shell ```shell
python c/epicardium/api/genapi.py -H c/epicardium/epicardium.h -c l0dable/src/client.c -s l0dable/src/server.c python card10-sys/firmware/epicardium/api/genapi.py -H card10-sys/firmware/epicardium/epicardium.h -c card10-sys/vendor/client.c -s card10-sys/vendor/server.c
``` ```
4) Rebuild your app :) 4) Rebuild your app :)
......
...@@ -14,20 +14,8 @@ keywords = [ ...@@ -14,20 +14,8 @@ keywords = [
"l0dable", "l0dable",
] ]
categories = ["no-std"] categories = ["no-std"]
description = """ description = "make l0dables for the Card10 (CCCamp 2019) badge"
make l0dables for the Card10 (CCCamp 2019) badge
"""
include = [
"**/*.rs",
"Cargo.toml",
"l0dable.ld",
"../c/",
]
[dependencies] [dependencies]
r0 = "0.2" card10-sys = { path = "../card10-sys", version = "^0.1" }
panic-abort = "0.3"
[build-dependencies]
cc = "1.0"
bindgen = "0.51"
File moved
File moved
...@@ -4,7 +4,7 @@ use core::{ ...@@ -4,7 +4,7 @@ use core::{
mem::MaybeUninit, mem::MaybeUninit,
}; };
use crate::{bindings::*, errno}; use card10_sys::*;
pub trait SensorType { pub trait SensorType {
/// sensor_type in C, sensor_id in Python /// sensor_type in C, sensor_id in Python
......
use card10_sys::*;
use core::mem::uninitialized; use core::mem::uninitialized;
use super::bindings::*;
pub struct BME680; pub struct BME680;
pub type SensorData = bme680_sensor_data; pub type SensorData = bme680_sensor_data;
...@@ -24,6 +24,8 @@ impl BME680 { ...@@ -24,6 +24,8 @@ impl BME680 {
impl Drop for BME680 { impl Drop for BME680 {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { epic_bme680_deinit(); } unsafe {
epic_bme680_deinit();
}
} }
} }
use super::bindings::*; use card10_sys::*;
pub struct Buttons { pub struct Buttons {
state: u32, state: u32,
...@@ -6,12 +6,11 @@ pub struct Buttons { ...@@ -6,12 +6,11 @@ pub struct Buttons {
impl Buttons { impl Buttons {
pub fn read() -> Self { pub fn read() -> Self {
let mask = let mask = epic_button_BUTTON_LEFT_BOTTOM
epic_button_BUTTON_LEFT_BOTTOM | | epic_button_BUTTON_RIGHT_BOTTOM
epic_button_BUTTON_RIGHT_BOTTOM | | epic_button_BUTTON_LEFT_TOP
epic_button_BUTTON_LEFT_TOP | | epic_button_BUTTON_RIGHT_TOP
epic_button_BUTTON_RIGHT_TOP | | epic_button_BUTTON_RESET;
epic_button_BUTTON_RESET;
let state = unsafe { epic_buttons_read(mask as u8) }.into(); let state = unsafe { epic_buttons_read(mask as u8) }.into();
Buttons { state } Buttons { state }
} }
......
use super::bindings::*;
use super::framebuffer::FrameBuffer; use super::framebuffer::FrameBuffer;
use card10_sys::*;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
#[repr(C)] #[repr(C)]
...@@ -32,9 +32,7 @@ impl Color { ...@@ -32,9 +32,7 @@ impl Color {
pub fn rgb8(r8: u8, g8: u8, b8: u8) -> Self { pub fn rgb8(r8: u8, g8: u8, b8: u8) -> Self {
let c = let c =
((u16::from(r8) & 0xF8) << 8) | ((u16::from(r8) & 0xF8) << 8) | ((u16::from(g8) & 0xFA) << 3) | (u16::from(b8) & 0xF8);
((u16::from(g8) & 0xFA) << 3) |
(u16::from(b8) & 0xF8);
Color(c) Color(c)
} }
...@@ -84,16 +82,22 @@ impl Display { ...@@ -84,16 +82,22 @@ impl Display {
pub const FONT_H: u16 = 20; pub const FONT_H: u16 = 20;
pub fn open() -> Self { pub fn open() -> Self {
unsafe { epic_disp_open(); } unsafe {
epic_disp_open();
}
Display Display
} }
pub fn update(&self) { pub fn update(&self) {
unsafe { epic_disp_update(); } unsafe {
epic_disp_update();
}
} }
pub fn clear(&self, color: Color) { pub fn clear(&self, color: Color) {
unsafe { epic_disp_clear(color.0); } unsafe {
epic_disp_clear(color.0);
}
} }
/// s must be 0-terminated /// s must be 0-terminated
...@@ -109,19 +113,45 @@ impl Display { ...@@ -109,19 +113,45 @@ impl Display {
} }
} }
pub fn line(&self, x1: u16, y1: u16, x2: u16, y2: u16, color: Color, linestyle: LineStyle, pixelsize: u16) { pub fn line(
&self,
x1: u16,
y1: u16,
x2: u16,
y2: u16,
color: Color,
linestyle: LineStyle,
pixelsize: u16,
) {
unsafe { unsafe {
epic_disp_line(x1, y1, x2, y2, color.0, linestyle as u32, pixelsize); epic_disp_line(x1, y1, x2, y2, color.0, linestyle as u32, pixelsize);
} }
} }
pub fn rect(&self, x1: u16, y1: u16, x2: u16, y2: u16, color: Color, fillstyle: FillStyle, pixelsize: u16) { pub fn rect(
&self,
x1: u16,
y1: u16,
x2: u16,
y2: u16,
color: Color,
fillstyle: FillStyle,
pixelsize: u16,
) {
unsafe { unsafe {
epic_disp_rect(x1, y1, x2, y2, color.0, fillstyle as u32, pixelsize); epic_disp_rect(x1, y1, x2, y2, color.0, fillstyle as u32, pixelsize);
} }
} }
pub fn circ(&self, x: u16, y: u16, rad: u16, color: Color, fillstyle: FillStyle, pixelsize: u16) { pub fn circ(
&self,
x: u16,
y: u16,
rad: u16,
color: Color,
fillstyle: FillStyle,
pixelsize: u16,
) {
unsafe { unsafe {
epic_disp_circ(x, y, rad, color.0, fillstyle as u32, pixelsize); epic_disp_circ(x, y, rad, color.0, fillstyle as u32, pixelsize);
} }
...@@ -134,6 +164,8 @@ impl Display { ...@@ -134,6 +164,8 @@ impl Display {
impl Drop for Display { impl Drop for Display {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { epic_disp_close(); } unsafe {
epic_disp_close();
}
} }
} }
File moved
use crate::Display;
use card10_sys::*;
use core::mem::{transmute, uninitialized}; use core::mem::{transmute, uninitialized};
use core::ops::{Index, IndexMut}; use core::ops::{Index, IndexMut};
use crate::bindings::*;
use crate::Display;
mod font; mod font;
pub use font::*; pub use font::*;
...@@ -13,14 +13,11 @@ pub struct FrameBuffer<'d> { ...@@ -13,14 +13,11 @@ pub struct FrameBuffer<'d> {
buffer: disp_framebuffer, buffer: disp_framebuffer,
} }
impl<'d> FrameBuffer<'d> { impl<'d> FrameBuffer<'d> {
pub fn uninitialized(display: &'d Display) -> Self { pub fn uninitialized(display: &'d Display) -> Self {
FrameBuffer { FrameBuffer {
_display: display, _display: display,
buffer: unsafe { buffer: unsafe { uninitialized() },
uninitialized()
},
} }
} }
...@@ -33,18 +30,26 @@ impl<'d> FrameBuffer<'d> { ...@@ -33,18 +30,26 @@ impl<'d> FrameBuffer<'d> {
pub fn clear(&mut self, color: RawColor) { pub fn clear(&mut self, color: RawColor) {
for y in 0..Display::H { for y in 0..Display::H {
for x in 0..Display::W { for x in 0..Display::W {
let bytes: &mut RawColor = unsafe { let bytes: &mut RawColor =
transmute(&mut self.buffer.fb[y as usize][x as usize]) unsafe { transmute(&mut self.buffer.fb[y as usize][x as usize]) };
};
*bytes = color; *bytes = color;
} }
} }
} }
pub fn text<'a, 'f>(&'a mut self, x: isize, y: isize, font: &'f Font, color: RawColor) -> TextRenderer<'a, 'd, 'f> { pub fn text<'a, 'f>(
&'a mut self,
x: isize,
y: isize,
font: &'f Font,
color: RawColor,
) -> TextRenderer<'a, 'd, 'f> {
TextRenderer { TextRenderer {
framebuffer: self, framebuffer: self,
x, y, font, color, x,
y,
font,
color,
} }
} }
} }
...@@ -54,9 +59,7 @@ impl<'d> Index<(u16, u16)> for FrameBuffer<'d> { ...@@ -54,9 +59,7 @@ impl<'d> Index<(u16, u16)> for FrameBuffer<'d> {
fn index(&self, (x, y): (u16, u16)) -> &Self::Output { fn index(&self, (x, y): (u16, u16)) -> &Self::Output {
let x = usize::from(Display::W - 1 - x); let x = usize::from(Display::W - 1 - x);
let y = usize::from(Display::H - 1 - y); let y = usize::from(Display::H - 1 - y);
unsafe { unsafe { transmute(&self.buffer.fb[y][x]) }
transmute(&self.buffer.fb[y][x])
}
} }
} }
...@@ -64,9 +67,7 @@ impl<'d> IndexMut<(u16, u16)> for FrameBuffer<'d> { ...@@ -64,9 +67,7 @@ impl<'d> IndexMut<(u16, u16)> for FrameBuffer<'d> {
fn index_mut(&mut self, (x, y): (u16, u16)) -> &mut Self::Output { fn index_mut(&mut self, (x, y): (u16, u16)) -> &mut Self::Output {
let x = usize::from(Display::W - 1 - x); let x = usize::from(Display::W - 1 - x);
let y = usize::from(Display::H - 1 - y); let y = usize::from(Display::H - 1 - y);
unsafe { unsafe { transmute(&mut self.buffer.fb[y][x]) }
transmute(&mut self.buffer.fb[y][x])
}
} }
} }
......
use core::str::from_utf8_unchecked;
use core::mem::uninitialized; use core::mem::uninitialized;
use super::bindings::*; use core::str::from_utf8_unchecked;
use card10_sys::*;
type Result<T> = core::result::Result<T, Error>; type Result<T> = core::result::Result<T, Error>;
...@@ -28,9 +29,7 @@ impl From<i32> for Error { ...@@ -28,9 +29,7 @@ impl From<i32> for Error {
pub fn read_dir(path: &str) -> Result<ReadDir> { pub fn read_dir(path: &str) -> Result<ReadDir> {
let pathbuf = crate::str_to_cstr(path); let pathbuf = crate::str_to_cstr(path);
Error::check(|| unsafe { Error::check(|| unsafe { epic_file_opendir(pathbuf.as_ptr()) }).map(|fd| ReadDir { fd })
epic_file_opendir(pathbuf.as_ptr())
}).map(|fd| ReadDir { fd })
} }
pub struct ReadDir { pub struct ReadDir {
...@@ -68,7 +67,7 @@ pub struct epic_stat { ...@@ -68,7 +67,7 @@ pub struct epic_stat {
/// not u32 as generated by bindgen /// not u32 as generated by bindgen
pub type_: epic_stat_type, pub type_: epic_stat_type,
pub size: u32, pub size: u32,
pub name: [super::ctypes::c_char; 256usize], pub name: [ctypes::c_char; 256usize],
pub _reserved: [u8; 12usize], pub _reserved: [u8; 12usize],
} }
...@@ -97,20 +96,15 @@ impl DirStat { ...@@ -97,20 +96,15 @@ impl DirStat {
} }
pub fn name(&self) -> &str { pub fn name(&self) -> &str {
let len = self.entry.name.iter().position(|b| *b == 0) let len = self.entry.name.iter().position(|b| *b == 0).unwrap_or(0);
.unwrap_or(0); unsafe { from_utf8_unchecked(&self.entry.name[0..len]) }
unsafe {
from_utf8_unchecked(&self.entry.name[0..len])
}
} }
} }
pub fn rename(from: &str, to: &str) -> Result<()> { pub fn rename(from: &str, to: &str) -> Result<()> {
let frombuf = crate::str_to_cstr(from); let frombuf = crate::str_to_cstr(from);
let tobuf = crate::str_to_cstr(to); let tobuf = crate::str_to_cstr(to);
Error::check(|| unsafe { Error::check(|| unsafe { epic_file_rename(frombuf.as_ptr(), tobuf.as_ptr()) }).map(|_| ())
epic_file_rename(frombuf.as_ptr(), tobuf.as_ptr())
}).map(|_| ())
} }
pub struct File { pub struct File {
...@@ -121,21 +115,22 @@ impl File { ...@@ -121,21 +115,22 @@ impl File {
pub fn open(path: &str) -> Result<File> { pub fn open(path: &str) -> Result<File> {
let pathbuf = crate::str_to_cstr(path); let pathbuf = crate::str_to_cstr(path);
let modebuf = b"r\0"; let modebuf = b"r\0";
Error::check(|| unsafe { Error::check(|| unsafe { epic_file_open(pathbuf.as_ptr(), modebuf.as_ptr()) })
epic_file_open(pathbuf.as_ptr(), modebuf.as_ptr()) .map(|fd| File { fd })
}).map(|fd| File { fd })
} }
pub fn read<'a>(&mut self, buf: &'a mut [u8]) -> Result<&'a [u8]> { pub fn read<'a>(&mut self, buf: &'a mut [u8]) -> Result<&'a [u8]> {
let bytes = Error::check(|| unsafe { let bytes =
epic_file_read(self.fd, buf.as_ptr() as *mut _, buf.len()) Error::check(|| unsafe { epic_file_read(self.fd, buf.as_ptr() as *mut _, buf.len()) })?
})? as usize; as usize;
Ok(&buf[0..bytes]) Ok(&buf[0..bytes])
} }
} }
impl Drop for File { impl Drop for File {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { epic_file_close(self.fd); } unsafe {
epic_file_close(self.fd);
}
} }
} }
#![no_std]
mod os;
pub use os::*;
mod display;
pub use display::{Color, Display, FillStyle, LineStyle};
mod buttons;
pub mod framebuffer;
pub use buttons::Buttons;
pub mod uart;
pub const UART: uart::Uart = uart::Uart;
mod light_sensor;
pub use light_sensor::LightSensor;
mod rtc;
pub mod trng;
pub mod vibra;
pub use rtc::{MilliSeconds, Seconds, Time};
mod fmt_buffer;
pub use fmt_buffer::{str_to_cstr, FmtBuffer};
mod bme680;
pub use bme680::BME680;
mod bhi160;
pub use bhi160::{
Accelerometer, Error as BHI160Error, Gyroscope, Orientation, Sensor as BHI160,
SensorData as BHI160Data,
};
pub mod fs;
/// Type check the user-supplied entry function.
#[macro_export]
macro_rules! main {
($path:path) => {
#[export_name = "main"]
pub unsafe fn __main() {
// type check the given path
let f: fn() = $path;
f()
}
};
}
use super::bindings::*; use card10_sys::*;
pub struct LightSensor { pub struct LightSensor {
// Prevent creation of this struct by all but this module. // Prevent creation of this struct by all but this module.
...@@ -25,6 +25,8 @@ impl LightSensor { ...@@ -25,6 +25,8 @@ impl LightSensor {
impl Drop for LightSensor { impl Drop for LightSensor {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { epic_light_sensor_stop(); } unsafe {
epic_light_sensor_stop();
}
} }
} }
use super::bindings::*; use card10_sys::*;
pub fn exec(path: &str) -> ! { pub fn exec(path: &str) -> ! {
let mut pathbuf = super::str_to_cstr(path); let mut pathbuf = super::str_to_cstr(path);
......
use card10_sys::*;
use core::ops::Sub; use core::ops::Sub;
use super::bindings::*;
pub trait Time { pub trait Time {
fn time() -> Self; fn time() -> Self;
......
use super::bindings::*; use card10_sys::*;
pub fn read(dest: &mut [u8]) -> bool { pub fn read(dest: &mut [u8]) -> bool {
unsafe { epic_trng_read(dest.as_mut_ptr(), dest.len()) != 0 } unsafe { epic_trng_read(dest.as_mut_ptr(), dest.len()) != 0 }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment