sam4l/
spi.rs

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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Implementation of DMA-based SPI master and slave communication for the
//! SAM4L.
//!
//! Driver for the SPI hardware (separate from the USARTS), described in chapter
//! 26 of the datasheet.
//!
//! - Authors: Sam Crow <samcrow@uw.edu>, Philip Levis <pal@cs.stanford.edu>

use crate::dma::DMAChannel;
use crate::dma::DMAClient;
use crate::dma::DMAPeripheral;
use crate::pm;
use core::cell::Cell;
use core::cmp;
use kernel::hil::spi;
use kernel::hil::spi::ClockPhase;
use kernel::hil::spi::ClockPolarity;
use kernel::hil::spi::SpiMasterClient;
use kernel::hil::spi::SpiSlaveClient;
use kernel::platform::chip::ClockInterface;
use kernel::utilities::cells::OptionalCell;
use kernel::utilities::leasable_buffer::SubSliceMut;
use kernel::utilities::peripheral_management::{PeripheralManagement, PeripheralManager};
use kernel::utilities::registers::interfaces::{ReadWriteable, Readable, Writeable};
use kernel::utilities::registers::{self, register_bitfields, ReadOnly, ReadWrite, WriteOnly};
use kernel::utilities::StaticRef;
use kernel::ErrorCode;

#[repr(C)]
pub struct SpiRegisters {
    cr: WriteOnly<u32, Control::Register>,
    mr: ReadWrite<u32, Mode::Register>,
    rdr: ReadOnly<u32>,
    tdr: WriteOnly<u32, TransmitData::Register>,
    sr: ReadOnly<u32, Status::Register>,
    ier: WriteOnly<u32, InterruptFlags::Register>,
    idr: WriteOnly<u32, InterruptFlags::Register>,
    imr: ReadOnly<u32, InterruptFlags::Register>,
    _reserved0: [ReadOnly<u32>; 4],
    csr: [ReadWrite<u32, ChipSelectParams::Register>; 4],
    _reserved1: [ReadOnly<u32>; 41],
    wpcr: ReadWrite<u32, WriteProtectionControl::Register>,
    wpsr: ReadOnly<u32>,
    _reserved2: [ReadOnly<u32>; 3],
    features: ReadOnly<u32>,
    version: ReadOnly<u32>,
}

register_bitfields![u32,
    Control [
        LASTXFER 24,
        FLUSHFIFO 8,
        SWRST 7,
        SPIDIS 1,
        SPIEN 0
    ],

    /// Mode of the SPI peripheral.
    Mode [
        /// Delay between chip selects
        DLYBCS   OFFSET(24)  NUMBITS(8) [],
        /// Peripheral chip select
        PCS      OFFSET(16)  NUMBITS(4) [
            /// One-hot encoding
            PCS0 = 0b1110,
            PCS1 = 0b1101,
            PCS2 = 0b1011,
            PCS3 = 0b0111
        ],
        /// Local loopback enable
        LLB      OFFSET( 7)  NUMBITS(1) [],
        /// FIFO in reception enable
        RXFIFOEN OFFSET( 6)  NUMBITS(1) [],
        /// Mode fault detection
        MODFDIS  OFFSET( 4)  NUMBITS(1) [],
        /// Chip select decode
        PCSDEC   OFFSET( 2)  NUMBITS(1) [],
        /// Peripheral select
        PS       OFFSET( 1)  NUMBITS(1) [],
        /// Master/slave mode
        MSTR     OFFSET( 0)  NUMBITS(1) []
    ],

    TransmitData [
        LASTXFER OFFSET(24)  NUMBITS(1),
        PCS      OFFSET(16)  NUMBITS(4),
        TD       OFFSET(0)   NUMBITS(16)
    ],

    Status [
        SPIENS  OFFSET(16),
        UNDES   OFFSET(10),
        TXEMPTY OFFSET(9),
        NSSR    OFFSET(8),
        OVRES   OFFSET(3),
        MODF    OFFSET(2),
        TDRE    OFFSET(1),
        RDRF    OFFSET(0)
    ],

    InterruptFlags [
        UNDES 10,
        TXEMPTY 9,
        NSSR 8,
        OVRES 3,
        MODF 2,
        TDRE 1,
        RDRF 0
    ],

    ChipSelectParams [
        DLYBCT OFFSET(24)  NUMBITS(8) [],
        DLYBS  OFFSET(16)  NUMBITS(8) [],
        SCBR   OFFSET(8)   NUMBITS(8) [],
        BITS   OFFSET(4)   NUMBITS(8) [
            Eight = 0,
            Nine = 1,
            Ten = 2,
            Eleven = 3,
            Twelve = 4,
            Thirteen = 5,
            Fourteen = 6,
            Fifteen = 7,
            Sixteen = 8,
            Four = 9,
            Five = 10,
            Six = 11,
            Seven = 12
        ],
        CSAAT OFFSET(3)  NUMBITS(1) [
            ActiveAfterTransfer = 1,
            InactiveAfterTransfer = 0
        ],
        CSNAAT OFFSET(2)  NUMBITS(1) [
            DoNotRiseBetweenTransfers = 0,
            RiseBetweenTransfers = 1
        ],
        NCPHA OFFSET(1)  NUMBITS(1) [
            CaptureLeading = 1,
            CaptureTrailing = 0
        ],
        CPOL OFFSET(0)  NUMBITS(1) [
            InactiveHigh = 1,
            InactiveLow = 0
        ]
    ],

    WriteProtectionControl [
        SPIWPKEY OFFSET(8) NUMBITS(24) [
            Key = 0x535049
        ],
        SPIWPEN OFFSET(0) NUMBITS(1) []
    ]
];

