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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#[derive(Clone, Debug)]
#[repr(C, align(4))]
pub struct Descriptor {
    configuration: u32,
    source_address: u32,
    destination_address: u32,
    byte_counter: u32,
    parameter: u32,
    link: u32,
}

// TODO: THIS COULD PROBABLY BE A BITFIELD LIBRARY
pub struct DescriptorConfig {
    pub source: *const (),
    pub destination: *mut (),

    // NOTE: Max is < 2^25, or < 32MiB
    pub byte_counter: usize,
    pub link: Option<*const ()>,
    pub wait_clock_cycles: u8,

    pub bmode: BModeSel,

    pub dest_width: DataWidth,
    pub dest_addr_mode: AddressMode,
    pub dest_block_size: BlockSize,
    pub dest_drq_type: DestDrqType,

    pub src_data_width: DataWidth,
    pub src_addr_mode: AddressMode,
    pub src_block_size: BlockSize,
    pub src_drq_type: SrcDrqType,
}

#[derive(Eq, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum SrcDrqType {
    Sram = 0,
    Dram = 1,
    OwaRx = 2,
    I2sPcm0Rx = 3,
    I2sPcm1Rx = 4,
    I2sPcm2Rx = 5,
    AudioCodec = 7,
    Dmic = 8,
    GpADC = 12,
    TpADC = 13,
    Uart0Rx = 14,
    Uart1Rx = 15,
    Uart2Rx = 16,
    Uart3Rx = 17,
    Uart4Rx = 18,
    Uart5Rx = 19,
    Spi0Rx = 22,
    Spi1Rx = 23,
    Usb0Ep1 = 30,
    Usb0Ep2 = 31,
    Usb0Ep3 = 32,
    Usb0Ep4 = 33,
    Usb0Ep5 = 34,
    Twi0 = 43,
    Twi1 = 44,
    Twi2 = 45,
    Twi3 = 46,
}

#[derive(Eq, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum DestDrqType {
    Sram = 0,
    Dram = 1,
    OwaTx = 2,
    I2sPcm0Tx = 3,
    I2sPcm1Tx = 4,
    I2sPcm2Tx = 5,
    AudioCodec = 7,
    IrTx = 13,
    Uart0Tx = 14,
    Uart1Tx = 15,
    Uart2Tx = 16,
    Uart3Tx = 17,
    Uart4Tx = 18,
    Uart5Tx = 19,
    Spi0Tx = 22,
    Spi1Tx = 23,
    Usb0Ep1 = 30,
    Usb0Ep2 = 31,
    Usb0Ep3 = 32,
    Usb0Ep4 = 33,
    Usb0Ep5 = 34,
    Ledc = 42,
    Twi0 = 43,
    Twi1 = 44,
    Twi2 = 45,
    Twi3 = 46,
}

// TODO: Verify bits or bytes?
#[derive(Eq, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum BlockSize {
    Byte1 = 0b00,
    Byte4 = 0b01,
    Byte8 = 0b10,
    Byte16 = 0b11,
}

#[derive(Eq, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum AddressMode {
    LinearMode = 0,
    IoMode = 1,
}

#[derive(Eq, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum DataWidth {
    Bit8 = 0b00,
    Bit16 = 0b01,
    Bit32 = 0b10,
    Bit64 = 0b11,
}

#[derive(Eq, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum BModeSel {
    Normal,
    BMode,
}

// Descriptor

impl Descriptor {
    pub fn set_source(&mut self, source: u64) {
        assert!(source < (1 << 34));
        self.source_address = source as u32;
        //                  332222222222 11 11 11111100 00000000
        //                  109876543210 98 76 54321098 76543210
        self.parameter &= 0b111111111111_11_00_11111111_11111111;
        self.parameter |= (((source >> 32) & 0b11) << 16) as u32;
    }

    pub fn set_dest(&mut self, dest: u64) {
        assert!(dest < (1 << 34));
        self.destination_address = dest as u32;
        //                  332222222222 11 11 11111100 00000000
        //                  109876543210 98 76 54321098 76543210
        self.parameter &= 0b111111111111_00_11_11111111_11111111;
        self.parameter |= (((dest >> 32) & 0b11) << 18) as u32;
    }

    pub fn end_link(&mut self) {
        self.link = 0xFFFF_F800;
    }
}

