drone-cortex-m 0.7.0

Drone for ARM Cortex-M.
Documentation
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
//! Serial peripheral interface.

#[allow(unused_imports)]
use core::ptr::{read_volatile, write_volatile};
#[cfg(any(feature = "stm32f100", feature = "stm32f101",
          feature = "stm32f102", feature = "stm32f103",
          feature = "stm32f107", feature = "stm32l4x1",
          feature = "stm32l4x2", feature = "stm32l4x3",
          feature = "stm32l4x5", feature = "stm32l4x6"))]
use reg::{spi1, spi2, spi3};
use reg::prelude::*;
#[cfg(any(feature = "stm32f100", feature = "stm32f101",
          feature = "stm32f102", feature = "stm32f103",
          feature = "stm32f107", feature = "stm32l4x1",
          feature = "stm32l4x2", feature = "stm32l4x3",
          feature = "stm32l4x5", feature = "stm32l4x6"))]
use thread::irq::{IrqSpi1, IrqSpi2, IrqSpi3};
use thread::prelude::*;

/// Generic SPI.
#[allow(missing_docs)]
pub trait Spi
where
  Self: Sized + Send + Sync + 'static,
  Self::Tokens: From<Self>,
{
  /// Generic SPI input tokens.
  type InputTokens;

  /// Generic SPI tokens.
  type Tokens;

  type Cr1: for<'a> WRegShared<'a, Ftt>;
  type Cr2: for<'a> WRegShared<'a, Ftt>;
  type Crcpr: Reg<Stt>;
  type Dr: Reg<Stt>;
  type Rxcrcr: Reg<Stt>;
  type Sr: Reg<Stt>;
  type Txcrcr: Reg<Stt>;

  /// Creates a new `Spi` driver from provided `tokens`.
  fn new(tokens: Self::InputTokens) -> Self;

  fn cr1(&self) -> &Self::Cr1;
  fn cr2(&self) -> &Self::Cr2;
  fn crcpr(&self) -> &Self::Crcpr;
  fn dr(&self) -> &Self::Dr;
  fn rxcrcr(&self) -> &Self::Rxcrcr;
  fn sr(&self) -> &Self::Sr;
  fn txcrcr(&self) -> &Self::Txcrcr;

  /// Writes `u8` value to the data register.
  fn send_byte(&self, value: u8);

  /// Writes `u16` value to the data register.
  fn send_hword(&self, value: u16);

  /// Reads `u8` value from the data register.
  fn recv_byte(&self) -> u8;

  /// Reads `u16` value from the data register.
  fn recv_hword(&self) -> u16;

  /// Waits while SPI is busy in communication or Tx buffer is not empty.
  fn busy_wait(&self);

  /// Moves `self` into `f` while `SPE` is cleared, and then sets `SPE`.
  fn spe_after<F, R>(self, cr1_val: <Self::Cr1 as Reg<Ftt>>::Val, f: F) -> R
  where
    F: FnOnce(Self) -> R;

  /// Moves `self` into `f` while `TXDMAEN` is cleared, and then sets `TXDMAEN`.
  fn txdmaen_after<F, R>(
    self,
    cr2_val: <Self::Cr2 as Reg<Ftt>>::Val,
    f: F,
  ) -> R
  where
    F: FnOnce(Self) -> R;
}

/// Generic interrupt-driven SPI.
#[allow(missing_docs)]
pub trait SpiIrq<T: Thread, I: ThreadNumber>: Spi {
  fn irq(&self) -> ThreadToken<T, I>;
}