#[allow(unused_variables, dead_code)]
// Per-register masks defined in the SPI manual in chapter 26.8
mod spi_consts {
    pub mod rdr {
        pub const RD: u32 = 0xFFFF;
    }

    pub mod tdr {
        pub const TD: u32 = 0xFFFF;
        // PCSx masks from MR also apply here
        // LASTXFER from CR also applies here
    }
}

/// Values for selected peripherals
#[derive(Copy, Clone)]
pub enum Peripheral {
    Peripheral0,
    Peripheral1,
    Peripheral2,
    Peripheral3,
}

impl spi::cs::IntoChipSelect<Peripheral, spi::cs::ActiveLow> for Peripheral {
    fn into_cs(self) -> Peripheral {
        self
    }
}

#[derive(Copy, Clone, PartialEq)]
pub enum SpiRole {
    SpiMaster,
    SpiSlave,
}

/// Abstraction of the SPI Hardware
pub struct SpiHw<'a> {
    client: OptionalCell<&'a dyn SpiMasterClient>,
    dma_read: OptionalCell<&'static DMAChannel>,
    dma_write: OptionalCell<&'static DMAChannel>,
    // keep track of which how many DMA transfers are pending to correctly
    // issue completion event only after both complete.
    transfers_in_progress: Cell<u8>,
    dma_length: Cell<usize>,

    // Slave client is distinct from master client
    slave_client: OptionalCell<&'a dyn SpiSlaveClient>,
    role: Cell<SpiRole>,
    pm: &'a pm::PowerManager,
}

const SPI_BASE: StaticRef<SpiRegisters> =
    unsafe { StaticRef::new(0x40008000 as *const SpiRegisters) };

impl PeripheralManagement<pm::Clock> for SpiHw<'_> {
    type RegisterType = SpiRegisters;

    fn get_registers(&self) -> &SpiRegisters {
        &SPI_BASE
    }

    fn get_clock(&self) -> &pm::Clock {
        &pm::Clock::PBA(pm::PBAClock::SPI)
    }

    fn before_peripheral_access(&self, clock: &pm::Clock, _: &SpiRegisters) {
        clock.enable();
    }

    fn after_peripheral_access(&self, clock: &pm::Clock, registers: &SpiRegisters) {
        if !registers.sr.is_set(Status::SPIENS) {
            clock.disable();
        }
    }
}

type SpiRegisterManager<'a, 'm> = PeripheralManager<'m, SpiHw<'a>, pm::Clock>;