impl TryFrom<DescriptorConfig> for Descriptor {
    type Error = ();

    fn try_from(value: DescriptorConfig) -> Result<Self, Self::Error> {
        let source = value.source as usize;
        let destination = value.destination as usize;

        if source as usize >= (1 << 34) {
            return Err(());
        }
        if destination as usize >= (1 << 34) {
            return Err(());
        }
        if value.byte_counter >= (1 << 25) {
            return Err(());
        }
        if let Some(link) = value.link {
            let link = link as usize;
            if (link & 0b11) != 0 {
                return Err(());
            }
        }

        let mut descriptor = Descriptor {
            configuration: 0,
            source_address: 0,
            destination_address: 0,
            byte_counter: 0,
            parameter: 0,
            link: 0,
        };

        // Set source
        descriptor.source_address = source as u32;
        //                        332222222222 11 11 11111100 00000000
        //                        109876543210 98 76 54321098 76543210
        descriptor.parameter &= 0b111111111111_11_00_11111111_11111111;
        descriptor.parameter |= (((source >> 32) & 0b11) << 16) as u32;

        // Set dest
        descriptor.destination_address = destination as u32;
        //                        332222222222 11 11 11111100 00000000
        //                        109876543210 98 76 54321098 76543210
        descriptor.parameter &= 0b111111111111_00_11_11111111_11111111;
        descriptor.parameter |= (((destination >> 32) & 0b11) << 18) as u32;

        descriptor.byte_counter = value.byte_counter as u32;

        // Set configuration
        descriptor.configuration |= value.bmode.to_desc_bits();
        descriptor.configuration |= value.dest_width.to_desc_bits_dest();
        descriptor.configuration |= value.dest_addr_mode.to_desc_bits_dest();
        descriptor.configuration |= value.dest_block_size.to_desc_bits_dest();
        descriptor.configuration |= value.dest_drq_type.to_desc_bits();
        descriptor.configuration |= value.src_data_width.to_desc_bits_src();
        descriptor.configuration |= value.src_addr_mode.to_desc_bits_src();
        descriptor.configuration |= value.src_block_size.to_desc_bits_src();
        descriptor.configuration |= value.src_drq_type.to_desc_bits();

        if let Some(link) = value.link {
            descriptor.link = link as u32;
            // We already verified above the low bits of `value.link` are clear,
            // no need to re-mask the current state of `descriptor.link`.
            descriptor.link |= ((link as usize >> 32) as u32) & 0b11
        } else {
            descriptor.end_link();
        }

        Ok(descriptor)
    }
}

// DescriptorConfig

// SrcDrqType

impl SrcDrqType {
    #[inline(always)]
    fn to_desc_bits(&self) -> u32 {
        // 6 bits, no shift
        ((*self as u8) & 0b11_1111) as u32
    }
}

// DestDrqType

impl DestDrqType {
    #[inline(always)]
    fn to_desc_bits(&self) -> u32 {
        (((*self as u8) & 0b11_1111) as u32) << 16
    }
}

// BlockSize

impl BlockSize {
    #[inline(always)]
    fn to_desc_bits_dest(&self) -> u32 {
        (((*self as u8) & 0b11) as u32) << 22
    }

    #[inline(always)]
    fn to_desc_bits_src(&self) -> u32 {
        (((*self as u8) & 0b11) as u32) << 6
    }
}

// AddressMode

impl AddressMode {
    #[inline(always)]
    fn to_desc_bits_src(&self) -> u32 {
        (((*self as u8) & 0b1) as u32) << 8
    }

    #[inline(always)]
    fn to_desc_bits_dest(&self) -> u32 {
        (((*self as u8) & 0b1) as u32) << 24
    }
}

// DataWidth

impl DataWidth {
    #[inline(always)]
    fn to_desc_bits_dest(&self) -> u32 {
        (((*self as u8) & 0b11) as u32) << 25
    }

    #[inline(always)]
    fn to_desc_bits_src(&self) -> u32 {
        (((*self as u8) & 0b11) as u32) << 9
    }
}

// BModeSel

impl BModeSel {
    #[inline(always)]
    fn to_desc_bits(&self) -> u32 {
        (((*self as u8) & 0b1) as u32) << 30
    }
}