#[allow(unused_macros)]
macro_rules! spi_shared {
  (
    $spi:ident,
    $spi_cr1:ident,
    $spi_cr2:ident,
    $spi_crcpr:ident,
    $spi_dr:ident,
    $spi_rxcrcr:ident,
    $spi_sr:ident,
    $spi_txcrcr:ident,
  ) => {
    type Cr1 = $spi::Cr1<Ftt>;
    type Cr2 = $spi::Cr2<Ftt>;
    type Crcpr = $spi::Crcpr<Stt>;
    type Dr = $spi::Dr<Stt>;
    type Rxcrcr = $spi::Rxcrcr<Stt>;
    type Sr = $spi::Sr<Stt>;
    type Txcrcr = $spi::Txcrcr<Stt>;

    #[inline(always)]
    fn cr1(&self) -> &Self::Cr1 {
      &self.tokens.$spi_cr1
    }

    #[inline(always)]
    fn cr2(&self) -> &Self::Cr2 {
      &self.tokens.$spi_cr2
    }

    #[inline(always)]
    fn crcpr(&self) -> &Self::Crcpr {
      &self.tokens.$spi_crcpr
    }

    #[inline(always)]
    fn dr(&self) -> &Self::Dr {
      &self.tokens.$spi_dr
    }

    #[inline(always)]
    fn rxcrcr(&self) -> &Self::Rxcrcr {
      &self.tokens.$spi_rxcrcr
    }

    #[inline(always)]
    fn sr(&self) -> &Self::Sr {
      &self.tokens.$spi_sr
    }

    #[inline(always)]
    fn txcrcr(&self) -> &Self::Txcrcr {
      &self.tokens.$spi_txcrcr
    }

    #[inline(always)]
    fn send_byte(&self, value: u8) {
      unsafe {
        write_volatile(self.tokens.$spi_dr.to_mut_ptr() as *mut _, value);
      }
    }

    #[inline(always)]
    fn send_hword(&self, value: u16) {
      unsafe {
        write_volatile(self.tokens.$spi_dr.to_mut_ptr() as *mut _, value);
      }
    }

    #[inline(always)]
    fn recv_byte(&self) -> u8 {
      unsafe { read_volatile(self.tokens.$spi_dr.to_ptr() as *const _) }
    }

    #[inline(always)]
    fn recv_hword(&self) -> u16 {
      unsafe { read_volatile(self.tokens.$spi_dr.to_ptr() as *const _) }
    }

    #[inline(always)]
    fn busy_wait(&self) {
      while self.sr().bsy.read_bit_band() {}
    }

    #[inline]
    fn spe_after<F, R>(
      mut self,
      mut cr1_val: <Self::Cr1 as Reg<Ftt>>::Val,
      f: F,
    ) -> R
    where
      F: FnOnce(Self) -> R,
    {
      let cr1 = self.tokens.$spi_cr1.fork();
      let cr1_spe = self.tokens.$spi_cr1.spe.fork();
      cr1_spe.clear(&mut cr1_val);
      cr1.store_val(cr1_val);
      let result = f(self);
      cr1_spe.set(&mut cr1_val);
      cr1.store_val(cr1_val);
      result
    }

    #[inline]
    fn txdmaen_after<F, R>(
      mut self,
      mut cr2_val: <Self::Cr2 as Reg<Ftt>>::Val,
      f: F,
    ) -> R
    where
      F: FnOnce(Self) -> R,
    {
      let cr2 = self.tokens.$spi_cr2.fork();
      let cr2_txdmaen = self.tokens.$spi_cr2.txdmaen.fork();
      cr2_txdmaen.clear(&mut cr2_val);
      cr2.store_val(cr2_val);
      let result = f(self);
      cr2_txdmaen.set(&mut cr2_val);
      cr2.store_val(cr2_val);
      result
    }
  }
}

