1extern crate core;
6use core::cell::UnsafeCell;
7use core::ops::*;
8
9use crate::types::*;
10
11extern "C" {
12 fn xwds_i2cm_xfer(i2cm: *mut XwdsI2cm,
13 msg: *mut Msg,
14 to: XwTm) -> XwEr;
15 fn xwds_i2cm_abort(i2cm: *mut XwdsI2cm,
16 address: u16, addrmode: u16,
17 to: XwTm) -> XwEr;
18}
19
20#[cfg(target_pointer_width = "32")]
22pub const SIZEOF_XWDS_I2CM: usize = 224;
23
24#[cfg(target_pointer_width = "64")]
26pub const SIZEOF_XWDS_I2CM: usize = 448;
27
28#[repr(C)]
30#[cfg_attr(target_pointer_width = "32", repr(align(8)))]
31#[cfg_attr(target_pointer_width = "64", repr(align(16)))]
32pub struct XwdsI2cm {
33 pub(crate) obj: [u8; SIZEOF_XWDS_I2CM],
34}
35
36#[repr(C)]
38pub struct I2cm {
39 pub(crate) i2cm: UnsafeCell<XwdsI2cm>,
40}
41
42impl I2cm {
43 pub fn xfer(&self,
63 addr: u16, flag: MsgFlag, data: &mut [u8], size: XwSz,
64 to: XwTm) -> XwEr {
65 unsafe {
66 let mut msg: Msg = Msg {
67 addr: addr,
68 flag: flag.0,
69 data: data.as_mut_ptr(),
70 size: size,
71 };
72 xwds_i2cm_xfer(self.i2cm.get(), &mut msg as *mut Msg, to)
73 }
74 }
75
76 pub fn abort(&self,
94 addr: u16, addrflag: MsgFlag,
95 to: XwTm) -> XwEr {
96 unsafe {
97 xwds_i2cm_abort(self.i2cm.get(), addr, addrflag.0, to)
98 }
99 }
100}
101
102#[repr(C)]
104struct Msg {
105 addr: u16,
106 flag: u16,
107 data: *mut u8,
108 size: XwSz,
109}
110
111pub struct MsgFlag(u16);
113
114impl MsgFlag {
115 pub const ADDR7BIT: MsgFlag = MsgFlag(0);
117 pub const ADDR10BIT: MsgFlag = MsgFlag(bit!(0));
119 pub const ADDRMSG: MsgFlag = MsgFlag(bit!(0));
121
122 pub const WR: MsgFlag = MsgFlag(0);
124 pub const RD: MsgFlag = MsgFlag(bit!(1));
126 pub const DIRMSG: MsgFlag = MsgFlag(bit!(1));
128
129 pub const START: MsgFlag = MsgFlag(bit!(2));
131
132 pub const STOP: MsgFlag = MsgFlag(bit!(3));
134
135}
136
137impl From<u16> for MsgFlag {
138 fn from(flag: u16) -> Self {
139 Self(flag)
140 }
141}
142
143impl BitAnd for MsgFlag {
144 type Output = Self;
145
146 fn bitand(self, other: Self) -> Self {
147 Self(self.0 & other.0)
148 }
149}
150
151impl BitAndAssign for MsgFlag {
152 fn bitand_assign(&mut self, other: Self) {
153 self.0 &= other.0;
154 }
155}
156
157impl BitOr for MsgFlag {
158 type Output = Self;
159
160 fn bitor(self, other: Self) -> Self {
161 Self(self.0 | other.0)
162 }
163}
164
165impl BitOrAssign for MsgFlag {
166 fn bitor_assign(&mut self, other: Self) {
167 self.0 |= other.0;
168 }
169}
170
171impl BitXor for MsgFlag {
172 type Output = Self;
173
174 fn bitxor(self, other: Self) -> Self {
175 Self(self.0 ^ other.0)
176 }
177}
178
179impl BitXorAssign for MsgFlag {
180 fn bitxor_assign(&mut self, other: Self) {
181 self.0 ^= other.0;
182 }
183}