drone-sd-core 0.2.2

Secure Digital cards driver for Drone.
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
use consts::NCR;
use core::mem;
use crc::{Crc, Crc16, Crc7};
use drone_core::drv::{Driver, Resource};
use drone_core::thr::prelude::*;
use errors::{CmdError, DmaError, R1Error, SpiInitError};
use futures::prelude::*;
use sd_spi::{SdSpi, SdSpiRes};
use tokens::{
  Cond, Csd, Ocr, ReadResponse, Status, WriteResponse, BLOCK_START,
  BLOCK_START_MULTI_WRITE, R1, STOP_TRAN,
};

const STARTUP_CYCLES: usize = 10;
const INIT_TIMEOUT: usize = 100;

static mut RX_FORGET: u8 = 0;
static TX_IDLE: u8 = 0xFF;

/// SD SPI session driver.
#[derive(Driver)]
#[driver(forward)]
pub struct SdSpiSessCore<T: SdSpiSessResCore>(T);

/// SD SPI session resource.
pub trait SdSpiSessResCore: Resource<Source = Self> + Driver {
  /// SD resource.
  type SdRes: SdSpiRes;

  /// SPI thread token.
  type SpiInt: ThrToken<Ltt>;

  /// Returns a reference to the SD driver.
  fn sd(&self) -> &SdSpi<Self::SdRes>;

  /// Returns a mutable reference to the SD driver.
  fn sd_mut(&mut self) -> &mut SdSpi<Self::SdRes>;

  /// Opens a SPI communication.
  fn spi_open(&self);

  /// Closes a SPI communication.
  fn spi_close(&self);

  /// Opens a two-way DMA communication.
  ///
  /// # Arguments
  ///
  /// * `rx_ptr` - A mutable pointer to the receiving byte array.
  /// * `rx_inc` - Whether to increment `rx_ptr` after each write.
  /// * `rx_len` - Number of bytes to receive.
  /// * `tx_ptr` - A pointer to the transmitting byte array.
  /// * `tx_inc` - Whether to increment `tx_ptr` after each read.
  /// * `tx_len` - Number of bytes to transmit.
  ///
  /// # Safety
  ///
  /// `rx_ptr` and `tx_ptr` must remain valid until the returned future
  /// completes.
  unsafe fn dma_exchange<'sess>(
    &'sess mut self,
    rx_ptr: *mut u8,
    rx_inc: bool,
    rx_len: usize,
    tx_ptr: *const u8,
    tx_inc: bool,
    tx_len: usize,
  ) -> Box<Future<Item = (), Error = DmaError> + 'sess>;

  /// Skips bytes for which `f` returns `None`. Returns `x` for the first
  /// `Some(x)`.
  fn wait_byte<'sess, R, F>(
    &'sess mut self,
    first_byte: u8,
    max_skip: usize,
    f: F,
  ) -> Box<Future<Item = R, Error = CmdError> + 'sess>
  where
    R: Send + 'static,
    F: FnMut(u8) -> Option<R> + Send + 'static;

  /// Sends a byte.
  fn send_byte(&mut self, value: u8);

  /// Returns a future, which resolves after 1 ms.
  fn startup_delay(&mut self) -> Box<Future<Item = (), Error = !>>;
}

impl<T: SdSpiSessResCore> SdSpiSessCore<T> {
  /// Waits 1 ms.
  #[inline(always)]
  pub fn startup_wait(&mut self) -> Box<Future<Item = (), Error = !>> {
    self.0.startup_delay()
  }