#[allow(unused_macros)]
macro_rules! spi {
  (
    $doc:expr,
    $name:ident,
    $name_macro:ident,
    $doc_irq:expr,
    $name_irq:ident,
    $name_irq_macro:ident,
    $doc_tokens:expr,
    $name_tokens:ident,
    $doc_irq_tokens:expr,
    $name_irq_tokens:ident,
    $irq_ty:ident,
    $spi:ident,
    $spi_cr1:ident,
    $spi_cr2:ident,
    $spi_crcpr:ident,
    $spi_dr:ident,
    $spi_rxcrcr:ident,
    $spi_sr:ident,
    $spi_txcrcr:ident,
  ) => {
    #[doc = $doc]
    pub struct $name {
      tokens: $name_tokens<Ftt>,
    }

    #[doc = $doc_irq]
    pub struct $name_irq<T: Thread, I: $irq_ty> {
      tokens: $name_irq_tokens<T, I, Ftt>,
    }

    #[doc = $doc_tokens]
    #[allow(missing_docs)]
    pub struct $name_tokens<R: RegTag> {
      pub $spi_cr1: $spi::Cr1<R>,
      pub $spi_cr2: $spi::Cr2<R>,
      pub $spi_crcpr: $spi::Crcpr<Stt>,
      pub $spi_dr: $spi::Dr<Stt>,
      pub $spi_rxcrcr: $spi::Rxcrcr<Stt>,
      pub $spi_sr: $spi::Sr<Stt>,
      pub $spi_txcrcr: $spi::Txcrcr<Stt>,
    }

    #[doc = $doc_irq_tokens]
    #[allow(missing_docs)]
    pub struct $name_irq_tokens<T: Thread, I: $irq_ty, R: RegTag> {
      pub $spi: ThreadToken<T, I>,
      pub $spi_cr1: $spi::Cr1<R>,
      pub $spi_cr2: $spi::Cr2<R>,
      pub $spi_crcpr: $spi::Crcpr<Stt>,
      pub $spi_dr: $spi::Dr<Stt>,
      pub $spi_rxcrcr: $spi::Rxcrcr<Stt>,
      pub $spi_sr: $spi::Sr<Stt>,
      pub $spi_txcrcr: $spi::Txcrcr<Stt>,
    }

    /// Creates a new `Spi` driver from tokens.
    #[macro_export]
    macro_rules! $name_macro {
      ($regs:ident) => {
        $crate::peripherals::spi::Spi::new(
          $crate::peripherals::spi::$name_tokens {
            $spi_cr1: $regs.$spi_cr1,
            $spi_cr2: $regs.$spi_cr2,
            $spi_crcpr: $regs.$spi_crcpr,
            $spi_dr: $regs.$spi_dr,
            $spi_rxcrcr: $regs.$spi_rxcrcr,
            $spi_sr: $regs.$spi_sr,
            $spi_txcrcr: $regs.$spi_txcrcr,
          }
        )
      }
    }

    /// Creates a new `SpiIrq` driver from tokens.
    #[macro_export]
    macro_rules! $name_irq_macro {
      ($thrd:ident, $regs:ident) => {
        $crate::peripherals::spi::SpiIrq::new(
          $crate::peripherals::spi::$name_irq_tokens {
            $spi: $thrd.$spi,
            $spi_cr1: $regs.$spi_cr1,
            $spi_cr2: $regs.$spi_cr2,
            $spi_crcpr: $regs.$spi_crcpr,
            $spi_dr: $regs.$spi_dr,
            $spi_rxcrcr: $regs.$spi_rxcrcr,
            $spi_sr: $regs.$spi_sr,
            $spi_txcrcr: $regs.$spi_txcrcr,
          }
        )
      }
    }

    impl From<$name> for $name_tokens<Ftt> {
      #[inline(always)]
      fn from(spi: $name) -> Self {
        spi.tokens
      }
    }

    impl Spi for $name {
      type InputTokens = $name_tokens<Stt>;
      type Tokens = $name_tokens<Ftt>;

      #[inline(always)]
      fn new(tokens: Self::InputTokens) -> Self {
        Self {
          tokens: Self::Tokens {
            $spi_cr1: tokens.$spi_cr1.into(),
            $spi_cr2: tokens.$spi_cr2.into(),
            $spi_crcpr: tokens.$spi_crcpr,
            $spi_dr: tokens.$spi_dr,
            $spi_rxcrcr: tokens.$spi_rxcrcr,
            $spi_sr: tokens.$spi_sr,
            $spi_txcrcr: tokens.$spi_txcrcr,
          }
        }
      }

      spi_shared! {
        $spi,
        $spi_cr1,
        $spi_cr2,
        $spi_crcpr,
        $spi_dr,
        $spi_rxcrcr,
        $spi_sr,
        $spi_txcrcr,
      }
    }

    impl<T, I> From<$name_irq<T, I>> for $name_irq_tokens<T, I, Ftt>
    where
      T: Thread,
      I: $irq_ty,
    {
      #[inline(always)]
      fn from(spi_irq: $name_irq<T, I>) -> Self {
        spi_irq.tokens
      }
    }

    impl<T: Thread, I: $irq_ty> Spi for $name_irq<T, I> {
      type InputTokens = $name_irq_tokens<T, I, Stt>;
      type Tokens = $name_irq_tokens<T, I, Ftt>;

      #[inline(always)]
      fn new(tokens: Self::InputTokens) -> Self {
        Self {
          tokens: Self::Tokens {
            $spi: tokens.$spi,
            $spi_cr1: tokens.$spi_cr1.into(),
            $spi_cr2: tokens.$spi_cr2.into(),
            $spi_crcpr: tokens.$spi_crcpr,
            $spi_dr: tokens.$spi_dr,
            $spi_rxcrcr: tokens.$spi_rxcrcr,
            $spi_sr: tokens.$spi_sr,
            $spi_txcrcr: tokens.$spi_txcrcr,
          }
        }
      }

      spi_shared! {
        $spi,
        $spi_cr1,
        $spi_cr2,
        $spi_crcpr,
        $spi_dr,
        $spi_rxcrcr,
        $spi_sr,
        $spi_txcrcr,
      }
    }

    impl<T: Thread, I: $irq_ty> SpiIrq<T, I> for $name_irq<T, I> {
      #[inline(always)]
      fn irq(&self) -> ThreadToken<T, I> {
        self.tokens.$spi
      }
    }
  }
}

