|
| 1 | +use core::marker::PhantomData; |
| 2 | + |
| 3 | +use embedded_dma::{ReadBuffer, WriteBuffer}; |
| 4 | + |
| 5 | +use crate::gpdma::{ |
| 6 | + config::{Config as DmaConfig, MemoryToPeripheral, PeripheralToMemory}, |
| 7 | + Channel, Transfer as DmaTransfer, Word, |
| 8 | +}; |
| 9 | + |
| 10 | +use super::{Error, FrameSize, Instance, Spi}; |
| 11 | + |
| 12 | +pub struct DmaRx<SPI, W> { |
| 13 | + _spi: PhantomData<SPI>, |
| 14 | + _word: PhantomData<W>, |
| 15 | +} |
| 16 | + |
| 17 | +impl<SPI, W> DmaRx<SPI, W> { |
| 18 | + fn new() -> Self { |
| 19 | + Self { |
| 20 | + _spi: PhantomData, |
| 21 | + _word: PhantomData, |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +unsafe impl<SPI: Instance, W: FrameSize> ReadBuffer for DmaRx<SPI, W> { |
| 27 | + type Word = W; |
| 28 | + |
| 29 | + unsafe fn read_buffer(&self) -> (*const Self::Word, usize) { |
| 30 | + ((*SPI::ptr()).rxdr().as_ptr() as *const W, 1) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +pub struct DmaTx<SPI, W> { |
| 35 | + _spi: PhantomData<SPI>, |
| 36 | + _word: PhantomData<W>, |
| 37 | +} |
| 38 | + |
| 39 | +impl<SPI, W> DmaTx<SPI, W> { |
| 40 | + fn new() -> Self { |
| 41 | + Self { |
| 42 | + _spi: PhantomData, |
| 43 | + _word: PhantomData, |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +unsafe impl<SPI: Instance, W: FrameSize> WriteBuffer for DmaTx<SPI, W> { |
| 49 | + type Word = W; |
| 50 | + |
| 51 | + unsafe fn write_buffer(&mut self) -> (*mut Self::Word, usize) { |
| 52 | + ((*SPI::ptr()).txdr().as_ptr() as *mut W, 1) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +pub struct RxDmaTransfer<SPI, W: FrameSize, CH, D> { |
| 57 | + spi: Spi<SPI, W>, |
| 58 | + transfer: DmaTransfer<CH, DmaRx<SPI, W>, D, PeripheralToMemory>, |
| 59 | +} |
| 60 | + |
| 61 | +impl<SPI, W, CH, D> RxDmaTransfer<SPI, W, CH, D> |
| 62 | +where |
| 63 | + SPI: Instance, |
| 64 | + W: FrameSize + Word, |
| 65 | + CH: Channel, |
| 66 | + D: WriteBuffer<Word = W>, |
| 67 | +{ |
| 68 | + pub fn new(spi: Spi<SPI, W>, channel: CH, destination: D) -> Self { |
| 69 | + let config = DmaConfig::new().with_request(SPI::rx_dma_request()); |
| 70 | + let source = DmaRx::new(); |
| 71 | + let transfer = DmaTransfer::peripheral_to_memory( |
| 72 | + config, |
| 73 | + channel, |
| 74 | + source, |
| 75 | + destination, |
| 76 | + ); |
| 77 | + Self { spi, transfer } |
| 78 | + } |
| 79 | + |
| 80 | + pub fn start(&mut self) -> Result<(), crate::gpdma::Error> { |
| 81 | + self.spi.enable_rx_dma(); |
| 82 | + self.transfer.start_with(|_, _| { |
| 83 | + self.spi.enable(); |
| 84 | + self.spi.start_transfer(); |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +pub struct TxDmaTransfer<SPI, W: FrameSize, CH, S> { |
| 90 | + spi: Spi<SPI, W>, |
| 91 | + transfer: DmaTransfer<CH, S, DmaTx<SPI, W>, MemoryToPeripheral>, |
| 92 | +} |
| 93 | + |
| 94 | +impl<SPI, W, CH, S> TxDmaTransfer<SPI, W, CH, S> |
| 95 | +where |
| 96 | + SPI: Instance, |
| 97 | + W: FrameSize + Word, |
| 98 | + CH: Channel, |
| 99 | + S: ReadBuffer<Word = W>, |
| 100 | +{ |
| 101 | + pub fn new(spi: Spi<SPI, W>, channel: CH, source: S) -> Self { |
| 102 | + let config = DmaConfig::new().with_request(SPI::tx_dma_request()); |
| 103 | + let destination = DmaTx::new(); |
| 104 | + let transfer = DmaTransfer::memory_to_peripheral( |
| 105 | + config, |
| 106 | + channel, |
| 107 | + source, |
| 108 | + destination, |
| 109 | + ); |
| 110 | + Self { spi, transfer } |
| 111 | + } |
| 112 | + |
| 113 | + pub fn start(&mut self) -> Result<(), crate::gpdma::Error> { |
| 114 | + self.transfer.start_with(|_, _| { |
| 115 | + self.spi.enable_tx_dma(); |
| 116 | + self.spi.enable(); |
| 117 | + self.spi.start_transfer(); |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +pub struct DuplexDmaTransfer<'a, SPI, W: FrameSize, TX, RX, S, D> { |
| 123 | + spi: &'a mut Spi<SPI, W>, |
| 124 | + tx_transfer: DmaTransfer<TX, S, DmaTx<SPI, W>, MemoryToPeripheral>, |
| 125 | + rx_transfer: DmaTransfer<RX, DmaRx<SPI, W>, D, PeripheralToMemory>, |
| 126 | +} |
| 127 | + |
| 128 | +impl<'a, SPI, W, RX, TX, S, D> DuplexDmaTransfer<'a, SPI, W, TX, RX, S, D> |
| 129 | +where |
| 130 | + SPI: Instance, |
| 131 | + W: FrameSize + Word, |
| 132 | + TX: Channel, |
| 133 | + RX: Channel, |
| 134 | + S: ReadBuffer<Word = W>, |
| 135 | + D: WriteBuffer<Word = W>, |
| 136 | +{ |
| 137 | + pub fn new( |
| 138 | + spi: &'a mut Spi<SPI, W>, |
| 139 | + tx_channel: TX, |
| 140 | + rx_channel: RX, |
| 141 | + source: S, |
| 142 | + destination: D, |
| 143 | + ) -> Self { |
| 144 | + let tx_config = DmaConfig::new().with_request(SPI::tx_dma_request()); |
| 145 | + let tx_destination = DmaTx::new(); |
| 146 | + let tx_transfer = DmaTransfer::memory_to_peripheral( |
| 147 | + tx_config, |
| 148 | + tx_channel, |
| 149 | + source, |
| 150 | + tx_destination, |
| 151 | + ); |
| 152 | + let rx_source = DmaRx::new(); |
| 153 | + let rx_config = DmaConfig::new().with_request(SPI::rx_dma_request()); |
| 154 | + let rx_transfer = DmaTransfer::peripheral_to_memory( |
| 155 | + rx_config, |
| 156 | + rx_channel, |
| 157 | + rx_source, |
| 158 | + destination, |
| 159 | + ); |
| 160 | + Self { |
| 161 | + spi, |
| 162 | + tx_transfer, |
| 163 | + rx_transfer, |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + pub fn start(&mut self) -> Result<(), Error> { |
| 168 | + self.spi.enable_rx_dma(); |
| 169 | + self.rx_transfer.start()?; |
| 170 | + self.tx_transfer.start_with(|_, _| { |
| 171 | + self.spi.enable_tx_dma(); |
| 172 | + self.spi.enable(); |
| 173 | + self.spi.start_transfer(); |
| 174 | + })?; |
| 175 | + Ok(()) |
| 176 | + } |
| 177 | + |
| 178 | + pub fn is_dma_complete(&self) -> Result<bool, Error> { |
| 179 | + let complete = self.tx_transfer.is_transfer_complete()? |
| 180 | + && self.rx_transfer.is_transfer_complete()?; |
| 181 | + Ok(complete) |
| 182 | + } |
| 183 | + |
| 184 | + pub fn wait_for_complete(&mut self) -> Result<(), Error> { |
| 185 | + while !self.is_dma_complete()? {} |
| 186 | + self.spi.end_transaction(); |
| 187 | + self.spi.disable_dma(); |
| 188 | + Ok(()) |
| 189 | + } |
| 190 | + |
| 191 | + pub fn end_transfer(&mut self) { |
| 192 | + self.spi.end_transaction(); |
| 193 | + self.spi.disable_dma(); |
| 194 | + } |
| 195 | + |
| 196 | + pub fn free(self) -> Result<(TX, RX, S, D), Error> { |
| 197 | + let (tx, s, _) = self.tx_transfer.free()?; |
| 198 | + let (rx, _, d) = self.rx_transfer.free()?; |
| 199 | + Ok((tx, rx, s, d)) |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +type DuplexInplaceDmaTransfer<'a, SPI, W, TX, RX> = |
| 204 | + DuplexDmaTransfer<'a, SPI, W, TX, RX, &'static [W], &'static mut [W]>; |
| 205 | + |
| 206 | +impl<SPI: Instance, W: FrameSize + Word> Spi<SPI, W> { |
| 207 | + pub fn dma_transfer_inplace<TX: Channel, RX: Channel>( |
| 208 | + &mut self, |
| 209 | + buffer: &'static mut [W], |
| 210 | + tx_channel: TX, |
| 211 | + rx_channel: RX, |
| 212 | + ) -> Result<DuplexInplaceDmaTransfer<SPI, W, TX, RX>, Error> { |
| 213 | + // Note (unsafe): Data will be read from the start of the buffer before data is written |
| 214 | + // to those locations just like for blocking non-DMA in-place transfers |
| 215 | + let source = unsafe { *(buffer.as_ptr() as *const &[W]) }; |
| 216 | + let mut transfer = DuplexDmaTransfer::new( |
| 217 | + self, tx_channel, rx_channel, source, buffer, |
| 218 | + ); |
| 219 | + transfer.start()?; |
| 220 | + Ok(transfer) |
| 221 | + } |
| 222 | +} |
0 commit comments