impl<'a> SpiHw<'a> {
    /// Creates a new SPI object, with peripheral 0 selected
    pub const fn new(pm: &'a pm::PowerManager) -> SpiHw<'a> {
        SpiHw {
            client: OptionalCell::empty(),
            dma_read: OptionalCell::empty(),
            dma_write: OptionalCell::empty(),
            transfers_in_progress: Cell::new(0),
            dma_length: Cell::new(0),

            slave_client: OptionalCell::empty(),
            role: Cell::new(SpiRole::SpiMaster),
            pm,
        }
    }

    fn init_as_role(&self, spi: &SpiRegisterManager<'a, '_>, role: SpiRole) {
        self.role.set(role);

        if role == SpiRole::SpiMaster {
            // Only need to set LASTXFER if we are master
            spi.registers.cr.write(Control::LASTXFER::SET);
        }

        // Sets bits per transfer to 8
        let csr = self.get_active_csr(spi);
        csr.modify(ChipSelectParams::BITS::Eight);

        // Set mode to master or slave
        let mode = match self.role.get() {
            SpiRole::SpiMaster => Mode::MSTR::SET,
            SpiRole::SpiSlave => Mode::MSTR::CLEAR,
        };

        // Disable mode fault detection (open drain outputs not supported)
        spi.registers.mr.modify(mode + Mode::MODFDIS::SET);
    }

    fn enable(&self) {
        let spi = &SpiRegisterManager::new(self);

        spi.registers.cr.write(Control::SPIEN::SET);

        if self.role.get() == SpiRole::SpiSlave {
            spi.registers.ier.write(InterruptFlags::NSSR::SET); // Enable NSSR
        }
    }

    fn disable(&self) {
        let spi = &SpiRegisterManager::new(self);

        // TODO(alevy): we actually probably want to do this asynchrounously but
        // because we're using DMA, a transfer may have completed with a byte
        // still in the TX buffer.
        while !spi.registers.sr.is_set(Status::TXEMPTY) {}

        self.dma_read.map(|read| read.disable());
        self.dma_write.map(|write| write.disable());
        spi.registers.cr.write(Control::SPIDIS::SET);

        if self.role.get() == SpiRole::SpiSlave {
            spi.registers.idr.write(InterruptFlags::NSSR::SET); // Disable NSSR
        }
    }

    /// Sets the approximate baud rate for the active peripheral,
    /// and return the actual baud rate set.
    ///
    /// Since the only supported baud rates are (system clock / n) where n
    /// is an integer from 1 to 255, the exact baud rate may not
    /// be available. In that case, the next lower baud rate will
    /// be selected.
    ///
    /// The lowest available baud rate is 188235 baud. If the
    /// requested rate is lower, 188235 baud will be selected.
    pub fn set_baud_rate(&self, rate: u32) -> u32 {
        // Main clock frequency
        let mut real_rate = rate;
        let clock = self.pm.get_system_frequency();

        if real_rate < 188235 {
            real_rate = 188235;
        }
        if real_rate > clock {
            real_rate = clock;
        }

        // Divide and truncate, resulting in a n value that might be too low
        let mut scbr = clock / real_rate;
        // If the division was not exact, increase the n to get a slower baud
        // rate, but only if we are not at the slowest rate. Since scbr is the
        // clock rate divisor, the highest divisor 0xFF corresponds to the
        // lowest rate.
        if clock % real_rate != 0 && scbr != 0xFF {
            scbr += 1;
        }
        let spi = &SpiRegisterManager::new(self);
        let csr = self.get_active_csr(spi);
        csr.modify(ChipSelectParams::SCBR.val(scbr));
        clock / scbr
    }

    fn get_baud_rate(&self) -> u32 {
        let spi = &SpiRegisterManager::new(self);
        let clock = 48000000;
        let scbr = self.get_active_csr(spi).read(ChipSelectParams::SCBR);
        clock / scbr
    }

    fn set_polarity(&self, polarity: ClockPolarity) {
        let spi = &SpiRegisterManager::new(self);
        let csr = self.get_active_csr(spi);
        match polarity {
            ClockPolarity::IdleHigh => csr.modify(ChipSelectParams::CPOL::InactiveHigh),
            ClockPolarity::IdleLow => csr.modify(ChipSelectParams::CPOL::InactiveLow),
        };
    }

    fn get_polarity(&self) -> ClockPolarity {
        let spi = &SpiRegisterManager::new(self);
        let csr = self.get_active_csr(spi);
        if csr.matches_all(ChipSelectParams::CPOL::InactiveLow) {
            ClockPolarity::IdleLow
        } else {
            ClockPolarity::IdleHigh
        }
    }

    fn set_phase(&self, phase: ClockPhase) {
        let spi = &SpiRegisterManager::new(self);
        let csr = self.get_active_csr(spi);
        match phase {
            ClockPhase::SampleLeading => csr.modify(ChipSelectParams::NCPHA::CaptureLeading),
            ClockPhase::SampleTrailing => csr.modify(ChipSelectParams::NCPHA::CaptureTrailing),
        };
    }

    fn get_phase(&self) -> ClockPhase {
        let spi = &SpiRegisterManager::new(self);
        let csr = self.get_active_csr(spi);
        if csr.matches_all(ChipSelectParams::NCPHA::CaptureTrailing) {
            ClockPhase::SampleTrailing
        } else {
            ClockPhase::SampleLeading
        }
    }

    /// Returns the currently active peripheral
    fn get_active_peripheral(&self, spi: &SpiRegisterManager<'a, '_>) -> Peripheral {
        if self.role.get() == SpiRole::SpiMaster {
            if spi.registers.mr.matches_all(Mode::PCS::PCS3) {
                Peripheral::Peripheral3
            } else if spi.registers.mr.matches_all(Mode::PCS::PCS2) {
                Peripheral::Peripheral2
            } else if spi.registers.mr.matches_all(Mode::PCS::PCS1) {
                Peripheral::Peripheral1
            } else {
                // default
                Peripheral::Peripheral0
            }
        } else {
            // The active peripheral is always 0 in slave mode
            Peripheral::Peripheral0
        }
    }

    /// Returns the value of CSR0, CSR1, CSR2, or CSR3,
    /// whichever corresponds to the active peripheral
    fn get_active_csr<'s>(
        &'s self,
        spi: &SpiRegisterManager<'a, 's>,
    ) -> &'s registers::ReadWrite<u32, ChipSelectParams::Register> {
        match self.get_active_peripheral(spi) {
            Peripheral::Peripheral0 => &spi.registers.csr[0],
            Peripheral::Peripheral1 => &spi.registers.csr[1],
            Peripheral::Peripheral2 => &spi.registers.csr[2],
            Peripheral::Peripheral3 => &spi.registers.csr[3],
        }
    }

    /// Set the DMA channels used for reading and writing.
    pub fn set_dma(&self, read: &'static DMAChannel, write: &'static DMAChannel) {
        self.dma_read.set(read);
        self.dma_write.set(write);
    }

    pub fn handle_interrupt(&self) {
        let spi = &SpiRegisterManager::new(self);

        self.slave_client.map(|client| {
            if spi.registers.sr.is_set(Status::NSSR) {
                // NSSR
                client.chip_selected()
            }
            // TODO: Do we want to support byte-level interrupts too?
            // They currently conflict with DMA.
        });
    }

    /// Asynchronous buffer read/write of SPI.
    ///
    /// Returns:
    /// - `Ok(())` if operation starts (will receive callback through
    ///   SpiMasterClient)
    /// - `INVAL` if no buffers were passed in
    // The write buffer has to be mutable because it's passed back to
    // the caller, and the caller may want to be able write into it.
    fn read_write_bytes(
        &self,
        write_buffer: Option<SubSliceMut<'static, u8>>,
        read_buffer: Option<SubSliceMut<'static, u8>>,
    ) -> Result<
        (),
        (
            ErrorCode,
            Option<SubSliceMut<'static, u8>>,
            Option<SubSliceMut<'static, u8>>,
        ),
    > {
        let count = match (&write_buffer, &read_buffer) {
            (Some(ref wb), Some(ref rb)) => cmp::min(wb.len(), rb.len()),
            (Some(ref wb), None) => wb.len(),
            (None, Some(ref rb)) => rb.len(),
            (None, None) => return Err((ErrorCode::INVAL, write_buffer, read_buffer)),
        };

        // Start by enabling the SPI driver.
        self.enable();

        // Configure DMA to transfer that many bytes.
        self.dma_length.set(count);

        // Reset the number of transfers in progress. This is incremented
        // depending on the presence of the read/write below
        self.transfers_in_progress.set(0);

        // Only setup the RX channel if we were passed a read_buffer inside
        // of the option. `map()` checks this for us.
        // The read DMA transfer has to be set up before the write because
        // otherwise when the CPU speed is not significantly faster than the
        // SPI's baud rate, transfer_done does not capture the interrupt
        // signaling the RX is done - may be due to missing the first read
        // byte when you start read after write.
        read_buffer.map(|rbuf| {
            self.transfers_in_progress
                .set(self.transfers_in_progress.get() + 1);
            self.dma_read.map(move |read| {
                read.enable();
                read.do_transfer(DMAPeripheral::SPI_RX, rbuf.take(), count);
            });
        });

        // The ordering of these operations matters.
        // For transfers 4 bytes or longer, this will work as expected.
        // For shorter transfers, the first byte will be missing.
        write_buffer.map(|buf| {
            self.transfers_in_progress
                .set(self.transfers_in_progress.get() + 1);
            self.dma_write.map(move |write| {
                write.enable();
                write.do_transfer(DMAPeripheral::SPI_TX, buf.take(), count);
            });
        });

        Ok(())
    }
}

impl<'a> spi::SpiMaster<'a> for SpiHw<'a> {
    type ChipSelect = Peripheral;

    fn set_client(&self, client: &'a dyn SpiMasterClient) {
        self.client.set(client);
    }

    /// By default, initialize SPI to operate at 40KHz, clock is
    /// idle on low, and sample on the leading edge.
    fn init(&self) -> Result<(), ErrorCode> {
        let spi = &SpiRegisterManager::new(self);
        self.init_as_role(spi, SpiRole::SpiMaster);
        Ok(())
    }

    fn is_busy(&self) -> bool {
        self.transfers_in_progress.get() != 0
    }

    /// Write a byte to the SPI and discard the read; if an
    /// asynchronous operation is outstanding, do nothing.
    fn write_byte(&self, out_byte: u8) -> Result<(), ErrorCode> {
        let spi = &SpiRegisterManager::new(self);

        let tdr = (out_byte as u32) & spi_consts::tdr::TD;
        // Wait for data to leave TDR and enter serializer, so TDR is free
        // for this next byte
        while !spi.registers.sr.is_set(Status::TDRE) {}
        spi.registers.tdr.set(tdr);
        Ok(())
    }

    /// Write 0 to the SPI and return the read; if an
    /// asynchronous operation is outstanding, do nothing.
    fn read_byte(&self) -> Result<u8, ErrorCode> {
        self.read_write_byte(0)
    }

    /// Write a byte to the SPI and return the read; if an
    /// asynchronous operation is outstanding, do nothing.
    fn read_write_byte(&self, val: u8) -> Result<u8, ErrorCode> {
        let spi = &SpiRegisterManager::new(self);

        self.write_byte(val)?;
        while !spi.registers.sr.is_set(Status::RDRF) {}
        Ok(spi.registers.rdr.get() as u8)
    }

    /// Asynchronous buffer read/write of SPI. `write_buffer` must be present;
    /// `read_buffer` may be `None`. If read_buffer is present, then the length
    /// of the read/write is the minimum of two buffer lengths.
    ///
    /// Returns:
    /// - `Ok(())` if operation starts (will receive callback through
    ///   SpiMasterClient)
    /// - `BUSY` if the operation does not start
    // The write buffer has to be mutable because it's passed back to
    // the caller, and the caller may want to be able write into it.
    fn read_write_bytes(
        &self,
        write_buffer: SubSliceMut<'static, u8>,
        read_buffer: Option<SubSliceMut<'static, u8>>,
    ) -> Result<
        (),
        (
            ErrorCode,
            SubSliceMut<'static, u8>,
            Option<SubSliceMut<'static, u8>>,
        ),
    > {
        // If busy, don't start.
        if self.is_busy() {
            return Err((ErrorCode::BUSY, write_buffer, read_buffer));
        }

        if let Err((err, write_buffer, read_buffer)) =
            self.read_write_bytes(Some(write_buffer), read_buffer)
        {
            Err((
                err,
                write_buffer.unwrap_or((&mut [] as &mut [u8]).into()),
                read_buffer,
            ))
        } else {
            Ok(())
        }
    }

    fn set_rate(&self, rate: u32) -> Result<u32, ErrorCode> {
        Ok(self.set_baud_rate(rate))
    }

    fn get_rate(&self) -> u32 {
        self.get_baud_rate()
    }

    fn set_polarity(&self, polarity: ClockPolarity) -> Result<(), ErrorCode> {
        self.set_polarity(polarity);
        Ok(())
    }

    fn get_polarity(&self) -> ClockPolarity {
        self.get_polarity()
    }

    fn set_phase(&self, phase: ClockPhase) -> Result<(), ErrorCode> {
        self.set_phase(phase);
        Ok(())
    }

    fn get_phase(&self) -> ClockPhase {
        self.get_phase()
    }

    fn hold_low(&self) {
        let spi = &SpiRegisterManager::new(self);
        let csr = self.get_active_csr(spi);
        csr.modify(ChipSelectParams::CSAAT::ActiveAfterTransfer);
    }

    fn release_low(&self) {
        let spi = &SpiRegisterManager::new(self);
        let csr = self.get_active_csr(spi);
        csr.modify(ChipSelectParams::CSAAT::InactiveAfterTransfer);
    }

    fn specify_chip_select(&self, cs: Self::ChipSelect) -> Result<(), ErrorCode> {
        // Slave cannot set active peripheral
        if self.role.get() == SpiRole::SpiMaster {
            let spi = &SpiRegisterManager::new(self);
            let mr = match cs {
                Peripheral::Peripheral0 => Mode::PCS::PCS0,
                Peripheral::Peripheral1 => Mode::PCS::PCS1,
                Peripheral::Peripheral2 => Mode::PCS::PCS2,
                Peripheral::Peripheral3 => Mode::PCS::PCS3,
            };
            spi.registers.mr.modify(mr);
            Ok(())
        } else {
            Err(ErrorCode::INVAL)
        }
    }
}

