Skip to content

Instantly share code, notes, and snippets.

@JayKickliter
Last active September 13, 2017 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayKickliter/a1b4c8dd5b14b993a148d12d94cb3ccb to your computer and use it in GitHub Desktop.
Save JayKickliter/a1b4c8dd5b14b993a148d12d94cb3ccb to your computer and use it in GitHub Desktop.
Generating Tock userland header from rust source

Common rust source for both rust driver and userland code.

use core::intrinsics::transmute;

#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub enum Partnum {
    CC1121,
    CC1120,
    CC1125,
    CC1175,
    Invalid,
}

impl From<u8> for Partnum {
    fn from(val: u8) -> Partnum {
        match val {
            0x40 => Partnum::CC1121,
            0x48 => Partnum::CC1120,
            0x58 => Partnum::CC1125,
            0x5A => Partnum::CC1175,
            _ => Partnum::Invalid,
        }
    }
}


#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub enum Command {
    Supported = 0,
    Partnum = 1,
    NotSupported,
}

impl From<Command> for usize {
    fn from(cmd: Command) -> usize {
        unsafe { transmute(cmd) }
    }
}

impl From<usize> for Command {
    fn from(val: usize) -> Command {
        match val {
            0 => Command::Supported,
            1 => Command::Partnum,
            _ => Command::NotSupported,
        }
    }
}

Generated c header:

#ifndef cheddar_generated_cc112x_h
#define cheddar_generated_cc112x_h


#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>



typedef enum Partnum {
	Partnum_CC1121,
	Partnum_CC1120,
	Partnum_CC1125,
	Partnum_CC1175,
	Partnum_Invalid,
} Partnum;

typedef enum Command {
	Command_Supported = 0,
	Command_Partnum = 1,
	Command_NotSupported,
} Command;



#ifdef __cplusplus
}
#endif


#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment