xwrust/xwds/spim.rs
1//! XWOS RUST:SPI主机模式控制器
2//! ========
3//!
4
5extern crate core;
6use core::cell::UnsafeCell;
7
8use crate::types::*;
9
10extern "C" {
11 fn xwds_spim_buscfg(spim: *mut XwdsSpim, cfgid: XwId, to: XwTm) -> XwEr;
12 fn xwds_spim_xfer(spim: *mut XwdsSpim,
13 txd: *const u8, rxd: *mut u8, size: *mut XwSz,
14 to: XwTm) -> XwEr;
15 fn xwds_spim_abort(spim: *mut XwdsSpim, to: XwTm) -> XwEr;
16}
17
18/// XWOS自旋锁占用的内存大小
19#[cfg(target_pointer_width = "32")]
20pub const SIZEOF_XWDS_SPIM: usize = 144;
21
22/// XWOS自旋锁占用的内存大小
23#[cfg(target_pointer_width = "64")]
24pub const SIZEOF_XWDS_SPIM: usize = 288;
25
26/// SPI主机模式控制器
27#[repr(C)]
28#[cfg_attr(target_pointer_width = "32", repr(align(8)))]
29#[cfg_attr(target_pointer_width = "64", repr(align(16)))]
30pub struct XwdsSpim {
31 pub(crate) obj: [u8; SIZEOF_XWDS_SPIM],
32}
33
34/// SPI主机模式控制器
35#[repr(C)]
36pub struct Spim {
37 pub(crate) spim: UnsafeCell<XwdsSpim>,
38}
39
40impl Spim {
41 /// 配置总线
42 ///
43 /// # 参数说明
44 ///
45 /// + cfgid: 总线配置ID
46 /// + to: 期望唤醒的时间点
47 ///
48 /// # 错误码
49 ///
50 /// + [`-ENOSYS`] 不支持配置总线操作
51 /// + [`-ECHRNG`] 配置ID不在配置表范围内
52 /// + [`-ETIMEDOUT`] 超时
53 ///
54 /// [`-ENOSYS`]: crate::errno::ENOSYS
55 /// [`-ECHRNG`]: crate::errno::ECHRNG
56 /// [`-ETIMEDOUT`]: crate::errno::ETIMEDOUT
57 pub fn buscfg(&self, cfgid: XwId, to: XwTm) -> XwEr {
58 unsafe {
59 xwds_spim_buscfg(self.spim.get(), cfgid, to)
60 }
61 }
62
63 /// 传输SPI消息
64 ///
65 /// # 参数说明
66 ///
67 /// + txd: 发送数据的缓冲区
68 /// + rxd: 接收数据的缓冲区
69 /// + size: 传输的大小
70 /// + (I) 作为输入时,表示缓冲区大小(单位:字节)
71 /// + (O) 作为输出时,返回实际传输的数据大小
72 /// + to: 期望唤醒的时间点
73 ///
74 /// # 错误码
75 ///
76 /// + [`-EBUSY`] 总线繁忙
77 /// + [`-EIO`] 传输错误
78 /// + [`-ETIMEDOUT`] 传输超时
79 ///
80 /// [`-EBUSY`]: crate::errno::EBUSY
81 /// [`-EIO`]: crate::errno::EIO
82 /// [`-ETIMEDOUT`]: crate::errno::ETIMEDOUT
83 pub fn xfer(&self,
84 txd: &[u8], rxd: &mut [u8], size: &mut XwSz,
85 to: XwTm) -> XwEr {
86 unsafe {
87 xwds_spim_xfer(self.spim.get(),
88 txd.as_ptr(), rxd.as_mut_ptr(), size as *mut XwSz,
89 to)
90 }
91 }
92
93 /// 中止SPI总线传输
94 ///
95 /// # 参数说明
96 ///
97 /// + to: 期望唤醒的时间点
98 ///
99 /// # 错误码
100 ///
101 /// + [`-ETIMEDOUT`] 传输超时
102 ///
103 /// [`-ETIMEDOUT`]: crate::errno::ETIMEDOUT
104 pub fn abort(&self,
105 to: XwTm) -> XwEr {
106 unsafe {
107 xwds_spim_abort(self.spim.get(), to)
108 }
109 }
110}