1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use core::u16;
use core::ptr;
use crate::{
StepSize,
BlockAction,
EventOutput,
};
bitflags! {
#[repr(transparent)]
#[derive(Default)]
pub(crate) struct RawBlockTransferCtrl: u16 {
const STEPSIZE_2 = 0x8000;
const STEPSIZE_1 = 0x4000;
const STEPSIZE_0 = 0x2000;
const STEPSEL = 0x1000;
const DSTINC = 0x0800;
const SRCINC = 0x0400;
const BEATSIZE_1 = 0x0200;
const BEATSIZE_0 = 0x0100;
const BLOCKACT_1 = 0x0010;
const BLOCKACT_0 = 0x0008;
const EVOSEL_1 = 0x0004;
const EVOSEL_0 = 0x0002;
const VALID = 0x0001;
}
}
#[repr(C)]
#[derive(Debug)]
pub struct TransferDescriptor {
btctrl: RawBlockTransferCtrl,
btcnt: u16,
srcaddr: *const (),
dstaddr: *const (),
descaddr: *mut TransferDescriptor,
}
impl Default for TransferDescriptor {
fn default() -> Self {
TransferDescriptor {
srcaddr: ptr::null(),
dstaddr: ptr::null(),
descaddr: ptr::null_mut(),
..Default::default()
}
}
}
impl TransferDescriptor {
pub fn new() -> TransferDescriptor {
Default::default()
}
pub fn get_src_addr(&self) -> *const () {
self.srcaddr
}
pub fn get_dst_addr(&self) -> *const () {
self.dstaddr
}
pub fn get_next_desc_addr(&self) -> *mut TransferDescriptor {
self.descaddr
}
pub fn set_src_addr(&mut self, addr: *const ()) {
self.srcaddr = addr;
}
pub fn set_dst_addr(&mut self, addr: *const ()) {
self.dstaddr = addr;
}
pub fn set_valid(&mut self) {
self.btctrl.insert(RawBlockTransferCtrl::VALID);
}
pub fn set_invalid(&mut self) {
self.btctrl.remove(RawBlockTransferCtrl::VALID);
}
pub fn is_valid(&self) -> bool {
self.btctrl.intersects(RawBlockTransferCtrl::VALID)
}
pub fn set_block_count(&mut self, block_count: u16) {
self.btcnt = block_count;
}
pub fn get_block_transfer_count(&self) -> u16 {
self.btcnt
}
pub fn get_step_size(&self) -> StepSize {
StepSize::from((self.btctrl.bits() & 0xe000) >> 13)
}
pub fn set_step_size(&mut self, step_size: StepSize) {
let value = step_size as u8;
self.btctrl.set(RawBlockTransferCtrl::STEPSIZE_2, value & 0b100 != 0);
self.btctrl.set(RawBlockTransferCtrl::STEPSIZE_1, value & 0b010 != 0);
self.btctrl.set(RawBlockTransferCtrl::STEPSIZE_0, value & 0b001 != 0);
}
pub fn get_step_selection(&self) -> bool {
self.btctrl.intersects(RawBlockTransferCtrl::STEPSEL)
}
pub fn set_step_selection(&mut self, step_sel: bool) {
self.btctrl.set(RawBlockTransferCtrl::STEPSEL, step_sel);
}
pub fn get_dest_addr_increment(&self) -> bool {
self.btctrl.intersects(RawBlockTransferCtrl::DSTINC)
}
pub fn set_dest_addr_increment(&mut self, increment: bool) {
self.btctrl.set(RawBlockTransferCtrl::DSTINC, increment);
}
pub fn get_src_addr_increment(&self) -> bool {
self.btctrl.intersects(RawBlockTransferCtrl::SRCINC)
}
pub fn set_src_addr_increment(&mut self, increment: bool) {
self.btctrl.set(RawBlockTransferCtrl::SRCINC, increment);
}
pub fn get_block_action(&self) -> BlockAction {
BlockAction::from((self.btctrl.bits() & 0x18) >> 3)
}
pub fn set_block_action(&mut self, block_action: BlockAction) {
let value = block_action as u8;
self.btctrl.set(RawBlockTransferCtrl::BLOCKACT_1, value & 0b10 != 0);
self.btctrl.set(RawBlockTransferCtrl::BLOCKACT_0, value & 0b01 != 0);
}
pub fn get_event_output(&self) -> EventOutput {
EventOutput::from((self.btctrl.bits() & 0x18) >> 3)
}
pub fn set_event_output(&mut self, event_output: EventOutput) {
let value = event_output as u8;
self.btctrl.set(RawBlockTransferCtrl::EVOSEL_1, value & 0b10 != 0);
self.btctrl.set(RawBlockTransferCtrl::EVOSEL_0, value & 0b01 != 0);
}
pub fn link_descriptor(&mut self, next: *mut TransferDescriptor) {
self.descaddr = next;
}
pub fn unlink_descriptor(&mut self) -> *mut TransferDescriptor {
let next = self.descaddr;
self.descaddr = ptr::null_mut();
next
}
}