impl<'a> spi::SpiSlave<'a> for SpiHw<'a> {
    // Set to None to disable the whole thing
    fn set_client(&self, client: Option<&'a dyn SpiSlaveClient>) {
        self.slave_client.insert(client);
    }

    fn has_client(&self) -> bool {
        self.slave_client.is_some()
    }

    fn init(&self) -> Result<(), ErrorCode> {
        let spi = &SpiRegisterManager::new(self);
        self.init_as_role(spi, SpiRole::SpiSlave);
        Ok(())
    }

    /// This sets the value in the TDR register, to be sent as soon as the
    /// chip select pin is low.
    fn set_write_byte(&self, write_byte: u8) {
        let spi = SpiRegisterManager::new(self);
        spi.registers.tdr.set(write_byte as u32);
    }

    /// Setup buffers for a SPI transaction initiated by the master device.
    ///
    /// Returns:
    /// - `Ok(())` if the operation starts. A callback will be generated.
    /// - `INVAL` if neither the read or write buffer is provided.
    fn read_write_bytes(
        &self,
        write_buffer: Option<&'static mut [u8]>,
        read_buffer: Option<&'static mut [u8]>,
        len: usize,
    ) -> Result<
        (),
        (
            ErrorCode,
            Option<&'static mut [u8]>,
            Option<&'static mut [u8]>,
        ),
    > {
        let write_buffer = write_buffer.map(|b| {
            let mut buf: SubSliceMut<u8> = b.into();
            if buf.len() > len {
                buf.slice(..len);
            }
            buf
        });
        let read_buffer = read_buffer.map(|b| {
            let mut buf: SubSliceMut<u8> = b.into();
            if buf.len() > len {
                buf.slice(..len);
            }
            buf
        });

        if let Err((e, mwb, mrb)) = self.read_write_bytes(write_buffer, read_buffer) {
            Err((e, mwb.map(|b| b.take()), mrb.map(|b| b.take())))
        } else {
            Ok(())
        }
    }

    fn set_polarity(&self, polarity: ClockPolarity) -> Result<(), ErrorCode> {
        self.set_polarity(polarity);
        Ok(())
    }

    fn get_polarity(&self) -> ClockPolarity {
        self.get_polarity()
    }

    fn set_phase(&self, phase: ClockPhase) -> Result<(), ErrorCode> {
        self.set_phase(phase);
        Ok(())
    }

    fn get_phase(&self) -> ClockPhase {
        self.get_phase()
    }
}