#[cfg(any(feature = "stm32f100", feature = "stm32f101",
          feature = "stm32f102", feature = "stm32f103",
          feature = "stm32f107", feature = "stm32l4x1",
          feature = "stm32l4x2", feature = "stm32l4x3",
          feature = "stm32l4x5", feature = "stm32l4x6"))]
spi! {
  "SPI1.",
  Spi1,
  peripheral_spi1,
  "SPI1 with interrupt.",
  Spi1Irq,
  peripheral_spi1_irq,
  "SPI1 tokens.",
  Spi1Tokens,
  "SPI1 with interrupt tokens.",
  Spi1IrqTokens,
  IrqSpi1,
  spi1,
  spi1_cr1,
  spi1_cr2,
  spi1_crcpr,
  spi1_dr,
  spi1_rxcrcr,
  spi1_sr,
  spi1_txcrcr,
}

#[cfg(any(feature = "stm32f100", feature = "stm32f101",
          feature = "stm32f102", feature = "stm32f103",
          feature = "stm32f107", feature = "stm32l4x1",
          feature = "stm32l4x2", feature = "stm32l4x3",
          feature = "stm32l4x5", feature = "stm32l4x6"))]
spi! {
  "SPI2.",
  Spi2,
  peripheral_spi2,
  "SPI2 with interrupt.",
  Spi2Irq,
  peripheral_spi2_irq,
  "SPI2 tokens.",
  Spi2Tokens,
  "SPI2 with interrupt tokens.",
  Spi2IrqTokens,
  IrqSpi2,
  spi2,
  spi2_cr1,
  spi2_cr2,
  spi2_crcpr,
  spi2_dr,
  spi2_rxcrcr,
  spi2_sr,
  spi2_txcrcr,
}

#[cfg(any(feature = "stm32f100", feature = "stm32f101",
          feature = "stm32f102", feature = "stm32f103",
          feature = "stm32f107", feature = "stm32l4x1",
          feature = "stm32l4x2", feature = "stm32l4x3",
          feature = "stm32l4x5", feature = "stm32l4x6"))]
spi! {
  "SPI3.",
  Spi3,
  peripheral_spi3,
  "SPI3 with interrupt.",
  Spi3Irq,
  peripheral_spi3_irq,
  "SPI3 tokens.",
  Spi3Tokens,
  "SPI3 with interrupt tokens.",
  Spi3IrqTokens,
  IrqSpi3,
  spi3,
  spi3_cr1,
  spi3_cr2,
  spi3_crcpr,
  spi3_dr,
  spi3_rxcrcr,
  spi3_sr,
  spi3_txcrcr,
}