  /// Executes SPI mode initialization flow.
  pub fn init<'sess>(
    &'sess mut self,
    voltage: Ocr,
    buf: &'sess mut [u8],
  ) -> impl Future<Item = Ocr, Error = SpiInitError> + 'sess {
    async(static move || {
      self.0.spi_open();
      await!(self.startup_cycles())?;
      self.0.sd().slave_select();
      await!(self.wait_until_ready(INIT_TIMEOUT))?;
      let r1 = await!(self.go_idle_state(buf))?;
      if !r1.in_idle_state() {
        return Err(SpiInitError::NotIdle);
      }
      await!(self.wait_until_ready(INIT_TIMEOUT))?;
      match await!(self.send_if_cond(0b0001, 0xAA, buf)) {
        Ok(cond) => {
          // Ver2.00 or later SD Memory Card
          if cond.is_err() {
            return Err(SpiInitError::CondMismatch);
          }
          await!(self.wait_until_ready(INIT_TIMEOUT))?;
          let (_r1, ocr) = await!(self.read_ocr(buf))?;
          if !ocr.supports(voltage) {
            return Err(SpiInitError::UnsupportedVoltage);
          }
          await!(self.wait_until_ready(INIT_TIMEOUT))?;
          await!(self.crc_on_off(true, buf))?;
          await!(self.wait_until_ready(INIT_TIMEOUT))?;
          while await!(self.sd_send_op_cond(true, buf))?.in_idle_state() {
            await!(self.wait_until_ready(INIT_TIMEOUT))?;
          }
          await!(self.wait_until_ready(INIT_TIMEOUT))?;
          let (_r1, ocr) = await!(self.read_ocr(buf))?;
          self.end();
          Ok(ocr)
        }
        Err(CmdError::R1(R1Error::Illegal)) => {
          // Ver1.X SD Memory Card or Not SD Memory Card
          await!(self.wait_until_ready(INIT_TIMEOUT))?;
          let (_r1, ocr) = await!(self.read_ocr(buf))?;
          if !ocr.supports(voltage) {
            return Err(SpiInitError::UnsupportedVoltage);
          }
          await!(self.wait_until_ready(INIT_TIMEOUT))?;
          await!(self.crc_on_off(true, buf))?;
          await!(self.wait_until_ready(INIT_TIMEOUT))?;
          while await!(self.sd_send_op_cond(false, buf))?.in_idle_state() {
            await!(self.wait_until_ready(INIT_TIMEOUT))?;
          }
          self.end();
          Ok(ocr)
        }
        Err(err) => Err(err)?,
      }
    })
  }

  /// Begins SPI session.
  pub fn begin(&self) {
    self.0.spi_open();
    self.0.sd().slave_select();
  }

  /// Ends SPI session.
  pub fn end(&self) {
    self.0.spi_close();
    self.0.sd().slave_unselect();
  }

  /// Resets the SD Memory Card.
  pub fn go_idle_state<'sess>(
    &'sess mut self,
    buf: &'sess mut [u8],
  ) -> impl Future<Item = R1, Error = CmdError> + 'sess {
    async(static move || {
      await!(self.send_cmd(0, 0x0000_0000, buf))?;
      Ok(await!(self.wait_r1())?)
    })
  }

  /// Sends SD Memory Card interface condition that includes host supply voltage
  /// information and asks the accessed card whether card can operate in
  /// supplied voltage range.
  pub fn send_if_cond<'sess>(
    &'sess mut self,
    voltage: u32,
    pattern: u32,
    buf: &'sess mut [u8],
  ) -> impl Future<Item = Result<(R1, Cond), (R1, Cond)>, Error = CmdError> + 'sess
  {
    async(static move || {
      await!(self.send_cmd(8, (voltage << 8) | pattern, buf))?;
      let r1 = await!(self.wait_r1())?;
      r1.errck(true)?;
      await!(self.recv_bytes(&mut buf[..4]))?;
      let cond = Cond::from_buf(buf);
      if cond.voltage_accepted() == voltage && cond.check_pattern() == pattern {
        Ok(Ok((r1, cond)))
      } else {
        Ok(Err((r1, cond)))
      }
    })
  }

  /// Asks the selected card to send its card-specific data (CSD).
  pub fn send_csd<'sess>(
    &'sess mut self,
    buf: &'sess mut [u8],
    timeout: usize,
  ) -> impl Future<Item = (R1, Csd), Error = CmdError> + 'sess {
    async(static move || {
      await!(self.wait_until_ready(timeout))?;
      await!(self.send_cmd(9, 0x0000_0000, buf))?;
      let r1 = await!(self.wait_r1())?;
      r1.errck(true)?;
      await!(self.wait_read_response(timeout))?;
      await!(self.recv_bytes(&mut buf[..16 + 2]))?;
      let (data, crc) = buf.split_at(16);
      self.data_crc_verify(data, crc)?;
      Ok((r1, Csd::from_buf(buf)))
    })
  }

  /// Asks the selected card to send its status register.
  pub fn send_status<'sess>(
    &'sess mut self,
    buf: &'sess mut [u8],
  ) -> impl Future<Item = (R1, Status), Error = CmdError> + 'sess {
    async(static move || {
      await!(self.send_cmd(13, 0x0000_0000, buf))?;
      Ok(await!(self.wait_r2())?)
    })
  }

  /// Reads the OCR register of a card.
  pub fn read_ocr<'sess>(
    &'sess mut self,
    buf: &'sess mut [u8],
  ) -> impl Future<Item = (R1, Ocr), Error = CmdError> + 'sess {
    async(static move || {
      await!(self.send_cmd(58, 0x0000_0000, buf))?;
      let r1 = await!(self.wait_r1())?;
      r1.errck(true)?;
      await!(self.recv_bytes(&mut buf[..4]))?;
      Ok((r1, Ocr::from_buf(buf)))
    })
  }

  /// Sends host capacity support information and activates the card's
  /// initialization process.
  pub fn sd_send_op_cond<'sess>(
    &'sess mut self,
    hcs: bool,
    buf: &'sess mut [u8],
  ) -> impl Future<Item = R1, Error = CmdError> + 'sess {
    async(static move || {
      await!(self.send_app_cmd(41, (hcs as u32) << 30, buf))?;
      Ok(await!(self.wait_r1())?)
    })
  }

  /// In case of SDSC Card, block length is set by this command.  In case of
  /// SDHC and SDXC Cards, block length of the memory access commands are fixed
  /// to 512 bytes.  The length of LOCK_UNLOCK command is set by this command
  /// regardless of card capacity.
  pub fn set_blocklen<'sess>(
    &'sess mut self,
    length: u32,
    buf: &'sess mut [u8],
  ) -> impl Future<Item = R1, Error = CmdError> + 'sess {
    async(static move || {
      await!(self.send_cmd(16, length, buf))?;
      Ok(await!(self.wait_r1())?)
    })
  }

  /// Turns the CRC option on or off.
  pub fn crc_on_off<'sess>(
    &'sess mut self,
    on: bool,
    buf: &'sess mut [u8],
  ) -> impl Future<Item = R1, Error = CmdError> + 'sess {
    async(static move || {
      await!(self.send_cmd(59, on as u32, buf))?;
      Ok(await!(self.wait_r1())?)
    })
  }

  /// Reads a block of data.
  pub fn read_block<'sess>(
    &'sess mut self,
    buf: &'sess mut [u8],
    data: &'sess mut [u8],
    address: u32,
    timeout: usize,
  ) -> impl Future<Item = (), Error = CmdError> + 'sess {
    async(static move || {
      await!(self.wait_until_ready(timeout))?;
      await!(self.send_cmd(17, address, buf))?;
      await!(self.wait_r1())?.errck(false)?;
      await!(self.wait_read_response(timeout))?;
      await!(self.recv_bytes(data))?;
      await!(self.recv_bytes(&mut buf[..2]))?;
      self.data_crc_verify(data, buf)?;
      Ok(())
    })
  }

  /// Reads multiple blocks of data.
  pub fn read_blocks<'sess>(
    &'sess mut self,
    buf: &'sess mut [u8],
    data: &'sess mut [u8],
    address: u32,
    block_length: usize,
    timeout: usize,
  ) -> impl Future<Item = R1, Error = CmdError> + 'sess {
    async(static move || {
      await!(self.wait_until_ready(timeout))?;
      await!(self.send_cmd(18, address, buf))?;
      await!(self.wait_r1())?.errck(false)?;
      for data in data.chunks_mut(block_length) {
        await!(self.wait_read_response(timeout))?;
        await!(self.recv_bytes(data))?;
        await!(self.recv_bytes(&mut buf[..2]))?;
        self.data_crc_verify(data, buf)?;
      }
      await!(self.send_cmd(12, 0x0000_0000, buf))?;
      Ok(await!(self.wait_r1())?)
    })
  }

  /// Writes a block of data.
  pub fn write_block<'sess>(
    &'sess mut self,
    buf: &'sess mut [u8],
    data: &'sess [u8],
    address: u32,
    timeout: usize,
  ) -> impl Future<Item = (), Error = CmdError> + 'sess {
    async(static move || {
      await!(self.wait_until_ready(timeout))?;
      await!(self.send_cmd(24, address, buf))?;
      await!(self.wait_r1_and_until_ready())?.errck(false)?;
      await!(self.send_data_block(BLOCK_START, data))?.errck()?;
      await!(self.wait_until_ready(timeout))?;
      let (_r1, status) = await!(self.send_status(buf))?;
      status.errck()?;
      Ok(())
    })
  }

  /// Writes multiple blocks of data.
  pub fn write_blocks<'sess>(
    &'sess mut self,
    buf: &'sess mut [u8],
    data: &'sess [u8],
    address: u32,
    block_length: usize,
    timeout: usize,
  ) -> impl Future<Item = (), Error = CmdError> + 'sess {
    async(static move || {
      await!(self.wait_until_ready(timeout))?;
      await!(self.send_cmd(25, address, buf))?;
      await!(self.wait_r1())?.errck(false)?;
      let mut resp;
      for data in data.chunks(block_length) {
        await!(self.wait_until_ready(timeout))?;
        resp = await!(self.send_data_block(BLOCK_START_MULTI_WRITE, data))?;
        resp.errck()?;
      }
      await!(self.wait_until_ready(timeout))?;
      await!(self.send_and_wait_until_ready(STOP_TRAN, timeout))?;
      let (_r1, status) = await!(self.send_status(buf))?;
      status.errck()?;
      Ok(())
    })
  }

  /// Erases multiple blocks of data.
  pub fn erase_blocks<'sess>(
    &'sess mut self,
    buf: &'sess mut [u8],
    start: u32,
    end: u32,
    timeout: usize,
  ) -> impl Future<Item = (), Error = CmdError> + 'sess {
    async(static move || {
      await!(self.wait_until_ready(timeout))?;
      await!(self.send_cmd(32, start, buf))?;
      await!(self.wait_r1())?.errck(false)?;
      await!(self.send_cmd(33, end, buf))?;
      await!(self.wait_r1())?.errck(false)?;
      await!(self.send_cmd(33, 0x0000_0000, buf))?;
      await!(self.wait_r1())?.errck(false)?;
      Ok(())
    })
  }

  fn startup_cycles<'sess>(
    &'sess mut self,
  ) -> Box<Future<Item = (), Error = DmaError> + 'sess> {
    unsafe {
      self.0.dma_exchange(
        &mut RX_FORGET,
        false,
        STARTUP_CYCLES,
        &TX_IDLE,
        false,
        STARTUP_CYCLES,
      )
    }
  }

  fn send_cmd<'sess>(
    &'sess mut self,
    cmd: u8,
    mut arg: u32,
    buf: &'sess mut [u8],
  ) -> Box<Future<Item = (), Error = DmaError> + 'sess> {
    assert!(buf.len() >= 6);
    let (mut buf, mut crc) = (buf.as_mut_ptr(), Crc7::default());
    unsafe {
      let mut push = |byte| {
        *buf = byte;
        buf = buf.offset(1);
        crc.add_byte(byte);
      };
      push(cmd | 0x40);
      for _ in 0..4 {
        arg = arg.rotate_left(8);
        push(arg as u8);
      }
    }
    unsafe {
      *buf = crc.value() | 1;
      buf = buf.offset(-5);
      self.0.dma_exchange(&mut RX_FORGET, false, 6, buf, true, 6)
    }
  }

  fn send_app_cmd<'sess>(
    &'sess mut self,
    cmd: u8,
    arg: u32,
    buf: &'sess mut [u8],
  ) -> impl Future<Item = (), Error = CmdError> + 'sess {
    async(static move || {
      await!(self.send_cmd(55, 0x0000_0000, buf))?;
      await!(self.wait_r1_and_until_ready())?.errck(false)?;
      await!(self.send_cmd(cmd, arg, buf))?;
      Ok(())
    })
  }

  fn send_and_wait_until_ready<'sess>(
    &'sess mut self,
    first_byte: u8,
    max_skip: usize,
  ) -> Box<Future<Item = (), Error = CmdError> + 'sess> {
    self.0.wait_byte(first_byte, max_skip, |byte| {
      if byte == 0xFF {
        Some(())
      } else {
        None
      }
    })
  }

  fn wait_until_ready<'sess>(
    &'sess mut self,
    max_skip: usize,
  ) -> Box<Future<Item = (), Error = CmdError> + 'sess> {
    self.send_and_wait_until_ready(0xFF, max_skip)
  }

  fn wait_r1<'sess>(
    &'sess mut self,
  ) -> Box<Future<Item = R1, Error = CmdError> + 'sess> {
    self.0.wait_byte(0xFF, NCR, R1::try_from)
  }

  fn wait_r1_and_until_ready<'sess>(
    &'sess mut self,
  ) -> Box<Future<Item = R1, Error = CmdError> + 'sess> {
    let mut r1 = None;
    self.0.wait_byte(0xFF, NCR, move |byte| {
      if r1.is_none() {
        r1 = R1::try_from(byte);
      } else if byte == 0xFF {
        return r1;
      }
      None
    })
  }

  fn wait_r2<'sess>(
    &'sess mut self,
  ) -> impl Future<Item = (R1, Status), Error = CmdError> + 'sess {
    let mut r1 = None;
    let resp = self.0.wait_byte(0xFF, NCR, move |byte| match r1 {
      None => {
        r1 = R1::try_from(byte);
        if let Some(r1) = r1 {
          if let Err(err) = r1.errck(true) {
            return Some(Err(err));
          }
        }
        None
      }
      Some(r1) => Some(Ok((r1, Status::new(byte)))),
    });
    async(static move || Ok(await!(resp)??))
  }

  fn wait_read_response<'sess>(
    &'sess mut self,
    max_skip: usize,
  ) -> impl Future<Item = (), Error = CmdError> + 'sess {
    async(static move || {
      await!(self.0.wait_byte(0xFF, max_skip, ReadResponse::try_from))?
        .errck()?;
      Ok(())
    })
  }

  fn recv_bytes<'sess>(
    &'sess mut self,
    buf: &'sess mut [u8],
  ) -> Box<Future<Item = (), Error = DmaError> + 'sess> {
    unsafe {
      self.0.dma_exchange(
        buf.as_mut_ptr(),
        true,
        buf.len(),
        &TX_IDLE,
        false,
        buf.len(),
      )
    }
  }

  fn send_data_block<'sess>(
    &'sess mut self,
    token: u8,
    data: &'sess [u8],
  ) -> impl Future<Item = WriteResponse, Error = DmaError> + 'sess {
    async(static move || {
      self.0.send_byte(token);
      unsafe {
        await!(self.0.dma_exchange(
          &mut RX_FORGET,
          false,
          data.len() + 1,
          data.as_ptr(),
          true,
          data.len(),
        ))?;
      }
      let crc = Crc16::from_bytes(data).value();
      let mut resp: WriteResponse = unsafe { mem::uninitialized() };
      let buf: [u8; 3] = [((crc & 0xFF_00) >> 8) as u8, crc as u8, 0xFF];
      unsafe {
        await!(self.0.dma_exchange(
          &mut resp as *mut _ as *mut u8,
          false,
          3,
          buf.as_ptr(),
          true,
          3
        ))?;
      }
      Ok(resp)
    })
  }

  #[cfg_attr(feature = "cargo-clippy", allow(cast_ptr_alignment))]
  #[inline(always)]
  fn data_crc_verify(&self, data: &[u8], crc: &[u8]) -> Result<(), CmdError> {
    assert!(crc.len() >= 2);
    let check_crc = unsafe { u16::from_be(*(crc.as_ptr() as *const u16)) };
    if Crc16::from_bytes(data).value() == check_crc {
      Ok(())
    } else {
      Err(CmdError::DataCrc)
    }
  }
}