-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathspim.rs
542 lines (464 loc) · 18.2 KB
/
spim.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
//! HAL interface to the SPIM peripheral.
//!
//! See product specification, chapter 31.
use core::ops::Deref;
use core::sync::atomic::{compiler_fence, Ordering::SeqCst};
use embedded_hal::digital::OutputPin;
use embedded_hal::spi::{self, ErrorKind, ErrorType, SpiBus};
#[cfg(any(feature = "9160", feature = "5340-app", feature = "5340-net"))]
use crate::pac::{spim0_ns as spim0, SPIM0_NS as SPIM0};
#[cfg(feature = "9160")]
use crate::pac::{SPIM1_NS as SPIM1, SPIM2_NS as SPIM2, SPIM3_NS as SPIM3};
#[cfg(not(any(feature = "9160", feature = "5340-app", feature = "5340-net")))]
use crate::pac::{spim0, SPIM0};
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
pub use spim0::frequency::FREQUENCY_A as Frequency;
use core::iter::repeat_with;
#[cfg(feature = "52811")]
use crate::pac::SPIM1;
#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
use crate::pac::{SPIM1, SPIM2};
#[cfg(any(feature = "52833", feature = "52840"))]
use crate::pac::SPIM3;
use crate::gpio::{Floating, Input, Output, Pin, PushPull};
use crate::target_constants::{EASY_DMA_SIZE, FORCE_COPY_BUFFER_SIZE};
use crate::{slice_in_ram, slice_in_ram_or, DmaSlice};
/// Interface to a SPIM instance.
///
/// This is a very basic interface that comes with the following limitations:
/// - The SPIM instances share the same address space with instances of SPIS,
/// SPI, TWIM, TWIS, and TWI. You need to make sure that conflicting instances
/// are disabled before using `Spim`. See product specification, section 15.2.
pub struct Spim<T>(T);
impl<T> ErrorType for Spim<T> {
type Error = Error;
}
impl<T: Instance> SpiBus for Spim<T> {
fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error> {
// A mutable slice can only be built from data in RAM.
assert!(slice_in_ram(words));
for chunk in words.chunks(EASY_DMA_SIZE) {
self.do_spi_dma_transfer(DmaSlice::null(), DmaSlice::from_slice(chunk))?;
}
Ok(())
}
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
if slice_in_ram(words) {
for chunk in words.chunks(EASY_DMA_SIZE) {
self.do_spi_dma_transfer(DmaSlice::from_slice(chunk), DmaSlice::null())?;
}
} else {
let mut buf = [0u8; FORCE_COPY_BUFFER_SIZE];
for chunk in words.chunks(FORCE_COPY_BUFFER_SIZE) {
buf[..chunk.len()].copy_from_slice(chunk);
self.do_spi_dma_transfer(
DmaSlice::from_slice(&buf[..chunk.len()]),
DmaSlice::null(),
)?;
}
}
Ok(())
}
fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> {
self.transfer_split_uneven_internal(write, read)
}
fn transfer_in_place(&mut self, words: &mut [u8]) -> Result<(), Self::Error> {
// A mutable slice can only be built from data in RAM.
assert!(slice_in_ram(words));
words.chunks(EASY_DMA_SIZE).try_for_each(|chunk| {
self.do_spi_dma_transfer(DmaSlice::from_slice(chunk), DmaSlice::from_slice(chunk))
})?;
Ok(())
}
fn flush(&mut self) -> Result<(), Self::Error> {
// This implementation doesn't buffer operations, so there is nothing to flush.
Ok(())
}
}
#[cfg(feature = "embedded-hal-02")]
impl<T> embedded_hal_02::blocking::spi::Transfer<u8> for Spim<T>
where
T: Instance,
{
type Error = Error;
fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Error> {
// If the slice isn't in RAM, we can't write back to it at all
slice_in_ram_or(words, Error::DMABufferNotInDataMemory)?;
words.chunks(EASY_DMA_SIZE).try_for_each(|chunk| {
self.do_spi_dma_transfer(DmaSlice::from_slice(chunk), DmaSlice::from_slice(chunk))
})?;
Ok(words)
}
}
#[cfg(feature = "embedded-hal-02")]
impl<T> embedded_hal_02::blocking::spi::Write<u8> for Spim<T>
where
T: Instance,
{
type Error = Error;
fn write<'w>(&mut self, words: &'w [u8]) -> Result<(), Error> {
// Mask on segment where Data RAM is located on nrf52840 and nrf52832
// Upper limit is choosen to entire area where DataRam can be placed
let needs_copy = !slice_in_ram(words);
let chunk_sz = if needs_copy {
FORCE_COPY_BUFFER_SIZE
} else {
EASY_DMA_SIZE
};
let step = if needs_copy {
Self::spi_dma_copy
} else {
Self::spi_dma_no_copy
};
words.chunks(chunk_sz).try_for_each(|c| step(self, c))
}
}
impl<T> Spim<T>
where
T: Instance,
{
#[cfg(feature = "embedded-hal-02")]
fn spi_dma_no_copy(&mut self, chunk: &[u8]) -> Result<(), Error> {
self.do_spi_dma_transfer(DmaSlice::from_slice(chunk), DmaSlice::null())
}
#[cfg(feature = "embedded-hal-02")]
fn spi_dma_copy(&mut self, chunk: &[u8]) -> Result<(), Error> {
let mut buf = [0u8; FORCE_COPY_BUFFER_SIZE];
buf[..chunk.len()].copy_from_slice(chunk);
self.do_spi_dma_transfer(DmaSlice::from_slice(&buf[..chunk.len()]), DmaSlice::null())
}
pub fn new(spim: T, pins: Pins, frequency: Frequency, mode: Mode, orc: u8) -> Self {
// Select pins.
match pins.sck {
Some(sck) => spim.psel.sck.write(|w| {
unsafe { w.bits(sck.psel_bits()) };
w.connect().connected()
}),
None => spim.psel.sck.write(|w| w.connect().disconnected()),
}
match pins.mosi {
Some(mosi) => spim.psel.mosi.write(|w| {
unsafe { w.bits(mosi.psel_bits()) };
w.connect().connected()
}),
None => spim.psel.mosi.write(|w| w.connect().disconnected()),
}
match pins.miso {
Some(miso) => spim.psel.miso.write(|w| {
unsafe { w.bits(miso.psel_bits()) };
w.connect().connected()
}),
None => spim.psel.miso.write(|w| w.connect().disconnected()),
}
// Enable SPIM instance.
spim.enable.write(|w| w.enable().enabled());
// Configure mode.
spim.config.write(|w| {
// Can't match on `mode` due to embedded-hal, see https://github.com/rust-embedded/embedded-hal/pull/126
if mode == MODE_0 {
w.order().msb_first();
w.cpol().active_high();
w.cpha().leading();
} else if mode == MODE_1 {
w.order().msb_first();
w.cpol().active_high();
w.cpha().trailing();
} else if mode == MODE_2 {
w.order().msb_first();
w.cpol().active_low();
w.cpha().leading();
} else {
w.order().msb_first();
w.cpol().active_low();
w.cpha().trailing();
}
w
});
// Configure frequency.
spim.frequency.write(|w| w.frequency().variant(frequency));
// Set over-read character to `0`.
spim.orc.write(|w|
// The ORC field is 8 bits long, so `0` is a valid value to write
// there.
unsafe { w.orc().bits(orc) });
Spim(spim)
}
/// Internal helper function to setup and execute SPIM DMA transfer.
fn do_spi_dma_transfer(&mut self, tx: DmaSlice, rx: DmaSlice) -> Result<(), Error> {
// Conservative compiler fence to prevent optimizations that do not
// take in to account actions by DMA. The fence has been placed here,
// before any DMA action has started.
compiler_fence(SeqCst);
// Set up the DMA write.
self.0.txd.ptr.write(|w| unsafe { w.ptr().bits(tx.ptr) });
self.0.txd.maxcnt.write(|w|
// Note that that nrf52840 maxcnt is a wider.
// type than a u8, so we use a `_` cast rather than a `u8` cast.
// The MAXCNT field is thus at least 8 bits wide and accepts the full
// range of values that fit in a `u8`.
unsafe { w.maxcnt().bits(tx.len as _ ) });
// Set up the DMA read.
self.0.rxd.ptr.write(|w|
// This is safe for the same reasons that writing to TXD.PTR is
// safe. Please refer to the explanation there.
unsafe { w.ptr().bits(rx.ptr) });
self.0.rxd.maxcnt.write(|w|
// This is safe for the same reasons that writing to TXD.MAXCNT is
// safe. Please refer to the explanation there.
unsafe { w.maxcnt().bits(rx.len as _) });
// Start SPI transaction.
self.0.tasks_start.write(|w|
// `1` is a valid value to write to task registers.
unsafe { w.bits(1) });
// Conservative compiler fence to prevent optimizations that do not
// take in to account actions by DMA. The fence has been placed here,
// after all possible DMA actions have completed.
compiler_fence(SeqCst);
// Wait for END event.
//
// This event is triggered once both transmitting and receiving are
// done.
while self.0.events_end.read().bits() == 0 {}
// Reset the event, otherwise it will always read `1` from now on.
self.0.events_end.write(|w| w);
// Conservative compiler fence to prevent optimizations that do not
// take in to account actions by DMA. The fence has been placed here,
// after all possible DMA actions have completed.
compiler_fence(SeqCst);
if self.0.txd.amount.read().bits() != tx.len {
return Err(Error::Transmit);
}
if self.0.rxd.amount.read().bits() != rx.len {
return Err(Error::Receive);
}
Ok(())
}
/// Read and write from a SPI slave, using a single buffer.
///
/// This method implements a complete read transaction, which consists of
/// the master transmitting what it wishes to read, and the slave responding
/// with the requested data.
///
/// Uses the provided chip select pin to initiate the transaction. Transmits
/// all bytes in `buffer`, then receives an equal number of bytes.
pub fn transfer(
&mut self,
chip_select: &mut Pin<Output<PushPull>>,
buffer: &mut [u8],
) -> Result<(), Error> {
slice_in_ram_or(buffer, Error::DMABufferNotInDataMemory)?;
chip_select.set_low().unwrap();
// Don't return early, as we must reset the CS pin.
let res = buffer.chunks(EASY_DMA_SIZE).try_for_each(|chunk| {
self.do_spi_dma_transfer(DmaSlice::from_slice(chunk), DmaSlice::from_slice(chunk))
});
chip_select.set_high().unwrap();
res
}
/// Read and write from a SPI slave, using separate read and write buffers.
///
/// This method implements a complete read transaction, which consists of
/// the master transmitting what it wishes to read, and the slave responding
/// with the requested data.
///
/// Uses the provided chip select pin to initiate the transaction. Transmits
/// all bytes in `tx_buffer`, then receives bytes until `rx_buffer` is full.
///
/// If `tx_buffer.len() != rx_buffer.len()`, the transaction will stop at the
/// smaller of either buffer.
pub fn transfer_split_even(
&mut self,
chip_select: &mut Pin<Output<PushPull>>,
tx_buffer: &[u8],
rx_buffer: &mut [u8],
) -> Result<(), Error> {
// NOTE: RAM slice check for `rx_buffer` is not necessary, as a mutable
// slice can only be built from data located in RAM.
slice_in_ram_or(tx_buffer, Error::DMABufferNotInDataMemory)?;
let txi = tx_buffer.chunks(EASY_DMA_SIZE);
let rxi = rx_buffer.chunks_mut(EASY_DMA_SIZE);
chip_select.set_low().unwrap();
// Don't return early, as we must reset the CS pin
let res = txi.zip(rxi).try_for_each(|(t, r)| {
self.do_spi_dma_transfer(DmaSlice::from_slice(t), DmaSlice::from_slice(r))
});
chip_select.set_high().unwrap();
res
}
/// Read and write from a SPI slave, using separate read and write buffers.
///
/// This method implements a complete read transaction, which consists of
/// the master transmitting what it wishes to read, and the slave responding
/// with the requested data.
///
/// Uses the provided chip select pin to initiate the transaction. Transmits
/// all bytes in `tx_buffer`, then receives bytes until `rx_buffer` is full.
///
/// This method is more complicated than the other `transfer` methods because
/// it is allowed to perform transactions where `tx_buffer.len() != rx_buffer.len()`.
/// If this occurs, extra incoming bytes will be discarded, OR extra outgoing bytes
/// will be filled with the `orc` value.
pub fn transfer_split_uneven(
&mut self,
chip_select: &mut Pin<Output<PushPull>>,
tx_buffer: &[u8],
rx_buffer: &mut [u8],
) -> Result<(), Error> {
slice_in_ram_or(tx_buffer, Error::DMABufferNotInDataMemory)?;
chip_select.set_low().unwrap();
// Don't return early, as we must reset the CS pin.
let res = self.transfer_split_uneven_internal(tx_buffer, rx_buffer);
chip_select.set_high().unwrap();
res
}
pub fn transfer_split_uneven_internal(
&mut self,
tx_buffer: &[u8],
rx_buffer: &mut [u8],
) -> Result<(), Error> {
// NOTE: RAM slice check for `rx_buffer` is not necessary, as a mutable
// slice can only be built from data located in RAM.
if slice_in_ram(tx_buffer) {
// For the tx and rx, we want to return a DmaSlice with a chunk as long
// as there is data to send. We then chain a repeat to the end so once
// all chunks have been exhausted, we will keep DmaSlice::null() out of
// the iterators.
let txi = tx_buffer
.chunks(EASY_DMA_SIZE)
.map(|chunk| DmaSlice::from_slice(chunk))
.chain(repeat_with(DmaSlice::null));
let rxi = rx_buffer
.chunks_mut(EASY_DMA_SIZE)
.map(|chunk| DmaSlice::from_slice(chunk))
.chain(repeat_with(DmaSlice::null));
// We then chain the iterators together, and once BOTH are giving null
// DmaSlices, then we are done sending and receiving.
for (t, r) in txi.zip(rxi).take_while(|(t, r)| t.ptr != 0 || r.ptr != 0) {
self.do_spi_dma_transfer(t, r)?;
}
} else {
let mut buf = [0u8; FORCE_COPY_BUFFER_SIZE];
let txi = tx_buffer
.chunks(FORCE_COPY_BUFFER_SIZE)
.map(Some)
.chain(repeat_with(|| None));
let rxi = rx_buffer
.chunks_mut(FORCE_COPY_BUFFER_SIZE)
.map(|chunk| DmaSlice::from_slice(chunk))
.chain(repeat_with(DmaSlice::null));
for (tx_chunk, r) in txi.zip(rxi).take_while(|(t, r)| t.is_some() || r.ptr != 0) {
let t = if let Some(tx_chunk) = tx_chunk {
buf[..tx_chunk.len()].copy_from_slice(tx_chunk);
DmaSlice::from_slice(&buf[..tx_chunk.len()])
} else {
DmaSlice::null()
};
self.do_spi_dma_transfer(t, r)?;
}
}
Ok(())
}
/// Write to an SPI slave.
///
/// This method uses the provided chip select pin to initiate the
/// transaction, then transmits all bytes in `tx_buffer`. All incoming
/// bytes are discarded.
pub fn write(
&mut self,
chip_select: &mut Pin<Output<PushPull>>,
tx_buffer: &[u8],
) -> Result<(), Error> {
slice_in_ram_or(tx_buffer, Error::DMABufferNotInDataMemory)?;
self.transfer_split_uneven(chip_select, tx_buffer, &mut [0u8; 0])
}
/// Return the raw interface to the underlying SPIM peripheral.
pub fn free(self) -> (T, Pins) {
let sck = self.0.psel.sck.read();
let mosi = self.0.psel.mosi.read();
let miso = self.0.psel.miso.read();
self.0.psel.sck.reset();
self.0.psel.mosi.reset();
self.0.psel.miso.reset();
(
self.0,
Pins {
sck: if sck.connect().is_connected() {
Some(unsafe { Pin::from_psel_bits(sck.bits()) })
} else {
None
},
mosi: if mosi.connect().is_connected() {
Some(unsafe { Pin::from_psel_bits(mosi.bits()) })
} else {
None
},
miso: if miso.connect().is_connected() {
Some(unsafe { Pin::from_psel_bits(miso.bits()) })
} else {
None
},
},
)
}
}
/// GPIO pins for SPIM interface
pub struct Pins {
/// SPI clock.
///
/// None if unused.
pub sck: Option<Pin<Output<PushPull>>>,
/// MOSI Master out, slave in
/// None if unused
pub mosi: Option<Pin<Output<PushPull>>>,
/// MISO Master in, slave out
/// None if unused
pub miso: Option<Pin<Input<Floating>>>,
}
#[derive(Debug)]
pub enum Error {
/// EasyDMA can only read from data memory, read only buffers in flash will fail.
DMABufferNotInDataMemory,
Transmit,
Receive,
}
impl spi::Error for Error {
fn kind(&self) -> ErrorKind {
ErrorKind::Other
}
}
/// Implemented by all SPIM instances.
pub trait Instance: Deref<Target = spim0::RegisterBlock> + sealed::Sealed {}
mod sealed {
pub trait Sealed {}
}
impl sealed::Sealed for SPIM0 {}
impl Instance for SPIM0 {}
#[cfg(any(
feature = "52832",
feature = "52833",
feature = "52840",
feature = "52811",
feature = "9160"
))]
mod _spim1 {
use super::*;
impl Instance for SPIM1 {}
impl sealed::Sealed for SPIM1 {}
}
#[cfg(any(
feature = "52832",
feature = "52833",
feature = "52840",
feature = "9160"
))]
mod _spim2 {
use super::*;
impl Instance for SPIM2 {}
impl sealed::Sealed for SPIM2 {}
}
#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))]
mod _spim3 {
use super::*;
impl Instance for SPIM3 {}
impl sealed::Sealed for SPIM3 {}
}