impl DMAClient for SpiHw<'_> {
    fn transfer_done(&self, _pid: DMAPeripheral) {
        // Only callback that the transfer is done if either:
        // 1) The transfer was TX only and TX finished
        // 2) The transfer was TX and RX, in that case wait for both of them to complete. Although
        //    both transactions happen simultaneously over the wire, the DMA may not finish copying
        //    data over to/from the controller at the same time, so we don't want to abort
        //    prematurely.

        self.transfers_in_progress
            .set(self.transfers_in_progress.get() - 1);

        if self.transfers_in_progress.get() == 0 {
            let txbuf = self.dma_write.map_or(None, |dma| {
                let buf = dma.abort_transfer();
                dma.disable();
                buf
            });

            let rxbuf = self.dma_read.map_or(None, |dma| {
                let buf = dma.abort_transfer();
                dma.disable();
                buf
            });

            let len = self.dma_length.get();
            self.dma_length.set(0);

            match self.role.get() {
                SpiRole::SpiMaster => {
                    self.client.map(|cb| {
                        txbuf.map(|txbuf| {
                            cb.read_write_done(txbuf.into(), rxbuf.map(|b| b.into()), Ok(len));
                        });
                    });
                }
                SpiRole::SpiSlave => {
                    self.slave_client.map(|cb| {
                        cb.read_write_done(txbuf, rxbuf, len, Ok(()));
                    });
                }
            }
            if self.transfers_in_progress.get() == 0 {
                self.disable();
            }
        }
    }
}